<?php
$this->load->view('partials/report_start', $this->data);
$report = isset($report) ? $report : array();
$itemtypes = isset($itemtypes) ? $itemtypes : array();
$filter_mode = isset($filter_mode) ? $filter_mode : 'price';
$start_price = isset($start_price) ? $start_price : '';
$end_price = isset($end_price) ? $end_price : '';
$from_barcode = isset($from_barcode) ? $from_barcode : '';
$to_barcode = isset($to_barcode) ? $to_barcode : '';
$itype = isset($itype) ? $itype : '';
$row_limit = isset($row_limit) ? $row_limit : '100';
$cost_total = isset($cost_total) ? (float) $cost_total : 0;
$this->load->helper('report');
$columns = isset($columns) && is_array($columns) && !empty($columns)
    ? $columns
    : report_catalogue_columns('book');
?>
<form method="post" class="js-price-range-form">
  <div class="row filter-row align-items-end mb-3">
    <div class="col-md-3">
      <div class="form-group mb-0">
        <label>Filter type</label>
        <select name="filter_mode" id="priceFilterMode" class="form-control js-select2" required>
          <option value="all" <?php echo ($filter_mode === 'all') ? 'selected' : ''; ?>>All</option>
          <option value="accession" <?php echo ($filter_mode === 'accession') ? 'selected' : ''; ?>>Accession wise</option>
          <option value="price" <?php echo ($filter_mode === 'price') ? 'selected' : ''; ?>>Price wise</option>
        </select>
      </div>
    </div>
    <div class="col-md-2 js-price-only">
      <div class="form-group mb-0">
        <label>From price</label>
        <input type="number" step="0.01" min="0" class="form-control" name="start_price"
               value="<?php echo htmlspecialchars((string) $start_price); ?>" placeholder="e.g. 100">
      </div>
    </div>
    <div class="col-md-2 js-price-only">
      <div class="form-group mb-0">
        <label>To price</label>
        <input type="number" step="0.01" min="0" class="form-control" name="end_price"
               value="<?php echo htmlspecialchars((string) $end_price); ?>" placeholder="e.g. 5000">
      </div>
    </div>
    <div class="col-md-2 js-acc-only">
      <div class="form-group mb-0">
        <label>From accession</label>
        <input type="text" class="form-control" name="from_barcode"
               value="<?php echo htmlspecialchars((string) $from_barcode); ?>" placeholder="e.g. 1">
      </div>
    </div>
    <div class="col-md-2 js-acc-only">
      <div class="form-group mb-0">
        <label>To accession</label>
        <input type="text" class="form-control" name="to_barcode"
               value="<?php echo htmlspecialchars((string) $to_barcode); ?>" placeholder="e.g. 500">
      </div>
    </div>
  </div>
  <div class="row filter-row align-items-end mb-3">
    <div class="col-md-3">
      <div class="form-group mb-0">
        <label>Item type</label>
        <select name="itype" class="form-control js-select2" required>
          <option value="ALL" <?php echo ($itype === '' || strtoupper((string) $itype) === 'ALL') ? 'selected' : ''; ?>>All</option>
          <?php
          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;
              }
              ?>
            <option value="<?php echo htmlspecialchars($code); ?>" <?php echo ($itype === $code) ? 'selected' : ''; ?>>
              <?php echo htmlspecialchars($desc . ' (' . $code . ')'); ?>
            </option>
          <?php } ?>
        </select>
      </div>
    </div>
    <div class="col-md-2">
      <div class="form-group mb-0">
        <label>Limit</label>
        <input type="number" min="1" max="100000" class="form-control" name="row_limit"
               value="<?php echo htmlspecialchars((string) $row_limit); ?>" required placeholder="e.g. 100">
      </div>
    </div>
    <div class="col-md-3">
      <?php $this->load->view('partials/report_actions'); ?>
    </div>
  </div>

  <?php if (!empty($price_error)) { ?>
    <div class="alert alert-warning py-2"><?php echo htmlspecialchars($price_error); ?></div>
  <?php } ?>

<?php if (!empty($searched)) { ?>
  <div class="report-results-toolbar">
    <p class="report-summary mb-0">
      <strong><?php echo count($report); ?></strong> item(s)
      · Total Cost <strong><?php echo number_format($cost_total, 2); ?></strong>
    </p>
    <?php $this->load->view('partials/report_exports'); ?>
  </div>
  <?php if (!empty($report)) { ?>
  <div class="report-datatable-wrap">
    <table id="priceRangeTable" class="table table-bordered table-striped table-hover table-sm table-report js-report-datatable" data-page-length="25" style="width:100%">
      <thead>
        <tr>
          <?php foreach ($columns as $label => $key) { ?>
            <th><?php echo htmlspecialchars($label); ?></th>
          <?php } ?>
        </tr>
      </thead>
      <tbody>
        <?php foreach ($report as $row) { ?>
          <tr>
            <?php foreach ($columns as $label => $key) { ?>
              <td><?php echo htmlspecialchars((string) report_cell($row, $key)); ?></td>
            <?php } ?>
          </tr>
        <?php } ?>
      </tbody>
      <tfoot>
        <tr style="font-weight:700;background:#f3f3f3;">
          <?php foreach ($columns as $label => $key) {
              if ($key === 'Title') {
                  echo '<th>TOTAL</th>';
              } elseif ($key === 'Price_INR') {
                  echo '<th>' . number_format($cost_total, 2) . '</th>';
              } else {
                  echo '<th></th>';
              }
          } ?>
        </tr>
      </tfoot>
    </table>
  </div>
  <?php } else { ?>
    <div class="report-empty">
      <i class="fas fa-table"></i>
      <p>No items found for the selected filters.</p>
    </div>
  <?php } ?>
<?php } ?>
</form>
<script>
(function () {
  function syncPriceFilters() {
    var sel = document.getElementById('priceFilterMode');
    if (!sel) return;
    var mode = sel.value;
    var priceOnly = document.querySelectorAll('.js-price-only');
    var accOnly = document.querySelectorAll('.js-acc-only');
    for (var i = 0; i < priceOnly.length; i++) {
      priceOnly[i].style.display = (mode === 'price') ? '' : 'none';
      var inp = priceOnly[i].querySelector('input');
      if (inp) inp.required = (mode === 'price');
    }
    for (var j = 0; j < accOnly.length; j++) {
      accOnly[j].style.display = (mode === 'accession') ? '' : 'none';
      var inp2 = accOnly[j].querySelector('input');
      if (inp2) inp2.required = (mode === 'accession');
    }
  }
  document.addEventListener('DOMContentLoaded', function () {
    syncPriceFilters();
    var sel = document.getElementById('priceFilterMode');
    if (sel) {
      sel.addEventListener('change', syncPriceFilters);
      if (window.jQuery) {
        jQuery(sel).on('change select2:select', syncPriceFilters);
      }
    }
  });
})();
</script>
<?php $this->load->view('partials/report_end'); ?>
