<?php
$this->load->view('partials/report_start', $this->data);
$report = isset($report) ? $report : array();
$acc_numbers = isset($acc_numbers) ? $acc_numbers : '';
$weed_member_count = isset($weed_member_count) && $weed_member_count !== '' ? (int) $weed_member_count : '';
$weed_members = isset($weed_members) && is_array($weed_members) ? array_values($weed_members) : array();
?>
<form method="post" class="js-lac-members-form" data-member-input-name="weed_member[]">
  <div class="row filter-row mb-3">
    <div class="col-md-12">
      <div class="form-group mb-0">
        <label>Accession numbers (jumset)</label>
        <textarea class="form-control" name="acc_numbers" rows="4" required
                  placeholder="e.g. 1, 2, 3 or one per line"><?php echo htmlspecialchars($acc_numbers); ?></textarea>
        <small class="text-muted">Enter accession numbers separated by comma, space, or new line.</small>
      </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>Member count</label>
        <input type="number" class="form-control js-lac-member-count" name="weed_member_count"
               min="1" max="20" value="<?php echo $weed_member_count === '' ? '' : (int) $weed_member_count; ?>"
               placeholder="e.g. 4" required>
      </div>
    </div>
  </div>

  <?php if (!empty($weed_error)) { ?>
    <div class="alert alert-warning py-2"><?php echo htmlspecialchars($weed_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($weed_error)) ? array_values($weed_members) : array()), ENT_QUOTES, 'UTF-8'); ?>'>
      <?php
      if ((!empty($searched) || !empty($weed_error)) && !empty($weed_members)) {
          foreach ($weed_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="weed_member[]" maxlength="40" required
                 value="<?php echo htmlspecialchars($code); ?>"
                 placeholder="Member <?php echo (int) ($i + 1); ?>"
                 title="Member <?php echo (int) ($i + 1); ?>">
        </div>
      <?php
          }
      }
      ?>
    </div>
    <p class="text-muted small mb-0 js-lac-member-hint<?php echo (!empty($weed_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-toolbar">
    <p class="report-summary mb-0">
      <strong><?php echo count($report); ?></strong> weed-out record(s)
      <?php if (!empty($weed_members)) { ?>
        — members: <strong><?php echo htmlspecialchars(implode(', ', $weed_members)); ?></strong>
      <?php } ?>
    </p>
    <?php $this->load->view('partials/report_exports'); ?>
  </div>
  <?php
  $this->load->view('partials/report_datatable', array(
      'columns' => array(
          'SNO' => 'S_No',
          'Acc No' => 'Acc_No',
          'Title' => 'Title',
          'Author' => 'Author',
          'Publisher Name' => 'Publisher_Name',
          'Publisher Year' => 'Publisher_Year',
          'Cost' => 'Cost',
          'Acc Date' => 'Acc_Date',
          'Item Type' => 'Item_Type',
      ),
      'rows' => $report,
      'table_id' => 'weedOutTable',
  ));
  ?>
<?php } ?>
</form>
<?php $this->load->view('partials/report_end'); ?>
