<?php
$this->load->view('partials/report_start', $this->data);
$report = isset($report) ? $report : array();
$categories = isset($categories) ? $categories : array();
$rt_options = isset($rt_options) ? $rt_options : array();
$category = isset($category) ? $category : array('All');
if (!is_array($category)) {
    $category = array($category);
}
$retired_transferred = isset($retired_transferred) ? $retired_transferred : array('All');
if (!is_array($retired_transferred)) {
    $retired_transferred = array($retired_transferred);
}
$is_all_cat = in_array('All', $category, true);
$is_all_rt = in_array('All', $retired_transferred, true);
?>
<form method="post">
  <div class="row filter-row align-items-end mb-3">
    <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">
      <div class="form-group mb-0">
        <label>Retired / Transferred</label>
        <select name="retired_transferred[]" class="form-control js-select2" multiple data-placeholder="Select retired / transferred">
          <option value="All" <?php echo $is_all_rt ? 'selected' : ''; ?>>All</option>
          <option value="__BLANK__" <?php echo in_array('__BLANK__', $retired_transferred, true) ? 'selected' : ''; ?>>(Blank / empty)</option>
          <?php foreach ($rt_options as $opt) { ?>
            <option value="<?php echo htmlspecialchars($opt); ?>" <?php echo in_array($opt, $retired_transferred, true) ? 'selected' : ''; ?>>
              <?php echo htmlspecialchars($opt); ?>
            </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> user(s)</p>
  <?php $this->load->view('partials/report_exports'); ?>
</div>
  <?php
  $this->load->view('partials/report_datatable', array(
      'columns' => array(
          'Cardnumber' => 'cardnumber',
          'Name' => 'Name',
          'Designation' => 'Designation',
          'Phone' => 'INT_Phone',
          'Enrollment Date' => 'dateenrolled',
          'Expiry Date' => 'dateexpiry',
          'Division' => 'Division',
          'Retired Transferred' => 'Retired_Transferred',
          'Category' => 'Category',
      ),
      'rows' => $report,
      'table_id' => 'userReportTable',
  ));
  ?>
<?php } ?>
</form>
<?php $this->load->view('partials/report_end'); ?>
