<?php //include('pdf_header_reverse.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=ambulance_report_excel.xls");
header("Pragma: no-cache");
header("Expires: 0");

$month = $_POST['month18'];
$year = $_POST['year18'];
$month_name = date("F", mktime(0, 0, 0, $month, 10));
$start_date = $_POST['startDate_dm'];
$end_date = $_POST['endDate_dm'];
$ambulance_no = $_POST['ambulance_no'];
?>
<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 align="center" style="font-size: 15px"><strong>AMBULANCE MOVEMENT REPORT</strong></td>

        </tr>
        <tr>
            <td align="left" style="font-size: 12px"><strong>Date: <?php echo $start_date; ?> TO <?php echo $end_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">
        <?php

        if ($_SESSION['RoleCode'] == 'HR') {
            if (!empty($ambulance_no)) {
                $sqll = "SELECT count(*) as total FROM ambulance_usage_details amb left join patient_master p on amb.patient_id=p.id WHERE usage_date between '$start_date' and  '$end_date' and ambulance_id ='$ambulance_no' and p.client_location_id='" . $_SESSION['client_location_id'] . "' ";
                error_log($sqll . "if");
            } else {
                $sqll = "SELECT count(*) as total FROM ambulance_usage_details amb left join patient_master p on amb.patient_id=p.id  WHERE usage_date='" . $ambulance_no . "' and  month(usage_date)='$month' and year(usase_date)='$year' and p.client_location_id='" . $_SESSION['client_location_id'] . "'";
                error_log($sqll, "else");
            }

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

            if (!empty($ambulance_no)) {
                $sql = "SELECT *, DATE_FORMAT(usage_date, '%d-%m-%y') as date FROM ambulance_usage_details amb left join patient_master p on amb.patient_id=p.id   WHERE  usage_date between '$start_date' and '$end_date' and ambulance_id ='$ambulance_no' and p.client_location_id='" . $_SESSION['client_location_id'] . "' order by usage_date ASC";
                error_log($sql . "if ");
            } else {
                $sql = "SELECT * , DATE_FORMAT(usage_date, '%d-%m-%y') as date FROM ambulance_usage_details amb left join patient_master p on amb.patient_id=p.id  WHERE usage_date='" . $ambulance_no . "' and  month(usage_date)='$month' and year(usage_date) =$year and p.client_location_id='" . $_SESSION['client_location_id'] . "' order by usage_date ASC";
                error_log($sql . "else ");
            }
        } else {
            if (!empty($ambulance_no)) {
                $sqll = "SELECT count(*) as total FROM ambulance_usage_details  WHERE usage_date between '$start_date' and  '$end_date' and ambulance_id ='$ambulance_no' ";
                error_log($sqll . "if");
            } else {
                $sqll = "SELECT count(*) as total FROM ambulance_usage_details  WHERE usage_date='" . $ambulance_no . "' and  month(usage_date)='$month' and year(usase_date)='$year' ";
                error_log($sqll, "else");
            }

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

            if (!empty($ambulance_no)) {
                $sql = "SELECT *, DATE_FORMAT(usage_date, '%d-%m-%y') as date FROM ambulance_usage_details   WHERE  usage_date between '$start_date' and '$end_date' and ambulance_id ='$ambulance_no' order by usage_date ASC";
                error_log($sql . "if ");
            } else {
                $sql = "SELECT * , DATE_FORMAT(usage_date, '%d-%m-%y') as date FROM ambulance_usage_details   WHERE usage_date='" . $ambulance_no . "' and  month(usage_date)='$month' and year(usage_date) =$year order by usage_date ASC";
                error_log($sql . "else ");
            }
        }

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

        ?>

        <strong>
            <tr bgcolor="#eeeeee">
                <td align="left" width="2%">Sr.</td>
                <td align="left" width="10%">Date</td>
                <td align="left" width="10%">Ambulance No</td>
                <td align="left" width="8%">Driver Name</td>
                <td align="left" width="10%">From</td>
                <td valign="top" align="left" width="8%">To</td>
                <td valign="top" align="left" width="8%">Out Km</td>
                <td valign="top" align="left" width="8%">In Km</td>
                <td valign="top" align="left" width="8%">Total Km</td>
                <td valign="top" align="left" width="8%">out Time</td>
                <td valign="top" align="left" width="8%">In Time</td>
                <td valign="top" align="left" width="8%">Duty Type</td>
                <td valign="top" align="left" width="8%">Used By</td>
                <td valign="top" align="left" width="8%">Details</td>
            </tr>

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

                <tr>
                    <td><?php echo  $count ?></td>
                    <td><?php echo $row1['date'] ?></td>
                    <td><?php echo getTableFieldValue('ambulance_details_new', 'ambulance_number', 'id', $row1['ambulance_id']) ?></td>
                    <td><?php echo getTableFieldValue('staff_master', 'staff_name', 'staff_id', $row1['driver_id']) ?></td>
                    <td><?php echo $row1['ambulance_from'] ?></td>
                    <td><?php echo $row1['ambulance_to'] ?></td>
                    <td><?php echo $row1['ambulance_out_km'] ?></td>
                    <td><?php echo $row1['ambulance_in_km'] ?></td>
                    <td><?php echo $row1['ambulance_total_km'] ?></td>
                    <td><?php echo $row1['ambulance_out_time'] ?></td>
                    <td><?php echo $row1['ambulance_in_time'] ?></td>
                    <td><?php echo getTableFieldValue('duty_type_master', 'duty', 'duty_id', $row1['duty_type']) ?></td>
                    <td><?php echo getTableFieldValue('patient_master', 'patient_name', 'id', $row1['patient_id']) ?></td>
                    <td><?php echo $row1['ambulance_details'] ?></td>

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