<?php
$commonFields = [
    [
        ":::Aneurysm repair|Appendectomy|Back surgery|Bariatric surgery/gastric bypass|Bilateral tubal ligation|Breast resection/mastectomy|CABG|Carotid endarterectomy/stent|Carpal tunnel release surgery",
    ],
    [
        ":::Cataract/lens surgery|Cesarean section|Cholecystectomy/bile duct surgery|Dilation and curettage|Hemorrhoid surgery|Hip arthroplasty|Hip replacement|Hysterectomy|Inguinal hernia repair",
    ],
    [
        ":::Knee arthroplasty|LASIK|Laminectomy|Nasal surgery|PTCA/PCI|Pacemaker/defibrillator|Prostate surgery|Prostatectomy|Rotator cuff surgery",
    ],
    [
        ":::Sinus surgery|Skin cancer excision|Spinal fusion|TAH-BSO|TURP|Tonsillectomy/Adenoidectomy|Vasectomy",
    ]
];

$customFields = [
    [
        ":::Bladder surgery/repair posts|Cardiac catheterization|Cardiac Stent Placement",
    ],
    [
        ":::IVC filter/stent|Knee Replacement|Lumpectomy",
    ],
    [
        ":::Multiple heart stents|Oral surgery|Rt elbow ulnar repair",
    ],
    [
        ":::Rt Nephrectomy|Thyroidectomy|Valve replacement with stent",
    ]
];

$contentData = false;
if($section){
    $contentData = json_decode($section->content_data, true);
}
if(!$contentData) {
    $contentData = [];
}
for ($i = 0; $i < count($commonFields); $i++) {
    for($j = 0; $j < count($commonFields[$i]); $j++) {
        $parts = explode(":::", $commonFields[$i][$j]);
        $head = 'common';
        if(!empty($parts[0])) $head = $parts[0];
        $values = explode("|", $parts[1]);
        for($k = 0; $k < count($values); $k++) {
            $fName = $head . '_' . sanitize_field_name($values[$k]);
            if(!isset($contentData[$fName])) {
                $contentData[$fName] = '';
            }
        }
    }
}
for ($i = 0; $i < count($customFields); $i++) {
    for($j = 0; $j < count($customFields[$i]); $j++) {
        $parts = explode(":::", $customFields[$i][$j]);
        $head = 'custom';
        if(!empty($parts[0])) $head = $parts[0];
        $values = explode("|", $parts[1]);
        for($k = 0; $k < count($values); $k++) {
            $fName = $head . '_' . sanitize_field_name($values[$k]);
            if(!isset($contentData[$fName])) {
                $contentData[$fName] = '';
            }
        }
    }
}
$formID = rand(0, 100000);
?>
<form method="POST" action="/process_form_submit" onsubmit="return submitForm_NoteSection_{{ $formID }}(this);">
    <?php if($section): ?>
        <input type="hidden" name="section_uid" value="<?= $section->uid?>">
    <?php else: ?>
        <input type="hidden" name="note_uid" value="<?= $note->uid?>">
        <input type="hidden" name="section_template_uid" value="<?= $sectionTemplate->uid ?>">
    <?php endif; ?>

    <div class="font-weight-bold mb-2">Common Surgeries</div>

    <div class="row">
        @for ($i = 0; $i < count($commonFields); $i++)
            <div class="col-md-3">
                @for($j = 0; $j < count($commonFields[$i]); $j++)
                    <?php
                        $parts = explode(":::", $commonFields[$i][$j]);
                        $head = 'common';
                        if(!empty($parts[0])) $head = $parts[0];
                        $values = explode("|", $parts[1]);
                    ?>
                    <div class="mb-3">
                        @if($head !== 'common')
                            <div class="font-weight-bold mb-2">{{ $head }}</div>
                        @endif
                        @for($k = 0; $k < count($values); $k++)
                            <?php
                                $fName = $head . '_' . sanitize_field_name($values[$k]);
                            ?>
                             <label class="d-flex align-items-center mb-1">
                                 <input type="checkbox" name="{{ $fName }}" {{ $contentData[$fName] ? 'checked' : '' }} class="m-0">
                                 <span class="ml-2">{{ $values[$k] }}</span>
                             </label>
                        @endfor
                    </div>
                @endfor
            </div>
        @endfor
    </div>

    <div class="font-weight-bold mb-2">Custom Items</div>

    <div class="row">
        @for ($i = 0; $i < count($customFields); $i++)
            <div class="col-md-3">
                @for($j = 0; $j < count($customFields[$i]); $j++)
                    <?php
                    $parts = explode(":::", $customFields[$i][$j]);
                    $head = 'custom';
                    if(!empty($parts[0])) $head = $parts[0];
                    $values = explode("|", $parts[1]);
                    ?>
                    <div class="mb-3">
                        @if($head !== 'custom')
                            <div class="font-weight-bold mb-2">{{ $head }}</div>
                        @endif
                        @for($k = 0; $k < count($values); $k++)
                            <?php
                            $fName = $head . '_' . sanitize_field_name($values[$k]);
                            ?>
                            <label class="d-flex align-items-center mb-1">
                                <input type="checkbox" name="{{ $fName }}" {{ $contentData[$fName] ? 'checked' : '' }} class="m-0">
                                <span class="ml-2">{{ $values[$k] }}</span>
                            </label>
                        @endfor
                    </div>
                @endfor
            </div>
        @endfor
    </div>

    <div class="row mb-3">
        <div class="col-12">
            <textarea rte type="text" class="form-control form-control-sm p-2"
                      name="comments"
                      data-shortcuts=""
                      placeholder="Value"><?= isset($contentData['comments']) ? $contentData['comments'] : '' ?></textarea>
        </div>
    </div>

    <div class="form-group m-0 d-flex">
        <button class="btn btn-sm btn-primary mr-2">Submit</button>
        <button class="btn btn-sm btn-default border" onclick="return cancelForm_NoteSection_{{ $formID }}(this)">Cancel</button>
    </div>
</form>
<script>
    function submitForm_NoteSection_{{ $formID }}(_form) {
        showMask();
        $.post(_form.action, $(_form).serialize(), function(_data) {
            fastReload();
        });
        return false;
    }
    function cancelForm_NoteSection_{{ $formID }}(_trigger) {
        $(_trigger).closest('.note-section').toggleClass('edit');
        return false;
    }
</script>
