<link rel="stylesheet" href="assets/font-awesome/4.5.0/css/font-awesome.min.css" />

<!-- page specific plugin styles -->
<link rel="stylesheet" href="assets/css/jquery-ui.custom.min.css" />
<link rel="stylesheet" href="assets/css/chosen.min.css" />
<link rel="stylesheet" href="assets/css/bootstrap-datepicker3.min.css" />
<link rel="stylesheet" href="assets/css/bootstrap-timepicker.min.css" />
<link rel="stylesheet" href="assets/css/daterangepicker.min.css" />
<link rel="stylesheet" href="assets/css/bootstrap-datetimepicker.min.css" />
<link rel="stylesheet" href="assets/css/bootstrap-colorpicker.min.css" />
<link rel="stylesheet" href="adminlte/bower_components/bootstrap/less/type.less" />
<script src="assets/js/jquery-ui.custom.min.js"></script>
<script src="assets/js/jquery.ui.touch-punch.min.js"></script>
<script src="assets/js/chosen.jquery.min.js"></script>
<script src="assets/js/spinbox.min.js"></script>
<script src="assets/js/bootstrap-datepicker.min.js"></script>
<script src="assets/js/bootstrap-timepicker.min.js"></script>
<script src="assets/js/jquery.dataTables.min.js"></script>
<script src="assets/js/jquery.dataTables.bootstrap.min.js"></script>
<script src="assets/js/dataTables.buttons.min.js"></script>
<script src="assets/js/buttons.flash.min.js"></script>
<script src="assets/js/buttons.html5.min.js"></script>
<script src="assets/js/buttons.print.min.js"></script>
<script src="assets/js/buttons.colVis.min.js"></script>
<script src="assets/js/dataTables.select.min.js"></script>


<script src="assets/js/moment.min.js"></script>
<script src="assets/js/daterangepicker.min.js"></script>
<script src="assets/js/bootstrap-datetimepicker.min.js"></script>
<script src="assets/js/bootstrap-colorpicker.min.js"></script>
<script src="assets/js/jquery.knob.min.js"></script>
<script src="assets/js/autosize.min.js"></script>
<script src="assets/js/jquery.inputlimiter.min.js"></script>
<script src="assets/js/jquery.maskedinput.min.js"></script>
<script src="assets/js/bootstrap-tag.min.js"></script>
<script src="assets/js/ace-elements.min.js"></script>

<script type="text/javascript">
    $(document).ready(function() {
        $(".glyphicon").css("top", "");


    });

    function open_treatments(appointment_id) {
        $('#modal-add-consultation').modal("show");

    }
</script>
<?php
include('includes/config/config.php');
$rolecode = $_SESSION['RoleCode'];

error_log('tttttfo' . $_REQUEST['followup_to_opd']);
error_log('tttttf' . $_REQUEST["followup_id"]);
error_log('ttttta' .  $_REQUEST['appointmentId']);
$followup_id = $_REQUEST["followup_id"];

$saved_ohc_id = $_SESSION['current_ohcttype'];

$num_rows_treatment = 0;
$num_rows_treatment_external = 0;
$_REQUEST['appointmentId'] = $app_id = $_REQUEST['followup_to_opd'] != '' ? $_REQUEST['followup_to_opd'] : $_REQUEST['appointmentId'];
error_log('app_id ' . $app_id);
if (isset($app_id)) {
    if ($followup_id != '' && $followup_id != null && $followup_id != 0) {
        $sql = "select * from followup_details where followup_id='" . $followup_id . "'";
    } else {
        $sql = "select * from employee_appointment where appointment_id='" . $app_id . "'";
    }

    // echo "query:".$sql;
    $result = mysqli_query($conn, $sql);
    $num_rows = @mysqli_num_rows($result);
    $row = null;

    // echo fhfh;
    if ($num_rows > 0) {
        $row = @mysqli_fetch_array($result);
        $saved_ohc_id = $row['ohc_type_id'];
    }


    if ($_REQUEST['followup_to_opd'] == '' || $_REQUEST['followup_to_opd'] == null || $_REQUEST['followup_to_opd'] == 0) {
        $sql_for_treatment = "select * from treatment where appointment_id='" . $app_id . "'  and is_display!='N' and followup_id='0' ";
    } else {

        if ($followup_id == '' || $followup_id == null) {
            $sql_for_treatment = "select * from treatment where appointment_id='" . $app_id . "'  and is_display!='N' and followup_id!='0' ";
        } else {
            $sql_for_treatment = "select * from treatment where appointment_id='" . $app_id . "'  and is_display!='N' and followup_id='" . $followup_id . "' ";
        }
    }


    error_log('tq' . $sql_for_treatment);
    $result_for_treatment = @mysqli_query($conn, $sql_for_treatment);
    $num_rows_treatment = @mysqli_num_rows($result_for_treatment);


    $sql_for_treatment_external = "select * from treatment_external where appointment_id='" . $app_id . "'";
    $result_for_treatment_external = @mysqli_query($conn, $sql_for_treatment_external);
    $num_rows_treatment_external = @mysqli_num_rows($result_for_treatment_external);

    // If Its a follow up OPD/INJURY case - then add treatment details from the existing
    $treatment_followup_picked = false;

    if (($num_rows_treatment == 0 && $num_rows_treatment_external == 0) && ($row['followup_to_opd'] != null && $row['followup_to_opd'] != "0")) {
        $follow_up_ohc  = getFieldFromTable('ohc_type_id', 'employee_appointment', 'appointment_id', $row['followup_to_opd']);
        if ($follow_up_ohc  == $_SESSION['current_ohcttype']) {

            $sql_for_treatment = "select * from treatment where appointment_id='" . $row['followup_to_opd'] . "'  and is_display!='N'";
            $result_for_treatment = @mysqli_query($conn, $sql_for_treatment);
            $num_rows_treatment = @mysqli_num_rows($result_for_treatment);

            $sql_for_treatment_external = "select * from treatment_external where appointment_id='" . $row['followup_to_opd'] . "'";
            $result_for_treatment_external = @mysqli_query($conn, $sql_for_treatment_external);
            $num_rows_treatment_external = @mysqli_num_rows($result_for_treatment_external);
            $treatment_followup_picked = true;
        }
    }

    error_log('appp' . $row['appointment_date']);
}

?>
<script>
    var rolecode = '<?= $_SESSION['RoleCode'] ?>';
</script>


<form id="opd_form" method="post">
    <div class="widget-box  box box-success" id="opd_form_pdf" style="margin-left: 5px; margin-right: 50px; width: 99%">
        <div class="widget-header" data-action="fullscreen">
            <h5 class="widget-title">Patient OPD > &nbsp; <?= $patient_name . " " . $emp_code ?> &nbsp;<a target="_blank" href="emp_dashboard.php?flex_patient_id=<?= $row['emp_id'] ?>"><i style="font-size: 17px;" class="glyphicon glyphicon-user"></i></a>

                &nbsp;<a target="_blank" href="edit_employee_details.php?active=docsTab&emp_id=<?= $row['emp_id'] ?>"><i style="font-size: 17px;" class="glyphicon glyphicon-file"></i></a>

                &nbsp;<a target="_blank" href="edit_employee_details.php?active=medicalExaminationHistroyTab&emp_id=<?= $row['emp_id'] ?>"><i style="font-size: 17px;" class="ace-icon fa fa-stethoscope"></i></a>



            </h5>



            <div class="widget-toolbar" style="float: right">

                <div class="clearfix">

                    <div class="pull-right tableTools-container">

                        <div class="dt-buttons btn-overlap btn-group  ">

                            <a class="dt-button buttons-csv buttons-html5 btn btn-white btn-primary btn-bold save_button" style="float: left;" onclick="add_consume();"><i class="fa fa-medkit  bigger-110 red" onclick="add_consume();"></i>CONSUMABLES</a>

                            <a class="dt-button buttons-csv buttons-html5 btn btn-white btn-primary btn-bold save_button"
                                style="float: left;" onclick="add_ambulance_stock();"><i class="fa fa-ambulance bigger-110 red"
                                    onclick="add_ambulance_stock();"></i>
                                AMBULANCE</a>







                            <a class="dt-button buttons-csv buttons-html5 btn btn-white btn-primary btn-bold save_button" id="forwardButton" tabindex="0" aria-controls="dynamic-table" data-original-title="" title=""><span><i class="fa fa-floppy-o  bigger-110 green"></i> <span class="hidden">Save</span></span></a> <a class="dt-button buttons-print btn btn-white btn-primary btn-bold" onclick="exporttopdf()" tabindex="0" aria-controls="dynamic-table" data-original-title="" title=""><span><i class="fa fa-print bigger-110 grey"></i> <span class="hidden">Print</span></span></a>



                            <a class="dt-button buttons-print btn btn-white btn-primary btn-bold" data-action="fullscreen" tabindex="0" aria-controls="dynamic-table" data-original-title="" title=""><span><i class="fa fa fa-expand bigger-110 grey"></i>
                                    <span class="hidden">Maximize</span></span></a>


                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="widget-body">
            <div class="widget-main">
                <div class="row">
                    <div class="col-xs-9 table-responsive">
                        <div class="widget-box box box-warning">

                            <div class="widget-body">
                                <div class="widget-main">
                                    <div class="row">
                                        <div class="col-xs-12">
                                            <div class="row">

                                                <div class="col-sm-3">
                                                    <label class="control-label" for="appoint"><strong style="font-size: 11px">Medical Record No</strong></label>
                                                    <input id="ticket_no" type="hidden" style="height: 30px" class="form-control" name="ticket_no" value="<?php echo $row['ticket_no']; ?>" /> <input id="appointment_id" type="hidden" style="height: 30px" class="form-control" name="appointment_id" value="<?= $_REQUEST['appointmentId'] ?>" />
                                                    <input id="followup_id" type="hidden" style="height: 30px" class="form-control" name="followup_id" value="<?= $_REQUEST['followup_id'] ?>" />

                                                    <div class="input-group">


                                                        <?php if ($row['ticket_no'] != '' || $row['ticket_no'] != null) {

                                                            if (($_REQUEST['followup_to_opd'] != '' && $_REQUEST['followup_to_opd'] != null) && ($_REQUEST['followup_id'] == '' || $_REQUEST['followup_id'] == null)) {
                                                                echo creatingTicketNoFollowup();
                                                            } else  if (($_REQUEST['followup_to_opd'] != '' && $_REQUEST['followup_to_opd'] != null) && ($_REQUEST['followup_id'] != '' && $_REQUEST['followup_id'] != null)) {
                                                                echo getTableFieldValue('followup_details', 'ticket_no', 'followup_id', $followup_id);
                                                            } else {

                                                                echo $row['ticket_no'];
                                                            }
                                                        } else {
                                                            if ($_REQUEST['followup_to_opd'] != '' && $_REQUEST['followup_to_opd'] != null) {
                                                                echo creatingTicketNoFollowup();
                                                            } else {
                                                                echo creatingTicketNoOPD();
                                                            }
                                                        } ?>
                                                    </div>
                                                </div>


                                                <div class="col-sm-3">
                                                    <label class="control-label" for="appointment_date"><strong style="font-size: 11px">In Time</strong></label>

                                                    <div class="input-group">
                                                        <span><?= date_format(date_create($row['appointment_date']), "d-M-Y h:i:sa  ") ?>
                                                        </span>
                                                    </div>

                                                </div>

                                                <div class="col-sm-3">
                                                    <label class="control-label" for="appointment_date"><strong style="font-size: 11px">Clearance Time</strong></label>

                                                    <div class="input-group">
                                                        <input id="out_time" type="text" style="height: 30px" class="form-control" name="out_time" /> <span class="input-group-addon"> <i class="fa fa-clock-o bigger-110" style="height: 5px;"></i>
                                                        </span>
                                                    </div>

                                                </div>
                                                <div class="col-sm-3">
                                                    <label class="control-label" for="ailment"><strong style="font-size: 11px">Followup to(if
                                                            Applicable)</strong></label></strong></label>

                                                    <div class="input-group">
                                                        <?php echo getFieldFromTable('ticket_no', 'employee_appointment', 'appointment_id', $row['followup_to_opd']); ?>
                                                    </div>

                                                </div>
                                                <!-- <div class=" col-sm-3">
                                                    <label class="control-label" for="followup_to_opd"><strong
                                                            style="font-size: 11px">Case Type</strong></label>

                                                    <div class="input-group">
                                                        <?php if (strcmp($row['case_type'], 'NEW') == 0) {
                                                            echo 'NEW CASE';
                                                        }
                                                        if (strcmp($row['case_type'], 'REP') == 0) {
                                                            echo 'REPEAT';
                                                        }
                                                        if (strcmp($row['case_type'], 'FUP') == 0) {
                                                            echo 'FOLLOW UP';
                                                        }
                                                        if (strcmp($row['case_type'], 'MED') == 0) {
                                                            echo 'MEDICATION';
                                                        }   ?>

                                                    </div>

                                                </div> -->
                                            </div>


                                            <div class="space-6"></div>

                                            <div class="row">



                                                <div class="col-sm-3">
                                                    <label class="control-label" for="form-field-8"><strong style="font-size: 11px">Complaints</strong></label>
                                                    <div class="input-group">
                                                        <?php
                                                        $complaint_ids = $row['complaints'];
                                                        $complaint_ids_array = array();
                                                        $complaint_ids_array = explode(",", $complaint_ids);
                                                        $complaints = "";
                                                        for ($i = 0; $i < count($complaint_ids_array); $i++) {
                                                            if ($i == 0) {
                                                                if (is_numeric($complaint_ids_array[$i])) {
                                                                    $complaints = getTableFieldValue('complaints', 'complaint', 'complaint_id', $complaint_ids_array[$i]);
                                                                } else {
                                                                    $complaints = $complaint_ids_array[$i];
                                                                }
                                                            } else {
                                                                if (is_numeric($complaint_ids_array[$i])) {
                                                                    $complaints = $complaints . "," . getTableFieldValue('complaints', 'complaint', 'complaint_id', $complaint_ids_array[$i]);
                                                                } else {
                                                                    $complaints = $complaints . "," . $complaint_ids_array[$i];
                                                                }
                                                            }
                                                        }

                                                        echo  $complaints; ?>

                                                    </div>
                                                </div>

                                                <!-- <div class="col-sm-3">
                                                    <label class="control-label" for="ailment_system"><strong
                                                            style="font-size: 11px">Body System</strong></label>
                                                    <div class="input-group">
                                                        <span><?php echo nl2br(
                                                                    getCommaSeperatedValuesForInClause("select ailment_sys_name from ailment_system ", "ailment_sys_id", $row['ailment_systems_new'])
                                                                ) ?></span>

                                                    </div>
                                                </div> -->


                                                <div class="col-sm-3">
                                                    <label class="control-label" for="ailment"><strong style="font-size: 11px">Diagnosis</strong></label>







                                                    <div class="input-group">
                                                        <?php echo nl2br(getCommaSeperatedValuesForInClause("select ailment_name from ailment ", "ailment_id", $row['ailments_new'])) ?>

                                                    </div>
                                                </div>

                                                <div class="col-sm-3">
                                                    <label class="control-label" for="ailment_system"><strong style="font-size: 11px">Disease Type</strong></label>

                                                    <div class="input-group">

                                                        <?php if (strcmp($row['disease_type'], 'COMM') == 0) {
                                                            echo 'COMMUNICABLE';
                                                        }
                                                        if (strcmp($row['disease_type'], 'NON-COMM') == 0) {
                                                            echo 'NON-COMMUNICABLE';
                                                        } ?>

                                                    </div>
                                                </div>
                                                <!-- 
                                                <div class="col-sm-3">
                                                    <label class="control-label" for="form-field-8"><strong
                                                            style="font-size: 11px">Examination
                                                            Findings</strong></label>
                                                    <div class="input-group">
                                                        <?php echo nl2br(
                                                            getCommaSeperatedValuesForInClause("select examination_finding from examination_findings ", "id", $row['examination_remarks'])
                                                        ) ?>
                                                    </div>
                                                </div> -->
                                            </div>

                                            <div class="space-6"></div>

                                            <div class="row">





                                                <!-- <div class="col-sm-3">
                                                    <label class="control-label" for="form-field-8"><strong
                                                            style="font-size: 11px">Referred By</strong></label>
                                                    <div class="input-group">
                                                        <?php echo getTableFieldValue('referred_by_master', 'referred_by', 'id', $row['referred_by']);
                                                        ?></div>
                                                </div> -->

                                                <!-- <div class="col-sm-3">
                                                    <label class="control-label" for="ailment_system"><strong
                                                            style="font-size: 11px">Referred To</strong></label>

                                                    <div class="input-group">

                                                        <?php if (strcmp($row['referred_to'], 'DEPT') == 0) {
                                                            echo "DEPARTMENT";
                                                        }
                                                        if (strcmp($row['referred_to'], 'HR') == 0) {
                                                            echo "HR";
                                                        }
                                                        if (strcmp($row['referred_to'], 'IR') == 0) {
                                                            echo "IR";
                                                        }
                                                        if (strcmp($row['referred_to'], 'HOS') == 0) {
                                                            echo "HOSPITAL";
                                                        } ?>

                                                    </div>
                                                </div> -->


                                                <!-- 
                                                <div class="col-sm-3">
                                                    <label class="control-label" for="form-field-8"><strong
                                                            style="font-size: 11px">Remarks/Follow-up Investigation
                                                            Details</strong></label>

                                                    <div class="input-group">

                                                        <?php echo $row['remarks_rece'] ?>

                                                    </div>
                                                </div> -->

                                            </div>

                                            <div class="space-6"></div>

                                            <div class="row">
                                                <div class="col-sm-3">
                                                    <label class="control-label" for="additional_advices"><strong style="font-size: 11px">Is the Patient taking any
                                                            Medications - Give Details:</strong></label>
                                                    <div class="input-group">
                                                        <?php echo $row['any_medication'] ?>
                                                    </div>
                                                </div>

                                                <div class="col-sm-3">
                                                    <label class="control-label" for="additional_advices"><strong style="font-size: 11px">H/O Drug
                                                            Allergies:</strong></label>
                                                    <div class="input-group">

                                                        <?php echo ucwords(strtolower(getDrugAllergyData($row['emp_id']))); ?>
                                                    </div>
                                                </div>



                                                <div class="col-sm-3">
                                                    <label class="control-label" for="followup"><strong style="font-size: 11px">Follow-up</strong></label>

                                                    <div class="input-group">
                                                        <?= nl2br(($row['followup'] != '' && $row['followup'] != null && $row['followup'] != '0000-00-00') ? $row['followup'] : "N/A") ?>
                                                    </div>
                                                </div>

                                                <div class="col-sm-3" style="word-wrap: break-word; word-break: break-all;">
                                                    <label class="control-label" for="referral"><strong style="font-size: 11px">Doctor Comment</strong></label>
                                                    <div class="input-group ">
                                                        <?php echo nl2br($row['doc_comment']) ?>
                                                    </div>
                                                </div>
                                                <!-- 
                                                <div class="col-sm-3">
                                                    <label class="control-label" for="additional_advices"><strong
                                                            style="font-size: 11px">Remarks:</strong></label>


                                                    <?php echo $row['add_remarks'] ?>
                                                </div> -->
                                            </div>

                                            <div class="space-6"></div>

                                            <div class="row">

                                                <div class="col-sm-3">
                                                    <label class="control-label " for="medicine_disbursement"><strong style="font-size: 11px">Medicine Disbursement</strong>
                                                    </label>
                                                    &nbsp;&nbsp;&nbsp;&nbsp;
                                                    <?= ($row['medi_disbursement'] == 'Y') ? "Yes" : "No" ?>

                                                </div>


                                                <div class="col-sm-3">
                                                    <label class=" control-label " for="doctor_discussed"><strong style="font-size: 11px">Doctor Discussed?</strong> </label>
                                                    &nbsp;&nbsp;&nbsp;&nbsp;
                                                    <?= ($row['doctor_discussed'] == 'Y') ? "Yes" : "No" ?>
                                                </div>
                                                <div class="col-sm-3">
                                                    <label class=" control-label " for="doctor_discussed"><strong style="font-size: 11px">Doctor Consulted?</strong> </label>
                                                    &nbsp;&nbsp;&nbsp;&nbsp;
                                                    <?= ($row['doctor_attended_flag'] == 'Y') ? "Yes" : "No" ?>
                                                </div>
                                                <?php
                                                $issue_by = '';
                                                $doc_emp_id = getTableFieldValue("tbl_users", "emp_id", "user_id", '"' . $row['doctor_last_attended'] . '"');
                                                $issue_by = getTableFieldValue('patient_master', "patient_name", 'id', '"' . $doc_emp_id . '"');

                                                if ($issue_by != '' && $issue_by != null) {
                                                ?>
                                                    <div class="col-sm-3">
                                                        <label class=" control-label " for="doctor_discussed"><strong style="font-size: 11px">Consulted Doctor</strong> </label>
                                                        &nbsp;&nbsp;&nbsp;&nbsp;
                                                        <?php echo $issue_by; ?>
                                                    </div>
                                                <?php } ?>
                                            </div>

                                            <div class="space-6"></div>

                                            <!-- <div class="row">
                                                <div class="col-sm-12 table-responsive">
                                                    <?php $appointment_id = $_REQUEST['appointmentId']; ?>
                                                    <?php include('treatment_partial_edit_common.php'); ?>

                                                </div>
                                            </div> -->


                                            <div class="row">
                                                <!-- <div class="col-sm-12">
                                                    <button type="button" class="btn" data-toggle="modal"
                                                        data-target="#medDetails" onclick="loadMedData();"><i
                                                            class="fa fa-info-circle"></i></button>
                                                    <?php if ($row['doctor_discussed'] == 'N' || $row['doctor_discussed'] == '') { ?>
                                                    <button type="button" class="btn" id="load-treatment-btn"
                                                        onclick="loadTreatment();">Load Treatment</button>
                                                    <?php } ?>
                                                    <div class="loader"></div>
                                                </div> -->
                                                <div class="col-sm-12 table-responsive">
                                                    <table id="treatmentTable<?php echo $row_history['appointment_id'] ?>" class="table table-striped table-bordered" style="padding: 2px; margin: 2px;">
                                                        <thead>
                                                            <tr class="success">
                                                                <th style="font-size: 9px; text-align: center;">Medicine
                                                                </th>
                                                                <th style="font-size: 9px; text-align: center;">
                                                                    Frequency</th>
                                                                <th style="font-size: 9px; text-align: center;">Timing
                                                                </th>
                                                                <th style="font-size: 9px; text-align: center;">Admin.
                                                                    Route</th>
                                                                <th style="font-size: 9px; text-align: center;">Duration
                                                                </th>
                                                                <th style="font-size: 9px; text-align: center;"> Daily
                                                                    Dose Qty
                                                                </th>
                                                                <th style="font-size: 9px; text-align: center;">Total
                                                                    Qty</th>
                                                                <th style="font-size: 9px; text-align: center;">
                                                                    Available
                                                                    Qty</th>
                                                                <th style="font-size: 9px; text-align: center;">Qty
                                                                    Issued</th>
                                                                <th style="font-size: 9px; text-align: center;"></th>
                                                            </tr>
                                                        </thead>
                                                    </table>
                                                </div>
                                            </div>


                                            <!-- sick-leave -->
                                            <?php

                                            $sickness_id = "";
                                            $past_ticket_no = "";
                                            $past_agency = "";
                                            $past_des = "";
                                            $past_from_date = "";
                                            $past_to_date = "";
                                            $past_date_absent = "";

                                            $past_date_absent_to = "";
                                            $past_date_return = "";
                                            $existingAilmentList = "";
                                            $ailment_id = "";
                                            $height = "";
                                            $weight = "";
                                            $bp = "";
                                            $pulse = "";
                                            $chest_ex = "";
                                            $chest_uex = "";
                                            $nose = "";
                                            $throat = "";
                                            $rs = "";
                                            $cvs = "";
                                            $cns = "";
                                            $sickness_status = "";
                                            $blood = '';
                                            $half_day = '';
                                            $certificate_type = '';
                                            if (!empty($app_id)) {

                                                $query_sick = "select s.*,DATEDIFF(date_absent_to ,date_absent) AS date_difference, e.patient_name from sickness s, patient_master e where s.emp_id=e.id and s.followup_to_opd = '" . $_REQUEST['appointmentId'] . "' and  s.followup_id = '" . $_REQUEST['followup_id'] . "' ";
                                                //    echo ('leave='.$query_sick);
                                                if (!$result_sick = @mysqli_query($conn, $query_sick)) {
                                                    die(mysqli_error($conn));
                                                } else {
                                                    $row_sick = @mysqli_fetch_array($result_sick);
                                                    @extract($row_sick);


                                                    $sickness_id = $row_sick['sickness_id'];
                                                    $past_ticket_no = $row_sick['ticket_no'];
                                                    $past_agency = $row_sick['agency'];
                                                    $past_des = $row_sick['des_future'];
                                                    $past_from_date = $row_sick['from_date'];
                                                    $past_to_date = $row_sick['to_date'];
                                                    $past_date_absent = $row_sick['date_absent'];
                                                    $certificate_type = $row_sick['certificate_type'];
                                                    // echo "ddd".$row_sick['date_absent_to'];
                                                    $past_date_absent_to = $row_sick['date_absent_to'];
                                                    $past_date_return = $row_sick['date_return'];
                                                    $existingAilmentList = $row_sick['ailment_name'];
                                                    $ailment_id = $row_sick['ailment_system'];
                                                    $height = $row_sick['height'];
                                                    $weight = $row_sick['weight'];
                                                    $bp = $row_sick['bp'];
                                                    $pulse = $row_sick['pulse'];
                                                    $chest_ex = $row_sick['chest_ex'];
                                                    $chest_uex = $row_sick['chest_uex'];
                                                    $nose = $row_sick['nose'];
                                                    $throat = $row_sick['throat'];
                                                    $rs = $row_sick['rs'];
                                                    $cvs = $row_sick['cvs'];
                                                    $cns = $row_sick['cns'];
                                                    $sickness_status = $row_sick['attended_status'];
                                                    $blood = $row_sick['blood'];
                                                    $half_day = $row_sick['half_day'];
                                                    $date_difference = $row_sick['date_difference'];
                                                }
                                            }

                                            if (empty($sickness_status)) {
                                                if ($_SESSION['RoleCode'] == 'DOC') {
                                                    $sickness_status = 'DRA';
                                                } else {
                                                    $sickness_status = 'DRP';
                                                }
                                            }
                                            if ($sickness_id != '' && $sickness_id != null && $sickness_id != 0) {
                                            ?>
                                                <div class="space-6"></div>



                                                <div class="row">
                                                    <div class="col-xs-12">


                                                        <?php
                                                        //$ticket_no = getTableFieldValue('employee_appointment','max(appointment_id)');
                                                        if (!empty($past_ticket_no)) {
                                                            $ticket_no_sick = $past_ticket_no;
                                                        } else {
                                                            $ticket_no_sick = creatingTicketNoFitness();
                                                        }

                                                        ?>
                                                        <input id="past_ticket_no" type="hidden" name="past_ticket_no" value="<?php echo $ticket_no_sick; ?>" />
                                                        <input type="hidden" class="form-control" name="sickness_id" id="sickness_id" value="<?php echo $sickness_id ?>" required />


                                                        <input id="rest_from_time" type="hidden" class="form-control" style="height: 30px" name="rest_from_time" />

                                                        <input id="rest_to_time" type="hidden" class="form-control" style="height: 30px" name="rest_to_time" />

                                                        <input class="form-control" type="hidden" name="sickness_status" id="sickness_status" value="<?php echo $sickness_status ?>" required />

                                                        <div class="form-group col-sm-1" style=" padding-left: 0px;">
                                                            <label for="role"><b>Half Day</b>&nbsp;</label>
                                                            <div class=" form-group ">

                                                                <?php if ($half_day == 1) {
                                                                    echo 'Yes';
                                                                } else {
                                                                    echo 'No';
                                                                } ?>
                                                            </div>

                                                        </div>
                                                        <div class="form-group col-sm-2">
                                                            <label for="role">Period of Rest From</label>
                                                            <div class="input-group form-group ">
                                                                <?php echo  date_format(date_create($past_date_absent), "d-M-Y")  ?>

                                                            </div>

                                                        </div>

                                                        <div class="form-group col-sm-2">
                                                            <label for="role">Period 0f Rest To</label>

                                                            <div class="input-group">
                                                                <?php echo  date_format(date_create($past_date_absent_to), "d-M-Y")  ?>

                                                            </div>
                                                        </div>


                                                        <div class="form-group col-sm-2">
                                                            <label for="role">Days Of Rest</label>

                                                            <div class="input-group">
                                                                <?php if ($date_difference == '' || $date_difference == 0 || $date_difference == null) {
                                                                    if ($half_day == 1) {
                                                                        echo ' ';
                                                                    } else {
                                                                        echo '1';
                                                                    }
                                                                } else {
                                                                    if ($half_day == 1) {
                                                                        echo $date_difference;
                                                                    } else {
                                                                        echo $date_difference + 1;
                                                                    }
                                                                }
                                                                if ($half_day == 1) {
                                                                    if ($date_difference == '' || $date_difference == 0 || $date_difference == null) {
                                                                        echo 'Half ';
                                                                    } else {
                                                                        echo ' And Half ';
                                                                    }
                                                                } ?>
                                                                Days

                                                            </div>
                                                        </div>

                                                        <div class="col-sm-2">
                                                            <label for="role">Certification</label>
                                                            <div class="input-group ">
                                                                <?php if ($certificate_type != '' || $certificate_type != null) {
                                                                    echo "<a href=\"#\" class=\"green\" onclick=\"open_pdf_fitness_certificate('" . $sickness_id . "','" . $certificate_type . "');\"><i  class=\"fa fa-file-pdf-o bigger-130\" style=\"font-size:17px\" ></i></a>";
                                                                } ?>
                                                            </div>
                                                        </div>


                                                        <div class="col-sm-3">
                                                            <label class="control-label" for="appointment_id"><strong style="font-size: 11px">Leave
                                                                    Remarks:</strong></label>
                                                            <div class="input-group ">
                                                                <?php echo $row['injury_remarks'] ?>
                                                            </div>
                                                        </div>


                                                    </div>

                                                </div>
                                            <?php  } ?>

                                            <div class="space-6"></div>


                                            <!-- <div class="row">
                                                <div class="col-sm-6  table-responsive">
                                                    <label class="control-label" for="referral"><strong style="font-size: 11px">Additional Recommendations(If
                                                            Any)</strong></label>
                                                    <div class="input-group ">
                                                        <?php echo nl2br($row['external_treatments']) ?>
                                                    </div>
                                                </div>
                                                <div class="col-sm-6  table-responsive">
                                                    <label class="control-label" for="referral"><strong style="font-size: 11px">Doctor Comment</strong></label>
                                                    <div class="input-group ">
                                                        <?php echo nl2br($row['doc_comment']) ?>
                                                    </div>
                                                </div>
                                            </div> -->
                                            <div class="space-6"></div>




                                        </div>
                                        <!-- /.col-xs-12 -->
                                    </div>
                                    <!--row end-->

                                </div>
                                <!--end of widget-main-->
                            </div>
                            <!--end of widget-body-->
                        </div>
                        <!--end of widget-box-->
                    </div>




                    <div class="col-xs-3 table-responsive">
                        <div class="widget-box box box-danger">

                            <div class="widget-body">
                                <div class="widget-main">
                                    <div class="row">
                                        <!-- <div class="col-sm-12">
                                            <label class="control-label" for="form-field-select-1"><strong style="font-size: 11px">Emergency?</strong></label>
                                            : <?php if ($row['IsEmergency'] == 0) {
                                                    echo "No";
                                                } else {
                                                    echo "Yes";
                                                } ?>


                                        </div> -->
                                        <!-- <div class="col-sm-6">
                                            <label class="control-label" for="form-field-select-1"><strong style="font-size: 11px">IsChargeable?</strong></label>
                                            : <?php if ($row['IsChargeable'] == 0) {
                                                    echo "No";
                                                } else {
                                                    echo "Yes";
                                                } ?>
                                        </div> -->
                                    </div>
                                    <div class="space-6"></div>
                                    <div class="row">
                                        <div class="col-sm-4">WEIGHT:<?php echo $row['weight'] ?></div>
                                        <div class="col-sm-4">HEIGHT:<?php echo $row['height'] ?></div>
                                        <div class="col-sm-4">BMI:<?php echo  $row['bmi'] ?></div>
                                    </div>
                                    <div class="space-6"></div>

                                    <div class="row">

                                        <div class="col-sm-4">SBP:<?php echo $row['bp_sbp'] ?></div>
                                        <div class="col-sm-4">DBP:<?php echo $row['bp_dbp'] ?></div>

                                        <div class="col-sm-4">Temp:<?= $row['temperature'] ?></div>
                                    </div>

                                    <div class="space-6"></div>
                                    <!-- <div class="row">
                                        <div class="col-sm-4">FBS:<?= $row['blood_sugar_fbs'] ?></div>
                                        <div class="col-sm-4">RBS:<?= $row['blood_sugar_rbs'] ?></div>
                                        <div class="col-sm-4">PPBS:<?= $row['blood_sugar_ppbs'] ?></div>

                                    </div> -->
                                    <div class="space-6"></div>
                                    <div class="row">

                                        <div class="col-sm-4">SPOC2%:<?= $row['spo2_percent'] ?></div>
                                        <div class="col-sm-4">R RATE:<?php echo $row['respiratory_rate'] ?></div>
                                        <div class="col-sm-4">Heart Rate:<?php echo $row['heart_rate'] ?></div>
                                    </div>
                                    <div class="space-6"></div>
                                    <div class="row">

                                        <div class="col-sm-4">Pulse:<?php echo $row['pulse_output'] ?></div>
                                        <div class="col-sm-6">GLASGOW COMA
                                            SCALE:<?php echo $row['glasgow_coma_scale'] ?></div>


                                    </div>
                                    <div class="space-6"></div>
                                    <!-- <div class="row">
                                        <div class="col-sm-4">AVPU:<?php echo $row['avpu'] ?></div>
                                        <div class="col-sm-4">TRAMA:<?php echo $row['trama'] ?></div>
                                        <div class="col-sm-4">MOBILITY:<?php echo $row['mobility'] ?></div>
                                    </div> -->
                                    <div class="space-6"></div>

                                    <!-- <div class="space-6"></div> -->
                                    <!-- <div class="row">
                                        <div class="col-sm-12">Total Cholesterol :
                                            <?php echo $row['total_cholesterol'] ?></div>
                                    </div> -->
                                    <!-- <div class="space-6"></div> -->

                                    <!-- <div class="row">
                                        <div class="col-sm-12">
                                            <label class="control-label" for="injury_part"><strong
                                                    style="font-size: 11px">Affected Body Parts</strong></label>

                                            <div class="input-group"> <?php echo $row['injury_parts_new'] ?></div>


                                           
                                        </div>

                                    </div> -->
                                    <input name="isTreatmentOnly" type="hidden" value="Y">
                                    <div class="space-6"></div>
                                    <div class="row">
                                        <div class="col-sm-12">
                                            <label class="control-label" for="health_advice_name"><strong style="font-size: 11px">Advices</strong></label>
                                            <div class="input-group"><?php echo nl2br($row['health_advices_new']) ?>
                                            </div>

                                        </div>
                                    </div>
                                    <div class="space-6"></div>
                                    <div class="row">
                                        <div class="col-sm-12">
                                            <label class="control-label" for="additional_advices"><strong style="font-size: 11px">Doctor Recommended Tests</strong></label>

                                            <div class="input-group"> <?php echo determineDataFormat($row['recommended_tests_new']); ?></div>

                                        </div>
                                    </div>
                                    <div class="row">
                                        <div class="col-sm-12">
                                            <label class="control-label" for="additional_advices"><strong style="font-size: 11px">Doctor Recommended Test Parameters</strong></label>

                                            <div class="input-group"> <?php
                                                                        echo ucwords(strtolower(getCommaSeperatedValuesForInClause("select Parameter_name from checkup_parameter", "checkup_parameter_id", $row['recommended_test_param_new'])));
                                                                        ?></div>

                                        </div>
                                    </div>
                                    <div class="row">
                                        <div class="col-sm-12">
                                            <label class="control-label" for="additional_advices"><strong style="font-size: 11px">Chronic Illness</strong></label>

                                            <div class="input-group">
                                                <?php
                                                echo ucwords(strtolower(implode(",", getChronicIllness($row['emp_id']))));
                                                ?>
                                            </div>

                                        </div>
                                    </div>
                                    <div class="space-6"></div>
                                    <!-- <div class="row">
                                        <div class="col-sm-12">
                                            <label class="control-label" for="additional_advices"><strong style="font-size: 11px">Additional Precautions</strong></label>
                                            <div class="input-group"><?php echo nl2br($row['remarks']) ?></div>

                                        </div>
                                    </div> -->
                                    <div class="space-6"></div>
                                    <!-- <div class="row">
                                        <div class="col-sm-12  table-responsive">
                                            <label class="control-label" for="followup_remarks"><strong style="font-size: 11px">Follow-Up Remarks(If Any)</strong></label>
                                            <div class="input-group"><?= $row['followup_remarks'] ?></div>
                                        </div>
                                    </div> -->
                                    <div class="space-6"></div>
                                    <!-- <div class="row">
                                        <div class="col-sm-12  table-responsive">
                                            <label class="control-label" for="test_result"><strong style="font-size: 11px">Test Result</strong></label>
                                            <div class="input-group"><?= $row['test_result'] ?></div>
                                        </div>
                                    </div> -->

                                </div>
                            </div>


                        </div>
                    </div>

                    <div class="col-xs-9 table-responsive">
                        <div class="widget-box box box-success">

                            <div class="widget-body">
                                <div class="widget-main">

                                    <div class="row">
                                        <div class="col-xs-12">

                                            <div class="col-sm-3">
                                                <label class="control-label" for="referral"><strong style="font-size: 11px">Referral</strong></label>
                                                <div class="input-group">
                                                    <?php echo getCommaSeperatedValuesForInClause("select referral_point_name from referral_point ", "referral_point_id ", $row['referral']) . ' - ' . getCommaSeperatedValuesForInClause("select city from referral_point ", "referral_point_id ", $row['referral']) . ' ( ' . getCommaSeperatedValuesForInClause("select hospital_name from referral_point ", "referral_point_id ", $row['referral']) . ' ) ' ?>
                                                </div>
                                            </div>
                                            <div class="col-sm-4">
                                                <label class="control-label" for="referral"><strong style="font-size: 11px">Referral Reason</strong></label>
                                                <div class="input-group" style="word-wrap: break-word; word-break: break-all;">
                                                    <?php echo $row['referral_doc'] ?>
                                                </div>
                                            </div>




                                            <div class="col-sm-3">
                                                <label class="control-label" for="additional_advices"><strong style="font-size: 11px">Ambulance Out Time</strong></label>

                                                <div class="input-group" id="discharge">
                                                        <span><?= $row['ambulance_out_time']!=null?date_format(date_create($row['ambulance_out_time']), "d-M-Y h:i:sa  "):'NA' ?>
                                                        </span>
                                                    </div>

                                                <?php if (strcmp($row['is_admitted'], 'Y') == 0) { ?>

                                                    <!-- <div class="input-group" id="discharge">
                                                        <span><?= date_format(date_create($row['discharge_time']), "d-M-Y h:i:sa  ") ?>
                                                        </span>
                                                    </div> -->
                                                <?php }
                                                if (strcmp($row['is_admitted'], 'N') == 0) { ?>
                                                    <!-- <div>
                                                        <span>N/A</span>
                                                    </div> -->
                                                <?php } ?>
                                            </div>

                                            <div class="col-sm-2">
                                                <label class="control-label" for="additional_advices"><strong style="font-size: 11px">Ambulance In Time</strong></label>

                                                <div class="input-group" id="discharge">
                                                        <span><?= $row['ambulance_in_time']!=null?date_format(date_create($row['ambulance_in_time']), "d-M-Y h:i:sa  "):'NA' ?>
                                                        </span>
                                                </div>

                                                <?php //if (strcmp($row['ambulance_used'], 'Y') == 0) {
                                                    // echo "YES";
                                                // }
                                                // if (strcmp($row['ambulance_used'], 'N') == 0) {
                                                    // echo "NO";
                                                // }  
                                                  ?>


                                            </div>

                                           


                                        </div>

                                        <div class="col-xs-12">
                                            <div class="col-sm-2">
                                                    <label class="control-label" for="additional_advices"><strong style="font-size: 11px">Is Hospitalized</strong></label>
                                                    
                                                    <div class="input-group" id="is_hospitalized">
                                                        <span><?= $row['is_hospitalized']=='Y'?"Yes":"No" ?>
                                                        </span>
                                                    </div>

                                            </div>

                                        </div>


                                    </div>
                                </div>

                            </div>
                        </div>

                    </div>


                    <!-- <div class="col-xs-3 table-responsive">
                        <div class="widget-box box box-danger">

                            <div class="widget-body">
                                <div class="widget-main">
                                    <div class="row">
                                        <div class="col-sm-12">
                                            <label class="control-label" for="appoint"><strong style="font-size: 11px">Ecg Findings</strong></label>

                                            <div class="input-group">


                                                <?php echo $row['ecg_findings']; ?>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div> -->
                </div>
            </div>
        </div>
    </div>
</form>


<form id="pdfform" target="_blank">
    <input type="hidden" id="appointment_id_pdf" value="<?= $row['appointment_id'] ?>" name="appointment_id_pdf">
    <input type="hidden" id="appointment_idpdf" value="<?= $row['appointment_id'] ?>" name="appointment_idpdf">
</form>

<script type="text/javascript">
    function exporttopdf() {


        document.forms['pdfform'].action = "opd_form_pdf.php";
        document.forms['pdfform'].method = "post";
        document.forms['pdfform'].submit();
    }

    function exporttoSortpdf_common(appointment_type, appointment_id) {

        $("#appointment_idpdf").val(appointment_id);
        document.forms['pdfform'].action = "opd_form_short_pdf.php";
        document.forms['pdfform'].method = "post";
        document.forms['pdfform'].submit();

    }
    var flag_empty_partial = 0;
    jQuery(function($) {

        $("#forwardButton").on("click", function() {

            document.getElementById("forwardButton").style.display = 'none';

            let flag_success_partial = 1;
            var counter = $("#count_treatment_item").val();
            for (let i = 0; i < counter; i++) {
                let treatment_qty = $("#treatment_item_total_" + i).val();
                let issued_qty = $("#treatment_item_qtygvn_" + i).val();
                let treatment_item_id = $("#treatment_item_" + i).val();
                // alert(issued_qty + "  row " + i + " item id " + treatment_item_id);
                if (treatment_item_id == undefined || treatment_item_id == '' || issued_qty == undefined) {
                    continue;
                }
                if (issued_qty == null || issued_qty == 0 || issued_qty == '') {
                    if (flag_empty_partial == 0) {
                        BootstrapDialog.confirm(
                            "Issued Qty For Some items is Empty or Zero.Are you ok with it?",
                            function(result) {
                                if (result) {
                                    flag_empty_partial = 1;
                                    flag_success_partial = 0;
                                } else {
                                    flag_empty_partial = 0;
                                    flag_success_partial = 0;
                                }
                            });
                        document.getElementById("forwardButton").style.display = 'block';
                        flag_success_partial = 0;
                        return false;
                    }
                }
            }
            if (flag_success_partial == 1) update_medicine_dispensary();

        });

    });





    function validate_update(formId, appointment_id) {
        // var appointment_date = $('#appointment_date'+appointment_id).val();
        // if(appointment_date == ''){
        // 	BootstrapDialog.alert('Please Enter Appointment Date.!!!');
        // 	return false;
        // }
        // var complaints = $('#complaints'+appointment_id).val();
        // if(complaints == ''){
        // 	BootstrapDialog.alert('Please Enter Complaints.!!!');
        // 	return false;
        // }
        update_appointment(formId, appointment_id);
    }

    function update_medicine_dispensary(formId, appointment_id) {
        $("#save_button").hide();

        $.ajax({
            url: 'save_opd.php',
            type: "POST",
            data: $("#opd_form").serialize(),
            success: function(data) {
                BootstrapDialog.show({
                    message: 'Medical Details Saved Successfully.',
                    buttons: [{
                        label: 'OK',
                        action: function(dialogItself) {
                            dialogItself.close();
                            exporttoSortpdf_common('O', '<?= $app_id ?>');
                            <?php if (isset($_REQUEST['returnPage'])) {
                            ?>
                                location.href = "<?= $_REQUEST['returnPage'] ?>";
                            <?php
                            }
                            ?>
                        }
                    }]
                });

                document.getElementById("forwardButton").style.display = 'block';

                return;
            },
            error: function(data) {
                BootstrapDialog.alert('Error Updating Details');
                document.getElementById("forwardButton").style.display = 'block';
                return;
            }
        });
        //window.location.reload();
    }

    function setFieldDisableOrEnable(value) {
        var count = $("#count_treatment_item").val();
        if (value == 'Y') {
            //  $("[name='field07']").prop("disabled", false);
            for (var i = 0; i < count; i++) {
                $("#treatment_item_qtygvn_" + i).prop("disabled", false);
            }
        } else if (value == 'N') {

            for (var i = 0; i < count; i++) {
                $("#treatment_item_qtygvn_" + i).prop("disabled", true);
            }
        }


    }


    // function checkQty() {


    // var counter = $("#count_treatment_item").val();
    // for (var i = 0; i < counter; i++) {
    //     var treatment_qty = $("#treatment_item_total_" + i).val();
    //     var issued_qty = $("#treatment_item_qtygvn_" + i).val();
    //     if (issued_qty == null || issued_qty == '') {
    //         issued_qty = 0;
    //     }
    /** if(issued_qty>treatment_qty){
    		   BootstrapDialog.alert(' Issue Qty should be less than or equal to Treatment Qty .!!!');
    		 
    			return false;
    	   }**/
    // }

    // }

    function toCheckItemStock() {

        var counter = $("#count_treatment_item").val();
        for (var i = 0; i < counter; i++) {
            var treatment_qty = $("#treatment_item_total_" + i).val();
            var issued_qty = $("#treatment_item_qtygvn_" + i).val();
            if (issued_qty == null || issued_qty == '') {
                issued_qty = 0;
            }
            /* if(issued_qty>treatment_qty){
		   BootstrapDialog.alert(' Issue Qty should be less than or equal to Treatment Qty .!!!');
		 
			return false;
	   }*/
        }

    }

    function validate_template(formId) {
        $("#save_button").hide();

        $.ajax({
            url: 'save_opd_template.php',
            type: "POST",
            data: $("#opd_form").serialize(),
            success: function(data) {
                BootstrapDialog.show({
                    message: 'Details Updated Successfully.',
                    buttons: [{
                        label: 'OK',
                        action: function(dialogItself) {
                            dialogItself.close();

                        }
                    }]
                });


                return;
            },
            error: function(data) {
                BootstrapDialog.alert('Error Templates Details');
                return;
            }
        });
        //window.location.reload();
    }

    // Save & Update
    function validate() {
        // var appointment_date = $('#appointment_date').val();
        // if(appointment_date == ''){
        // 	BootstrapDialog.alert('Please Enter Appointment Name.!!!');
        // 	return false;
        // }
        // var complaints = $('#complaints').val();
        // if(complaints == ''){
        // 	BootstrapDialog.alert('Please Enter Complaints.!!!');
        // 	return false;
        // }
        save_appointment();
    }

    function save_appointment() {
        $.ajax({
            url: 'save_opd.php',
            type: "POST",
            data: $("#opd_form").serialize(),
            success: function(data) {
                var data = $.parseJSON(data);
                //alert(data);
                if (data.appointment_id != null) {
                    $("#appointment_id").val(data.appointment_id);
                    $("#appointment_id_pdf").val(data.appointment_id);
                }

                BootstrapDialog.alert('Details Saved Successfully.');
                //loadAppointmentHistory('<?= $_REQUEST['flex_opd_id'] ?>');
                //return;
                window.location.reload();
            },
            error: function(data) {
                BootstrapDialog.alert('Error Saving Details');
                //return;
            }
        });
    }


    function add_consume() {
        $('#consumables').modal("show");

    }
    function add_ambulance_stock(){
        $('#miscellaneous_stock').modal("show");
    }

    if (!ace.vars['old_ie']) $('#out_time').datetimepicker({
        format: 'DD/MM/YYYY h:mm A', //use this option to display seconds
        <?php if (isset($row['clearance_time']) && $row['clearance_time'] != '0000-00-00 00:00:00') { ?>
            defaultDate: new Date("<?php echo $row['clearance_time'] ?>"),
            <?php } ?>
            maxDate: new Date(new Date().setDate(new Date().getDate() + 30)),
            icons: {
                time: 'fa fa-clock-o',
                date: 'fa fa-calendar',
                up: 'fa fa-chevron-up',
                down: 'fa fa-chevron-down',
                previous: 'fa fa-chevron-left',
                next: 'fa fa-chevron-right',
                today: 'fa fa-arrows ',
                clear: 'fa fa-trash',
                close: 'fa fa-times'
            }
    }).next().on(ace.click_event, function() {
        $(this).prev().focus();
    });
</script>
<?php include('opd_injury_common_code.php'); ?>
<?php include('consumables.php'); ?>
<?php include ('miscellaneous_stock.php'); ?>