<?php 
header("Content-type:application/octet-stream");

header("Content-type: application/x-msdownload"); 
header("Content-Disposition: attachment; filename=excel_checklist.xls"); 
header("Pragma: no-cache"); 
header("Expires: 0"); 
error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED);
?>
<?php 
include('includes/config/config.php');
include('includes/functions.php')
//include('pop_up_top.php');
?>

<body>
    <?php include('excel_ohc_header.php') ?>
    <?php

$shift = $_POST["shift"];
$cat = $_POST["inventory_cat"];
error_log("PARA::" . $cat);
if ($_POST["inventory_cat"] != 'AMB_CHECK') {
    $type = $_POST["inventory_type"];
} else {
    $type = $_POST["inventory_type2"];
}


$db_new_date = "";
$last_day = "";
$m = 0;
$y = 0;
$selectedParamMonth = $_REQUEST['param_month'];
//  echo "month......"+$selectedShiftMonth;
$selectedParamYear = $_POST['param_year'];
//echo "year.........".$selectedShiftYear;
if (isset($selectedParamYear) && $selectedParamYear != "" && isset($selectedParamMonth) && $selectedParamMonth != "") {
    $last_day = date("t-m-Y", strtotime("" . $selectedParamYear . "-" . ($selectedParamMonth + 1) . "-01"));
} else {
    $last_day = date('t-m-Y');
    //echo "last:date1".$last_day;
}
$m = date('m', strtotime("" . $selectedParamYear . "-" . ($selectedParamMonth + 1) . "-01"));
////echo "month det".$m;
$y = date('Y', strtotime("" . $selectedParamYear . "-" . ($selectedParamMonth + 1) . "-01"));
//echo "year det".$y;



?>
    <script>
    var jsonArrayParamData = <?php echo json_encode($param_data); ?>;
    </script>


    <style>
    #modal-add-ailment {
        overflow-y: scroll;
    }

    #myTable thead td {

        font-size: 12px;
        font-weight: bold;
    }

    #myTable td {

        height: 50px;
        border: groove;
        text-align: center;
    }
    </style>
    <table width="100%">
        <td colspan="5">
            <h1 style="text-align:center;"><?php
            $issue_type=$_POST["issue_type"];
                                        $amb_id = $_REQUEST['amb_idd'];
                                        if ($cat == 'INVENTORY') {
                                            echo "INVENTORY  " . $type;
                                        } else {
                                            if($issue_type=='AMB'){
                                                echo getTableFieldValue('ambulance_details_new', "CONCAT(ambulance_name , '  ( ' , ambulance_number ,' )' )", 'id', $amb_id);}
                                                else{
                                                    echo getTableFieldValue('ohc_type', "CONCAT(ohc_type_name)", 'ohc_type_id', $amb_id);
                                                }
                                        }
                                                        
                        $shift_name=getTableFieldValue('shift_status', "status_name", 'shift_status_id', $shift);
                        echo '('.$shift_name.' Shift)';
                                        ?>- <span style="color:crimson; ">
                    <?= date("F", mktime(0, 0, 0, $m, 10)); ?> ( <?= $y ?> )
            </h1>
        </td>
        <td>
            <button align="center" id="printPageButton" class="btn btn-success" onClick="window.print();">Print</button>
        </td>
    </table>
    <table width="100%" style="margin-top: 30px; border: groove;" id="myTable" class="">
        <thead>
            <tr>
                <td></td>
                <td>Date</td>
                <?php

            for ($j = 1; $j <= $last_day; $j++) {
                $new_date = $j . "-" . $m . "-" . $y;
                $day = date('D', strtotime($new_date));
            ?>
                <td><?php echo $j ?></td>

                <?php } ?>

            </tr>
            <tr>
                <td>Sr. No.</td>
                <td>Description</td>
                <?php
            //  $last_day = date('t-m-Y');
            // $m = date('m');
            // $y = date('Y');
            for ($k = 1; $k <= $last_day; $k++) {
                $new_date = $k . "-" . $m . "-" . $y;
                $day = date('D', strtotime($new_date));
            ?>

                <td><?php echo $day ?></td>


                <?php } ?>

            </tr>

        </thead>

        <tbody>
            <?php $count = 1;
                                $items_array = array();
                                $item = array();
                                $sub_count = 1;
                                $cat = $_POST["inventory_cat"];
                                if ($_POST["inventory_cat"] != 'AMB_CHECK') {
                                    $type = $_POST["inventory_type"];
                                } else {
                                    $type = $_POST["inventory_type2"];
                                }
                                $item_cat=$_POST["inventory_item_type2"];
$item_cat_query='';
if($item_cat=="" || $item_cat=='ALL'||$item_cat==null ){$item_cat_query='';}
else if($item_cat=="comm"){$item_cat_query='and item_type="'.$item_cat.'"';}
else{

    $item_cat_query='and item_type="item" and item_id in (select item_id from tbl_items where cat="'.$item_cat.'") ';
}

                                error_log("type " . $amb_id);
                                $ohc = "SELECT ohc_type_id  from ohc_type where ohc_type_id = '" . $_SESSION['current_ohcttype'] . "'  ";
                                error_log("PARAMS1111111::" . $ohc);
                                $results_ohc = mysqli_query($conn, $ohc);
                                $row_ohc = mysqli_fetch_array($results_ohc);
                                error_log("ohc Id" . $row_ohc['ohc_type_id']);
                                $i = 1;
                                $query = "SELECT ambulance_issue_master_id from ambulance_issue_master where ohc_type_id = '" . $row_ohc['ohc_type_id'] . "' AND ambulance_id = '$amb_id' And issue_type='$issue_type' ";
                                error_log("PARAMS::" . $query);
                                $result = @mysqli_query($conn, $query);
                                while ($row = mysqli_fetch_assoc($result)) {


                                    $item_desc = "";
                                    $stock_issue_item_id = "SELECT a.* from ambulance_checklist_item a  WHERE  a.ambulance_issue_master_id ='" . $row['ambulance_issue_master_id'] . "' $item_cat_query";
                                      error_log($stock_issue_item_id);
                                    $results_issue_item_id = mysqli_query($conn, $stock_issue_item_id);
                                    while ($row_issue_item_id = mysqli_fetch_assoc($results_issue_item_id)) {

                                        $item_desc = "";
                                        $sql_issue_items = "SELECT a.* from ambulance_checklist_item a  WHERE a.id='" .$row_issue_item_id["id"]."' and a.ambulance_issue_master_id ='" . $row['ambulance_issue_master_id'] . "' $item_cat_query";

                                        $results_issue_items = mysqli_query($conn, $sql_issue_items);
                                        while ($row_issue_items = mysqli_fetch_assoc($results_issue_items)) {
                                            array_push($items_array, $row_issue_items);
                                            array_push($item, $row_issue_items['item_id'].'$'.$row_issue_items['item_type']);
                                        }
                                    }
                                }
                                $item = array_unique($item);

                                // error_log('item_arr' . print_r($items_array, true));
                                sort($item);
                                $row_issue_qty = array();
                                for ($i = 0; $i < count($item); $i++) {
                                    $qty = 0;
                                    for ($j = 0; $j < count($items_array); $j++) {

                                        if ($item[$i] == $items_array[$j]['item_id'].'$'.$items_array[$j]['item_type']) {
                                            $qty += $items_array[$j]['issue_qty'];
                                        }
                                    }
                                    error_log($item[$i] . '--' . $qty);

                                    array_push($row_issue_qty, $qty);
                                }



                                $row_issue_items['item_id'] = $item;
                                $row_issue_items['issue_qty'] = $row_issue_qty;
                                
                                for ($x = 0; $x < count($row_issue_items['item_id']); $x++) {
                                    
                                    $item_desc = '';
                                    $itemWithType=explode('$',$row_issue_items['item_id'][$x]);
                                    $row_issue_items['item_id'][$x]=$itemWithType[0];
                                    $row_issue_items['item_type'][$x]=$itemWithType[1];
                                    if($row_issue_items['item_type'][$x]=='item'){
                                

                                        $item_desc = $item_desc . ' <p> ' . getItemWithFormName($row_issue_items['item_id'][$x]) . " ";

                                        $item_unit_id = getTableFieldValue('tbl_items', 'unit_id', 'item_id', $row_issue_items['item_id'][$x]);
                                        $item_unit = getTableFieldValue('unit_master', 'unit_name', 'unit_id', $item_unit_id);

                                        error_log("Userid " . $row_issue_items['item_type'][$x]);
                                        if ($row_issue_items['issue_qty'][$x] != 0) {

                                        $item_desc = $item_desc . '<b> Qty: ' . $row_issue_items['issue_qty'][$x] . ' ' . $item_unit . '</b>';
                                        }
                                        $item_desc = $item_desc . "</p>";
                                    }else{
                                    $item_desc = $item_desc.' <p> '.getTableFieldValue('questionaire_master','question','question_id',$row_issue_items['item_id'][$x]);
                                    $item_desc=$item_desc."</p>";
                                }
                                ?>
            <tr>
                <td><?php echo $sub_count;
                                                $sub_count++; ?></td>
                <td><?php echo $item_desc; ?></td>



                <input type="hidden" name="param_id<?php echo $i ?>" id="param_id<?php echo $i ?>"
                    value="<?php echo $row_issue_items['item_id'][$x].'$'.$row_issue_items['item_type'][$x]; ?>" />
                <?php $status_id = "";
                                        $param_data = array();
                                        $query1 = "SELECT * from inventory_parameters_status where month(param_date)='" . $m . "' and year(param_date)='" . $y . "' and shift='$shift' ";
                                        error_log("PARAMETER QUERY." . $query1);

                                        $result1 = @mysqli_query($conn, $query1);
                                        $remark = array();
                                        while ($row_param = @mysqli_fetch_assoc($result1)) {


                                            $status_id = $row_param['status_id'];
                                            $key = $row_param['param_id'] . '_' . $row_param['param_date'];

                                            $value = $row_param['param_status'];

                                            $remark += array(
                                                $key => $row_param['param_remark'],
                                            );

                                            // error_log("  REMARK " . print_r($remark, true));
                                            $param_data[$key] = $value;


                                        ?>

                <input type="hidden" name="status_id<?php echo $i ?>" id="status_id<?php echo $i ?>"
                    value="<?php echo $status_id ?>" />
                <?php } ?>
                <?php
                                        // error_log(" REAMM " . print_r($remark, true));
                                        $db_new_date = "";



                                        $mm = 0;

                                        for ($k = 1; $k <= $last_day; $k++) {
                                            $new_date = $k . "-" . $m . "-" . $y;
                                            $db_new_date = $y . "-" . $m . "-" . $k;
                                            $day = date('D', strtotime($new_date));
                                            if ($k < 10) {
                                                $db_new_date = $y . "-" . $m . "-0" . $k;
                                            }

                                        ?>
                <?php


                                            ?>

                <td>
                    <div align='center'>
                        <input type="checkbox" class="<?php echo date('Y-m-d', strtotime($new_date)) ?>"
                            id="param_status_<?php echo $i ?>_<?php echo $k ?>" style="width: 50%;margin-left:5%"
                            name="param_status_<?php echo $i ?>_<?php echo $k ?>[]"
                            onchange="save_params(<?php echo $i ?>,<?php echo $k ?>)"
                            <?php if($param_data[$row_issue_items['item_id'][$x].'$'.$row_issue_items['item_type'][$x] . '_' . $db_new_date] ==1)echo 'CHECKED'?>
                            value="<?php echo $param_data[$row_issue_items['item_id'][$x].'$'.$row_issue_items['item_type'][$x] . '_' . $db_new_date] ?>" />

                        <br>
                        <input type="text" name="param_remark_<?php echo $i ?>_<?php echo $k ?>" style="text-align:left"
                            id="param_remark_<?php echo $i ?>_<?php echo $k ?>" onchange="" placeholder="Remarks"
                            value="<?php echo $remark[$row_issue_items['item_id'][$x].'$'.$row_issue_items['item_type'][$x] . '_' . $db_new_date] ?>">

                        <script>
                        $(document).ready(function() {
                            <?php $key = $row_issue_items['item_id'][$x].'$'.$row_issue_items['item_type'][$x] . '_' . $db_new_date; ?>
                            var existingParamValues<?php echo $i ?>_<?php echo $k ?> =
                                "<?php echo $param_data[$key] ?>"
                            document.getElementById(
                                    "param_status_<?php echo $i ?>_<?php echo $k ?>"
                                ).value =
                                existingParamValues<?php echo $i ?>_<?php echo $k ?>;

                            if (document.getElementById(
                                    "param_status_<?php echo $i ?>_<?php echo $k ?>"
                                ).value == "1" || document.getElementById(
                                    "param_status_<?php echo $i ?>_<?php echo $k ?>"
                                ).value == "on") {
                                document.getElementById(
                                    "param_status_<?php echo $i ?>_<?php echo $k ?>"
                                ).checked = true;
                                document.getElementById(
                                    "param_remark_<?php echo $i ?>_<?php echo $k ?>"
                                ).value = "<?php
                                                                                echo $remark[$key]; ?>";
                            }

                        });
                        </script>




                    </div>

                </td>
                <input type="hidden" name="param_date_<?php echo $i ?>_<?php echo $k ?>"
                    id="param_date_<?php echo $i ?>_<?php echo $k ?>" value="<?php echo $new_date ?>" />

                <?php } ?>
            </tr>


            <?php
                                    
                                        $count++;
                                    
                                    $i++;
                                };

                                    ?>



        </tbody>

    </table>

    <br><br>
    <br><br>

    <?php $query_app = "select e.emp_sign,e.image_type from employee_signature e left join tbl_users u on u.emp_id=e.emp_id left join inventory_checklist_status c on u.user_id=c.approver_user_id where c.status_id ='" . $_REQUEST['status_id'] . "'";
 error_log("app Signatures :" . $query_app);

$result_app = mysqli_query($conn, $query_app);

$app_sign_row = mysqli_fetch_array($result_app);

extract($app_sign_row);


$query_ck = "select e.emp_sign,e.image_type from employee_signature e left join tbl_users u on u.emp_id=e.emp_id left join inventory_checklist_status c on u.user_id=c.checker_user_id where c.status_id ='" . $_REQUEST['status_id'] . "'";
 error_log("ch Signatures :" . $query_ck);

$result_ck = mysqli_query($conn, $query_ck);

$ck_sign_row = mysqli_fetch_array($result_ck);

extract($ck_sign_row);
?>

    <div class="row">
        <div style="width: 100%;" class="leftailgn">
            <?php if($ck_sign_row['emp_sign']!='' || $ck_sign_row['emp_sign']!=null){ ?>
            <img id='sign' align="left" style="text-align:center; width:150px; height:80px;" class="card-img-bottm"
                src="data:<?= $ck_sign_row['image_type']; ?>;base64,<?= base64_encode($ck_sign_row['emp_sign']) ?>">
            <?php }?>
            <?php if($app_sign_row['emp_sign']!='' || $app_sign_row['emp_sign']!=null){ ?>
            <img align="right" id='sign' style="text-align:center; width:150px; height:80px;" class="card-img-bottm"
                src="data:<?= $app_sign_row['image_type']; ?>;base64,<?= base64_encode($app_sign_row['emp_sign']) ?>">
            <?php }?>
        </div>
    </div>
    <!-- <div class="row">
    <div style="width: 100%;" class="leftailgn">
        <div style="width: 100%;" class="leftailgn">
            <span align='left'>(SIGNATURE OF PT.)</span>
            <span align='right'>(SIGNATURE OF DOCTOR)</span>
        </div>
    </div>
</div> -->

    <br><br>
    <br><br>
    <br><br>
    <div class="row">
        <div style="width: 100%;" class="">
            <?php if($ck_sign_row['emp_sign']!='' || $ck_sign_row['emp_sign']!=null){ ?>
            <div align='left'>
                <span>(SIGNATURE OF CHECKER)</span>
            </div>
            <?php }?>
            <?php if($app_sign_row['emp_sign']!='' || $app_sign_row['emp_sign']!=null){ ?>
            <div align='right'>
                <span> (SIGNATURE OF APPROVER)</span>
            </div>
            <?php }?>
        </div>
    </div>

    <!-- <div class="row">
    <div style="width: 100%;" class="leftailgn">
        <span align='left'>(SIGNATURE OF PT.)</span>

        <span align='right'>(SIGNATURE OF DOCTOR)</span>
    </div>
</div> -->
</body>