<?php
include 'layout.php';
include 'db.php';
include 'auth.php';
require_right('View Categories');

// Handle Add Category
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['new_category_name'])) {
    $stmt = $conn->prepare("INSERT INTO categories (name) VALUES (?)");
    $stmt->bind_param("s", $_POST['new_category_name']);
    $stmt->execute();
    header("Location: category_list.php");
    exit;
}

// Handle Add Sub Category
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['new_sub_category_name'])) {
    $stmt = $conn->prepare("INSERT INTO sub_categories (category_id, name) VALUES (?, ?)");
    $stmt->bind_param("is", $_POST['new_sub_category_category_id'], $_POST['new_sub_category_name']);
    $stmt->execute();
    header("Location: category_list.php");
    exit;
}

// Fetch all categories with their sub-categories
$query = "
    SELECT 
        c.id AS category_id,
        c.name AS category_name,
        GROUP_CONCAT(sc.name ORDER BY sc.name SEPARATOR ', ') AS sub_categories
    FROM categories c
    LEFT JOIN sub_categories sc ON sc.category_id = c.id
    GROUP BY c.id
    ORDER BY c.id DESC
";

$result = $conn->query($query);
$catOptions = $conn->query("SELECT id, name FROM categories");
?>

<div class="page-wrapper">
    <div class="content">
        <div class="page-header d-flex justify-content-between align-items-center">
            <div class="page-title">
                <h4>Category List</h4>
                <h6>View all categories and their sub-categories</h6>
            </div>
            <div class="page-btn">
                <button class="btn btn-primary me-2" data-bs-toggle="modal" data-bs-target="#addCategoryModal">
                    <i class="fa fa-plus"></i> Add Category
                </button>
                <button class="btn btn-secondary" data-bs-toggle="modal" data-bs-target="#addSubCategoryModal">
                    <i class="fa fa-plus"></i> Add Sub Category
                </button>
            </div>
        </div>

        <div class="card">
            <div class="table-responsive">
                <table class="table datatable">
                    <thead>
                        <tr>
                            <th>ID</th>
                            <th>Category Name</th>
                            <th>Sub Categories</th>
                            <th class="text-end">Actions</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php while ($row = $result->fetch_assoc()): ?>
                        <tr>
                            <td><?= $row['category_id'] ?></td>
                            <td><?= htmlspecialchars($row['category_name']) ?></td>
                            <td><?= htmlspecialchars($row['sub_categories'] ?? '—') ?></td>
                            <td class="text-end">
                                <a href="edit_category.php?id=<?= $row['category_id'] ?>" class="btn btn-sm btn-outline-warning">
                                    <i class="fa fa-edit"></i>
                                </a>
                                <a href="delete_category.php?id=<?= $row['category_id'] ?>" onclick="return confirm('Delete this category and its sub-categories?')" class="btn btn-sm btn-outline-danger">
                                    <i class="fa fa-trash"></i>
                                </a>
                            </td>
                        </tr>
                        <?php endwhile; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>

<!-- Add Category Modal -->
<div class="modal fade" id="addCategoryModal" tabindex="-1">
    <div class="modal-dialog">
        <form method="POST" action="category_list.php" class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">Add Category</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <div class="modal-body">
                <input type="text" name="new_category_name" placeholder="Category Name" class="form-control mb-3" required>
            </div>
            <div class="modal-footer">
                <button class="btn btn-primary">Save</button>
            </div>
        </form>
    </div>
</div>

<!-- Add Sub Category Modal -->
<div class="modal fade" id="addSubCategoryModal" tabindex="-1">
    <div class="modal-dialog">
        <form method="POST" action="category_list.php" class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">Add Sub Category</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <div class="modal-body">
                <select name="new_sub_category_category_id" class="form-control mb-3" required>
                    <option value="">Select Category</option>
                    <?php while ($cat = $catOptions->fetch_assoc()): ?>
                        <option value="<?= $cat['id'] ?>"><?= $cat['name'] ?></option>
                    <?php endwhile; ?>
                </select>
                <input type="text" name="new_sub_category_name" placeholder="Sub Category Name" class="form-control mb-3" required>
            </div>
            <div class="modal-footer">
                <button class="btn btn-primary">Save</button>
            </div>
        </form>
    </div>
</div>
