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

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

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

// Handle Save (Add/Edit)
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $data = [
        'title' => $_POST['title'],
        'results_data' => $_POST['results_data'],
        'description' => $_POST['description'],
        'img' => $_POST['img'],
        'video_url' => $_POST['video_url'],
        'sort_order' => (int)$_POST['sort_order']
    ];

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

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

// Fetch list
$items = $pdo->query("SELECT * FROM transformations ORDER BY sort_order ASC, id DESC")->fetchAll();

// Fetch for edit
$item = null;
if ($action === 'edit' && $id) {
    $stmt = $pdo->prepare("SELECT * FROM transformations WHERE id = ?");
    $stmt->execute([$id]);
    $item = $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="transformations.php?action=add" class="btn btn-sm btn-primary">新增</a>
        <?php else: ?>
            <a href="transformations.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 ($items as $i): ?>
                        <tr>
                            <td><img src="../<?= htmlspecialchars($i['img'] ?: 'assets/images/placeholder.jpg') ?>" width="60" class="rounded"></td>
                            <td><strong><?= htmlspecialchars($i['title']) ?></strong></td>
                            <td><?= htmlspecialchars($i['results_data']) ?></td>
                            <td><?= $i['sort_order'] ?></td>
                            <td>
                                <a href="transformations.php?action=edit&id=<?= $i['id'] ?>" class="btn btn-sm btn-outline-primary"><i class="fas fa-edit"></i></a>
                                <a href="transformations.php?action=delete&id=<?= $i['id'] ?>" class="btn btn-sm btn-outline-danger" onclick="return confirm('确定删除吗？')"><i class="fas fa-trash"></i></a>
                            </td>
                        </tr>
                        <?php endforeach; ?>
                        <?php if (empty($items)): ?>
                        <tr>
                            <td colspan="5" class="text-center py-4 text-muted">暂无数据</td>
                        </tr>
                        <?php endif; ?>
                    </tbody>
                </table>
            </div>
        <?php else: ?>
            <form method="POST">
                <div class="row">
                    <div class="col-md-8 mb-3">
                        <label class="form-label">标题</label>
                        <input type="text" name="title" class="form-control" value="<?= htmlspecialchars($item['title'] ?? '') ?>" required>
                    </div>
                    <div class="col-md-4 mb-3">
                        <label class="form-label">排序</label>
                        <input type="number" name="sort_order" class="form-control" value="<?= $item['sort_order'] ?? 0 ?>">
                    </div>
                    <div class="col-md-6 mb-3">
                        <label class="form-label">核心数据/成果 (例如: 效率提升 200%)</label>
                        <input type="text" name="results_data" class="form-control" value="<?= htmlspecialchars($item['results_data'] ?? '') ?>">
                    </div>
                    <div class="col-md-6 mb-3">
                        <label class="form-label">图片 URL</label>
                        <input type="text" name="img" class="form-control" value="<?= htmlspecialchars($item['img'] ?? '') ?>">
                    </div>
                    <div class="col-md-12 mb-3">
                        <label class="form-label">视频 URL (可选)</label>
                        <input type="text" name="video_url" class="form-control" value="<?= htmlspecialchars($item['video_url'] ?? '') ?>">
                    </div>
                    <div class="col-md-12 mb-3">
                        <label class="form-label">详细描述</label>
                        <textarea name="description" class="form-control" rows="5"><?= htmlspecialchars($item['description'] ?? '') ?></textarea>
                    </div>
                </div>
                <div class="mt-4">
                    <button type="submit" class="btn btn-primary">保存修改</button>
                    <a href="transformations.php" class="btn btn-light ms-2">取消</a>
                </div>
            </form>
        <?php endif; ?>
    </div>
</div>

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