<div class="ticket-grid" style="grid-template-columns:1fr 340px">
  <div class="card">
    <div class="table-wrap">
      <table class="table">
        <thead><tr><th>Name</th><th>Module</th><th>Status</th><th></th></tr></thead>
        <tbody>
        <?php foreach ($products as $p) : $fid = 'prod-form-' . $p['id']; ?>
          <tr>
            <td>
              <input type="text" name="name" form="<?= $fid ?>" value="<?= e($p['name']) ?>" required style="min-width:140px">
            </td>
            <td>
              <input type="text" name="module" form="<?= $fid ?>" value="<?= e($p['module']) ?>" style="min-width:140px">
            </td>
            <td><span class="badge <?= $p['is_active'] ? 'badge-resolved' : 'badge-closed' ?>"><?= $p['is_active'] ? 'Active' : 'Inactive' ?></span></td>
            <td>
              <form method="post" id="<?= $fid ?>" action="<?= url('/admin/products/' . $p['id']) ?>"><?= csrf_field() ?></form>
              <div class="flex gap-2">
                <button class="btn btn-sm" form="<?= $fid ?>">Save</button>
                <form method="post" action="<?= url('/admin/products/' . $p['id'] . '/toggle') ?>">
                  <?= csrf_field() ?>
                  <button class="btn btn-sm"><?= $p['is_active'] ? 'Deactivate' : 'Activate' ?></button>
                </form>
                <form method="post" action="<?= url('/admin/products/' . $p['id'] . '/delete') ?>" onsubmit="return confirm('Delete this product? This only works if it is not referenced by any ticket, contract, KB article or SLA policy.');">
                  <?= csrf_field() ?>
                  <button class="btn btn-sm btn-danger">Delete</button>
                </form>
              </div>
            </td>
          </tr>
        <?php endforeach; ?>
        <?php if (empty($products)) : ?><tr><td colspan="4" class="text-faint">No products yet.</td></tr><?php endif; ?>
        </tbody>
      </table>
    </div>
  </div>
  <div class="card">
    <div class="card-header"><h3>Add product / module</h3></div>
    <div class="card-body">
      <form method="post" action="<?= url('/admin/products') ?>">
        <?= csrf_field() ?>
        <div class="form-group"><label>Product name <span class="req">*</span></label><input type="text" name="name" required></div>
        <div class="form-group"><label>Module</label><input type="text" name="module"></div>
        <button class="btn btn-primary btn-block">Add</button>
      </form>
      <p class="help-text mt-2">Deleting is only possible while a product is unused. Once it's referenced by a ticket, contract, KB article or SLA policy, deactivate it instead — that keeps historical records intact (FR-SUP-099).</p>
    </div>
  </div>
</div>
