<?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 services WHERE id = ?");
    $stmt->execute([$id]);
    header('Location: services.php?msg=deleted');
    exit;
}

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $data = [
        'title' => $_POST['title'],
        'icon' => $_POST['icon'],
        'description' => $_POST['description'],
        'sort_order' => (int)$_POST['sort_order']
    ];

    if ($id) {
        $sql = "UPDATE services SET title=:title, icon=:icon, description=:description, sort_order=:sort_order WHERE id=:id";
        $data['id'] = $id;
    } else {
        $sql = "INSERT INTO services (title, icon, description, sort_order) VALUES (:title, :icon, :description, :sort_order)";
    }

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

$services = $pdo->query("SELECT * FROM services ORDER BY sort_order ASC, id DESC")->fetchAll();
$service = null;
if ($action === 'edit' && $id) {
    $stmt = $pdo->prepare("SELECT * FROM services WHERE id = ?");
    $stmt->execute([$id]);
    $service = $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="services.php?action=add" class="btn btn-sm btn-primary">新增服务</a>
        <?php else: ?>
            <a href="services.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 ($services as $s): ?>
                        <tr>
                            <td><i class="bi bi-<?= htmlspecialchars($s['icon']) ?> fs-3"></i></td>
                            <td><strong><?= htmlspecialchars($s['title']) ?></strong></td>
                            <td class="small text-muted"><?= htmlspecialchars($s['description']) ?></td>
                            <td><?= $s['sort_order'] ?></td>
                            <td>
                                <a href="services.php?action=edit&id=<?= $s['id'] ?>" class="btn btn-sm btn-outline-primary"><i class="fas fa-edit"></i></a>
                                <a href="services.php?action=delete&id=<?= $s['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="title" class="form-control" value="<?= htmlspecialchars($service['title'] ?? '') ?>" required>
                    </div>
                    <div class="col-md-6 mb-3">
                        <label class="form-label">图标 (Bootstrap Icon 类名，如 hdd-network)</label>
                        <input type="text" name="icon" class="form-control" value="<?= htmlspecialchars($service['icon'] ?? '') ?>" required>
                    </div>
                    <div class="col-md-12 mb-3">
                        <label class="form-label">描述</label>
                        <textarea name="description" class="form-control" rows="3"><?= htmlspecialchars($service['description'] ?? '') ?></textarea>
                    </div>
                    <div class="col-md-4 mb-3">
                        <label class="form-label">排序</label>
                        <input type="number" name="sort_order" class="form-control" value="<?= $service['sort_order'] ?? 0 ?>">
                    </div>
                </div>
                <div class="mt-4">
                    <button type="submit" class="btn btn-primary">保存</button>
                    <a href="services.php" class="btn btn-light ms-2">取消</a>
                </div>
            </form>
        <?php endif; ?>
    </div>
</div>

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