item_groups.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. require_once __DIR__ . '/auth.php';
  3. require_once __DIR__ . '/layout.php';
  4. require_admin();
  5. $pdo = get_pdo();
  6. if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  7. $action = $_POST['action'] ?? '';
  8. if ($action === 'add') {
  9. $name = trim($_POST['groupName'] ?? '');
  10. $order = (int)($_POST['sortOrder'] ?? 0);
  11. if ($name !== '') {
  12. $pdo->prepare("INSERT INTO item_groups (groupName, sortOrder) VALUES (?,?)")->execute([$name, $order]);
  13. flash('success', "Group '$name' added.");
  14. } else { flash('error', 'Name required.'); }
  15. }
  16. if ($action === 'edit') {
  17. $id = (int)$_POST['groupID'];
  18. $name = trim($_POST['groupName'] ?? '');
  19. $order = (int)($_POST['sortOrder'] ?? 0);
  20. if ($name !== '') {
  21. $pdo->prepare("UPDATE item_groups SET groupName=?,sortOrder=? WHERE groupID=?")->execute([$name,$order,$id]);
  22. flash('success', 'Group updated.');
  23. } else { flash('error', 'Name required.'); }
  24. }
  25. if ($action === 'delete') {
  26. $id = (int)$_POST['groupID'];
  27. $pdo->prepare("DELETE FROM item_groups WHERE groupID=?")->execute([$id]);
  28. flash('success', 'Group deleted.');
  29. }
  30. header('Location: item_groups.php'); exit;
  31. }
  32. $groups = $pdo->query("SELECT * FROM item_groups ORDER BY sortOrder, groupID")->fetchAll();
  33. admin_head('Item Groups', 'item_groups.php');
  34. show_alerts();
  35. ?>
  36. <h1>Item Groups</h1>
  37. <div class="card">
  38. <h2>Add New Group</h2>
  39. <form method="POST">
  40. <input type="hidden" name="action" value="add">
  41. <div class="form-row">
  42. <div>
  43. <label>Group Name</label>
  44. <input type="text" name="groupName" placeholder="e.g. Clothing" required>
  45. </div>
  46. <div>
  47. <label>Sort Order</label>
  48. <input type="number" name="sortOrder" value="0" min="0">
  49. </div>
  50. </div>
  51. <div style="display:flex;gap:.5rem;">
  52. <button type="submit" class="btn btn-primary">➕ Add Group</button>
  53. </div>
  54. </form>
  55. </div>
  56. <table class="tbl">
  57. <thead><tr><th>#</th><th>Name</th><th>Sort</th><th>Items</th><th></th></tr></thead>
  58. <tbody>
  59. <?php foreach ($groups as $g):
  60. $stmt = $pdo->prepare("SELECT COUNT(*) FROM items WHERE groupID=?");
  61. $stmt->execute([$g['groupID']]);
  62. $cnt = $stmt->fetchColumn();
  63. $id = $g['groupID'];
  64. ?>
  65. <tr id="view-<?= $id ?>">
  66. <td style="color:var(--muted)"><?= $id ?></td>
  67. <td><?= htmlspecialchars($g['groupName']) ?></td>
  68. <td><?= $g['sortOrder'] ?></td>
  69. <td><span class="badge badge-group"><?= $cnt ?></span></td>
  70. <td style="display:flex;gap:.4rem;justify-content:flex-end;">
  71. <button type="button" onclick="startEdit(<?= $id ?>)" class="btn btn-sm btn-teal">Edit</button>
  72. <form method="POST" onsubmit="return confirm('Delete group and ALL its items?')">
  73. <input type="hidden" name="action" value="delete">
  74. <input type="hidden" name="groupID" value="<?= $id ?>">
  75. <button class="btn btn-sm btn-danger">Delete</button>
  76. </form>
  77. </td>
  78. </tr>
  79. <tr id="edit-<?= $id ?>" style="display:none;background:rgba(79,209,197,.04);">
  80. <td colspan="5">
  81. <form method="POST" style="display:flex;gap:.75rem;align-items:flex-end;flex-wrap:wrap;padding:.25rem 0;">
  82. <input type="hidden" name="action" value="edit">
  83. <input type="hidden" name="groupID" value="<?= $id ?>">
  84. <div>
  85. <label>Group Name</label>
  86. <input type="text" name="groupName" value="<?= htmlspecialchars($g['groupName']) ?>" required style="margin-bottom:0;min-width:180px;">
  87. </div>
  88. <div>
  89. <label>Sort Order</label>
  90. <input type="number" name="sortOrder" value="<?= (int)$g['sortOrder'] ?>" min="0" style="margin-bottom:0;max-width:100px;">
  91. </div>
  92. <div style="display:flex;gap:.4rem;padding-bottom:1px;">
  93. <button type="submit" class="btn btn-sm btn-primary">💾 Save</button>
  94. <button type="button" onclick="cancelEdit(<?= $id ?>)" class="btn btn-sm btn-secondary">Cancel</button>
  95. </div>
  96. </form>
  97. </td>
  98. </tr>
  99. <?php endforeach; ?>
  100. </tbody>
  101. </table>
  102. <script>
  103. function startEdit(id) {
  104. document.getElementById('view-' + id).style.display = 'none';
  105. document.getElementById('edit-' + id).style.display = '';
  106. document.querySelector('#edit-' + id + ' input[name="groupName"]').focus();
  107. }
  108. function cancelEdit(id) {
  109. document.getElementById('edit-' + id).style.display = 'none';
  110. document.getElementById('view-' + id).style.display = '';
  111. }
  112. </script>
  113. <?php admin_foot(); ?>