<?php
require_once __DIR__ . '/header.php';
$pdo = db();

$action = $_GET['action'] ?? 'list';
$id = $_GET['id'] ?? null;

if ($action === 'delete' && $id) {
    $stmt = $pdo->prepare("DELETE FROM pricing_plans WHERE id = ?");
    $stmt->execute([$id]);
    header('Location: pricing.php?msg=deleted');
    exit;
}

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $data = [
        'name' => $_POST['name'],
        'price_range' => $_POST['price_range'],
        'features' => $_POST['features'],
        'is_featured' => isset($_POST['is_featured']) ? 1 : 0,
        'sort_order' => (int)$_POST['sort_order']
    ];

    if ($id) {
        $sql = "UPDATE pricing_plans SET name=:name, price_range=:price_range, features=:features, is_featured=:is_featured, sort_order=:sort_order WHERE id=:id";
        $data['id'] = $id;
    } else {
        $sql = "INSERT INTO pricing_plans (name, price_range, features, is_featured, sort_order) VALUES (:name, :price_range, :features, :is_featured, :sort_order)";
    }

    $stmt = $pdo->prepare($sql);
    $stmt->execute($data);
    header('Location: pricing.php?msg=saved');
    exit;
}

$plans = $pdo->query("SELECT * FROM pricing_plans ORDER BY sort_order ASC, id DESC")->fetchAll();
$plan = null;
if ($action === 'edit' && $id) {
    $stmt = $pdo->prepare("SELECT * FROM pricing_plans WHERE id = ?");
    $stmt->execute([$id]);
    $plan = $stmt->fetch();
}
?>

<div class="card">
    <div class="card-header d-flex justify-content-between align-items-center">
        <span><?= $action === 'edit' ? '编辑方案' : ($action === 'add' ? '新增方案' : '报价方案列表') ?></span>
        <?php if ($action === 'list'): ?>
            <a href="pricing.php?action=add" class="btn btn-sm btn-primary">新增方案</a>
        <?php else: ?>
            <a href="pricing.php" class="btn btn-sm btn-secondary">返回列表</a>
        <?php endif; ?>
    </div>
    <div class="card-body">
        <?php if ($action === 'list'): ?>
            <div class="table-responsive">
                <table class="table align-middle">
                    <thead>
                        <tr>
                            <th>方案名称</th>
                            <th>价格区间</th>
                            <th>核心特征</th>
                            <th>精选</th>
                            <th>操作</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($plans as $p): ?>
                        <tr>
                            <td><strong><?= htmlspecialchars($p['name']) ?></strong></td>
                            <td><span class="text-primary fw-bold"><?= htmlspecialchars($p['price_range']) ?></span></td>
                            <td class="small text-muted"><?= htmlspecialchars($p['features']) ?></td>
                            <td><?= $p['is_featured'] ? '<span class="badge bg-success">是</span>' : '<span class="badge bg-light text-dark">否</span>' ?></td>
                            <td>
                                <a href="pricing.php?action=edit&id=<?= $p['id'] ?>" class="btn btn-sm btn-outline-primary"><i class="fas fa-edit"></i></a>
                                <a href="pricing.php?action=delete&id=<?= $p['id'] ?>" class="btn btn-sm btn-outline-danger" onclick="return confirm('确定删除吗？')"><i class="fas fa-trash"></i></a>
                            </td>
                        </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        <?php else: ?>
            <form method="POST">
                <div class="row">
                    <div class="col-md-6 mb-3">
                        <label class="form-label">方案名称</label>
                        <input type="text" name="name" class="form-control" value="<?= htmlspecialchars($plan['name'] ?? '') ?>" required>
                    </div>
                    <div class="col-md-6 mb-3">
                        <label class="form-label">价格区间 (如: ¥49,900 起)</label>
                        <input type="text" name="price_range" class="form-control" value="<?= htmlspecialchars($plan['price_range'] ?? '') ?>" required>
                    </div>
                    <div class="col-md-12 mb-3">
                        <label class="form-label">核心特征 / 描述</label>
                        <textarea name="features" class="form-control" rows="3"><?= htmlspecialchars($plan['features'] ?? '') ?></textarea>
                    </div>
                    <div class="col-md-6 mb-3">
                        <label class="form-label">排序</label>
                        <input type="number" name="sort_order" class="form-control" value="<?= $plan['sort_order'] ?? 0 ?>">
                    </div>
                    <div class="col-md-6 mb-3">
                        <div class="form-check mt-4">
                            <input class="form-check-input" type="checkbox" name="is_featured" id="is_featured" <?= ($plan['is_featured'] ?? 0) ? 'checked' : '' ?>>
                            <label class="form-check-label" for="is_featured">设置为精选</label>
                        </div>
                    </div>
                </div>
                <div class="mt-4">
                    <button type="submit" class="btn btn-primary">保存</button>
                    <a href="pricing.php" class="btn btn-light ms-2">取消</a>
                </div>
            </form>
        <?php endif; ?>
    </div>
</div>

<?php require_once __DIR__ . '/footer.php'; ?>
