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

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

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

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

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

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