<div class="mt-3 p-3 border-top" id="ccm-agreement-{{$note->id}}">
    <p class="mb-3">Dear Patient,</p>

    <p class="mb-3">You are eligible for a new Medicare program that enables us to provide you with a round-the clock service to oversee your chronic conditions and improve your overall wellness.</p>

    <p class="mb-3">Chronic conditions are ongoing medical problems like diabetes, high blood pressure, dementia, heart disease, depression, osteoporosis, and many others. These conditions must be managed effectively in partnership between the healthcare team and patient to maintain the best possible overall health and wellness.</p>

    <p class="mb-3">CCM involves a combination of face-to-face and non-face-to-face services to ensure that each patient’s healthcare needs are met. The non-face-to-face component of CCM involves the creation of a patient-centered plan of care, medication monitoring, management of care transitions, electronic care coordination and exchange of health information with other health care providers as necessary, while providing you or your caregiver 24/7 access to your care team.</p>

    <p class="mb-3">Each month, after we provide you with a minimum of 20 minutes of non-face-to-face services, we will bill your insurer(s). Either you or your supplementary insurer may be responsible for any patient responsibility.</p>

    <p class="mb-3">You may stop this service at any time, for any reason. If you choose to stop the service we will provide it only through the last day of the calendar month of your decision.</p>

    <?php $patient = $note->client; ?>

    <div class="border rounded p-3 mb-3 max-width-500px" id="ccm-agreement-reasons-{{$note->id}}">
        <input type="hidden" name="client-ccm-reasons-count" value="{{count($patient->cmReasons)}}">
        <p><b>CCM Reasons @if(!$patient->cmReasons || count($patient->cmReasons) < 2)(2 reasons required)@else <i class="fa fa-check text-success ml-1"></i> @endif</b></p>
        <div class="min-width-300px">
            @if(!$patient->cmReasons || !count($patient->cmReasons))
                <span class="text-secondary d-block mb-2">None</span>
            @else
                <div>
                    @foreach($patient->cmReasons as $reason)
                        <div class="border-bottom pb-1 mb-1 d-flex align-items-start bg-white">
                            <b class="min-width-70px">{{$reason->code}}</b>
                            <span class="max-width-200px mr-3">{{$reason->description}}</span>
                            <div moe relative class="ml-auto">
                                <a start show><i class="fa fa-edit"></i></a>
                                <form url="/api/clientCmRmReason/updateBasic" class="mcp-theme-1" right>
                                    <input type="hidden" name="uid" value="{{$reason->uid}}">
                                    <div class="mb-2">
                                        <label class="text-sm mb-1 text-secondary">Code *</label>
                                        <input type="text" class="form-control form-control-sm"
                                               icd-autocomplete-code
                                               name="code" value="{{$reason->code}}" required>
                                    </div>
                                    <div class="mb-2">
                                        <label class="text-sm mb-1 text-secondary">Description</label>
                                        <input type="text" class="form-control form-control-sm"
                                               icd-autocomplete-description
                                               name="description" value="{{$reason->description}}">
                                    </div>
                                    <div>
                                        <button submit class="btn btn-sm btn-primary mr-1">Submit</button>
                                        <button cancel class="btn btn-sm btn-default border">Cancel</button>
                                    </div>
                                </form>
                            </div>
                            <div moe relative class="ml-2">
                                <a start show><i class="fa fa-trash-alt text-danger on-hover-opaque"></i></a>
                                <form url="/api/clientCmRmReason/remove" class="mcp-theme-1" right>
                                    <input type="hidden" name="uid" value="{{$reason->uid}}">
                                    <p>Remove this CM reason?</p>
                                    <div>
                                        <button submit class="btn btn-sm btn-primary mr-1">Submit</button>
                                        <button cancel class="btn btn-sm btn-default border">Cancel</button>
                                    </div>
                                </form>
                            </div>
                        </div>
                    @endforeach
                </div>
            @endif
            @if(!$patient->cmReasons || count($patient->cmReasons) < 2)
                <div class="d-flex align-items-baseline">
                    <div moe>
                        <a start show>+ Add From Dx</a>
                        <form url="/api/clientCmRmReason/create" class="mcp-theme-1">
                            <input type="hidden" name="clientUid" value="{{$patient->uid}}">
                            <input type="hidden" name="cmOrRm" value="CM">
                            <input type="hidden" name="code" value="">
                            <input type="hidden" name="description" value="">
                            <div class="mb-2">
                                <label class="text-sm mb-1 text-secondary">Reason *</label>
                                <select type="text" class="form-control form-control-sm problem-select" name="problem" required>
                                    <?php $problems = \App\Models\Point::getPointsOfCategory($patient, "PROBLEM"); ?>
                                    <option value="">-- select --</option>
                                    @foreach($problems as $problem)
                                        @if(!!@($problem->data->icd))
                                            <option value="{{$problem->uid}}" data-icd="{{$problem->data->icd}}" data-name="{{$problem->data->name}}">{{$problem->data->icd}} - {{$problem->data->name}}</option>
                                        @endif
                                    @endforeach
                                </select>
                            </div>
                            <div>
                                <button submit class="btn btn-sm btn-primary mr-1">Submit</button>
                                <button cancel class="btn btn-sm btn-default border">Cancel</button>
                            </div>
                        </form>
                    </div>
                    <span class="mx-2 text-secondary text-sm">|</span>
                    <div moe>
                        <a start show>+ Add</a>
                        <form url="/api/clientCmRmReason/create" class="mcp-theme-1">
                            <input type="hidden" name="clientUid" value="{{$patient->uid}}">
                            <input type="hidden" name="cmOrRm" value="CM">
                            <div class="mb-2">
                                <label class="text-sm mb-1 text-secondary">Code *</label>
                                <input type="text" class="form-control form-control-sm"
                                       icd-autocomplete-code
                                       name="code" value="" required>
                            </div>
                            <div class="mb-2">
                                <label class="text-sm mb-1 text-secondary">Description</label>
                                <input type="text" class="form-control form-control-sm"
                                       icd-autocomplete-description
                                       name="description" value="">
                            </div>
                            <div>
                                <button submit class="btn btn-sm btn-primary mr-1">Submit</button>
                                <button cancel class="btn btn-sm btn-default border">Cancel</button>
                            </div>
                        </form>
                    </div>
                </div>
            @endif
        </div>
    </div>

    <form action="/nop" method="post" id="ccm-agreement-form">
        <label class="d-flex align-items-center">
            <input type="checkbox" name="ccm-agreement" required class="mr-2">
            <span>I agree to participate in the Chronic Care Management program.</span>
        </label>
    </form>

    <hr class="m-neg-3">

    <div class="d-flex align-items-center justify-content-center">
        <button class="btn btn-primary btn-sm font-weight-bold px-3 btn-ccm-agreement-submit" {{count($patient->cmReasons) < 2 ? 'disabled' : ''}}>Submit</button>
        <button class="ml-3 btn btn-default border btn-sm px-3" onclick="return closeStagPopup()">Close</button>
    </div>

</div>

<script>
    (function() {

        function init() {

            let parentForm = $('#ccm-agreement-{{$note->id}}');

            parentForm.find('.btn-ccm-agreement-submit')
                .off('click')
                .on('click', function() {
                    @if(!$patient->cmReasons || count($patient->cmReasons) < 2)
                        toastr.error('2 reasons are required for CCM');
                        return false;
                    @else
                        let form = parentForm.find('#ccm-agreement-form')[0];
                        if (!form.checkValidity()) {
                            form.reportValidity();
                            return false;
                        }
                        showMask();

                        <?php $cmReasons = $patient->cmReasons->toArray(); ?>
                        let payload = {
                            uid: '{{$note->client->uid}}',
                            cmReason1Icd: '{{$cmReasons[0]['code']}}',
                            cmReason1Description: '{{$cmReasons[0]['description']}}',
                            cmReason2Icd: '{{$cmReasons[1]['code']}}',
                            cmReason2Description: '{{$cmReasons[1]['description']}}',
                        };

                        $.post('/api/client/setIsEnrolledInCmToTrue', payload, _data => {
                            hideMask();
                            if (!hasResponseError(_data)) {
                                closeStagPopup();
                                if(!parentForm.closest('.stag-popup').length) {
                                    fastReload();
                                }
                            }
                        }, 'json');
                    @endif
                });

            runMCInitializer('icd-autocomplete');
        }

        addMCInitializer('ccm-agreement-{{$note->id}}', init, '#ccm-agreement-{{$note->id}}')

    }).call(window);
</script>