<?php
// admin_categories.php - Manage blog categories
session_start();
require_once 'config.php';

if (!isset($_SESSION['admin'])) {
    header('Location: admin.php');
    exit;
}

// Add/Edit category
if (isset($_POST['save_category'])) {
    $id = (int)($_POST['category_id'] ?? 0);
    $name = $conn->real_escape_string($_POST['name']);
    $slug = $conn->real_escape_string(strtolower(str_replace(' ', '-', $name)));
    $description = $conn->real_escape_string($_POST['description']);
    $icon = $conn->real_escape_string($_POST['icon']);
    $display_order = (int)$_POST['display_order'];
    $is_active = isset($_POST['is_active']) ? 1 : 0;
    
    if ($id > 0) {
        $conn->query("UPDATE blog_categories SET name='$name', slug='$slug', description='$description', icon='$icon', display_order=$display_order, is_active=$is_active WHERE id=$id");
        $msg = "Category updated!";
    } else {
        $conn->query("INSERT INTO blog_categories (name, slug, description, icon, display_order, is_active) VALUES ('$name', '$slug', '$description', '$icon', $display_order, $is_active)");
        $msg = "Category added!";
    }
    header('Location: admin_categories.php?msg=' . urlencode($msg));
    exit;
}

// Delete category
if (isset($_GET['delete'])) {
    $id = (int)$_GET['delete'];
    $conn->query("DELETE FROM blog_categories WHERE id=$id");
    header('Location: admin_categories.php?msg=Category+deleted');
    exit;
}

$categories = $conn->query("SELECT * FROM blog_categories ORDER BY display_order");
$msg = $_GET['msg'] ?? '';
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Manage Categories | Admin</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" rel="stylesheet">
</head>
<body>
<div class="container-fluid p-4">
    <div class="d-flex justify-content-between mb-4">
        <h3><i class="fas fa-folder"></i> Blog Categories</h3>
        <a href="admin.php" class="btn btn-secondary">← Back</a>
    </div>
    
    <?php if($msg): ?>
        <div class="alert alert-success"><?=htmlspecialchars($msg)?></div>
    <?php endif; ?>
    
    <div class="row">
        <div class="col-md-5">
            <div class="card">
                <div class="card-header bg-success text-white">Add New Category</div>
                <div class="card-body">
                    <form method="POST">
                        <input type="hidden" name="category_id" id="cat_id" value="0">
                        <div class="mb-3">
                            <label>Category Name</label>
                            <input type="text" name="name" id="cat_name" class="form-control" required>
                        </div>
                        <div class="mb-3">
                            <label>Icon (Font Awesome)</label>
                            <input type="text" name="icon" id="cat_icon" class="form-control" value="fas fa-folder">
                            <small>e.g., fas fa-leaf, fas fa-heartbeat</small>
                        </div>
                        <div class="mb-3">
                            <label>Description</label>
                            <textarea name="description" id="cat_desc" class="form-control" rows="2"></textarea>
                        </div>
                        <div class="mb-3">
                            <label>Display Order</label>
                            <input type="number" name="display_order" id="cat_order" class="form-control" value="0">
                        </div>
                        <div class="mb-3 form-check">
                            <input type="checkbox" name="is_active" id="cat_active" class="form-check-input" checked>
                            <label>Active</label>
                        </div>
                        <button type="submit" name="save_category" class="btn btn-success">Save Category</button>
                        <button type="button" class="btn btn-secondary" onclick="resetForm()">Reset</button>
                    </form>
                </div>
            </div>
        </div>
        <div class="col-md-7">
            <div class="card">
                <div class="card-header bg-primary text-white">Categories List</div>
                <div class="card-body p-0">
                    <table class="table table-hover mb-0">
                        <thead class="table-light">
                            <tr><th>Icon</th><th>Name</th><th>Slug</th><th>Order</th><th>Status</th><th>Action</th></tr>
                        </thead>
                        <tbody>
                            <?php while($cat = $categories->fetch_assoc()): ?>
                            <tr>
                                <td><i class="<?=$cat['icon']?>"></i></td>
                                <td><strong><?=htmlspecialchars($cat['name'])?></strong></td>
                                <td><small><?=$cat['slug']?></small></td>
                                <td><?=$cat['display_order']?></td>
                                <td><?=$cat['is_active']?'✅ Active':'❌ Inactive'?></td>
                                <td>
                                    <button class="btn btn-sm btn-warning" onclick="editCategory(<?=htmlspecialchars(json_encode($cat))?>)"><i class="fas fa-edit"></i></button>
                                    <a href="?delete=<?=$cat['id']?>" class="btn btn-sm btn-danger" onclick="return confirm('Delete?')"><i class="fas fa-trash"></i></a>
                                </td>
                            </tr>
                            <?php endwhile; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
function editCategory(cat) {
    document.getElementById('cat_id').value = cat.id;
    document.getElementById('cat_name').value = cat.name;
    document.getElementById('cat_icon').value = cat.icon;
    document.getElementById('cat_desc').value = cat.description;
    document.getElementById('cat_order').value = cat.display_order;
    document.getElementById('cat_active').checked = cat.is_active == 1;
    document.querySelector('.card-header.bg-success').innerText = 'Edit Category';
}

function resetForm() {
    document.getElementById('cat_id').value = 0;
    document.getElementById('cat_name').value = '';
    document.getElementById('cat_icon').value = 'fas fa-folder';
    document.getElementById('cat_desc').value = '';
    document.getElementById('cat_order').value = 0;
    document.getElementById('cat_active').checked = true;
    document.querySelector('.card-header.bg-success').innerText = 'Add New Category';
}
</script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>