<?php
$this->load->view('partials/report_start', $this->data);
$status = isset($status) ? $status : array('ALL');
if (!is_array($status)) {
    $status = array($status);
}
$status_options = isset($status_options) ? $status_options : array();
$is_all = in_array('ALL', $status, true);
$status_labels = array();
foreach ($status as $s) {
    if ($s === 'ALL') {
        $status_labels[] = 'ALL';
    } elseif ($s === '__BLANK__') {
        $status_labels[] = '(Blank)';
    } else {
        $status_labels[] = $s;
    }
}
$ajax_url = isset($ajax_url) ? $ajax_url : base_url('index.php/report/accession_register_data');
$this->load->helper('report');
$columns = report_catalogue_columns('book');
?>
<form method="post" class="js-accession-form">
  <div class="row filter-row align-items-end mb-3">
    <div class="col-md-5">
      <div class="form-group mb-0">
        <label>Status</label>
        <select name="status[]" class="form-control js-select2" multiple data-placeholder="Select one or more statuses">
          <option value="ALL" <?php echo $is_all ? 'selected' : ''; ?>>ALL</option>
          <option value="__BLANK__" <?php echo in_array('__BLANK__', $status, true) ? 'selected' : ''; ?>>(Blank / empty)</option>
          <?php foreach ($status_options as $opt) { ?>
            <option value="<?php echo htmlspecialchars($opt); ?>" <?php echo in_array($opt, $status, true) ? 'selected' : ''; ?>>
              <?php echo htmlspecialchars($opt); ?>
            </option>
          <?php } ?>
        </select>
      </div>
    </div>
    <div class="col-md-4">
      <p class="mb-0 text-muted small mt-4">Filter by Status (999$S), then Submit. Results load page-by-page. Export downloads all matching records.</p>
    </div>
    <div class="col-md-3">
      <?php $this->load->view('partials/report_actions'); ?>
    </div>
  </div>
<?php if (!empty($searched)) { ?>
  <div class="report-results-wrap">
    <div class="report-results-toolbar">
      <p class="report-summary mb-0">
        <strong class="js-accession-total">…</strong> accession record(s)
        <?php if (!$is_all) { ?>
          — Status: <strong><?php echo htmlspecialchars(implode(', ', $status_labels)); ?></strong>
        <?php } ?>
      </p>
      <?php $this->load->view('partials/report_exports'); ?>
    </div>
    <div class="report-table-stage">
      <div id="krcTableLoading" class="krc-report-loading is-hidden" aria-live="polite" aria-busy="false">
        <div class="krc-report-loading-inner">
          <i class="fas fa-sync-alt fa-spin"></i>
          <span>Loading table data&hellip;</span>
        </div>
      </div>
      <div class="report-datatable-wrap">
        <table
          id="accessionTable"
          class="table table-bordered table-striped table-hover table-sm table-report js-report-datatable js-server-side"
          data-page-length="25"
          data-ajax-url="<?php echo htmlspecialchars($ajax_url); ?>"
          data-server-side="1"
          style="width:100%"
        >
          <thead>
            <tr>
              <?php foreach ($columns as $label => $key) {
                  echo '<th data-data="' . htmlspecialchars($key) . '">' . htmlspecialchars($label) . '</th>';
              } ?>
            </tr>
          </thead>
          <tbody></tbody>
        </table>
      </div>
    </div>
  </div>
<?php } ?>
</form>
<?php $this->load->view('partials/report_end'); ?>
