<?php
include('includes/config/config.php');
include('includes/functions.php');
$start = $_POST['startexpDate'];
$end = $_POST['endexpDate'];
$ohc_id = $_SESSION['current_ohcttype'];

$queryc = "select * from company_profile ";

$resultc = mysqli_query($conn, $queryc);

$row_company = mysqli_fetch_array($resultc);

@extract($row_company);

?>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Mulish:wght@300;400;500;600;700;800&display=swap" rel="stylesheet">
<link href="includes/css-js/admin.css" rel="stylesheet" type="text/css" />

<style>
    * {
        font-family: 'Mulish', sans-serif;
    }

    @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;
        }
    }

    #main-table {
        width: 100%;
        border-collapse: collapse;
        border: 1px solid black;
        font-size: 11px;
    }

    #main-table td {
        padding: 5px;
        border: 1px solid black;
        font-size: 11px;
    }

    #main-table tr:nth-child(even) {
        background-color: lightgoldenrodyellow;
        color: black;
    }

    #main-table thead tr {
        background-color: lightcoral;
    }
</style>

<body>
    <table width="100%" cellspacing="0">
        <tr>

            <td width="20%">
                <?php if (isset($row_company['company_logo']) && $row_company['company_logo'] != null) { ?>
                    <img src="data:<?php echo $row_company['image_type'] ?>;base64,<?php echo base64_encode($row_company['company_logo']) ?>" style="width: 150px; height: 60px; margin: 0px; padding: 0px;">
                <?php } ?>
            </td>

            <td width="60%" align="center">
                <div style="margin-left: 20px; margin-top: 5px; line-height: 1;">
                    <center style="font-size: 15px ; text-transform: capitalize;"><b>
                            <?php echo $row_company['company_name'] ?>
                            <br> <span style="font-size: 11px; ;">MEDICAL DEPARTMENT</span>
                            <pre style="margin-top: 0px; font-size:10px;"><?php echo $row_company['address'] ?></pre>

                            <br> <span style="font-size: 11px; text-decoration: underline;">Clinical Visit Summary Report</span>
                        </b>
                    </center>
                </div>
            </td>
            <td width="40%" align="right">
                <?php if (isset($row_company['right_com_logo']) && $row_company['right_com_logo'] != null) { ?>
                    <img src="data:<?php echo $row_company['right_image_type'] ?>;base64,<?php echo base64_encode($row_company['right_com_logo']) ?>" style="width: 150px; height: 60px; margin: 0px; padding: 0px;">

                <?php } ?>
            </td>
        </tr>



        <tr rowspan="2">
            <td colspan="1"></td>
            <td colspan="2"></td>
        </tr>



        <tr>
            <td colspan="1"></td>
            <td colspan="2"></td>
        </tr>
        <tr>
            <td colspan="1"></td>
            <td colspan="2"></td>
        </tr>

    </table>

    <table width="100%">

        <tr>
            <td align="left" style="font-size: 12px"><strong>Duration: <?php echo $start; ?> -
                    <?php echo $end; ?></strong></td>
            <td align="right" style="font-size: 15px"><button align="left" id="printPageButton" class="btn btn-success" onClick="window.print();">Print</button></td>
        </tr>

    </table>
    <br>

    <table id="main-table" border="1" width="100%" cellspacing="0">
        <thead>
            <tr>
                <th>Sr No.</th>
                <th>Details</th>
                <th>Count</th>
            </tr>
        </thead>


        <?php
        $opd_count = 0;
        $inj_count = 0;



        $query = "select count(*) as count from employee_appointment where  date(appointment_date) between STR_TO_DATE('" . $start . "', '%d-%m-%Y')  AND STR_TO_DATE('" . $end . "', '%d-%m-%Y') and ohc_type_id='" . $ohc_id . "' and appointment_type='O'";
        error_log("query to count employee in opd" . $query);

        if ($result = mysqli_query($conn, $query)) {
            $row = mysqli_fetch_assoc($result);
            $opd_count = $row['count'];
        }

        $query_inj = "select count(*) as count from employee_appointment where  date(appointment_date) between STR_TO_DATE('" . $start . "', '%d-%m-%Y')  AND STR_TO_DATE('" . $end . "', '%d-%m-%Y') and ohc_type_id='" . $ohc_id . "' and appointment_type='I'";
        error_log("query to count employee in opd" . $query_inj);

        if ($result_inj = mysqli_query($conn, $query_inj)) {
            $row_inj = mysqli_fetch_assoc($result_inj);
            $inj_count = $row_inj['count'];
        }

        $data = [];
        $medical_name = [];
        $checkup = "select * from checkup_type where type_status='Active' and type_state='No'";
        $res = mysqli_query($conn, $checkup);
        while ($row_checkup = mysqli_fetch_assoc($res)) {

            array_push($medical_name, $row_checkup['checkup_type_name']);

            $checkup_count = "select count(*) as count from checkup_form where  date(checkup_date) between STR_TO_DATE('" . $start . "', '%d-%m-%Y')  AND STR_TO_DATE('" . $end . "', '%d-%m-%Y') and checkup_type_id = '" . $row_checkup['checkup_type_id'] . "' and ohc_type_id='" . $ohc_id . "'";

            $result_checkup = mysqli_query($conn, $checkup_count);
            $row_checkup_count = mysqli_fetch_assoc($result_checkup);

            $data['emp'][$row_checkup['checkup_type_name']] = $row_checkup_count['count'];

            error_log("query for medical " . $checkup_count);
        }

        error_log("final checkup data  " . print_r($data, true));

        ?>
        <tbody>

            <tr>
                <td><?= ++$count ?></td>
                <td>Opd</td>
                <td><?= $opd_count ?></td>
            </tr>
            <tr>
                <td><?= ++$count ?></td>
                <td>Injury</td>
                <td><?= $inj_count ?></td>
            </tr>


            <?php
            for ($i = 0; $i < sizeof($data['emp']); $i++) {
            ?>
                <tr>
                    <td><?= ++$count ?></td>
                    <td style="text-transform: capitalize !important;"><?= $medical_name[$i] ?></td>
                    <td><?= $data['emp'][$medical_name[$i]] ?></td>
                </tr>

            <?php
            }
            ?>



        </tbody>
    </table>
</body>