<?php
include ('includes/config/config.php');
include ('includes/functions.php');
$start = $_POST['startbodyDate'];
$end = $_POST['endbodyDate'];
$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%;
        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>
            <?php include ('pdf_ohc_header.php'); ?>
            <table width="100%">
                <tr>
                    <td align="center" style="font-size: 18px">
                        <b>
                            <u><span style="font-size: 11px;">Body System Summary Report</span></u>
                        </b>
                    </td>
                </tr>
            </table><br>
        </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>
        <tbody>
            <?php
            // Fetch ailment IDs and names from ailment_system table
            $sql = "SELECT ailment_sys_id, ailment_sys_name as ailment_name FROM ailment_system";
            $result = $conn->query($sql);

            $ailments = [];
            if ($result->num_rows > 0) {
                while ($row = $result->fetch_assoc()) {
                    $ailments[] = $row;
                }
            }

            $counts = [];
            foreach ($ailments as $ailment) {
                $id = $ailment['ailment_sys_id'];
                $count_sql = "SELECT COUNT(*) as count FROM employee_appointment WHERE FIND_IN_SET('$id', ailment_systems_new) AND ohc_type_id='" . $ohc_id . "' AND date(appointment_date) between STR_TO_DATE('" . $start . "', '%d-%m-%Y')  AND STR_TO_DATE('" . $end . "', '%d-%m-%Y')";

                error_log("query for body system " . $count_sql);
                $count_result = $conn->query($count_sql);
                if ($count_result->num_rows > 0) {
                    while ($count_row = $count_result->fetch_assoc()) {
                        $counts[$id] = $count_row['count'];
                    }
                }
            }

            $sr_no = 1;
            $total = 0;
            foreach ($ailments as $ailment) {
                $id = $ailment['ailment_sys_id'];
                $name = $ailment['ailment_name'];
                $count = isset($counts[$id]) ? $counts[$id] : 0;
                $total += $count;
                if ($count > 0) {
                    ?>

                    <tr>
                        <td align="right"><?= $sr_no ?></td>
                        <td><?= $name ?></td>
                        <td align="right"><?= $count ?></td>
                    </tr>


                    <?php

                    $sr_no++;
                }
            }
            ?>
            <tr>
                <td colspan="3" align="right"> <?= $total ?></td>
            </tr>
        </tbody>
    </table>
</body>