| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- <?php
- require_once __DIR__ . '/auth.php';
- require_once __DIR__ . '/layout.php';
- require_admin();
- $pdo = get_pdo();
- if ($_SERVER['REQUEST_METHOD'] === 'POST') {
- $action = $_POST['action'] ?? '';
- if ($action === 'add') {
- $name = trim($_POST['groupName'] ?? '');
- $order = (int)($_POST['sortOrder'] ?? 0);
- if ($name !== '') {
- $pdo->prepare("INSERT INTO item_groups (groupName, sortOrder) VALUES (?,?)")->execute([$name, $order]);
- flash('success', "Group '$name' added.");
- } else { flash('error', 'Name required.'); }
- }
- if ($action === 'edit') {
- $id = (int)$_POST['groupID'];
- $name = trim($_POST['groupName'] ?? '');
- $order = (int)($_POST['sortOrder'] ?? 0);
- if ($name !== '') {
- $pdo->prepare("UPDATE item_groups SET groupName=?,sortOrder=? WHERE groupID=?")->execute([$name,$order,$id]);
- flash('success', 'Group updated.');
- } else { flash('error', 'Name required.'); }
- }
- if ($action === 'delete') {
- $id = (int)$_POST['groupID'];
- $pdo->prepare("DELETE FROM item_groups WHERE groupID=?")->execute([$id]);
- flash('success', 'Group deleted.');
- }
- header('Location: item_groups.php'); exit;
- }
- $groups = $pdo->query("SELECT * FROM item_groups ORDER BY sortOrder, groupID")->fetchAll();
- admin_head('Item Groups', 'item_groups.php');
- show_alerts();
- ?>
- <h1>Item Groups</h1>
- <div class="card">
- <h2>Add New Group</h2>
- <form method="POST">
- <input type="hidden" name="action" value="add">
- <div class="form-row">
- <div>
- <label>Group Name</label>
- <input type="text" name="groupName" placeholder="e.g. Clothing" required>
- </div>
- <div>
- <label>Sort Order</label>
- <input type="number" name="sortOrder" value="0" min="0">
- </div>
- </div>
- <div style="display:flex;gap:.5rem;">
- <button type="submit" class="btn btn-primary">➕ Add Group</button>
- </div>
- </form>
- </div>
- <?php foreach ($groups as $g): ?>
- <form id="edit-form-<?= $g['groupID'] ?>" method="POST">
- <input type="hidden" name="action" value="edit">
- <input type="hidden" name="groupID" value="<?= $g['groupID'] ?>">
- </form>
- <?php endforeach; ?>
- <table class="tbl">
- <thead><tr><th>#</th><th>Name</th><th>Sort</th><th>Items</th><th></th></tr></thead>
- <tbody>
- <?php foreach ($groups as $g):
- $stmt = $pdo->prepare("SELECT COUNT(*) FROM items WHERE groupID=?");
- $stmt->execute([$g['groupID']]);
- $cnt = $stmt->fetchColumn();
- $id = $g['groupID'];
- ?>
- <tr id="view-<?= $id ?>">
- <td style="color:var(--muted)"><?= $id ?></td>
- <td><?= htmlspecialchars($g['groupName']) ?></td>
- <td><?= $g['sortOrder'] ?></td>
- <td><span class="badge badge-group"><?= $cnt ?></span></td>
- <td style="display:flex;gap:.4rem;justify-content:flex-end;">
- <button type="button" onclick="startEdit(<?= $id ?>)" class="btn btn-sm btn-teal">Edit</button>
- <form method="POST" onsubmit="return confirm('Delete group and ALL its items?')">
- <input type="hidden" name="action" value="delete">
- <input type="hidden" name="groupID" value="<?= $id ?>">
- <button class="btn btn-sm btn-danger">Delete</button>
- </form>
- </td>
- </tr>
- <tr id="edit-<?= $id ?>" style="display:none;background:rgba(79,209,197,.04);">
- <td style="color:var(--muted)"><?= $id ?></td>
- <td><input type="text" name="groupName" form="edit-form-<?= $id ?>" value="<?= htmlspecialchars($g['groupName']) ?>" required style="margin:0;width:100%;"></td>
- <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>
- <td><span class="badge badge-group"><?= $cnt ?></span></td>
- <td style="display:flex;gap:.4rem;justify-content:flex-end;">
- <button type="submit" form="edit-form-<?= $id ?>" class="btn btn-sm btn-primary">💾 Save</button>
- <button type="button" onclick="cancelEdit(<?= $id ?>)" class="btn btn-sm btn-secondary">Cancel</button>
- </td>
- </tr>
- <?php endforeach; ?>
- </tbody>
- </table>
- <script>
- function startEdit(id) {
- document.getElementById('view-' + id).style.display = 'none';
- document.getElementById('edit-' + id).style.display = '';
- document.querySelector('#edit-' + id + ' input[name="groupName"]').focus();
- }
- function cancelEdit(id) {
- document.getElementById('edit-' + id).style.display = 'none';
- document.getElementById('view-' + id).style.display = '';
- }
- </script>
- <?php admin_foot(); ?>
|