<?php
$this->load->view('partials/report_start', $this->data);
$total_rows = isset($total_rows) ? (int) $total_rows : 0;
$lac_member_count = isset($lac_member_count) && $lac_member_count !== '' ? (int) $lac_member_count : '';
$lac_members = isset($lac_members) && is_array($lac_members) ? array_values($lac_members) : array();
$lac_from_date = isset($lac_from_date) ? $lac_from_date : date('d-m-Y', strtotime('-30 days'));
$lac_to_date = isset($lac_to_date) ? $lac_to_date : date('d-m-Y');
$lac_from_ymd = isset($lac_from_ymd) && $lac_from_ymd !== ''
    ? $lac_from_ymd
    : date('Y-m-d', strtotime(str_replace('/', '-', $lac_from_date)) ?: strtotime('-30 days'));
$lac_to_ymd = isset($lac_to_ymd) && $lac_to_ymd !== ''
    ? $lac_to_ymd
    : date('Y-m-d', strtotime(str_replace('/', '-', $lac_to_date)) ?: time());
$lac_columns = isset($lac_columns) && is_array($lac_columns) ? $lac_columns : array(
    'S.No' => 'S_No',
    'Author' => 'Author',
    'Title' => 'Title',
    'Publisher' => 'Publisher',
    'Year' => 'Year',
    'Subject' => 'Subject',
    'ISBN' => 'ISBN',
);
$period_label = isset($period_label) ? $period_label : date('d/m/Y');
$ajax_url = isset($ajax_url) ? $ajax_url : base_url('index.php/report/lac_new_titles_data');
?>
<form method="post" class="js-report-busy-form js-lac-members-form" data-busy-export-ms="600000">
  <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="lac_from_date"
               value="<?php echo htmlspecialchars($lac_from_date); ?>"
               placeholder="dd-mm-yyyy" autocomplete="off" required>
      </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="lac_to_date"
               value="<?php echo htmlspecialchars($lac_to_date); ?>"
               placeholder="dd-mm-yyyy" autocomplete="off" required>
      </div>
    </div>
    <div class="col-md-3">
      <div class="form-group mb-0">
        <label>LAC member count</label>
        <input type="number" class="form-control js-lac-member-count" name="lac_member_count"
               min="1" max="20" value="<?php echo $lac_member_count === '' ? '' : (int) $lac_member_count; ?>"
               placeholder="e.g. 8" required>
      </div>
    </div>
    <div class="col-md-5">
      <p class="mb-0 text-muted small mt-4">Filter by suggestion suggested date — table loads 200 rows per page.</p>
    </div>
  </div>

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

  <div class="lac-member-codes mb-3">
    <label class="d-block mb-2">Member short forms</label>
    <div class="row js-lac-member-fields"
         data-initial='<?php echo htmlspecialchars(json_encode(!empty($searched) || !empty($lac_error) ? array_values($lac_members) : array()), ENT_QUOTES, 'UTF-8'); ?>'>
      <?php
      if ((!empty($searched) || !empty($lac_error)) && !empty($lac_members)) {
          foreach ($lac_members as $i => $code) {
      ?>
        <div class="col-6 col-sm-4 col-md-3 col-lg-2 mb-2 js-lac-member-field">
          <input type="text" class="form-control form-control-sm text-uppercase js-lac-member-input"
                 name="lac_member[]" maxlength="12" required
                 value="<?php echo htmlspecialchars($code); ?>"
                 placeholder="Member <?php echo (int) ($i + 1); ?>"
                 title="Short form for member <?php echo (int) ($i + 1); ?>">
        </div>
      <?php
          }
      }
      ?>
    </div>
    <p class="text-muted small mb-0 js-lac-member-hint<?php echo (!empty($lac_members) ? ' d-none' : ''); ?>">Enter a member count above to show input boxes.</p>
  </div>

  <div class="row mb-3">
    <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-lac-total"><?php echo (int) $total_rows; ?></strong> title(s)
        — period: <strong><?php echo htmlspecialchars($period_label); ?></strong>
        — members: <strong><?php echo htmlspecialchars(implode(', ', $lac_members)); ?></strong>
        <span class="text-muted">— table loads 200 rows per page; use Excel for full download</span>
      </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="lacNewTitlesTable"
          class="table table-bordered table-striped table-hover table-sm table-report js-report-datatable js-server-side js-lac-server-side"
          data-page-length="200"
          data-ajax-url="<?php echo htmlspecialchars($ajax_url); ?>"
          data-server-side="1"
          data-lac-from-date="<?php echo htmlspecialchars($lac_from_date); ?>"
          data-lac-to-date="<?php echo htmlspecialchars($lac_to_date); ?>"
          data-lac-from-ymd="<?php echo htmlspecialchars($lac_from_ymd); ?>"
          data-lac-to-ymd="<?php echo htmlspecialchars($lac_to_ymd); ?>"
          style="width:100%"
        >
          <thead>
            <tr>
              <?php foreach ($lac_columns as $label => $key) {
                  if (is_int($label)) {
                      $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'); ?>
