<?php

use App\Models\Point;
use App\Models\Client;
use App\Models\Note;

/** @var Client $patient */
/** @var Note $note */

$points = Point
    ::where('client_id', $patient->id)
    ->where('category', 'SUPPLEMENT')
    ->where('is_removed_due_to_entry_error', false)
    ->where('is_removed', false)
    ->orderBy('created_at')
    ->get();
foreach ($points as $point) {
    if ($point->data) {
        $point->data = json_decode($point->data);
    }
}
$supplements = $points;
?>

<div class="mt-3 p-3 border-top" id="supplements-reconcile-{{$note->id}}">
    <div class="mb-2 font-weight-bold text-secondary">The following supplements will be marked as reconciled</div>
    <table class="table table-sm table-striped table-bordered">
        <thead>
        <tr>
            <th class="border-bottom-0">Supplement</th>
            <th class="border-bottom-0">Subjective</th>
        </tr>
        </thead>
        @foreach($supplements as $supplement)
        <tr>
            <td class="w-50">{{$supplement->data->name}}</td>
            <td class="p-0">
                <input type="text" class="form-control form-control-sm border-0 rounded-0 shadow-none" data-point-uid="{{$supplement->uid}}" value="Reconciled">
            </td>
        </tr>
        @endforeach
    </table>
    <div class="d-flex align-items-center justify-content-center">
        <button class="btn btn-sm btn-primary mr-2 btn-save-reviews">Save</button>
        <button class="btn btn-sm btn-default border mr-2" onclick="closeStagPopup()">Cancel</button>
    </div>
</div>

<script>
    (function() {
        function init() {
            $('#supplements-reconcile-{{$note->id}} .btn-save-reviews')
                .off('click')
                .on('click', function() {
                    showMask();
                    let payload = {
                        noteUid: '{{$note->uid}}',
                        reviews: []
                    }
                    $('#supplements-reconcile-{{$note->id}} [data-point-uid]').each(function() {
                         payload.reviews.push({
                             parentPointUid: $(this).attr('data-point-uid'),
                             data: JSON.stringify({
                                 isReconciled: true,
                                 value: $(this).val()
                             })
                         });
                    });

                    $.ajax({
                        url: '/api/visitPoint/upsertChildReviewMulti',
                        type:"POST",
                        data: JSON.stringify(payload),
                        contentType:"application/json; charset=utf-8",
                        dataType:"json",
                        success: function(_data) {
                            if(!hasResponseError(_data)) {
                                closeStagPopup();
                            }
                        }
                    }).then(() => {
                        hideMask();
                    });

                });
        }
        addMCInitializer('supplements-reconcile-{{$note->id}}', init, '#supplements-reconcile-{{$note->id}}')
    }).call(window);
</script>