<?php
// include('pdf_header.php');
include('includes/config/config.php');
include('includes/functions.php');
header('Content-Type: application/force-download');
header('Content-disposition: attachment; filename=fitness_certificate.xls');

error_reporting(E_ERROR | E_PARSE);

$from_date1 = date('Y-m-d', strtotime($_POST['fitnessDate1']));
$from_date = date('d-m-Y', strtotime($from_date1));


$from_date2 = date('Y-m-d', strtotime($_POST['fitnessDate2']));
$to_date = date('d-m-Y', strtotime($from_date2));


function getAllDates($startingDate, $endingDate)
{
  $datesArray = [];

  $startingDate = strtotime($startingDate);
  $endingDate = strtotime($endingDate);

  for ($currentDate = $startingDate; $currentDate <= $endingDate; $currentDate += (86400)) {
    $date = date('Y-m-d', $currentDate);
    $datesArray[] = $date;
  }

  return $datesArray;
}

$dates = getAllDates($from_date1, $from_date2);
$sickness_id = array();
foreach ($dates as $key => $val) {
  $sqlid = "SELECT sickness_id as id FROM `sickness` a join patient_master b on a.emp_id=b.id WHERE  a.ohc_type_id='" . $_SESSION['current_ohcttype'] . "' and str_to_date('" . $val . "','%Y-%m-%d') BETWEEN date(a.date_absent) and  date(a.date_absent_to) ";

  $result_sqlid = mysqli_query($conn, $sqlid);

  while ($rowid = mysqli_fetch_assoc($result_sqlid)) {
    array_push($sickness_id, $rowid['id']);
  }
}
$emp_id = $_REQUEST['patient2_id'];
if ($emp_id != 'NULL' && $emp_id != '') {
  $emp_filter = " and emp_id='" . $emp_id . "'";
}

$status = $_POST['status_fit'];
$tital = '';
if ($status == 'all') {
  $status = "";
  $tital = 'Fitness And Sickness Report';
} else {
  if ($status == 'APPROVED') {
    $tital = 'Fitness Report';
  } else {
    $tital = 'Sickness Report';
  }
  $status = "and fitness_status='" . $status . "'";
}
$month_name = date("F", mktime(0, 0, 0, $month, 10));
$query_for_company = "select company_name, company_logo, image_type from company_profile ";
//echo $query_for_company;
$result_for_company = @mysqli_query($conn, $query_for_company);
if ($row_for_company = @mysqli_fetch_array($result_for_company)) {
  @extract($row_for_company);
}
?>

<body>
  <?php include('excel_ohc_header.php'); ?>
  <table width="100%">
    <tr>

      <td align="center" style="font-size: 18px">
        <b>
          <u><?= $tital ?></u>
        </b>
      </td>

    </tr>
  </table><br>

  <table width="100%">
    <tr>
      <td width='100%' align="left" style="font-size: 12px"><strong>From Date:
          <?php echo date("d-M-Y", strtotime($from_date)); ?>&nbsp;&nbsp;&nbsp;To Date:
          <?php echo date("d-M-Y", strtotime($to_date)); ?>
        </strong></td>
      <!-- <td align="center" style="font-size: 18px">
                <b>
                    <u><?= $tital ?></u>
                </b>
            </td> -->
      <td align="right" style="font-size: 15px"><button align="center" id="printPageButton" class="btn btn-success" onClick="window.print();">Print</button></td>
    </tr>

  </table>
  <table border="1" width="100%" cellspacing="0">
    <strong>
      <tr bgcolor="#eeeeee">
        <td align="left" width="2%">Sr.</td>
        <td align="left" width="6%">Cert. NO</td>
        <td align="left" width="10%">Date</td>
        <td align="left" width="8%">EMP. NO</td>
        <td align="left" width="10%"> Name </td>
        <td align="left" width="5%">Department</td>
        <td width="10%" align="left">Section</td>
        <td width="5%" align="left">Employer</td>
        <!-- <th>Body System </th> -->
        <td width="10%" align="left">Diagnosis </td>
        <td width="10%" align="left">Plant</td>
        <td align="left" width="7%">Period of Rest From</td>
        <td align="left" width="7%">Period of Rest To</td>
        <td align="left" width="5%">Days of Illness</td>
        <td align="left" width="5%">Fitness Date</td>
        <td align="left" width="5%">Doctor Name</td>
      </tr>

      <?php
      $count = 1;
      $id_arr = array();
      foreach ($sickness_id as $key => $val) {
        if (!in_array($val, $id_arr)) {


          //$sql_waste="SELECT c.ohc_type_name,a.waste_category_name,b.*  FROM `waste_category` a inner join bio_medical_waste b on a.waste_category_id=b.waste_category inner join ohc_type c on c.ohc_type_id=b.ohc_type_id where b.ohc_type_id='".$_SESSION['current_ohcttype']."' ";
          //$results_issue_items = mysqli_query($conn,$sql_issue_items);
          //$sql=base64_decode($filterkey);

          //$results= mysqli_query($conn,$sql_waste);
          $sqll = "SELECT count(*) as total FROM `sickness` a join patient_master b on a.emp_id=b.id WHERE  a.ohc_type_id='" . $_SESSION['current_ohcttype'] . "' and sickness_id='" . $val . "' " . $status . $emp_filter;

          $result_sqll = mysqli_query($conn, $sqll);
          $num_rows = mysqli_num_rows($result_sqll);
          $row0 = mysqli_fetch_assoc($result_sqll);
          // error_log("total".$row0['total']);

          $sql = "SELECT a.*,b.patient_name,b.plant_id,b.emp_code,b.section_id,half_day,b.dept_id,b.employer_contractor_id,DATEDIFF(date_absent_to ,date_absent) AS date_difference FROM `sickness` a join patient_master b on a.emp_id=b.id WHERE   a.ohc_type_id='" . $_SESSION['current_ohcttype'] . "' and sickness_id='" . $val . "'  " . $status . $emp_filter;
          error_log($sql . "get sql");

          $result = mysqli_query($conn, $sql);

      ?>


          <?php

          while ($row1 = mysqli_fetch_assoc($result)) {
            extract($row1);
            //  error_log($row1['employer_contractor_id']);

          ?>

            <tr>
              <td><?php echo $count ?></td>
              <td><?php echo $row1['ticket_no'] ?></td>
              <td><?php echo date('d-m-Y', strtotime($row1['sickness_date'])) ?></td>
              <td><?php echo $row1['emp_code'] ?></td>
              <td><?php echo $row1['patient_name'] ?></td>

              <td><?php echo getTableFieldValue('department', 'dept_name', $row1['dept_id'], 'dept_id') ?></td>
              <td><?php echo getTableFieldValue('section', 'section_name', 'section_id', $row1['section_id']) ?></td>
              <td><?php echo getTableFieldValue('employer_contractor', 'employer_contractor_name', $row1['employer_contractor_id'], 'id') ?>
              </td>
              <!-- <td><?php echo getTableFieldValue('employer_contractor', 'employer_contractor_name', $row1['employer_contractor_id'], 'id') ?>
                </td> -->

              <td><?php
                  $ali_id = getTableFieldValue('employee_appointment', 'ailments_new', 'appointment_id', '"' . $row1['followup_to_opd'] . '"');


                  echo getCommaSeperatedValuesForInClause("select ailment_name from ailment ", "ailment_id", $ali_id);
                  ?></td>
              <td><?php echo getTableFieldValue('plant_master', 'plant_name', 'plant_id', $row1['plant_id']) ?></td>
              <td><?php echo date('d-m-Y', strtotime($row1['date_absent'])) ?></td>
              <td><?php echo date('d-m-Y', strtotime($row1['date_absent_to'])) ?></td>
              <td><?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</td>
              <td><?php if ($row1['approval_date'] != '' &&  $row1['approval_date'] != null && $row1['approval_date'] != '0000-00-00') {
                    echo date('d-m-Y', strtotime($row1['approval_date']));
                  } ?></td>
              <td><?php echo getTableFieldValue('patient_master', 'patient_name', 'id', $row1['doctor_last_attended']); ?></td>



            </tr>
      <?php
            $count++;
          }
          array_push($id_arr, $val);
        }
      }
      ?>
      <tr Height="10px">
        <td colspan="14"><b>TOTAL: &nbsp;<?php echo $count - 1 ?></b></td>
      </tr>
  </table>
</body>