<?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 = [
    "common" => [],
    "customFields" => [],
    "comments" => "",
];
$isempty = false;
if ($patient->canvas_data) {
    $canvasData = json_decode($patient->canvas_data, true);
    if (isset($canvasData["sochx"])) {
        $contentData = $canvasData["sochx"];
    }
    else {
        $isempty = true;
    }
}
else {
    $isempty = true;
}

if($isempty) {
    echo '<span class="text-secondary">No social history in the system</span>';
}
else {

    for ($i = 0; $i < count($fields); $i++):
        foreach($fields[$i] as $head => $values):
            for($k = 0; $k < count($values); $k++):
                $fName = $head . '_' . sanitize_field_name($values[$k]);
                if(@$contentData['common'][$fName]): ?>
                    <div>
                        <?= ucwords($head) ?>
                        <i class="fa fa-arrow-right text-sm text-secondary"></i>
                        <span class="font-weight-bold"><?= $values[$k] ?></span>
                        <?php if(@$contentData['common'][$fName . '__comments']): ?>
                            <span class="text-sm ml-1 text-secondary">(<?= $contentData['common'][$fName . '__comments'] ?>)</span>
                        <?php endif; ?>
                    </div>
                <?php
                endif;
            endfor;
        endforeach;
    endfor;

    // custom fields
    if(count($contentData['customFields'])):
        ?> <div class="mt-2"> <?php
        for ($i = 0; $i < count($contentData['customFields']); $i++):
            $item = $contentData['customFields'][$i];
            if($item['value']): ?>
                <div>
                    Custom
                    <i class="fa fa-arrow-right text-sm text-secondary"></i>
                    <span class="font-weight-bold"><?= $item['label'] ?></span>
                    <?php if($item['comments']): ?>
                        <span class="text-sm ml-1 text-secondary">(<?= $item['comments'] ?>)</span>
                    <?php endif; ?>
                </div>
            <?php
            endif;
        endfor;
        ?> </div> <?php
    endif;

    if(isset($contentData['comments'])) { ?>
        <div class="mt-2 mb-1">
            <div class="font-weight-bold">Comments:</div>
            <div><?= $contentData['comments'] ?></div>
        </div>
    <?php }
}
?>
