<?php
ob_start();
/**
 * 2026企业CMS - 后台幻灯片管理
 */
require_once __DIR__ . '/header.php';
$pageTitle = '幻灯片管理';
$action = $_GET['action'] ?? 'list';
$id = intval($_GET['id'] ?? 0);

if ($action === 'delete' && $id) {
    check_permission('editor');
    db_exec("DELETE FROM `slides` WHERE `id`=?", [$id]);
    add_log('delete', 'slides', '删除幻灯片');
    header('Location: /admin/slides.php?msg=' . urlencode('删除成功！'));
    exit;
}

if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['save'])) {
	// CSRF 保护
	if (!csrf_validate($_POST["csrf_token"] ?? "")) {
	    die("CSRF 验证失败");
	}

    check_permission('editor');
    $title = trim($_POST['title'] ?? '');
    $description = trim($_POST['description'] ?? '');
    $url = trim($_POST['url'] ?? '');
    $image = trim($_POST['image'] ?? '');
    $sortOrder = intval($_POST['sort_order'] ?? 0);
    
    if ($action === 'add') {
        db_exec("INSERT INTO `slides` (`title`,`description`,`image`,`url`,`sort_order`) VALUES (?,?,?,?,?)",
            [$title, $description, $image, $url, $sortOrder]);
        add_log('add', 'slides', '添加幻灯片: ' . $title);
    } elseif ($action === 'edit' && $id) {
        db_exec("UPDATE `slides` SET `title`=?,`description`=?,`image`=?,`url`=?,`sort_order`=? WHERE `id`=?",
            [$title, $description, $image, $url, $sortOrder, $id]);
        add_log('edit', 'slides', '编辑幻灯片: ' . $title);
    }
    header('Location: /admin/slides.php?msg=' . urlencode('保存成功！'));
    exit;
}

$slide = [];
if ($action === 'add' || ($action === 'edit' && $id)) {
    if ($id) $slide = db_query_one("SELECT * FROM `slides` WHERE `id`=?", [$id]);
?>
<div class="page-header">
    <h2><?php echo htmlspecialchars($action, ENT_QUOTES, "UTF-8")=='add'?'添加幻灯片':'编辑幻灯片'; ?></h2>
    <a href="/admin/slides.php" class="btn">← 返回列表</a>
</div>
<div class="form-card">
    <form method="post" action="">
        <?php echo csrf_input(); ?>
        <div class="form-group">
            <label>标题</label>
            <input type="text" name="title" value="<?php echo xss_clean($slide['title'] ?? ''); ?>">
        </div>
        <div class="form-group">
            <label>描述文字</label>
            <input type="text" name="description" value="<?php echo xss_clean($slide['description'] ?? ''); ?>" placeholder="幻灯片上的描述文字">
        </div>
        <div class="form-row">
            <div class="form-group">
                <label>链接地址</label>
                <input type="url" name="url" value="<?php echo xss_clean($slide['url'] ?? ''); ?>" placeholder="点击幻灯片跳转的地址">
            </div>
            <div class="form-group">
                <label>排序</label>
                <input type="number" name="sort_order" value="<?php echo $slide['sort_order'] ?? 0; ?>">
            </div>
        </div>
        <div class="form-group">
            <label>图片地址（URL）</label>
            <input type="text" name="image" id="image-input" value="<?php echo xss_clean($slide['image'] ?? ''); ?>" placeholder="输入图片URL或上传">
            <div class="hint">建议尺寸：1920x500px</div>
            <?php if (!empty($slide['image'])): ?>
            <div style="margin-top:10px;">
                <img id="image-preview" src="<?php echo xss_clean($slide['image']); ?>" style="max-width:300px;max-height:100px;border:1px solid #ddd;border-radius:4px;">
            </div>
            <?php endif; ?>
        </div>
        <div style="margin-top:30px;">
            <button type="submit" name="save" class="btn btn-primary" style="margin-right:10px;">保存</button>
            <a href="/admin/slides.php" class="btn">取消</a>
        </div>
    </form>
</div>
<?php
require_once __DIR__ . '/footer.php';
exit;
}

$slides = db_query("SELECT * FROM `slides` ORDER BY `sort_order` ASC, `id` ASC");
?>
<div class="page-header">
    <h2>幻灯片管理</h2>
    <a href="/admin/slides.php?action=add" class="btn btn-primary"><i class="fas fa-plus"></i> 添加幻灯片</a>
</div>

<div class="table-wrapper">
    <table>
        <thead><tr><th>ID</th><th>图片预览</th><th>标题</th><th>链接</th><th>排序</th><th>操作</th></tr></thead>
        <tbody>
            <?php foreach ($slides as $s): ?>
            <tr>
                <td><?php echo $s['id']; ?></td>
                <td>
                    <?php if ($s['image']): ?>
                    <img src="<?php echo xss_clean($s['image']); ?>" style="width:120px;height:40px;object-fit:cover;border-radius:4px;">
                    <?php endif; ?>
                </td>
                <td><?php echo xss_clean($s['title']); ?></td>
                <td><a href="<?php echo xss_clean($s['url']); ?>" target="_blank" style="font-size:12px;"><?php echo xss_clean(str_cut($s['url'],30)); ?></a></td>
                <td><?php echo $s['sort_order']; ?></td>
                <td class="actions">
                    <a href="/admin/slides.php?action=edit&id=<?php echo $s['id']; ?>" class="btn btn-sm btn-primary">编辑</a>
                    <a href="javascript:void(0)" onclick="if(confirmDelete(<?php echo $s['id']; ?>,'<?php echo xss_clean($s['title']); ?>')){location.href='/admin/slides.php?action=delete&id=<?php echo $s['id']; ?>'}" class="btn btn-sm btn-danger">删除</a>
                </td>
            </tr>
            <?php endforeach; ?>
        </tbody>
    </table>
</div>
<?php require_once __DIR__ . '/footer.php'; ?>
