<?php
$fields = [
    [
        "Tobacco:::Current every day smoker|Current some day smoker|Former smoker|Heavy tobacco smoker|Light tobacco smoker|Never smoker|Smoker, current status unknown|Unknown if ever smoked ",
    ],
    [
        "Alcohol:::Do not drink|Drink daily|Frequently drink|Hx of Alcoholism|Occasional drink",
        "Drug Abuse:::IVDU|Illicit drug use|No illicit drug use",
    ],
    [
        "Cardiovascular:::Eat healthy meals|Regular exercise|Take daily aspirin",
        "Safety:::Household Smoke detector|Keep Firearms in home|Wear seatbelts",
    ],
    [
        "Sexual Activity:::Exposure to STI|Homosexual encounters|Not sexually active|Safe sex practices|Sexually active",
        "Birth Gender:::Male|Female|Undifferentiated",
    ]
];

$contentData = false;
if($section){
    $contentData = json_decode($section->content_data, true);
}
if(!$contentData) {
    $contentData = [];
}
for ($i = 0; $i < count($fields); $i++) {
    for($j = 0; $j < count($fields[$i]); $j++) {
        $parts = explode(":::", $fields[$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="row">
        @for ($i = 0; $i < count($fields); $i++)
            <div class="col-md-3">
                @for($j = 0; $j < count($fields[$i]); $j++)
                    <?php
                        $parts = explode(":::", $fields[$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="{{ $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>
