<?php //include('pdf_header.php');
include('includes/config/config.php');
include('log_entry.php');
include_once('includes/functions.php');
header('Content-Type: application/force-download');
header('Content-disposition: attachment; filename=medical_examination_excel.xls');
?>
<!-- <script>
	var checkup_type_id_select = sessionStorage.getItem("checkup_type_id_select");
	console.log(checkup_type_id_select);

</script> -->

<center>

    <?php
    $checkup_type_id = $_REQUEST['checkup_type_id'];
    error_log("checkup_type_id " . $checkup_type_id);
     $sql210 = "select * from checkup_type where checkup_type_id='" . $checkup_type_id . "'";
    error_log("sql210" . $sql210);
    $result210 = mysqli_query($conn, $sql210);
    while ($row210 = mysqli_fetch_array($result210)) {
        $checkup = $row210['checkup_form_section_ids'];
    }
    $sql = 'select distinct section_id,section_name,count(*) as count from (select* from checkup_form_section where Status="Active"and section_id in (' . $checkup . ') ) as c left JOIN checkup_parameter  on checkup_parameter.checkup_form_section_id=c.section_id and enabled="Y" group by section_id order by display_order';
    error_log("sql 1st" . $sql);
    $result = mysqli_query($conn, $sql);


    $sql1 = 'select parameter_value,input_type,section_id,column_order,checkup_parameter_id,parameter_name,column_name from (select checkup_form_section_id,column_order,checkup_parameter_id ,parameter_name,input_type,column_name,parameter_value from checkup_parameter WHERE enabled="Y" and checkup_form_section_id in (' . $checkup . ')) as c inner JOIN checkup_form_section on checkup_form_section.section_id=c.checkup_form_section_id and Status="Active"  order by display_order,column_order';
    $result1 = mysqli_query($conn, $sql1);

    error_log("Fetched result Value " . $sql1);
    ?>
    <table border="1">
        <thead>
            <tr>
                <th>examination_date</th>
                <!-- <th>examination_type</th> -->
                <th>emp_code</th>
                <th>ticket</th>
                <th>patient_name</th>
                <th>father_name</th>
                <th>dob</th>
                <th>doj</th>
                <th>age</th>
                <th>gender</th>
                <th>blood_group</th>
                <th>designation_id</th>
                <th>dept_id</th>
                <th>patient_cat_id</th>
                <th>employer_contractor_id</th>
                <th>bu_id</th>
                <th>aadhar_no</th>
                <th>primary_phone</th>
                <?php
                while ($row = mysqli_fetch_array($result1)) {
                ?>
                    <th>
                        <?= $row['column_name'] ?></th>

                <?php
                }
                ?>

            </tr>

            <tr>
                <th rowspan="2">Examination date </th>
                <!-- <th rowspan="2">Examination Type</th> -->
                <th colspan="16">Patient details</th>
                <?php
                while ($row1 = mysqli_fetch_array($result)) {
                ?>
                    <th colspan="<?= $row1['count'] ?>">
                        <?= $row1['section_name'] ?></th><?php
                                                        }
                                                            ?>
            </tr>

            <tr>
                <th>Employee Code</th>
                <th>Ticket</th>
                <th>Patient name</th>
                <th>Father name</th>
                <th>DOB</th>
                <th>DOJ</th>
                <th>Age</th>
                <th>Gender</th>
                <th>Blood Group</th>
                <th>Designation</th>
                <th>Department</th>
                <th>Patient Category(Employee/Non Employee/Contractor Employee)</th>
                <th>Employer Contractor</th>
                <th>Division</th>
                <th>Adhaar Number</th>
                <th>Phone</th>
                <?php
                mysqli_free_result($result1);
                $result1 = mysqli_query($conn, $sql1);
                while ($row2 = mysqli_fetch_array($result1)) {
                    if ($row2['input_type'] == 'select') {
                        $values = getCommaSeperatedValuesForInClause("select parameter_value_name from checkup_parameter_value", 'parameter_value_id', $row2['parameter_value']);
                ?>
                        <th><?= $row2['parameter_name'] . "(write any one from these only : " . $values . ")" ?></th>

                    <?php
                    } else if ($row2['input_type'] == 'multiselect') {
                        $multivalues = getCommaSeperatedValuesForInClause("select parameter_value_name from checkup_parameter_value", 'parameter_value_id', $row2['parameter_value']);
                    ?>
                        <th><?= $row2['parameter_name'] . "(choose from these and can write multiple with comma separated: " . $multivalues . ")" ?></th>
                    <?php
                    } else {
                    ?>
                        <th><?= $row2['parameter_name'] ?></th>
                <?php
                    }
                }
                ?>
            </tr>
        </thead>
    </table>
</center>