<?php
require_once 'config.php';
checkAdminAuth();

if (isPost()) {
    if (isset($_POST['add'])) {
        $name = $conn->real_escape_string($_POST['name']);
        $conn->query("INSERT INTO categories (name) VALUES ('$name')");
        $cache->clear();
        $_SESSION['success'] = 'تمت الإضافة';
        header('Location: categories.php');
        exit();
    }
    if (isset($_POST['delete'])) {
        $id = (int)$_POST['id'];
        $conn->query("DELETE FROM categories WHERE id=$id");
        $cache->clear();
        $_SESSION['success'] = 'تم الحذف';
        header('Location: categories.php');
        exit();
    }
}

$cats = $cache->get('cats_list');
if (!$cats) {
    $cats = db_query("SELECT * FROM categories ORDER BY name", 3600);
    $cache->set('cats_list', $cats, 3600);
}

$page_title = 'التصنيفات';
include 'includes/header.php';
include 'includes/sidebar.php';
?>

<div class="main-content">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h4 class="mb-0"><i class="bi bi-tags text-primary me-2"></i>التصنيفات</h4>
        <button class="btn btn-primary" onclick="openModal()"><i class="bi bi-plus-lg me-1"></i>إضافة</button>
    </div>

    <div class="row g-3">
        <?php foreach ($cats as $c): ?>
        <div class="col-md-3 col-6">
            <div class="card h-100">
                <div class="card-body text-center p-3">
                    <i class="bi bi-tag-fill text-primary fs-2 mb-2"></i>
                    <h6 class="mb-3"><?php echo e($c['name']); ?></h6>
                    <form method="POST">
                        <input type="hidden" name="id" value="<?php echo $c['id']; ?>">
                        <button type="submit" name="delete" class="btn btn-sm btn-danger w-100" onclick="return confirm('تأكيد الحذف؟')">
                            <i class="bi bi-trash me-1"></i>حذف
                        </button>
                    </form>
                </div>
            </div>
        </div>
        <?php endforeach; ?>
    </div>
</div>

<!-- Modal -->
<div class="modal fade" id="addModal" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">إضافة تصنيف</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <form method="POST">
                <div class="modal-body">
                    <input type="text" class="form-control" name="name" placeholder="اسم التصنيف" required>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">إلغاء</button>
                    <button type="submit" name="add" class="btn btn-primary">حفظ</button>
                </div>
            </form>
        </div>
    </div>
</div>

<script>
function openModal() { new bootstrap.Modal(document.getElementById('addModal')).show(); }
</script>

<?php include 'includes/footer.php'; ?>