<?php session_start();

include('includes/config/config.php');
include_once("includes/functions.php");
include_once("log_entry.php");
mysqli_query($conn, 'SET character_set_results=utf8');
error_reporting(0);

$query = "select * from company_profile where company_id = 5";
$result = mysqli_query($conn, $query);
$row_for_company = mysqli_fetch_array($result);
@extract($row_for_company);
?>



<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
    <title>Medical Certificate of Fitness For Rest</title>
    <style>
        .print {
            position: absolute;
            right: 11%;
        }

        body {
            height: 100%;
            /* background-color: black; */
            width: auto;
        }

        @media print {
            #printPageButton {
                display: none;
            }
        }

        body {
            font-family: Arial, Helvetica, sans-serif
        }

        table {
            border-collapse: collapse;
        }

        .canteen {
            background-color: #76448A;
            /* position: relative; */
            left: 20%;
            text-align: center;
            padding-left: 10%;
            padding-right: 10%;
            color: white;
            font-weight: bold;
        }

        .leftalign {
            text-align: left;
        }

        .rightalign {
            text-align: right;
        }

        .subTitle {
            text-align: center;
            font-size: medium;
            font-weight: bold;
        }

        p {
            font-size: medium;
        }

        .center {

            word-wrap: break-word;
            font-weight: bold;
        }
    </style>
</head>

<body>
    <div id="opd_form_div" style="display: none">
        <form id="opd_form" name="opd_form">

            <center>
                <table>
                    <tr>
                        <td>

                            <div class="container">
                                <div class="container">
                                    <div class="container">

                                        <?php
                                        include "new_pdf_header.php";
                                        ?>
                                        <br>
                                        <hr>
                                        <br>

                                        <center>
                                            <h4 class="canteen" style="background-color: #A3E4D7; padding-top: 5px; padding-bottom: 5px;">Medical Fitness Certificate</h4>

                                            <h6 class="">To Whomsoever It May Concern</h6>

                                        </center>

                                        <br>
                                        <?php
                                        error_reporting(E_ERROR | E_PARSE);
                                        $id = $_REQUEST['flex_sickness_id'];



                                        //echo $id;

                                        $query = "select s.*, e.patient_name,emp_code,e.dob from sickness s, patient_master e where s.emp_id=e.id and s.sickness_id = '" . $id . "' ";
                                        //echo $query;
                                        $existingAilmentList = "";
                                        $ailment = array();
                                        if (!$result = @mysqli_query($conn, $query)) {
                                            error_log("error in loading rest pdf " . mysqli_error($conn));
                                            die(mysqli_error($conn));
                                        } else {
                                            $row = mysqli_fetch_array($result);
                                            extract($row);
                                        }

                                        $DocAttendid = $row['doctor_last_attended'];



                                        ?>

                                        <table style="width: 100%;">
                                            <tr style="font-weight: bold; font-size: 12px">
                                                <td style="width: 50%;">
                                                    <span class=""> <?= $row['ticket_no'] ?> </span>

                                                </td>
                                                <!-- <td style="width: 50%; text-align: right">
                                                <?php if ($row['approval_date'] != '' && $row['approval_date'] != null && $row['approval_date'] != '0000-00-00') { ?>
                                                    <span class="">DATE
                                                        :<?php echo date("d-M-Y", strtotime($row['approval_date'])) ?>
                                                    </span>
                                                <?php } ?>
                                                </td> -->
                                            </tr>
                                        </table>
                                        <br><br>
                                        <div style="font-size: 10px" class="center">
                                            <span style="font-weight: bold; font-size: 12px">THIS IS TO CERTIFY THAT MR./MRS./MISS :</span>
                                            <span style="font-weight: bold; font-size: 12px">&nbsp;<?= '&nbsp;' . $row['patient_name'] . " &nbsp; " ?></span>
                                            <span style="font-weight: bold; font-size: 12px">Age </span>
                                            <span style="font-weight: bold; font-size: 12px">&nbsp;<?= '&nbsp;' . (date('Y') - date('Y', strtotime($row['dob']))) . " &nbsp;  Years" ?></span>
                                            <br>
                                            <br>
                                            <span style="font-weight: bold; font-size: 12px">Emp Code</span>
                                            <span style="font-weight: bold; font-size: 12px">&nbsp;<?= '&nbsp;' . $row['emp_code'] . " &nbsp; " ?></span>
                                            <span style="font-weight: bold; font-size: 12px">IS/WAS UNDER TREATMENT SINCE</span>
                                            <?php if ($row['date_absent'] != '' && $row['date_absent'] != null && $row['date_absent'] != '0000-00-00') {
                                                $sickFrom = date('Y-m-d', strtotime($row['date_absent']));
                                                $sickTo = date('Y-m-d', strtotime($row['date_absent_to']));
                                                $half_day = $row['half_day'];

                                                $fromDate = new DateTime($sickFrom);
                                                $toDate = new DateTime($sickTo);

                                                $interval = $fromDate->diff($toDate);

                                                $days = 0;
                                                if ($half_day == '1') {
                                                    $days = $interval->days + 1 - 0.5;
                                                } else {
                                                    $days = $interval->days + 1;
                                                }
                                            ?>


                                                <span style="font-weight: bold; font-size: 12px">&nbsp;<?= '&nbsp;' . date('d-m-Y', strtotime($row['date_absent'])) . " &nbsp; " ?></span>
                                                <span style="font-weight: bold; font-size: 12px">To </span>
                                                <span style="font-weight: bold; font-size: 12px">&nbsp;<?= '&nbsp;' . date('d-m-Y', strtotime($row['date_absent_to'])) . " &nbsp; " ?></span>

                                                <span style="font-weight: bold; font-size: 12px">FOR <u><?= $days ?> &nbsp;Day(s)</u>
                                                </span>

                                            <?php
                                            } else {
                                                $appointMentid = $row['followup_to_opd'];

                                                $AppointDate = getFieldFromTable('appointment_date', 'employee_appointment', 'appointment_id', $appointMentid);
                                            ?>

                                                <span style="font-weight: bold; font-size: 12px">&nbsp;<?= '&nbsp;' . date('d-m-Y', strtotime($AppointDate)) . " &nbsp; " ?></span>
                                                <span style="font-weight: bold; font-size: 12px">To </span>
                                                <span style="font-weight: bold; font-size: 12px">&nbsp;<?= '&nbsp;' . date('d-m-Y', strtotime($AppointDate)) . " &nbsp; " ?></span>

                                                <span style="font-weight: bold; font-size: 12px">FOR <u><?= "1" ?> &nbsp;Day(s)</u>
                                                </span>



                                            <?php
                                            }
                                            ?>

                                            <!-- <span> Due To >
                                            </span><span style="font-weight: bold; font-size: 12px">&nbsp;<?= '&nbsp;' . $existingAilmentList . " &nbsp; " ?></span> -->

                                            <br><br>

                                            <span style="font-weight: bold; font-size: 12px">HE/SHE IS/WAS ADVISED TREATMENT AND REST FOR THIS PERIOD</span>
                                            <br><br>
                                            <?php
                                            if ($row['comments'] != null && trim($row['comments']) != '') {
                                            ?>
                                                <span style="font-weight: bold; font-size: 12px">DOCTOR COMMENTS : <?= $row['comments'] ?></span>
                                            <?php
                                            }
                                            ?>
                                            <br>
                                            <br>

                                            <b>Fit to resume duty from</b>

                                            <?php
                                            $date = date_create($row['approval_date']);
                                            echo date_format($date, "d-M-Y");
                                            ?>

                                            <br>
                                            <br><br>
                                            <br><br>
                                            <?php

                                            $doc_emp_id = getTableFieldValue('tbl_users', 'emp_id', 'user_id', $row['doctor_last_attended'], '');

                                            error_log("DOC EMP ID : " . $doc_emp_id);

                                            $doc_name = getTableFieldValue('patient_master', 'patient_name', 'id', $DocAttendid, '');

                                            // $result = mysqli_query($conn, $query);

                                            // $row = mysqli_fetch_array($result);

                                            @extract($row);

                                            $sql_doc_details = "select * from employee_signature where emp_id='$DocAttendid'";

                                            $result_doc_details = mysqli_query($conn, $sql_doc_details);
                                            $row_doc_details = mysqli_fetch_array($result_doc_details);
                                            ?>
                                            <table width="100%">
                                                <tr>
                                                    <td><img src="data:<?php echo $row_doc_details['image_type'] ?>;base64,<?php echo base64_encode($row_doc_details['emp_sign']) ?>" style="width: 120px; height: 80px;float: right" /></td>
                                                </tr>

                                            </table>

                                            <br>
                                            <br>
                                            <br>
                                            <br>
                                            <table width="100%">
                                                <tr>
                                                    <td>
                                                        <span style="float: right;">Signature & Stamp</span>
                                                    </td>
                                                </tr>
                                            </table>

                                            <br>
                                            <table width="100%">
                                                <tr>
                                                    <td><span style="float: right;">
                                                            <?php
                                                            echo $doc_name . " ," . $row_doc_details['qualification'];
                                                            ?>
                                                        </span></td>
                                                </tr>
                                            </table>
                                            <br>
                                            <table width="100%">
                                                <tr>

                                                    <td><span style="float: right;">KMC Reg No:
                                                            <?php echo $row_doc_details['registration_no'] ?>
                                                        </span>
                                                    </td>
                                                </tr>

                                            </table>



                                        </div>



                                    </div>
                                </div>
                            </div>
                        </td>
                    </tr>
                </table>
            </center>




            <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous">
            </script>
            <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous">
            </script>
            <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous">
            </script>
        </form>
    </div>

    <?php

    if ($_REQUEST['attachment'] == 'pdf') { ?>

        <form id="employeeDetailsFormPdf" action="" method="POST">

            <input type="text" name="htmlText" id="htmlText" />
            <input type="hidden" name="param" id="param" value="<?php echo $patient_name . "_" . "FITNESS" ?>" />
            <input type="hidden" name="param_value" id="param_value" value="<?php echo $id ?>" />

        </form>

        <script>
            formSubmit();

            function formSubmit() {

                $("#htmlText").val($("#opd_form").html());

                document.forms['employeeDetailsFormPdf'].action = "generate_html_pdf.php";

                document.forms['employeeDetailsFormPdf'].method = "post";

                document.forms['employeeDetailsFormPdf'].submit();

            }
        </script>

    <?php
    } else {

    ?>
        <form id="employeeDetailsFormPdf" action="" method="POST">

            <input type="hidden" name="htmlText" id="htmlText" />

        </form>
        </div>
        <script>
            formSubmit();

            function formSubmit() {

                $("#htmlText").val($("#opd_form").html());

                document.forms['employeeDetailsFormPdf'].action = "pdf_dynamic.php";

                document.forms['employeeDetailsFormPdf'].method = "post";

                document.forms['employeeDetailsFormPdf'].submit();

            }
        </script>
    <?php
    }
    ?>
</body>

</html>