items.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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['itemName'] ?? '');
  10. $gid = (int)$_POST['groupID'];
  11. $order = (int)($_POST['sortOrder'] ?? 0);
  12. if ($name !== '' && $gid > 0) {
  13. $pdo->prepare("INSERT INTO items (groupID,itemName,sortOrder) VALUES (?,?,?)")->execute([$gid,$name,$order]);
  14. flash('success', "Item '$name' added.");
  15. } else { flash('error', 'Please fill all fields.'); }
  16. }
  17. if ($action === 'edit') {
  18. $id = (int)$_POST['itemID'];
  19. $name = trim($_POST['itemName'] ?? '');
  20. $gid = (int)$_POST['groupID'];
  21. $order = (int)($_POST['sortOrder'] ?? 0);
  22. if ($name !== '') {
  23. $pdo->prepare("UPDATE items SET itemName=?,groupID=?,sortOrder=? WHERE itemID=?")->execute([$name,$gid,$order,$id]);
  24. flash('success', 'Item updated.');
  25. } else { flash('error', 'Name required.'); }
  26. }
  27. if ($action === 'delete') {
  28. $id = (int)$_POST['itemID'];
  29. $pdo->prepare("DELETE FROM items WHERE itemID=?")->execute([$id]);
  30. flash('success', 'Item deleted.');
  31. }
  32. header('Location: items.php'); exit;
  33. }
  34. $igroups = $pdo->query("SELECT * FROM item_groups ORDER BY sortOrder,groupID")->fetchAll();
  35. $items = $pdo->query("
  36. SELECT i.*, ig.groupName
  37. FROM items i
  38. JOIN item_groups ig ON ig.groupID = i.groupID
  39. ORDER BY ig.sortOrder, i.sortOrder, i.itemID
  40. ")->fetchAll();
  41. admin_head('Items', 'items.php');
  42. show_alerts();
  43. ?>
  44. <h1>Items</h1>
  45. <div class="card">
  46. <h2>Add New Item</h2>
  47. <form method="POST">
  48. <input type="hidden" name="action" value="add">
  49. <div class="form-row">
  50. <div>
  51. <label>Item Name</label>
  52. <input type="text" name="itemName" placeholder="e.g. Rain jacket" required>
  53. </div>
  54. <div>
  55. <label>Group</label>
  56. <select name="groupID" required>
  57. <option value="">– select –</option>
  58. <?php foreach ($igroups as $g): ?>
  59. <option value="<?= $g['groupID'] ?>"><?= htmlspecialchars($g['groupName']) ?></option>
  60. <?php endforeach; ?>
  61. </select>
  62. </div>
  63. </div>
  64. <div>
  65. <label>Sort Order</label>
  66. <input type="number" name="sortOrder" value="0" min="0" style="max-width:120px;">
  67. </div>
  68. <div style="display:flex;gap:.5rem;margin-top:.25rem;">
  69. <button type="submit" class="btn btn-primary">➕ Add</button>
  70. </div>
  71. </form>
  72. </div>
  73. <table class="tbl">
  74. <thead><tr><th>#</th><th>Name</th><th>Group</th><th>Sort</th><th></th></tr></thead>
  75. <tbody>
  76. <?php foreach ($items as $item):
  77. $id = $item['itemID'];
  78. ?>
  79. <tr id="view-<?= $id ?>">
  80. <td style="color:var(--muted)"><?= $id ?></td>
  81. <td><?= htmlspecialchars($item['itemName']) ?></td>
  82. <td><span class="badge badge-group"><?= htmlspecialchars($item['groupName']) ?></span></td>
  83. <td><?= $item['sortOrder'] ?></td>
  84. <td style="display:flex;gap:.4rem;justify-content:flex-end;">
  85. <button type="button" onclick="startEdit(<?= $id ?>)" class="btn btn-sm btn-teal">Edit</button>
  86. <form method="POST" onsubmit="return confirm('Delete this item?')">
  87. <input type="hidden" name="action" value="delete">
  88. <input type="hidden" name="itemID" value="<?= $id ?>">
  89. <button class="btn btn-sm btn-danger">Delete</button>
  90. </form>
  91. </td>
  92. </tr>
  93. <tr id="edit-<?= $id ?>" style="display:none;background:rgba(79,209,197,.04);">
  94. <td colspan="5">
  95. <form method="POST" style="display:flex;gap:.75rem;align-items:flex-end;flex-wrap:wrap;padding:.25rem 0;">
  96. <input type="hidden" name="action" value="edit">
  97. <input type="hidden" name="itemID" value="<?= $id ?>">
  98. <div>
  99. <label>Item Name</label>
  100. <input type="text" name="itemName" value="<?= htmlspecialchars($item['itemName']) ?>" required style="margin-bottom:0;min-width:180px;">
  101. </div>
  102. <div>
  103. <label>Group</label>
  104. <select name="groupID" required style="margin-bottom:0;min-width:140px;">
  105. <?php foreach ($igroups as $g): ?>
  106. <option value="<?= $g['groupID'] ?>" <?= $item['groupID'] == $g['groupID'] ? 'selected' : '' ?>>
  107. <?= htmlspecialchars($g['groupName']) ?>
  108. </option>
  109. <?php endforeach; ?>
  110. </select>
  111. </div>
  112. <div>
  113. <label>Sort Order</label>
  114. <input type="number" name="sortOrder" value="<?= (int)$item['sortOrder'] ?>" min="0" style="margin-bottom:0;max-width:90px;">
  115. </div>
  116. <div style="display:flex;gap:.4rem;padding-bottom:1px;">
  117. <button type="submit" class="btn btn-sm btn-primary">💾 Save</button>
  118. <button type="button" onclick="cancelEdit(<?= $id ?>)" class="btn btn-sm btn-secondary">Cancel</button>
  119. </div>
  120. </form>
  121. </td>
  122. </tr>
  123. <?php endforeach; ?>
  124. </tbody>
  125. </table>
  126. <script>
  127. function startEdit(id) {
  128. document.getElementById('view-' + id).style.display = 'none';
  129. document.getElementById('edit-' + id).style.display = '';
  130. document.querySelector('#edit-' + id + ' input[name="itemName"]').focus();
  131. }
  132. function cancelEdit(id) {
  133. document.getElementById('edit-' + id).style.display = 'none';
  134. document.getElementById('view-' + id).style.display = '';
  135. }
  136. </script>
  137. <?php admin_foot(); ?>