item_groups.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. <?php foreach ($groups as $g): ?>
  57. <form id="edit-form-<?= $g['groupID'] ?>" method="POST">
  58. <input type="hidden" name="action" value="edit">
  59. <input type="hidden" name="groupID" value="<?= $g['groupID'] ?>">
  60. </form>
  61. <?php endforeach; ?>
  62. <table class="tbl">
  63. <thead><tr><th>#</th><th>Name</th><th>Sort</th><th>Items</th><th></th></tr></thead>
  64. <tbody>
  65. <?php foreach ($groups as $g):
  66. $stmt = $pdo->prepare("SELECT COUNT(*) FROM items WHERE groupID=?");
  67. $stmt->execute([$g['groupID']]);
  68. $cnt = $stmt->fetchColumn();
  69. $id = $g['groupID'];
  70. ?>
  71. <tr id="view-<?= $id ?>">
  72. <td style="color:var(--muted)"><?= $id ?></td>
  73. <td><?= htmlspecialchars($g['groupName']) ?></td>
  74. <td><?= $g['sortOrder'] ?></td>
  75. <td><span class="badge badge-group"><?= $cnt ?></span></td>
  76. <td style="display:flex;gap:.4rem;justify-content:flex-end;">
  77. <button type="button" onclick="startEdit(<?= $id ?>)" class="btn btn-sm btn-teal">Edit</button>
  78. <form method="POST" onsubmit="return confirm('Delete group and ALL its items?')">
  79. <input type="hidden" name="action" value="delete">
  80. <input type="hidden" name="groupID" value="<?= $id ?>">
  81. <button class="btn btn-sm btn-danger">Delete</button>
  82. </form>
  83. </td>
  84. </tr>
  85. <tr id="edit-<?= $id ?>" style="display:none;background:rgba(79,209,197,.04);">
  86. <td style="color:var(--muted)"><?= $id ?></td>
  87. <td><input type="text" name="groupName" form="edit-form-<?= $id ?>" value="<?= htmlspecialchars($g['groupName']) ?>" required style="margin:0;width:100%;"></td>
  88. <td><input type="number" name="sortOrder" form="edit-form-<?= $id ?>" value="<?= (int)$g['sortOrder'] ?>" min="0" style="margin:0;width:100%;max-width:80px;"></td>
  89. <td><span class="badge badge-group"><?= $cnt ?></span></td>
  90. <td style="display:flex;gap:.4rem;justify-content:flex-end;">
  91. <button type="submit" form="edit-form-<?= $id ?>" class="btn btn-sm btn-primary">💾 Save</button>
  92. <button type="button" onclick="cancelEdit(<?= $id ?>)" class="btn btn-sm btn-secondary">Cancel</button>
  93. </td>
  94. </tr>
  95. <?php endforeach; ?>
  96. </tbody>
  97. </table>
  98. <script>
  99. function startEdit(id) {
  100. document.getElementById('view-' + id).style.display = 'none';
  101. document.getElementById('edit-' + id).style.display = '';
  102. document.querySelector('#edit-' + id + ' input[name="groupName"]').focus();
  103. }
  104. function cancelEdit(id) {
  105. document.getElementById('edit-' + id).style.display = 'none';
  106. document.getElementById('view-' + id).style.display = '';
  107. }
  108. </script>
  109. <?php admin_foot(); ?>