<?php //include('pdf_header_reverse.php');
include('includes/config/config.php');
include('includes/functions.php');
//include('pop_up_top.php');
header("Content-type:application/octet-stream");

header("Content-type: application/x-msdownload");
header("Content-Disposition: attachment; filename=daily_cases_excel.xls");
header("Pragma: no-cache");
header("Expires: 0");
$detention_cat = $_REQUEST['detention_cat'];
$detention_sub_cat = $_REQUEST['detention_sub_cat'];
$only_referral = $_REQUEST['only_referral'];
$is_depend = '';
$cat = '';
$referral = '';

$emp_id = $_REQUEST['patient1_id'];
if ($emp_id != 'NULL' && $emp_id != '') {
    $emp_filter = " and emp_id='" . $emp_id . "'";
}

if ($only_referral == 'Y') {
    $referral = " and referral!='' ";
}
if ($detention_sub_cat == 'dep') {
    $is_depend = "and primary_patient_id!=0";
}


error_log("detention_cat" . $detention_cat);
if ($detention_cat == '' || $detention_cat == null || $detention_cat == '7') {
    $cat = "";
} else {
    $cat = " and patient_cat_id='$detention_cat'";
}
// $from_date1 = date('Y-m-d', strtotime($_POST['opdDateDailyFrom']));
// $from_date = date('d-m-Y', strtotime($from_date1));
// $from_date2 = date('Y-m-d', strtotime($_POST['opdDateDailyTo']));
// $to_date = date('d-m-Y', strtotime($from_date2));

$from_date = date('Y-m-d', strtotime($_POST['opdDateDailyFrom']));
$to_date = date('Y-m-d', strtotime($_POST['opdDateDailyTo']));
?>
<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">Daily Cases 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>
        </tr>

    </table>
    <br>
    <table border="1" width="100%" cellspacing="0">
        <?php
        $sqll = "SELECT count(*) as total FROM employee_appointment a JOIN patient_master b on a.emp_id=b.id WHERE  a.ohc_type_id='" . $_SESSION['current_ohcttype'] . "' and date(a.appointment_date) BETWEEN '$from_date' and  '$to_date'" . $cat . $is_depend . $referral . $emp_filter . "  ";

        $result_sqll = mysqli_query($conn, $sqll);
        $num_rows = mysqli_num_rows($result_sqll);
        $row0 = mysqli_fetch_assoc($result_sqll);
        error_log("total:" . $sqll);

        $sql = "SELECT DATE_FORMAT(a.appointment_date, '%d-%m-%y') as date,a.*,b.* FROM employee_appointment a JOIN patient_master b on a.emp_id=b.id WHERE date(a.appointment_date) BETWEEN '$from_date' and  '$to_date' and  a.ohc_type_id='" . $_SESSION['current_ohcttype'] . "'" . $cat . $is_depend . $referral . $emp_filter . "  order by a.appointment_date ASC";
        error_log($sql);

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

        ?>

        <strong>
            <tr bgcolor="#eeeeee">
                <td>Sr.</td>

                <td align="left" width="3%">Date</td>
                <td align="left" width="3%">Time In</td>
                <td align="left" width="3%">Time Out</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="3%">Disease Type</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%">Chronic Days</td>
                <td valign="top" align="left" width="3%">Complaint</td>
                <td valign="top" align="left" width="3%">Diagnosis</td>

                <td valign="top" align="left" width="5%">Treatment</td>
                <td valign="top" align="left" width="3%">Plant</td>
                <?php if ($only_referral == 'Y') { ?>
                    <td valign="top" align="left" width="3%">Referred By</td>
                    <!-- <td valign="top" align="left" width="3%">Referred To</td> -->
                <?php } ?>
                <?php if ($only_referral == 'Y') { ?>
                    <td class="referral" valign="top" align="left" width="5%">Referral</td>
                <?php } ?>
                <td valign="top" align="left" width="5%">Remarks</td>
            </tr>

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

                //  error_log($row1['employer_contractor_id']);

            ?>

                <tr>
                    <td><?php echo  $count ?></td>
                    <td><?php echo $row1['date'] ?></td>
                    <td><?php echo date("d/M/Y H:i ", strtotime($row1['appointment_date'])) ?></td>
                    <td><?php if ($row1['clearance_time'] != null && $row1['clearance_time'] != '' && $row1['clearance_time'] != "00-00-00 00:00:00")
                            echo date("d/M/Y H:i ", strtotime($row1['clearance_time'])) ?></td>
                    <!-- <td><?php

                                if ($row1['clearance_time'] != null && $row1['clearance_time'] != '' && $row1['clearance_time'] != "00-00-00 00:00:00") {
                                    $from = strtotime($row1['appointment_date']);

                                    $to   = strtotime($row1['clearance_time']);
                                    $diff = abs($to - $from);

                                    // To get the year divide the resultant date into
                                    // total seconds in a year (365*60*60*24)
                                    $years = floor($diff / (365 * 60 * 60 * 24));

                                    // To get the month, subtract it with years and
                                    // divide the resultant date into
                                    // total seconds in a month (30*60*60*24)
                                    $months = floor(($diff - $years * 365 * 60 * 60 * 24)
                                        / (30 * 60 * 60 * 24));

                                    // To get the day, subtract it with years and
                                    // months and divide the resultant date into
                                    // total seconds in a days (60*60*24)
                                    $days = floor(($diff - $years * 365 * 60 * 60 * 24 -
                                        $months * 30 * 60 * 60 * 24) / (60 * 60 * 24));

                                    // To get the hour, subtract it with years,
                                    // months & seconds and divide the resultant
                                    // date into total seconds in a hours (60*60)
                                    $hours = floor(($diff - $years * 365 * 60 * 60 * 24
                                        - $months * 30 * 60 * 60 * 24 - $days * 60 * 60 * 24)
                                        / (60 * 60));

                                    // To get the minutes, subtract it with years,
                                    // months, seconds and hours and divide the
                                    // resultant date into total seconds i.e. 60
                                    $minutes = floor(($diff - $years * 365 * 60 * 60 * 24
                                        - $months * 30 * 60 * 60 * 24 - $days * 60 * 60 * 24
                                        - $hours * 60 * 60) / 60);

                                    // To get the minutes, subtract it with years,
                                    // months, seconds, hours and minutes
                                    $seconds = floor(($diff - $years * 365 * 60 * 60 * 24
                                        - $months * 30 * 60 * 60 * 24 - $days * 60 * 60 * 24
                                        - $hours * 60 * 60 - $minutes * 60));
                                    error_log('year' . $months);
                                    // Print the result
                                    if ($years != 0 || $years != '0') printf($years . " year,",);

                                    if ($months != 0 || $months != '0') echo ($months . " month,");
                                    if ($days != 0 || $days != '0') echo ($days . " day,");
                                    if ($hours != 0 || $hours != '0') echo ($hours . " hours,");
                                    if ($minutes != 0 || $minutes != '0') echo ($minutes . " minutes,");
                                    if ($seconds != 0 || $seconds != '0') echo ($seconds . " seconds,");
                                } ?>
                    </td> -->
                    <!-- <td><?php echo $row1['shift_type'] ?></td> -->
                    <!-- <td><?php if ($row1['injury_def'] == 'init') {
                                    echo 'Initiated';
                                } elseif ($row1['injury_def'] == 'not-init') {
                                    echo 'Not Initiated';
                                } ?></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 if ($row1['disease_type'] == 'COMM') {
                            echo 'Communicable';
                        } else if ($row1['disease_type'] == 'NON-COMM') {
                            echo 'Non-Communicable';
                        } ?>
                    </td>
                    <!-- <td><?php echo getFieldFromTable('case_name', 'case_type', 'case_code', $row1['case_type'], '') ?></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>
                    <td><?php echo getTableFieldValue('department', 'dept_name', $row1['dept_id'], 'dept_id') ?></td>
                    <!-- <td><?php echo getTableFieldValue('sub_section', 'sub_section_name', $row1['sub_section_id'], 'sub_section_id') ?>
                    </td> -->
                    <td><?php echo getTableFieldValue('section', 'section_name', $row1['section_id'], 'section_id') ?>
                    </td>
                    <td><?= $row1['chronic_day'] ?></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 $complaints ?></td>
                    <td><?php echo getCommaSeperatedValuesForInClause("select ailment_name from ailment ", "ailment_id", $row1['ailments_new']); ?></td>

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

                    <td><?php echo getTableFieldValue('plant_master', 'plant_name', 'plant_id', $row1['plant_id']) ?></td>


                    <?php if ($only_referral == 'Y') { ?>
                        <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 } ?>

                    <?php if ($only_referral == 'Y') { ?>
                        <td class="referral">
                            <?php echo  getCommaSeperatedValuesForInClause("select referral_point_name from referral_point ", "referral_point_id ", $row1['referral']); ?>
                        </td>
                    <?php } ?>

                    <td><?php echo $row1['doc_comment'] ?></td>
                </tr>
            <?php
                $count++;
            }
            ?>
            <tr Height="10px">
                <td colspan="21"><b>TOTAL: &nbsp;<?php echo $row0['total'] ?></b></td>
            </tr>
    </table>
</body>