<?php
declare(strict_types=1);
require __DIR__ . '/_common.php';
ec_require_coordinator();

$PAGE_TITLE = 'Batches';
$ACTIVE     = 'batches';

list($scopeSql, $scopeParams) = coord_branch_scope('b');
$cols = ['id','branch_id'];
if (col($mysqli,'batches','name'))       $cols[]='name';
if (col($mysqli,'batches','code'))       $cols[]='code';
if (col($mysqli,'batches','start_date')) $cols[]='start_date';
if (col($mysqli,'batches','end_date'))   $cols[]='end_date';

$select = implode(', ', array_map(fn($c)=>"b.$c", $cols));
$rows = qall($mysqli, "SELECT $select FROM batches b WHERE $scopeSql ORDER BY b.id DESC",
  $scopeParams, str_repeat('i', count($scopeParams)));
?>
<!doctype html><html lang="en"><head>
  <meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Batches</title>
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.css" rel="stylesheet">
</head><body>
<div class="ec-shell">
  <?php include __DIR__ . '/partials/sidebar.php'; ?>
  <div class="ec-content">
    <?php include __DIR__ . '/partials/topbar.php'; ?>
    <div class="wrap container-fluid">
      <div class="card section-card">
        <div class="card-header bg-white"><strong>My Branch Batches</strong></div>
        <div class="card-body p-0">
          <div class="table-responsive">
            <table class="table table-hover mb-0">
              <thead class="table-light">
                <tr>
                  <th>ID</th>
                  <?php if (in_array('name',$cols,true)): ?><th>Name</th><?php endif; ?>
                  <?php if (in_array('code',$cols,true)): ?><th>Code</th><?php endif; ?>
                  <th>Branch</th>
                  <?php if (in_array('start_date',$cols,true)): ?><th>Start</th><?php endif; ?>
                  <?php if (in_array('end_date',$cols,true)): ?><th>End</th><?php endif; ?>
                </tr>
              </thead>
              <tbody>
              <?php if (!$rows): ?>
                <tr><td colspan="6" class="text-center text-muted py-4">No batches found for your scope.</td></tr>
              <?php else: foreach($rows as $r): ?>
                <tr>
                  <td><?php echo (int)$r['id']; ?></td>
                  <?php if (in_array('name',$cols,true)): ?><td class="fw-semibold"><?php echo h($r['name']); ?></td><?php endif; ?>
                  <?php if (in_array('code',$cols,true)): ?><td><?php echo h($r['code']); ?></td><?php endif; ?>
                  <td><?php echo (int)$r['branch_id']; ?></td>
                  <?php if (in_array('start_date',$cols,true)): ?><td><?php echo h($r['start_date']); ?></td><?php endif; ?>
                  <?php if (in_array('end_date',$cols,true)): ?><td><?php echo h($r['end_date']); ?></td><?php endif; ?>
                </tr>
              <?php endforeach; endif; ?>
              </tbody>
            </table>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>
</body></html>
