<?php
// app/views/partials/table.php
// Shared table template. Expects: $id, $headers (array), optional $tableClass, $wrapClass, $attrs, $footer (bool), $tbodyId

$id = $id ?? '';
$headers = is_array($headers ?? null) ? $headers : [];
$tableClass = $tableClass ?? '';
$wrapClass = $wrapClass ?? '';
$attrs = is_array($attrs ?? null) ? $attrs : [];
$footer = isset($footer) ? (bool)$footer : true;
$tbodyId = $tbodyId ?? '';
$filtersEnabled = isset($filtersEnabled) ? (bool)$filtersEnabled : true;

$id = (string)$id;
if ($id !== '' && !array_key_exists('data-table-id', $attrs)) {
  $attrs['data-table-id'] = $id;
}
if (!$filtersEnabled && !array_key_exists('data-filters-enabled', $attrs)) {
  $attrs['data-filters-enabled'] = '0';
}

$attrPairs = '';
foreach ($attrs as $k => $v) {
  $attrPairs .= ' ' . htmlspecialchars((string)$k, ENT_QUOTES) . '="' . htmlspecialchars((string)$v, ENT_QUOTES) . '"';
}
?>
<div class="table-wrap <?php echo htmlspecialchars($wrapClass, ENT_QUOTES); ?>">
  <div class="table-tools" data-table-tools>
    <button class="btn table-filter-toggle" type="button" data-table-filters-toggle>Filters</button>
    <button class="btn table-view-toggle" type="button" data-table-view-toggle>View</button>
    <span class="table-filter-summary" data-table-filters-summary></span>
  </div>
  <table class="table <?php echo htmlspecialchars($tableClass, ENT_QUOTES); ?>" id="<?php echo htmlspecialchars($id, ENT_QUOTES); ?>"<?php echo $attrPairs; ?>>
    <thead>
      <tr>
        <?php foreach ($headers as $h): ?>
          <?php
            $label = (string)($h['label'] ?? '');
            $i18n = (string)($h['i18n'] ?? '');
            $sort = (string)($h['sort'] ?? '');
            $class = (string)($h['class'] ?? '');
            $key = (string)($h['key'] ?? '');
            $filter = $h['filter'] ?? null;
            $filterJson = is_array($filter) ? json_encode($filter) : '';
          ?>
          <th<?php echo $class !== '' ? ' class="' . htmlspecialchars($class, ENT_QUOTES) . '"' : ''; ?>
              <?php echo $i18n !== '' ? ' data-i18n="' . htmlspecialchars($i18n, ENT_QUOTES) . '"' : ''; ?>
              <?php echo $sort !== '' ? ' data-sort="' . htmlspecialchars($sort, ENT_QUOTES) . '"' : ''; ?>
              <?php echo $key !== '' ? ' data-key="' . htmlspecialchars($key, ENT_QUOTES) . '"' : ''; ?>
              <?php echo $filterJson !== '' ? ' data-filter="' . htmlspecialchars($filterJson, ENT_QUOTES) . '"' : ''; ?>>
            <?php echo htmlspecialchars($label, ENT_QUOTES); ?>
          </th>
        <?php endforeach; ?>
      </tr>
    </thead>
    <tbody<?php echo $tbodyId !== '' ? ' id="' . htmlspecialchars((string)$tbodyId, ENT_QUOTES) . '"' : ''; ?>></tbody>
  </table>
  <?php if ($footer): ?>
    <div class="table-footer" data-table-footer>
      <div class="table-meta" data-table-meta></div>
      <div class="table-controls" data-table-controls>
        <button class="btn" type="button" data-table-prev>‹</button>
        <span class="table-page" data-table-page></span>
        <button class="btn" type="button" data-table-next>›</button>
      </div>
    </div>
  <?php endif; ?>
</div>
