<?php
require_once __DIR__ . '/config/config.php';
require_permission('categories.manage');

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $type = $_POST['type'] ?? '';
    if ($type === 'category') {
        $name = trim($_POST['name'] ?? '');
        if ($name !== '') {
            $pdo->prepare("INSERT INTO categories (name) VALUES (?)")->execute([$name]);
            flash('success', 'Category added.');
        }
    } elseif ($type === 'unit') {
        $name = trim($_POST['name'] ?? '');
        $abbr = trim($_POST['abbreviation'] ?? '');
        if ($name !== '' && $abbr !== '') {
            $pdo->prepare("INSERT INTO units (name, abbreviation) VALUES (?,?)")->execute([$name, $abbr]);
            flash('success', 'Unit added.');
        }
    }
    redirect('categories.php');
}
if (isset($_GET['del_cat'])) {
    $pdo->prepare("DELETE FROM categories WHERE id=?")->execute([$_GET['del_cat']]);
    redirect('categories.php');
}
if (isset($_GET['del_unit'])) {
    $pdo->prepare("DELETE FROM units WHERE id=?")->execute([$_GET['del_unit']]);
    redirect('categories.php');
}

$categories = $pdo->query("SELECT * FROM categories ORDER BY name")->fetchAll();
$units = $pdo->query("SELECT * FROM units ORDER BY name")->fetchAll();

$pageTitle = 'Categories & Units';
include __DIR__ . '/includes/header.php';
include __DIR__ . '/includes/sidebar.php';
?>
<h1 class="text-2xl font-bold mb-6">Categories & Units of Measure</h1>

<div class="grid grid-cols-1 md:grid-cols-2 gap-6">
  <div class="bg-white rounded-xl shadow-sm border border-slate-200 p-5">
    <h2 class="font-semibold mb-4">Item Categories</h2>
    <form method="POST" class="flex gap-2 mb-4">
      <input type="hidden" name="type" value="category">
      <input name="name" required placeholder="New category name" class="flex-1 border border-slate-300 rounded-lg px-3 py-2 text-sm">
      <button class="bg-blue-700 hover:bg-blue-800 text-white text-sm font-medium px-4 py-2 rounded-lg">Add</button>
    </form>
    <ul class="divide-y divide-slate-100">
      <?php foreach ($categories as $c): ?>
        <li class="py-2 flex justify-between items-center text-sm">
          <span><?= e($c['name']) ?></span>
          <a href="?del_cat=<?= $c['id'] ?>" onclick="return confirm('Delete category?')" class="text-red-600 hover:underline text-xs">Delete</a>
        </li>
      <?php endforeach; ?>
    </ul>
  </div>

  <div class="bg-white rounded-xl shadow-sm border border-slate-200 p-5">
    <h2 class="font-semibold mb-4">Units of Measure</h2>
    <form method="POST" class="flex gap-2 mb-4">
      <input type="hidden" name="type" value="unit">
      <input name="name" required placeholder="Unit name" class="flex-1 border border-slate-300 rounded-lg px-3 py-2 text-sm">
      <input name="abbreviation" required placeholder="Abbr." class="w-24 border border-slate-300 rounded-lg px-3 py-2 text-sm">
      <button class="bg-blue-700 hover:bg-blue-800 text-white text-sm font-medium px-4 py-2 rounded-lg">Add</button>
    </form>
    <ul class="divide-y divide-slate-100">
      <?php foreach ($units as $u): ?>
        <li class="py-2 flex justify-between items-center text-sm">
          <span><?= e($u['name']) ?> <span class="text-slate-400">(<?= e($u['abbreviation']) ?>)</span></span>
          <a href="?del_unit=<?= $u['id'] ?>" onclick="return confirm('Delete unit?')" class="text-red-600 hover:underline text-xs">Delete</a>
        </li>
      <?php endforeach; ?>
    </ul>
  </div>
</div>
<?php include __DIR__ . '/includes/footer.php'; ?>
