<?php
require_once 'auth.php';

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

// Handle Add/Edit
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $id = $_POST['id'] ?? null;
    $name = $_POST['name'];
    $cat_id = $_POST['category_id'];
    $desc = $_POST['description'];
    $content = $_POST['content'];
    $price = $_POST['price_usdt'];
    $stock = $_POST['stock'];
    $img = $_POST['image_url'];
    $is_hot = isset($_POST['is_hot']) ? 1 : 0;

    if ($id) {
        $stmt = $db->prepare("UPDATE products SET name=?, category_id=?, description=?, content=?, price_usdt=?, stock=?, image_url=?, is_hot=? WHERE id=?");
        $stmt->execute([$name, $cat_id, $desc, $content, $price, $stock, $img, $is_hot, $id]);
    } else {
        $stmt = $db->prepare("INSERT INTO products (name, category_id, description, content, price_usdt, stock, image_url, is_hot) VALUES (?, ?, ?, ?, ?, ?, ?, ?)");
        $stmt->execute([$name, $cat_id, $desc, $content, $price, $stock, $img, $is_hot]);
    }
    header("Location: products.php");
    exit;
}

$categories = $db->query("SELECT * FROM categories")->fetchAll();
$products = $db->query("SELECT p.*, c.name as cat_name FROM products p JOIN categories c ON p.category_id = c.id ORDER BY p.id DESC")->fetchAll();

$edit_product = null;
if (isset($_GET['edit'])) {
    $stmt = $db->prepare("SELECT * FROM products WHERE id = ?");
    $stmt->execute([$_GET['edit']]);
    $edit_product = $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 rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.0/font/bootstrap-icons.css">
    <style>
        body { background-color: #f8f9fa; }
        .sidebar { min-height: 100vh; background: #212529; color: white; }
        .sidebar a { color: rgba(255,255,255,0.8); text-decoration: none; padding: 10px 20px; display: block; }
        .sidebar a:hover { background: rgba(255,255,255,0.1); color: white; }
        .sidebar a.active { background: #0d6efd; color: white; }
    </style>
</head>
<body>
<div class="container-fluid">
    <div class="row">
        <div class="col-md-2 p-0 sidebar">
            <?php include 'sidebar.php'; ?>
        </div>
        <div class="col-md-10 p-4">
            <div class="d-flex justify-content-between align-items-center mb-4">
                <h2><?php echo $edit_product ? '编辑商品' : '商品管理'; ?></h2>
                <?php if ($edit_product): ?>
                    <a href="products.php" class="btn btn-secondary">添加新商品</a>
                <?php endif; ?>
            </div>

            <div class="card shadow-sm mb-4">
                <div class="card-body">
                    <form method="POST">
                        <?php if ($edit_product): ?>
                            <input type="hidden" name="id" value="<?php echo $edit_product['id']; ?>">
                        <?php endif; ?>
                        
                        <div class="row g-3">
                            <div class="col-md-6">
                                <label class="form-label">商品名称</label>
                                <input type="text" name="name" class="form-control" value="<?php echo htmlspecialchars($edit_product['name'] ?? ''); ?>" required>
                            </div>
                            <div class="col-md-3">
                                <label class="form-label">所属分类</label>
                                <select name="category_id" class="form-select">
                                    <?php foreach ($categories as $c): ?>
                                        <option value="<?php echo $c['id']; ?>" <?php echo (isset($edit_product['category_id']) && $edit_product['category_id'] == $c['id']) ? 'selected' : ''; ?>>
                                            <?php echo $c['name']; ?>
                                        </option>
                                    <?php endforeach; ?>
                                </select>
                            </div>
                            <div class="col-md-3">
                                <label class="form-label">价格 (USDT)</label>
                                <input type="number" step="0.01" name="price_usdt" class="form-control" value="<?php echo $edit_product['price_usdt'] ?? ''; ?>" required>
                            </div>
                            <div class="col-md-3">
                                <label class="form-label">库存数量</label>
                                <input type="number" name="stock" class="form-control" value="<?php echo $edit_product['stock'] ?? '999'; ?>" required>
                            </div>
                            <div class="col-md-6">
                                <label class="form-label">商品图片 URL</label>
                                <input type="text" name="image_url" class="form-control" value="<?php echo htmlspecialchars($edit_product['image_url'] ?? ''); ?>" required>
                            </div>
                            <div class="col-md-3 d-flex align-items-end">
                                <div class="form-check mb-2">
                                    <input class="form-check-input" type="checkbox" name="is_hot" value="1" id="isHot" <?php echo (isset($edit_product['is_hot']) && $edit_product['is_hot']) ? 'checked' : ''; ?>>
                                    <label class="form-check-label" for="isHot">设为热门推荐</label>
                                </div>
                            </div>
                            <div class="col-12">
                                <label class="form-label">简短描述 (显示在列表页)</label>
                                <input type="text" name="description" class="form-control" value="<?php echo htmlspecialchars($edit_product['description'] ?? ''); ?>">
                            </div>
                            <div class="col-12">
                                <label class="form-label">详细内容 (显示在详情页)</label>
                                <textarea name="content" class="form-control" rows="5"><?php echo htmlspecialchars($edit_product['content'] ?? ''); ?></textarea>
                            </div>
                            <div class="col-12">
                                <button type="submit" class="btn btn-primary px-5"><?php echo $edit_product ? '保存修改' : '立即添加'; ?></button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>

            <div class="card shadow-sm">
                <div class="card-body">
                    <table class="table table-hover">
                        <thead>
                            <tr>
                                <th>ID</th>
                                <th>商品</th>
                                <th>分类</th>
                                <th>价格</th>
                                <th>库存</th>
                                <th>状态</th>
                                <th>操作</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($products as $p): ?>
                            <tr>
                                <td><?php echo $p['id']; ?></td>
                                <td>
                                    <div class="d-flex align-items-center">
                                        <img src="<?php echo $p['image_url']; ?>" width="32" height="32" class="me-2 rounded">
                                        <?php echo $p['name']; ?>
                                    </div>
                                </td>
                                <td><?php echo $p['cat_name']; ?></td>
                                <td><?php echo $p['price_usdt']; ?> USDT</td>
                                <td><?php echo $p['stock']; ?></td>
                                <td>
                                    <?php if ($p['is_hot']): ?>
                                        <span class="badge bg-danger">热门</span>
                                    <?php endif; ?>
                                </td>
                                <td>
                                    <a href="?edit=<?php echo $p['id']; ?>" class="btn btn-sm btn-outline-primary">编辑</a>
                                    <a href="?delete=<?php echo $p['id']; ?>" class="btn btn-sm btn-outline-danger" onclick="return confirm('确定删除？')">删除</a>
                                </td>
                            </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>
</body>
</html>