<?php
$this->load->view('partials/report_start', $this->data);
$report = isset($report) ? $report : array();
$fdate = isset($fdate) ? $fdate : date('d-m-Y', strtotime('-7 days'));
$tdate = isset($tdate) ? $tdate : date('d-m-Y');
$categories = isset($categories) ? $categories : array();
$category = isset($category) ? $category : array('All');
if (!is_array($category)) {
    $category = array($category);
}
$is_all_cat = in_array('All', $category, true);
?>
<form method="post">
  <div class="row filter-row align-items-end mb-3">
    <div class="col-md-2">
      <div class="form-group mb-0">
        <label>From Date</label>
        <input type="text" class="form-control js-date" name="fdate" value="<?php echo htmlspecialchars($fdate); ?>" required placeholder="dd-mm-yyyy" autocomplete="off">
      </div>
    </div>
    <div class="col-md-2">
      <div class="form-group mb-0">
        <label>To Date</label>
        <input type="text" class="form-control js-date" name="tdate" value="<?php echo htmlspecialchars($tdate); ?>" required placeholder="dd-mm-yyyy" autocomplete="off">
      </div>
    </div>
    <div class="col-md-4">
      <div class="form-group mb-0">
        <label>Category</label>
        <select name="category[]" class="form-control js-select2" multiple data-placeholder="Select one or more categories">
          <option value="All" <?php echo $is_all_cat ? 'selected' : ''; ?>>All</option>
          <?php foreach ($categories as $cat) {
              $code = is_object($cat) ? $cat->categorycode : $cat['categorycode'];
              $desc = is_object($cat) ? (isset($cat->description) ? $cat->description : $code) : $cat['description'];
              ?>
            <option value="<?php echo htmlspecialchars($code); ?>" <?php echo in_array($code, $category, true) ? 'selected' : ''; ?>>
              <?php echo htmlspecialchars($desc . ' (' . $code . ')'); ?>
            </option>
          <?php } ?>
        </select>
      </div>
    </div>
    <div class="col-md-4">
      <?php $this->load->view('partials/report_actions'); ?>
    </div>
  </div>
<?php if (!empty($searched)) { ?>
  <div class="report-results-toolbar">
  <p class="report-summary mb-0"><strong><?php echo count($report); ?></strong> return(s) from <?php echo htmlspecialchars($fdate); ?> to <?php echo htmlspecialchars($tdate); ?></p>
  <?php $this->load->view('partials/report_exports'); ?>
</div>
  <?php
  $this->load->view('partials/report_datatable', array(
      'columns' => array(
          'S.No' => 'S_No',
          'ID No' => 'ID_No',
          'Name' => 'Name',
          'Designation' => 'Designation',
          'Acc No' => 'Acc_No',
          'Due Date' => 'Due_Date',
          'Return Date' => 'Return_Date',
      ),
      'rows' => $report,
      'table_id' => 'returnTable',
  ));
  ?>
<?php } ?>
</form>
<?php $this->load->view('partials/report_end'); ?>
