<?php
/**
 * Name: Nxz官网系统
 * URI: https://www.nxzlm.com/
 * Description: 逆行者官网系统源码，简单好用带后台
 * Version: 1.0.0
 * Author: 逆行者联盟
 */
require_once '../includes/common.php';
check_admin_login();

// 处理API请求
if (isset($_GET['api'])) {
    if ($_GET['api'] == 'list') {
        $stmt = $pdo->query("SELECT * FROM nxz_links ORDER BY sort ASC, id DESC");
        $data = $stmt->fetchAll();
        echo json_encode(['code' => 0, 'msg' => '', 'count' => count($data), 'data' => $data]);
        exit;
    }
    if ($_GET['api'] == 'save') {
        $id = $_POST['id'] ?? 0;
        $name = $_POST['name'] ?? '';
        $url = $_POST['url'] ?? '';
        $sort = $_POST['sort'] ?? 0;
        
        if (empty($name) || empty($url)) json_error('名称和链接不能为空');
        
        if ($id) {
            $stmt = $pdo->prepare("UPDATE nxz_links SET name=?, url=?, sort=? WHERE id=?");
            $stmt->execute([$name, $url, $sort, $id]);
        } else {
            $stmt = $pdo->prepare("INSERT INTO nxz_links (name, url, sort) VALUES (?, ?, ?)");
            $stmt->execute([$name, $url, $sort]);
        }
        json_success('保存成功');
    }
    if ($_GET['api'] == 'del') {
        $id = $_POST['id'] ?? 0;
        $pdo->prepare("DELETE FROM nxz_links WHERE id=?")->execute([$id]);
        json_success('删除成功');
    }
}
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>友情链接管理</title>
    <link rel="stylesheet" href="../assets/layui/css/layui.css">
    <style>body { background-color: #f2f2f2; padding: 15px; }</style>
</head>
<body>

<div class="layui-card">
    <div class="layui-card-header">友情链接管理</div>
    <div class="layui-card-body">
        <div style="padding-bottom: 10px;">
            <button class="layui-btn layui-btn-normal" id="btnAdd">添加友链</button>
        </div>
        <table id="dataTable" lay-filter="dataTable"></table>
    </div>
</div>

<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<!-- 弹窗表单 -->
<div id="formBox" style="display:none; padding: 20px;">
    <form class="layui-form" lay-filter="dataForm">
        <input type="hidden" name="id">
        <div class="layui-form-item">
            <label class="layui-form-label">网站名称</label>
            <div class="layui-input-block">
                <input type="text" name="name" lay-verify="required" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">网站链接</label>
            <div class="layui-input-block">
                <input type="text" name="url" lay-verify="required" placeholder="http:// 或 https://" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">排序</label>
            <div class="layui-input-block">
                <input type="number" name="sort" value="0" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveData">保存</button>
            </div>
        </div>
    </form>
</div>

<script src="../assets/layui/layui.js"></script>
<script>
layui.use(['table', 'form', 'layer', 'jquery'], function(){
    var table = layui.table;
    var form = layui.form;
    var layer = layui.layer;
    var $ = layui.jquery;
    
    table.render({
        elem: '#dataTable',
        url: '?api=list',
        cols: [[
            {field:'id', title:'ID', width:80},
            {field:'name', title:'网站名称'},
            {field:'url', title:'网站链接'},
            {field:'sort', title:'排序', width:80},
            {title:'操作', toolbar: '#barDemo', width:150}
        ]]
    });
    
    var openForm = function(data){
        form.val('dataForm', data || {id:'', name:'', url:'', sort:0});
        layer.open({
            type: 1,
            title: data ? '编辑友链' : '添加友链',
            area: ['500px', '300px'],
            content: $('#formBox')
        });
    };
    
    $('#btnAdd').on('click', function(){
        openForm();
    });
    
    table.on('tool(dataTable)', function(obj){
        if(obj.event === 'del'){
            layer.confirm('确定删除该友情链接吗？', function(index){
                $.post('?api=del', {id: obj.data.id}, function(res){
                    if(res.code === 0){
                        layer.msg('删除成功');
                        table.reload('dataTable');
                    } else {
                        layer.msg(res.msg);
                    }
                }, 'json');
                layer.close(index);
            });
        } else if(obj.event === 'edit'){
            openForm(obj.data);
        }
    });
    
    form.on('submit(saveData)', function(data){
        $.post('?api=save', data.field, function(res){
            if(res.code === 0){
                layer.closeAll('page');
                layer.msg('保存成功');
                table.reload('dataTable');
            } else {
                layer.msg(res.msg);
            }
        }, 'json');
        return false;
    });
});
</script>
</body>
</html>