<?php
require_once 'auth.php';

// Handle Delete
if (isset($_GET['delete'])) {
    $stmt = $db->prepare("DELETE FROM categories WHERE id = ?");
    $stmt->execute([$_GET['delete']]);
    header("Location: categories.php");
    exit;
}

// Handle Add/Edit
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $id = $_POST['id'] ?? null;
    $name = $_POST['name'];
    $icon = $_POST['icon'];

    if ($id) {
        $stmt = $db->prepare("UPDATE categories SET name=?, icon=? WHERE id=?");
        $stmt->execute([$name, $icon, $id]);
    } else {
        $stmt = $db->prepare("INSERT INTO categories (name, icon) VALUES (?, ?)");
        $stmt->execute([$name, $icon]);
    }
    header("Location: categories.php");
    exit;
}

$categories = $db->query("SELECT * FROM categories ORDER BY id ASC")->fetchAll();

$edit_cat = null;
if (isset($_GET['edit'])) {
    $stmt = $db->prepare("SELECT * FROM categories WHERE id = ?");
    $stmt->execute([$_GET['edit']]);
    $edit_cat = $stmt->fetch();
}
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>分类管理 - 豪软后台</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.0/font/bootstrap-icons.css" rel="stylesheet">
    <style>
        body { background: #f8f9fa; }
        .sidebar { min-height: 100vh; background: #212529; color: white; }
        .sidebar a { color: rgba(255,255,255,0.7); text-decoration: none; padding: 12px 20px; display: block; }
        .sidebar a:hover, .sidebar a.active { background: #343a40; color: white; }
    </style>
</head>
<body>
<div class="container-fluid">
    <div class="row">
        <div class="col-md-2 p-0 sidebar">
            <div class="p-4 fs-4 fw-bold border-bottom border-secondary">豪软管理</div>
            <a href="index.php"><i class="bi bi-speedometer2 me-2"></i> 仪表盘</a>
            <a href="products.php"><i class="bi bi-box-seam me-2"></i> 商品管理</a>
            <a href="categories.php" class="active"><i class="bi bi-grid me-2"></i> 分类管理</a>
            <a href="orders.php"><i class="bi bi-cart me-2"></i> 订单管理</a>
            <a href="settings.php"><i class="bi bi-gear me-2"></i> 站点设置</a>
            <div class="mt-auto p-3">
                <a href="../index.php" target="_blank" class="small"><i class="bi bi-house me-1"></i> 返回前台</a>
                <a href="../logout.php" class="small text-danger"><i class="bi bi-box-arrow-right me-1"></i> 退出</a>
            </div>
        </div>
        <div class="col-md-10 p-5">
            <div class="d-flex justify-content-between align-items-center mb-4">
                <h2><?php echo $edit_cat ? '编辑分类' : '新增分类'; ?></h2>
            </div>

            <div class="card p-4 mb-5">
                <form method="POST">
                    <?php if ($edit_cat): ?><input type="hidden" name="id" value="<?php echo $edit_cat['id']; ?>"><?php endif; ?>
                    <div class="row g-3">
                        <div class="col-md-5">
                            <label class="form-label">分类名称</label>
                            <input type="text" name="name" class="form-control" value="<?php echo $edit_cat['name'] ?? ''; ?>" required>
                        </div>
                        <div class="col-md-5">
                            <label class="form-label">图标 (Bootstrap Icon Class)</label>
                            <input type="text" name="icon" class="form-control" value="<?php echo $edit_cat['icon'] ?? 'bi-box'; ?>" placeholder="bi-wechat">
                        </div>
                        <div class="col-md-2 d-flex align-items-end">
                            <button type="submit" class="btn btn-primary w-100"><?php echo $edit_cat ? '更新' : '添加'; ?></button>
                        </div>
                    </div>
                </form>
            </div>

            <div class="card p-4">
                <table class="table table-hover align-middle">
                    <thead>
                        <tr>
                            <th>图标</th>
                            <th>分类名称</th>
                            <th>操作</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($categories as $c): ?>
                        <tr>
                            <td><i class="bi <?php echo $c['icon']; ?> fs-4"></i></td>
                            <td class="fw-bold"><?php echo $c['name']; ?></td>
                            <td>
                                <a href="?edit=<?php echo $c['id']; ?>" class="btn btn-sm btn-outline-info me-2">编辑</a>
                                <a href="?delete=<?php echo $c['id']; ?>" class="btn btn-sm btn-outline-danger" onclick="return confirm('确定删除?')">删除</a>
                            </td>
                        </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>
</body>
</html>
