<?php
require_once __DIR__ . '/../lib/core.php';
dc_require_login();
dc_require_csrf();

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $action = isset($_POST['action']) ? $_POST['action'] : 'save';
    $id = isset($_POST['id']) ? (int)$_POST['id'] : 0;
    if ($action === 'delete' && $id > 0) {
        $stmt = dc_db()->prepare('SELECT COUNT(*) FROM door_cert_doors WHERE category_id=?');
        $stmt->execute(array($id));
        if ((int)$stmt->fetchColumn() > 0) {
            dc_flash('error', 'Category is used by door records and cannot be deleted.');
        } else {
            $stmt = dc_db()->prepare('DELETE FROM door_cert_categories WHERE id=?');
            $stmt->execute(array($id));
            dc_flash('success', 'Category deleted.');
        }
        dc_redirect('admin/categories.php');
    }
    $code = dc_slug_code($_POST['code']);
    $name = trim($_POST['name']);
    $rating = trim($_POST['default_fire_rating']);
    if ($id > 0) {
        $stmt = dc_db()->prepare('UPDATE door_cert_categories SET code=?, name=?, default_fire_rating=? WHERE id=?');
        $stmt->execute(array($code, $name, $rating, $id));
        dc_flash('success', 'Category updated.');
    } else {
        $stmt = dc_db()->prepare('INSERT INTO door_cert_categories (code, name, default_fire_rating) VALUES (?, ?, ?)');
        $stmt->execute(array($code, $name, $rating));
        dc_flash('success', 'Category added.');
    }
    dc_redirect('admin/categories.php');
}

$edit = null;
if (isset($_GET['edit'])) {
    $stmt = dc_db()->prepare('SELECT * FROM door_cert_categories WHERE id=?');
    $stmt->execute(array((int)$_GET['edit']));
    $edit = $stmt->fetch();
}
$categories = dc_db()->query('SELECT * FROM door_cert_categories ORDER BY code')->fetchAll();

dc_admin_header('Door Categories');
?>
<div class="dc-card">
  <h2><?php echo $edit ? 'Edit Category' : 'Add Category'; ?></h2>
  <form method="post">
    <?php echo dc_csrf_field(); ?>
    <input type="hidden" name="id" value="<?php echo dc_h($edit ? $edit['id'] : 0); ?>">
    <div class="dc-grid">
      <div><label>Code</label><input name="code" value="<?php echo dc_h($edit ? $edit['code'] : ''); ?>" required></div>
      <div><label>Name</label><input name="name" value="<?php echo dc_h($edit ? $edit['name'] : ''); ?>" required></div>
    </div>
    <label>Default Fire Rating</label><input name="default_fire_rating" value="<?php echo dc_h($edit ? $edit['default_fire_rating'] : 'FD 120'); ?>">
    <p><button type="submit">Save Category</button></p>
  </form>
</div>
<div class="dc-card">
  <h2>Existing Categories</h2>
  <table><tr><th>Code</th><th>Name</th><th>Default Rating</th><th></th></tr>
    <?php foreach ($categories as $category): ?>
      <tr>
        <td><?php echo dc_h($category['code']); ?></td>
        <td><?php echo dc_h($category['name']); ?></td>
        <td><?php echo dc_h($category['default_fire_rating']); ?></td>
        <td class="dc-row-actions">
          <a href="categories.php?edit=<?php echo (int)$category['id']; ?>">Edit</a>
          <form method="post" class="dc-inline-form" onsubmit="return confirm('Delete this category?');">
            <?php echo dc_csrf_field(); ?>
            <input type="hidden" name="action" value="delete">
            <input type="hidden" name="id" value="<?php echo (int)$category['id']; ?>">
            <button type="submit" class="dc-link-button danger">Delete</button>
          </form>
        </td>
      </tr>
    <?php endforeach; ?>
  </table>
</div>
<?php dc_footer(); ?>
