<?php
include('includes/config/config.php');
include('includes/functions.php');

header("Content-type:application/octet-stream");

header("Content-type: application/x-msdownload");
header("Content-Disposition: attachment; filename=patient_opd_comparison_report_excel.xls");
header("Pragma: no-cache");
header("Expires: 0");
error_reporting(E_ERROR | E_PARSE);

$start = date('d-m-Y', strtotime($_POST['opdDate1']));
$end = date('d-m-Y', strtotime($_POST['opdDate2']));
$from_date1 = date('Y-m-d', strtotime($_POST['opdDate1']));
$from_date = date('d-m-Y', strtotime($from_date1));
$from_date2 = date('Y-m-d', strtotime($_POST['opdDate2']));
$to_date = date('d-m-Y', strtotime($from_date2));
$param_name = array();
$param_id = array();
$query_parameter = "SELECT * from opd_vital_parametres ";

$result_query = mysqli_query($conn, $query_parameter);
while ($row_query = mysqli_fetch_assoc($result_query)) {
    array_push($param_name, $row_query['param_name']);
    array_push($param_id, $row_query['column_name']);
}
//  echo print_r($param_id,true);




?>
<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>


<body>

    <?php include('excel_ohc_header.php') ?>
    <table width="100%">
        <tr>

            <td style="font-size: 15px;"><strong style="margin-left: 400px">Patient OPD Comparison Report
                    <?php if ($only_referral == 'Y') {
                        echo "(Referral)";
                    } ?> </strong></td>

        </tr>
        <tr>
            <td align="left" style="font-size: 12px"><strong>From Date:
                    <?php echo date("d-M-Y", strtotime($from_date)); ?>&nbsp;&nbsp;&nbsp;To Date:
                    <?php echo date("d-M-Y", strtotime($to_date)); ?>
                </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>
    <br>
    <table border="1" width="100%" cellspacing="0">
        <strong>
            <tr bgcolor="#eeeeee">
                <td align="left" width="2%">Sr.</td>
                <td align="left" width="4%">Date</td>
                <td valign="top" align="left" width="5%">Ticket No.</td>
                <td valign="top" align="left" width="3%">Emp. No.</td>
                <td valign="top" align="left" width="8%">Name</td>
                <td valign="top" align="left" width="2%">Age</td>
                <td valign="top" align="left" width="2%">Sex</td>

                <td valign="top" align="left" width="5%">Mobile No.</td>
                <!-- <td valign="top" align="left" width="3%">Employer</td>
                <td valign="top" align="left" width="5%">Designation</td>
                <td valign="top" align="left" width="3%">Division</td>
                <td valign="top" align="left" width="3%">Department</td>
                <td valign="top" align="left" width="3%">Section</td> -->

                <!-- <td valign="top" align="left" width="3%">Diagnosis</td> -->


                <!-- 
                <td valign="top" align="left" width="3%">Referred By</td>
 -->
                <?php
                foreach ($param_name as $key => $param) {
                ?>
                    <td valign="top" align="left" width="3%"><?= $param ?></td>

                <?php
                }
                ?>
                <!-- <td valign="top" align="left" width="5%">Treatment</td> -->
            </tr>

            <?php
            $all = $_POST['all'];
            $count = 1;
            $case_type = $_POST['case_type'];
            if ($_POST['case_type'] != '' && $_POST['case_type'] != NULL) {
                $case_filter = "and appointment_type='" . $_POST['case_type'] . "'";
            } else {
                $case_filter = "";
            }

            if ($all == '1') {
                $emp_filter = '';
            } else {
                $emp_filter = " and emp_id='" . $_POST['patient3_id'] . "' ";
            }


            $sql = "SELECT DATE_FORMAT(a.appointment_date, '%d-%m-%y') as date, a.*, b.dob, b.emp_code, b.patient_name, b.primary_phone, b.gender FROM employee_appointment a JOIN patient_master b ON a.emp_id = b.id WHERE a.ohc_type_id = '" . $_SESSION['current_ohcttype'] . "' AND STR_TO_DATE('$from_date1', '%Y-%m-%d') <= date(a.appointment_date) AND date(a.appointment_date) <= STR_TO_DATE('$from_date2', '%Y-%m-%d') " . $emp_filter . $case_filter . " ORDER BY a.appointment_date DESC";


            error_log(" query for data opd vitals" . $sql);


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


            while ($row1 = mysqli_fetch_assoc($result)) {


                if (!in_array($row1['appointment_id'], $app_ids)) {
                    array_push($app_ids, $row1['appointment_id']);

            ?>

                    <tr>

                        <td><?php echo  $count ?></td>
                        <td><?php echo $row1['date'] ?></td>
                        <td><?php echo $row1['ticket_no'] ?></td>
                        <td><?php echo $row1['emp_code'] ?></td>

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

                            $from = new DateTime($row1['dob']);

                            $to   = new DateTime('today');

                            echo $from->diff($to)->y;

                            ?></td>
                        <td><?php if ($row1['gender'] == 'M') {
                                echo 'Male';
                            } else {
                                echo 'Female';
                            } ?></td>

                        <td><?php echo $row1['primary_phone'] ?></td>
                        <!-- <td><?php echo getFieldFromTable('employer_contractor_name', 'employer_contractor', 'id', $row1['employer_contractor_id']) ?>
                </td>
                <td><?php echo getTableFieldValue('designation', 'designation_name', $row1['designation_id'], 'designation_id',) ?>
                </td>
                <td><?php echo getTableFieldValue('bussiness_unit', 'bu_name', $row1['bu_id'], 'bu_id') ?></td>

                <td><?php echo getTableFieldValue('department', 'dept_name', $row1['dept_id'], 'dept_id') ?></td>

                <td><?php echo getTableFieldValue('section', 'section_name', $row1['section_id'], 'section_id') ?>
                </td> -->
                        <?php
                        // $complaint_ids = $row1['complaints'];
                        // $complaint_ids_array = array();
                        // $complaint_ids_array = explode(",", $complaint_ids);
                        // $complaints = "";
                        // for ($i = 0; $i < count($complaint_ids_array); $i++) {
                        //     if ($i == 0) {
                        //         if (is_numeric($complaint_ids_array[$i])) {
                        //             $complaints = getTableFieldValue('complaints', 'complaint', 'complaint_id', $complaint_ids_array[$i]);
                        //         } else {
                        //             $complaints = $complaint_ids_array[$i];
                        //         }
                        //     } else {
                        //         if (is_numeric($complaint_ids_array[$i])) {
                        //             $complaints = $complaints . "," . getTableFieldValue('complaints', 'complaint', 'complaint_id', $complaint_ids_array[$i]);
                        //         } else {
                        //             $complaints = $complaints . "," . $complaint_ids_array[$i];
                        //         }
                        //     }
                        // }

                        ?>

                        <!-- <td> -->
                        <?php // echo getCommaSeperatedValuesForInClause("select ailment_name from ailment ", "ailment_id", $row1['ailments_new']); 
                        ?>
                        <!-- </td> -->

                        <!-- <td><?php
                                    // $emp_id_doc =  getTableFieldValue('tbl_users', 'emp_id', 'user_id', $row1['doctor_last_attended']);

                                    // echo getTableFieldValue('patient_master', 'patient_name', 'id', '"' . $emp_id_doc . '"');
                                    ?>
                </td> -->

                        <?php
                        foreach ($param_id as $key => $param) {
                        ?>
                            <td valign="top" align="left" width="3%"><?= $row1[$param]; ?></td>

                        <?php
                        } ?>
                        <!-- <td><?php

                                    // echo  $treatment_text = getTreatmentTextPdf($row1['appointment_id']);
                                    ?></td> -->

                    </tr>



            <?php
                    $count++;
                }
            }



            ?>

    </table>
</body>