<?php
include('includes/config/config.php');
include('includes/functions.php');
$start_date = $_POST['startDate2'];
$end_date = $_POST['endDate2'];
$exam_type = $_POST['exam_type'];
$bu_id = $_POST['bu_id'];
error_log($start_date);
error_log($end_date);
error_log('exam_type' . $exam_type);
error_log('bu_id' . $bu_id);
?>
<link href="includes/css-js/admin.css" rel="stylesheet" type="text/css" />
<style>
    @page {
        margin: 15px;
    }

    .btn {
        background-color: #4CAF50;
        border-radius: 5%;
        /* Green */
        border: none;
        color: white;
        padding: 5px 8px;
        text-align: center;
        text-decoration: none;
        display: inline-block;
        font-size: 12px;
        margin: 4px 2px;
        cursor: pointer;
    }

    @media print {
        #printPageButton {
            display: none;
        }
    }
</style>
<?php include('pdf_ohc_header.php') ?>


<table width="100%">
    <tr>

        <td align="center" style="font-size: 18px"><strong>Health Index Record of
                <?php if ($bu_id == 'all') { ?>All <?php } else {
                                                getTableFieldValue('bussiness_unit', 'bu_name', 'bu_id', $bu_id);
                                            } ?> Division</strong></td>

    </tr>
    <tr>

        <td align=" center" style="font-size: 13px;padding-top: 10px;"><strong>(For
                <?= getTableFieldValue('checkup_type', 'checkup_type_name', 'checkup_type_id', $exam_type) ?>)</strong>
        </td>

    </tr>

    <tr>
        <!-- <td align="left" style="font-size: 12px"><strong>For Month: <?= $month_name; ?></strong></td> -->
        <td align="right" style="font-size: 15px"><button align="center" id="printPageButton" class="btn btn-success" onClick="window.print();">Print</button></td>
    </tr>

</table>
<table border="1" width="100%" cellspacing="0">
    <?php

    if ($bu_id == "all") {
        $sql = "select cf.*,p.* from checkup_form cf left join patient_master p on cf.emp_id = p.id where cf.checkup_type_id = '" . $exam_type . "' and date(cf.checkup_date) between str_to_date('" . $start_date . "', '%d-%m-%Y')  and str_to_date('" . $end_date . "', '%d-%m-%Y') ORDER BY cf.checkup_date ASC";
    } else {
        $sql = "select cf.*,p.* from checkup_form cf left join patient_master p on cf.emp_id = p.id where p.bu_id='" . $bu_id . "' and cf.checkup_type_id = '" . $exam_type . "' and date(cf.checkup_date) between str_to_date('" . $start_date . "', '%d-%m-%Y')  and str_to_date('" . $end_date . "', '%d-%m-%Y') ORDER BY cf.checkup_date ASC";
    }
    error_log("query: " . $sql);

    $result = mysqli_query($conn, $sql);

    ?>

    <strong>


        <tr bgcolor="#eeeeee">
            <td width="3%">Sr.</td>
            <td align="left" width="10%">Medical Examination NO</td>
            <td align="left" width="8%">Date Of Exam</td>
            <td align="left" width="8%"> Name </td>
            <td align="left" width="5%"> Employee Code </td>
            <td align="left" width="8%"> Designation </td>
            <td align="left" width="5%"> Division </td>
            <td align="left" width="8%"> Department </td>
            <td align="left" width="3%"> UTE </td>
            <td align="left" width="8%"> Staion </td>
            <td align="left" width="3%"> Age </td>
            <td align="left" width="5%"> Sex </td>
            <td align="left" width="8%"> Mobile No.</td>
            <td align="left" width="3%"> Height (in cms)</td>
            <td align="left" width="3%"> Weight (in kgs)</td>
            <td width="5%" align="left">BMI</td>
            <td align="left" width="3%">FBS</td>
            <td align="left" width="3%">RBS</td>
            <td width="5%" align="left">PPBS</td>
            <td align="left" width="3%">Cholestrol</td>
            <td align="left" width="3%">SBP</td>
            <td align="left" width="3%">DBP</td>
            <td valign="top" align="left" width="8%">Health Index</td>


        </tr>



        <?php
        //   $health_index_sum = 0;
        $count = 1;
        while ($row1 = mysqli_fetch_assoc($result)) {
            extract($row1);

        ?>

            <tr>


                <td><?php echo $count ?></td>

                <td><?php echo $row1['ticket_no'] ?></td>
                <td><?php echo $row1['checkup_date'] ?></td>

                <td><?php echo $row1['patient_name'] ?></td>
                <td><?php echo $row1['emp_code'] ?></td>
                <td><?php echo getTableFieldValue('designation', 'designation_name', 'designation_id', $row1['designation_id']) ?>
                </td>
                <td><?php echo getTableFieldValue('bussiness_unit', 'bu_name', 'bu_id', $row1['bu_id']) ?></td>
                <td><?php echo getTableFieldValue('department', 'dept_name', 'dept_id', $row1['dept_id']) ?></td>
                <td><?php echo getTableFieldValue('section', 'section_name', 'section_id', $row1['section_id']) ?></td>
                <td><?php echo getTableFieldValue('sub_section', 'sub_section_name', 'sub_section_id', $row1['sub_section_id']) ?>
                </td>
                <td><?php echo ageCalculator($row1['dob']) ?></td>

                <td><?php echo $row1['gender'] ?></td>
                <td><?php echo $row1['primary_phone'] ?></td>

                <?php
                $sql_part = "select distinct(cv.checkup_form_key),cv.checkup_form_value,k.key_param_name from checkup_form cf left join checkup_form_key_value cv on cf.checkup_id = cv.checkup_form_id left join checkup_parameter cp on cv.checkup_form_key = cp.column_name  left join key_health_reportable_parameter_master k on k.key_param_id=cp.key_health_map_name where cp.key_health_map_name in (select key_param_id from key_health_reportable_parameter_master where key_param_name in ('height','weight','bmi','fbs','rbs','total cholesterol','ppbs','systolic blood pressure','diastolic blood pressure')) and cf.emp_id='" . $row1['emp_id'] . "' and cv.checkup_form_id='" . $row1['checkup_id'] . "'";
                $pat_val = array();
                error_log("sql part " . $sql_part);
                $result_part = mysqli_query($conn, $sql_part);
                $i = 0;
                while ($row_part = mysqli_fetch_assoc($result_part)) {
                    $pat_val[$row_part['key_param_name']] = $row_part['checkup_form_value'];
                }
                error_log("data :" . print_r($pat_val, true));
                error_log("  length " + sizeof($pat_val));
                ?>
                <td align="left" width="3%"><?= $pat_val['height'] ?></td>
                <td align="left" width="3%"><?= $pat_val['weight'] ?></td>
                <td align="left" width="3%"><?= $pat_val['bmi'] ?></td>
                <td align="left" width="3%"><?= $pat_val['fbs'] ?></td>
                <td align="left" width="3%"><?= $pat_val['rbs'] ?></td>
                <td align="left" width="3%"><?= $pat_val['ppbs'] ?></td>
                <td align="left" width="3%"><?= $pat_val['total cholesterol'] ?></td>
                <td align="left" width="3%"><?= $pat_val['systolic blood pressure'] ?></td>
                <td align="left" width="3%"><?= $pat_val['diastolic blood pressure'] ?></td>
                <?php if ($row1['health_index'] < 4) { ?>
                    <td align="left" width="3%" style='background-color:green;color:white;'><?= $row1['health_index'] ?></td>
                <?php } else if ($row1['health_index'] >= 4 && $row1['health_index'] <=6) { ?>
                    <td align="left" width="3%" style='background-color:orange;color:white;'><?= $row1['health_index'] ?></td>
                <?php } else { ?>
                    <td align="left" width="3%" style='background-color:red;color:white;'><?= $row1['health_index'] ?></td>
                <?php } ?>

            </tr>
        <?php
            $count++;
        }

        ?>
</table>
</body>

<script>
    $(document).ready(function() {
        window.print();

    });
</script>