<?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('admin');
    $p = db_query_one("SELECT `title` FROM `pages` WHERE `id`=?", [$id]);
    db_exec("DELETE FROM `pages` WHERE `id`=?", [$id]);
    add_log('delete', 'pages', '删除页面: ' . ($p['title'] ?? ''));
    header('Location: /admin/pages.php?msg=' . urlencode('删除成功！'));
    exit;
}

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

    check_permission('admin');
    $title = trim($_POST['title'] ?? '');
    $slug = trim($_POST['slug'] ?? '');
    $content = $_POST['content'] ?? '';
    $seoTitle = trim($_POST['seo_title'] ?? '');
    $seoKeys = trim($_POST['seo_keywords'] ?? '');
    $seoDesc = trim($_POST['seo_description'] ?? '');
    
    if (!$slug) $slug = generate_slug($title);
    
    if ($action === 'add') {
        db_exec("INSERT INTO `pages` (`title`,`slug`,`content`,`seo_title`,`seo_keywords`,`seo_description`) VALUES (?,?,?,?,?,?)",
            [$title, $slug, $content, $seoTitle, $seoKeys, $seoDesc]);
        add_log('add', 'pages', '添加页面: ' . $title);
    } elseif ($action === 'edit' && $id) {
        db_exec("UPDATE `pages` SET `title`=?,`slug`=?,`content`=?,`seo_title`=?,`seo_keywords`=?,`seo_description`=? WHERE `id`=?",
            [$title, $slug, $content, $seoTitle, $seoKeys, $seoDesc, $id]);
        add_log('edit', 'pages', '编辑页面: ' . $title);
    }
    header('Location: /admin/pages.php?msg=' . urlencode('保存成功！'));
    exit;
}

$page = [];
if ($action === 'add' || ($action === 'edit' && $id)) {
    if ($id) $page = db_query_one("SELECT * FROM `pages` WHERE `id`=?", [$id]);
?>
<div class="page-header">
    <h2><?php echo htmlspecialchars($action, ENT_QUOTES, "UTF-8")==='add'?'添加页面':'编辑页面'; ?></h2>
    <a href="/admin/pages.php" class="btn">← 返回列表</a>
</div>
<div class="form-card">
    <form method="post" action="">
        <?php echo csrf_input(); ?>
        <div class="form-row">
            <div class="form-group" style="flex:2;">
                <label>页面标题 <span style="color:red;">*</span></label>
                <input type="text" name="title" value="<?php echo xss_clean($page['title'] ?? ''); ?>" required>
            </div>
            <div class="form-group">
                <label>URL别名</label>
                <input type="text" name="slug" value="<?php echo xss_clean($page['slug'] ?? ''); ?>" placeholder="如: about">
                <div class="hint">访问地址: /<?php echo xss_clean($page['slug'] ?? 'slug'); ?>.html</div>
            </div>
        </div>
        <div class="form-group">
            <label>页面内容</label>
            <textarea name="content" id="content-editor"><?php echo $page['content'] ?? ''; ?></textarea>
        </div>
        <hr>
        <h3 style="margin-bottom:15px;font-size:16px;color:#333;">SEO设置</h3>
        <div class="form-row">
            <div class="form-group">
                <label>SEO标题</label>
                <input type="text" name="seo_title" value="<?php echo xss_clean($page['seo_title'] ?? ''); ?>">
            </div>
            <div class="form-group">
                <label>SEO关键词</label>
                <input type="text" name="seo_keywords" value="<?php echo xss_clean($page['seo_keywords'] ?? ''); ?>">
            </div>
        </div>
        <div class="form-group">
            <label>SEO描述</label>
            <textarea name="seo_description" rows="2"><?php echo xss_clean($page['seo_description'] ?? ''); ?></textarea>
        </div>
        <div style="margin-top:30px;">
            <button type="submit" name="save" class="btn btn-primary" style="margin-right:10px;">保存</button>
            <a href="/admin/pages.php" class="btn">取消</a>
        </div>
    </form>
</div>
<script src="https://cdn.ckeditor.com/4.25.1-lts/full/ckeditor.js"></script>
<script>if(typeof CKEDITOR!=='undefined'){CKEDITOR.replace('content-editor');}</script>
<?php
require_once __DIR__ . '/footer.php';
exit;
}

$pages = db_query("SELECT * FROM `pages` ORDER BY `sort_order` ASC, `id` ASC");
?>
<div class="page-header">
    <h2>单页管理</h2>
    <a href="/admin/pages.php?action=add" class="btn btn-primary"><i class="fas fa-plus"></i> 添加页面</a>
</div>
<p style="color:#999;margin-bottom:15px;">单页用于关于我们、联系我们、服务内容等不需要列表的页面</p>

<div class="table-wrapper">
    <table>
        <thead><tr><th>ID</th><th>页面标题</th><th>URL地址</th><th>操作</th></tr></thead>
        <tbody>
            <?php foreach ($pages as $p): ?>
            <tr>
                <td><?php echo $p['id']; ?></td>
                <td><?php echo xss_clean($p['title']); ?></td>
                <td><a href="/<?php echo $p['slug']; ?>.html" target="_blank">/<?php echo $p['slug']; ?>.html</a></td>
                <td class="actions">
                    <a href="/admin/pages.php?action=edit&id=<?php echo $p['id']; ?>" class="btn btn-sm btn-primary">编辑</a>
                    <?php if ($p['slug'] !== 'about' && $p['slug'] !== 'contact'): ?>
                    <a href="javascript:void(0)" onclick="if(confirmDelete(<?php echo $p['id']; ?>,'<?php echo xss_clean($p['title']); ?>')){location.href='/admin/pages.php?action=delete&id=<?php echo $p['id']; ?>'}" class="btn btn-sm btn-danger">删除</a>
                    <?php endif; ?>
                </td>
            </tr>
            <?php endforeach; ?>
        </tbody>
    </table>
</div>
<?php require_once __DIR__ . '/footer.php'; ?>
