<?php //include('pdf_header_reverse.php');
include('includes/config/config.php');
include('includes/functions.php');
//include('pop_up_top.php');
$start = $_POST['startexpDate'];
$end = $_POST['endexpDate'];
$pat_cat = $_POST['emp_id'];
// $charge = $_POST['chargeable'];
// $month_name = "$start - $end";
?>
<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;
    }

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

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

    #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 tr:last-child {
        background-color: lightgreen;
        color: black;
    }



    @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('pdf_ohc_header.php'); ?>
    <table width="100%">
        <tr>

            <?php if ($charge == '0') { ?>
                <td style="font-size: 15px;"><strong style="margin-left: 600px">Non Clinical Recovery Report</strong></td>

            <?php
            } else {
            ?>
                <td style="font-size: 10px;"><strong style="margin-left: 600px">Clinical Recovery</strong></td>
            <?php
            }
            ?>


        </tr>
        <tr>
            <td align="left" style="font-size: 12px"><strong>For 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>


    <?php
    $ohc_id = $_SESSION['current_ohcttype'];
    $sql = "select * from patient_master where id= '" . $pat_cat . "'";
    $result = mysqli_query($conn, $sql);

    while ($row = mysqli_fetch_assoc($result)) {
        $data = array();
        array_push($data, $row['id']);


    ?>



        <table id="details">

            <caption style="margin:5px">
                <h1>Patient Details</h1>
            </caption>


            <tr colsapn="2">
                <td width="50%"><b>PATIENT NAME:</b> <?php echo $row['patient_name'] ?> </td>


                <td width="50%"><b>FATHER'S NAME:</b> <?php echo $row['father_name'] ?></td>



            </tr>

            <tr colsapn="2">

                <td width="50%"><b>AGE:</b>
                    <?php echo isset($row['dob']) ? date_diff(date_create($row['dob']), date_create('today'))->y : "Not Available"; ?>
                </td>

                <td width="50%"><b>GENDER:</b> <?php echo $row['gender'] == 'M' ? "Male" : "Female" ?></td>


            </tr>



            <tr colsapn="2">



                <td width="50%"><b>DIVISION:</b>
                    <?php echo getTableFieldValue('bussiness_unit', 'bu_name', 'bu_id', $row['bu_id'], ''); ?></td>

                <td width="50%"><b>DEPARTMENT:</b>
                    <?php echo getTableFieldValue('department', 'dept_name', 'dept_id', $row['dept_id'], ''); ?>
                </td>



            </tr>

            <tr colsapn="2">


                <td width="50%"><b>EMPLOYEE ID:</b> <?php echo $row['emp_code'] ?></td>




                <td width="50%">
                    <b>AREA/SECTION:</b>
                    <?php echo getTableFieldValue('sub_section', 'sub_section_name', 'sub_section_id', $row['sub_section_id'], ''); ?>
                </td>

            </tr>

        </table>

        <br>
        <table border="1" width="100%" cellspacing="0" id="main-table">
            <caption style="margin:5px">
                <h1>Treatment Summary</h1>
            </caption>

            <strong>
                <tr style="background-color:cadetblue; height: 20px;">
                    <th align="left">Sr No</th>
                    <th align="left">Ticket No/Bill No</th>
                    <!-- <th align="left">Employee Name</th> -->
                    <!-- <?php if ($pat_cat == '3') { ?>
                        <th align="left">Employer Contractor</th>
                    <?php } ?> -->
                    <!-- <th align="left">Emp Code</th> -->
                    <th align="left">Treatment/Service</th>
                    <th align="left">Cost</th>
                </tr>
            </strong>

            <?php
            $dependent = "select * from patient_master where primary_patient_id='" . $row['id'] . "'";
            $dep_result = mysqli_query($conn, $dependent);
            while ($row_dep = mysqli_fetch_assoc($dep_result)) {
                array_push($data, $row_dep['id']);
            }

            $ids = join(",", $data);
            error_log("data  " . $ids);

            $opd_cost = 0;
            $lab_cost = 0;
            $xray_cost = 0;
            $opd_cost_details = "";
            $lab_cost_details = "";
            $xray_cost_details = "";

            $xray = "select * from bill_master b left join bill_details bd on bd.bill_id=b.bill_id where ohc_type_id='" . $ohc_id . "' and patient_id in ($ids) and date(bill_date) between STR_TO_DATE('" . $start . "', '%d-%m-%Y')  AND STR_TO_DATE('" . $end . "', '%d-%m-%Y') and bd.product_list = (select product_id from product_service_master where product_name='X RAY')";
            $result_xray = mysqli_query($conn, $xray);
            error_log("xray lab query bill " . $xray);

            if ($row_xray = mysqli_fetch_assoc($result_xray)) {
                $xray_cost = 0;


                $xray_cost_details = "X Ray";
                $xray_cost += $row_xray['total_amount'];
                $type = $row_xray['bill_no'];
            }
            $total_cost += $xray_cost;
            if ($type != "" || $type != null) {
            ?>
                <tr>
                    <td align="left"><?= ++$count ?></td>

                    <td align="left"><?= $type ?></td>
                    <td align="left"><?= $xray_cost_details ?></td>
                    <td align="left"><?= $xray_cost ?></td>


                </tr>

                <?php
            }

            $plab = "select bill_no, group_concat(product_list) as service, sum(total_amount) as lab_cost  from bill_master b left join bill_details bd on bd.bill_id=b.bill_id where ohc_type_id='" . $ohc_id . "' and patient_id in ($ids) and date(bill_date) between STR_TO_DATE('" . $start . "', '%d-%m-%Y')  AND STR_TO_DATE('" . $end . "', '%d-%m-%Y') and bd.product_list != (select product_id from product_service_master where product_name='X RAY') group by bill_no ";

            error_log("p lab query bill " . $plab);


            if ($result_lab = mysqli_query($conn, $plab)) {
                while ($row_lab = mysqli_fetch_assoc($result_lab)) {
                    $lab_cost = 0;

                    $bill_no = $row_lab['bill_no'];
                    $test = getCommaSeperatedValuesForInClause("select product_name from product_service_master", 'product_id', $row_lab['service']);
                    $lab_cost = $row_lab['lab_cost'];
                ?>
                    <tr>
                        <td align="left"><?= ++$count ?></td>

                        <td align="left"><?= $bill_no ?></td>
                        <td align="left"><?= $test ?></td>
                        <td align="left"><?= $lab_cost ?></td>


                    </tr>


                <?php
                    $total_cost += $lab_cost;
                }
            } else {
                $lab_cost =  0;
                $test = "";
                $bill_no = "";
            }




            $opd_details = "select  sum(issued_item_cost) as cost,em.appointment_id,appointment_type,ticket_no from employee_appointment em left join treatment_cost_master t  on t.appointment_id = em.appointment_id where t.ohc_type_id='" . $ohc_id . "' and date(em.appointment_date) between STR_TO_DATE('" . $start . "', '%d-%m-%Y')  AND STR_TO_DATE('" . $end . "', '%d-%m-%Y') and emp_id in ($ids) group by em.appointment_id";

            error_log("opd cost details query " . $opd_details);


            if ($result_opd_details = mysqli_query($conn, $opd_details)) {
                while ($row_opd_details = mysqli_fetch_assoc($result_opd_details)) {
                    $opd_cost = 0;


                    $opd_cost_details = getTreatmentText($row_opd_details['appointment_id']);
                    $opd_cost += $row_opd_details['cost'];
                    $opd_type = $row_opd_details['ticket_no'] . "";


                ?>

                    <tr>
                        <td align="left"><?= ++$count ?></td>

                        <td align="left"><?= $opd_type ?></td>
                        <td align="left"><?= $opd_cost_details ?></td>
                        <td align="left"><?= $opd_cost ?></td>


                    </tr>

                    <?php
                    $total_cost += $opd_cost;
                }
            } else {
                $opd_cost_details =  "";
                $opd_cost = 0;
                $opd_type = "";
            }

            $cost_arr = array();
            $type_arr = array();
            $final_total = 0;

            $checkup = "select * from checkup_type where type_state!='Yes' ";

            $res = mysqli_query($conn, $checkup);

            while ($rowc = mysqli_fetch_assoc($res)) {

                array_push($type_arr, $rowc['checkup_type_name']);

                $check_cost =  $rowc['cost'];

                $final = "select (count(*) * $check_cost) as total_cost from checkup_form c left join  checkup_type t on c.checkup_type_id=t.checkup_type_id where t.checkup_type_id = '" . $rowc['checkup_type_id'] . "' and c.ohc_type_id='" . $ohc_id . "' and c.emp_id in ($ids) and date(checkup_date) between STR_TO_DATE('" . $start . "', '%d-%m-%Y')  AND STR_TO_DATE('" . $end . "', '%d-%m-%Y') ";

                error_log("medical cost query " . $final);

                $resultc = mysqli_query($conn, $final);
                while ($rowsc = mysqli_fetch_assoc($resultc)) {
                    if ($rowsc['total_cost'] != 0) { ?>
                        <tr>
                            <td align="left"><?= ++$count ?></td>
                            <td align="left">Medical Examination</td>
                            <td align="left"><?= $rowc['checkup_type_name'] ?></td>
                            <td align="left"><?= $rowsc['total_cost'] ?></td>


                        </tr>
        <?php }
                    // $cost_arr[$rowc['checkup_type_name']] = $rowsc['total_cost'];

                    $final_total +=  $rowsc['total_cost'];
                    $total_cost += $rowsc['total_cost'];
                }
            }

            error_log("final medical cost val " . print_r($cost_arr, true));
        }

        ?>


        <tr>
            <td colspan="3">Total : </td>
            <td align="left"><?= $total_cost ?></td>
        </tr>
        </table>
</body>

<script>
    $(document).ready(function() {
        window.print();

    });
</script>