<?php
$this->load->view('partials/report_start', $this->data);
$report = isset($report) ? $report : array();
$from_barcode = isset($from_barcode) ? $from_barcode : '';
$to_barcode = isset($to_barcode) ? $to_barcode : '';
$itemtypes = isset($itemtypes) ? $itemtypes : array();
$itype = isset($itype) ? $itype : '';
$columns = isset($columns) ? $columns : array();
?>
<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>Item Type</label>
        <select name="itype" class="form-control js-select2" required>
          <option value="" <?php echo ($itype === '') ? 'selected' : ''; ?> disabled>Select item type</option>
          <?php
          $found = false;
          foreach ($itemtypes as $it) {
              $code = is_object($it) ? $it->itemtype : (isset($it['itemtype']) ? $it['itemtype'] : '');
              $desc = is_object($it)
                  ? (isset($it->description) ? $it->description : $code)
                  : (isset($it['description']) ? $it['description'] : $code);
              if ($code === '' || strtoupper($code) === 'ALL') {
                  continue;
              }
              if ($code === $itype) {
                  $found = true;
              }
              ?>
            <option value="<?php echo htmlspecialchars($code); ?>" <?php echo ($itype === $code) ? 'selected' : ''; ?>>
              <?php echo htmlspecialchars($desc . ' (' . $code . ')'); ?>
            </option>
          <?php } ?>
          <?php if ($itype !== '' && !$found) { ?>
            <option value="<?php echo htmlspecialchars($itype); ?>" selected><?php echo htmlspecialchars($itype); ?></option>
          <?php } ?>
        </select>
      </div>
    </div>
    <div class="col-md-2">
      <div class="form-group mb-0">
        <label>From Barcode</label>
        <input type="text" class="form-control" name="from_barcode" value="<?php echo htmlspecialchars($from_barcode); ?>" required placeholder="e.g. J1">
      </div>
    </div>
    <div class="col-md-2">
      <div class="form-group mb-0">
        <label>To Barcode</label>
        <input type="text" class="form-control" name="to_barcode" value="<?php echo htmlspecialchars($to_barcode); ?>" required placeholder="e.g. J50">
      </div>
    </div>
    <div class="col-md-2">
      <p class="mb-0 text-muted small mt-4">Enter full barcodes (e.g. J1–J50, R1–R50, 1001–1500).</p>
    </div>
    <div class="col-md-2">
      <?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> item(s)
    <?php if ($from_barcode !== '' && $to_barcode !== '') { ?>
      for barcodes <strong><?php echo htmlspecialchars($from_barcode); ?></strong>
      – <strong><?php echo htmlspecialchars($to_barcode); ?></strong>
    <?php } ?>
    <?php if ($itype !== '') { ?>
      · Item type <strong><?php echo htmlspecialchars($itype); ?></strong>
    <?php } ?>
  </p>
  <?php $this->load->view('partials/report_exports'); ?>
</div>
  <?php
  $this->load->view('partials/report_datatable', array(
      'columns' => $columns,
      'rows' => $report,
      'table_id' => 'barcodeRangeTable',
      'empty_message' => 'No items found for this barcode range and item type.',
  ));
  ?>
<?php } ?>
</form>
<?php $this->load->view('partials/report_end'); ?>
