<?php
ob_start();
/**
 * 2026企业CMS - 后台友情链接管理
 */
require_once __DIR__ . '/header.php';
check_permission('editor');
$pageTitle = '友情链接管理';
$action = $_GET['action'] ?? 'list';
$id = intval($_GET['id'] ?? 0);

if ($action === 'delete' && $id) {
    db_exec("DELETE FROM `links` WHERE `id`=?", [$id]);
    add_log('delete', 'links', '删除友情链接');
    header('Location: /admin/links.php?msg=' . urlencode('删除成功！'));
    exit;
}

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

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

$link = [];
if ($action === 'add' || ($action === 'edit' && $id)) {
    if ($id) $link = db_query_one("SELECT * FROM `links` WHERE `id`=?", [$id]);
?>
<div class="page-header">
    <h2><?php echo htmlspecialchars($action, ENT_QUOTES, "UTF-8")=='add'?'添加友情链接':'编辑友情链接'; ?></h2>
    <a href="/admin/links.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="name" value="<?php echo xss_clean($link['name'] ?? ''); ?>" required>
            </div>
            <div class="form-group">
                <label>排序</label>
                <input type="number" name="sort_order" value="<?php echo $link['sort_order'] ?? 0; ?>">
            </div>
        </div>
        <div class="form-group">
            <label>链接地址 <span style="color:red;">*</span></label>
            <input type="url" name="url" value="<?php echo xss_clean($link['url'] ?? ''); ?>" required placeholder="https://www.example.com">
        </div>
        <div class="form-group">
            <label>描述</label>
            <input type="text" name="description" value="<?php echo xss_clean($link['description'] ?? ''); ?>" placeholder="网站简介">
        </div>
        <div style="margin-top:30px;">
            <button type="submit" name="save" class="btn btn-primary" style="margin-right:10px;">保存</button>
            <a href="/admin/links.php" class="btn">取消</a>
        </div>
    </form>
</div>
<?php
require_once __DIR__ . '/footer.php';
exit;
}

$links = db_query("SELECT * FROM `links` WHERE `status`=1 ORDER BY `sort_order` ASC, `id` ASC");
?>
<div class="page-header">
    <h2>友情链接管理</h2>
    <a href="/admin/links.php?action=add" class="btn btn-primary"><i class="fas fa-plus"></i> 添加链接</a>
</div>
<p style="color:#999;margin-bottom:15px;">友情链接显示在网站底部，用于SEO交换和品牌曝光</p>

<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 ($links as $l): ?>
            <tr>
                <td><?php echo $l['id']; ?></td>
                <td><?php echo xss_clean($l['name']); ?></td>
                <td><a href="<?php echo xss_clean($l['url']); ?>" target="_blank" style="font-size:13px;"><?php echo xss_clean(str_cut($l['url'],40)); ?></a></td>
                <td><?php echo xss_clean(str_cut($l['description'],20)); ?></td>
                <td><?php echo $l['sort_order']; ?></td>
                <td class="actions">
                    <a href="/admin/links.php?action=edit&id=<?php echo $l['id']; ?>" class="btn btn-sm btn-primary">编辑</a>
                    <a href="javascript:void(0)" onclick="if(confirmDelete(<?php echo $l['id']; ?>,'<?php echo xss_clean($l['name']); ?>')){location.href='/admin/links.php?action=delete&id=<?php echo $l['id']; ?>'}" class="btn btn-sm btn-danger">删除</a>
                </td>
            </tr>
            <?php endforeach; ?>
        </tbody>
    </table>
</div>
<?php require_once __DIR__ . '/footer.php'; ?>
