<?php
/**
 * 系统设置 - 新版界面
**/
define('IN_ADMIN', true);
include("../includes/common.php");
$csrf_token = md5(mt_rand(0,999).time());
$_SESSION['csrf_token'] = $csrf_token;
$title='系统设置';
include './head.php';
if($islogin==1){}else exit("<script language='javascript'>window.location.href='./login.php';</script>");

$mod=isset($_GET['mod'])?$_GET['mod']:'site';

// 根据模块设置页面标题
$page_titles = [
    'site' => '网站信息设置',
    'user' => '用户登录设置',
    'traffic' => '流量与配额设置',
    'file' => '文件上传设置',
    'green' => '图片检测设置',
    'api' => '上传API设置',
    'iptype' => 'IP地址设置',
    'account' => '账号管理设置',
    'announcement' => '用户中心公告管理'
];
$current_title = isset($page_titles[$mod]) ? $page_titles[$mod] : '系统设置';
?>
<div class="container">
    <!-- 页面标题 -->
    <div class="page-header-section">
        <div class="page-title">
            <div class="page-title-icon"><i class="fa fa-cog"></i></div>
            <h1><?php echo $current_title; ?></h1>
        </div>
    </div>

    <!-- 设置内容区 -->
    <div class="settings-content">
        <input type="hidden" name="csrf_token" value="<?php echo $csrf_token; ?>">
        
        <?php if($mod=='site'): ?>
        <!-- 网站信息设置 -->
        <div class="card">
            <div class="card-header">
                <h3><i class="fa fa-globe"></i> 网站基本信息</h3>
            </div>
            <div class="card-body">
                <form onsubmit="return saveSetting(this)" method="post" class="settings-form">
                    <div class="form-row">
                        <div class="form-group">
                            <label class="form-label">网站标题</label>
                            <input type="text" name="title" value="<?php echo htmlspecialchars($conf['title']); ?>" class="form-control" required placeholder="请输入网站标题"/>
                            <span class="help-text">显示在浏览器标签页和网站头部的标题</span>
                        </div>
                    </div>
                    
                    <div class="form-row two-col">
                        <div class="form-group">
                            <label class="form-label">关键字</label>
                            <input type="text" name="keywords" value="<?php echo htmlspecialchars($conf['keywords']); ?>" class="form-control" placeholder="多个关键词用逗号分隔"/>
                            <span class="help-text">用于SEO优化，提高搜索引擎收录</span>
                        </div>
                        <div class="form-group">
                            <label class="form-label">网站描述</label>
                            <input type="text" name="description" value="<?php echo htmlspecialchars($conf['description']); ?>" class="form-control" placeholder="简短的网站描述"/>
                            <span class="help-text">显示在搜索引擎结果中的网站简介</span>
                        </div>
                    </div>

                    <div class="form-row">
                        <div class="form-group">
                            <label class="form-label">禁止访问IP</label>
                            <textarea class="form-control" name="blackip" rows="3" placeholder="多个IP用|隔开"><?php echo htmlspecialchars($conf['blackip'])?></textarea>
                            <span class="help-text">被列入黑名单的IP将无法访问网站，多个IP用 | 分隔</span>
                        </div>
                    </div>

                    <div class="form-row">
                        <div class="form-group">
                            <label class="form-label">首页公告</label>
                            <textarea class="form-control" name="gonggao" rows="3" placeholder="支持HTML代码，不填写则不显示"><?php echo htmlspecialchars($conf['gonggao'])?></textarea>
                            <span class="help-text">显示在网站首页顶部的公告内容，支持HTML格式</span>
                        </div>
                    </div>

                    <div class="form-row">
                        <div class="form-group">
                            <label class="form-label">文件查看页公告</label>
                            <textarea class="form-control" name="gg_file" rows="3" placeholder="支持HTML代码，不填写则不显示"><?php echo htmlspecialchars($conf['gg_file'])?></textarea>
                            <span class="help-text">显示在文件查看页面的公告内容</span>
                        </div>
                    </div>

                    <div class="form-row">
                        <div class="form-group">
                            <label class="form-label">统计代码</label>
                            <textarea class="form-control" name="tongji" rows="3" placeholder="百度统计、Google Analytics等代码"><?php echo htmlspecialchars($conf['tongji'])?></textarea>
                            <span class="help-text">第三方网站统计工具的JS代码，将自动插入到页面底部</span>
                        </div>
                    </div>

                    <div class="form-row two-col">
                        <div class="form-group">
                            <label class="form-label">文件搜索功能</label>
                            <select class="form-control" name="filesearch" default="<?php echo $conf['filesearch']?>">
                                <option value="0">关闭</option>
                                <option value="1">开启</option>
                            </select>
                            <span class="help-text">是否允许用户在网站上搜索文件</span>
                        </div>
                    </div>

                    <div class="form-actions">
                        <button type="submit" class="btn btn-primary">
                            <i class="fa fa-save"></i> 保存设置
                        </button>
                    </div>
                </form>
            </div>
        </div>

        <?php elseif($mod=='user'): ?>
        <!-- 用户登录设置 -->
        <div class="card">
            <div class="card-header">
                <h3><i class="fa fa-user"></i> 用户登录配置</h3>
            </div>
            <div class="card-body">
                <form onsubmit="return saveSetting(this)" method="post" class="settings-form">
                    <div class="form-row two-col">
                        <div class="form-group">
                            <label class="form-label">用户登录开关</label>
                            <select class="form-control" name="userlogin" default="<?php echo $conf['userlogin']?>">
                                <option value="0">关闭</option>
                                <option value="1">开启</option>
                            </select>
                            <span class="help-text">开启后用户可以使用账号密码登录</span>
                        </div>
                        <div class="form-group">
                            <label class="form-label">快捷登录开关</label>
                            <select class="form-control" name="quicklogin" id="quicklogin_select" default="<?php echo $conf['quicklogin']?>">
                                <option value="0">关闭</option>
                                <option value="1">开启</option>
                            </select>
                            <span class="help-text">开启后用户可以使用第三方快捷登录</span>
                        </div>
                    </div>

                    <div id="quicklogin_config" style="<?php echo $conf['quicklogin']==1?'':'display:none;'; ?>">
                        <div class="form-row two-col">
                            <div class="form-group">
                                <label class="form-label">聚合登录接口地址</label>
                                <input type="text" name="login_apiurl" value="<?php echo htmlspecialchars($conf['login_apiurl']); ?>" class="form-control" placeholder="https://example.com/"/>
                                <span class="help-text">接口地址要以http://或https://开头，以/结尾</span>
                            </div>
                        </div>

                        <div class="form-row two-col">
                            <div class="form-group">
                                <label class="form-label">应用APPID</label>
                                <input type="text" name="login_appid" value="<?php echo htmlspecialchars($conf['login_appid']); ?>" class="form-control" placeholder="从登录接口获取"/>
                            </div>
                            <div class="form-group">
                                <label class="form-label">应用APPKEY</label>
                                <input type="text" name="login_appkey" value="<?php echo htmlspecialchars($conf['login_appkey']); ?>" class="form-control" placeholder="从登录接口获取"/>
                            </div>
                        </div>

                        <div class="form-row">
                            <div class="form-group">
                                <label class="form-label">开启的登录方式</label>
                                <div class="checkbox-group">
                                    <input type="hidden" name="login_qq" value="0"/>
                                    <input type="hidden" name="login_wx" value="0"/>
                                    <label class="checkbox-item">
                                        <input type="checkbox" name="login_qq" value="1" <?php echo $conf['login_qq']?'checked':null;?>>
                                        <span class="checkmark"><i class="fa fa-qq"></i> QQ登录</span>
                                    </label>
                                    <label class="checkbox-item">
                                        <input type="checkbox" name="login_wx" value="1" <?php echo $conf['login_wx']?'checked':null;?>>
                                        <span class="checkmark"><i class="fa fa-weixin"></i> 微信登录</span>
                                    </label>
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="form-actions">
                        <button type="submit" class="btn btn-primary">
                            <i class="fa fa-save"></i> 保存设置
                        </button>
                    </div>
                </form>
            </div>
            <div class="card-footer info-footer">
                <i class="fa fa-info-circle"></i>
                <span>请勿随意更换快捷登录接口，以免导致用户无法登录。<br>推荐登录接口，注册就送无限QQ额度，<a href="https://log.knight.vin" target="_blank" rel="noreferrer">点击进入</a></span>
            </div>
        </div>

        <?php elseif($mod=='traffic'): ?>
        <!-- 流量与配额设置 -->
        <div class="card">
            <div class="card-header">
                <h3><i class="fa fa-hdd-o"></i> 用户存储配额设置</h3>
            </div>
            <div class="card-body">
                <form onsubmit="return saveSetting(this)" method="post" class="settings-form">
                    <div class="form-row two-col">
                        <div class="form-group">
                            <label class="form-label">用户默认存储配额</label>
                            <div class="input-group">
                                <input type="text" name="user_storage_quota" value="<?php echo htmlspecialchars($conf['user_storage_quota']); ?>" class="form-control" placeholder="0为不限制"/>
                                <span class="input-addon">MB</span>
                            </div>
                            <span class="help-text">每个用户默认可使用的存储空间大小</span>
                        </div>
                        <div class="form-group">
                            <label class="form-label">用户默认流量配额</label>
                            <div class="input-group">
                                <input type="text" name="user_traffic_quota" value="<?php echo htmlspecialchars($conf['user_traffic_quota']); ?>" class="form-control" placeholder="0为不限制"/>
                                <span class="input-addon">MB</span>
                            </div>
                            <span class="help-text">流量是指用户上传的文件被浏览、下载所消耗的流量</span>
                        </div>
                    </div>

                    <div class="form-row two-col">
                        <div class="form-group">
                            <label class="form-label">默认流量配额自动重置周期</label>
                            <div class="input-group">
                                <input type="text" name="traffic_quota_reset_interval" value="<?php echo htmlspecialchars($conf['traffic_quota_reset_interval']); ?>" class="form-control" placeholder="默认30天"/>
                                <span class="input-addon">天</span>
                            </div>
                            <span class="help-text">0为不自动重置，设置为N天则每N天自动重置一次用户流量配额</span>
                        </div>
                    </div>
            </div>
        </div>

        <div class="card">
            <div class="card-header">
                <h3><i class="fa fa-user-secret"></i> 游客配额设置</h3>
            </div>
            <div class="card-body">
                <div class="form-row two-col">
                    <div class="form-group">
                        <label class="form-label">游客默认存储配额</label>
                        <div class="input-group">
                            <input type="text" name="guest_storage_quota" value="<?php echo htmlspecialchars($conf['guest_storage_quota'] ?? ''); ?>" class="form-control" placeholder="0为不限制"/>
                            <span class="input-addon">MB</span>
                        </div>
                        <span class="help-text">游客默认可使用的存储空间大小（0表示使用用户默认配额）</span>
                    </div>
                    <div class="form-group">
                        <label class="form-label">游客默认流量配额</label>
                        <div class="input-group">
                            <input type="text" name="guest_traffic_quota" value="<?php echo htmlspecialchars($conf['guest_traffic_quota'] ?? ''); ?>" class="form-control" placeholder="0为不限制"/>
                            <span class="input-addon">MB</span>
                        </div>
                        <span class="help-text">游客每日可使用的流量配额（0表示使用用户默认配额）</span>
                    </div>
                </div>

                <div class="form-row two-col">
                    <div class="form-group">
                        <label class="form-label">游客超限自动封禁</label>
                        <select class="form-control" name="guest_auto_ban_enabled" default="<?php echo $conf['guest_auto_ban_enabled'] ?? '0'?>">
                            <option value="0">关闭（仅提示）</option>
                            <option value="1">开启（封禁IP）</option>
                        </select>
                        <span class="help-text">游客流量超限时是否自动封禁IP</span>
                    </div>
                    <div class="form-group">
                        <label class="form-label">游客封禁时长</label>
                        <div class="input-group">
                            <input type="text" name="guest_ban_duration" value="<?php echo htmlspecialchars($conf['guest_ban_duration'] ?? '86400'); ?>" class="form-control" placeholder="默认86400"/>
                            <span class="input-addon">秒</span>
                        </div>
                        <span class="help-text">游客被封禁的时间长度（86400秒=24小时）</span>
                    </div>
                </div>
            </div>
        </div>

        <div class="card">
            <div class="card-header">
                <h3><i class="fa fa-bar-chart"></i> 流量日志与监控</h3>
            </div>
            <div class="card-body">
                <div class="form-row two-col">
                        <div class="form-group">
                            <label class="form-label">启用流量日志记录</label>
                            <select class="form-control" name="traffic_log_enabled" default="<?php echo $conf['traffic_log_enabled']?>">
                                <option value="0">关闭</option>
                                <option value="1">开启</option>
                            </select>
                            <span class="help-text">开启后将记录详细的流量使用日志</span>
                        </div>
                        <div class="form-group">
                            <label class="form-label">流量日志保留天数</label>
                            <div class="input-group">
                                <input type="text" name="traffic_log_retention_days" value="<?php echo htmlspecialchars($conf['traffic_log_retention_days']); ?>" class="form-control" placeholder="默认90天"/>
                                <span class="input-addon">天</span>
                            </div>
                            <span class="help-text">超过此天数的流量日志将被自动清理</span>
                        </div>
                    </div>

                    <div class="form-row two-col">
                        <div class="form-group">
                            <label class="form-label">启用流量使用预警</label>
                            <select class="form-control" name="traffic_alert_enabled" default="<?php echo $conf['traffic_alert_enabled']?>">
                                <option value="0">关闭</option>
                                <option value="1">开启</option>
                            </select>
                            <span class="help-text">当用户流量使用超过阈值时发送预警</span>
                        </div>
                        <div class="form-group">
                            <label class="form-label">流量使用预警阈值</label>
                            <div class="input-group">
                                <input type="text" name="traffic_alert_threshold" value="<?php echo htmlspecialchars($conf['traffic_alert_threshold']); ?>" class="form-control" placeholder="默认80"/>
                                <span class="input-addon">%</span>
                            </div>
                            <span class="help-text">当流量使用达到配额的此百分比时触发预警</span>
                        </div>
                    </div>

                    <div class="form-row two-col">
                        <div class="form-group">
                            <label class="form-label">启用异常流量检测</label>
                            <select class="form-control" name="traffic_anomaly_detection" default="<?php echo $conf['traffic_anomaly_detection']?>">
                                <option value="0">关闭</option>
                                <option value="1">开启</option>
                            </select>
                            <span class="help-text">自动检测异常的流量使用模式</span>
                        </div>
                        <div class="form-group">
                            <label class="form-label">异常流量检测时间窗口</label>
                            <div class="input-group">
                                <input type="text" name="traffic_anomaly_window" value="<?php echo htmlspecialchars($conf['traffic_anomaly_window'] ?? '60'); ?>" class="form-control" placeholder="默认60"/>
                                <span class="input-addon">分钟</span>
                            </div>
                            <span class="help-text">检测异常流量的时间窗口（默认60分钟）</span>
                        </div>
                    </div>

                    <div class="form-row two-col">
                        <div class="form-group">
                            <label class="form-label">异常访问次数阈值</label>
                            <div class="input-group">
                                <input type="text" name="traffic_anomaly_count" value="<?php echo htmlspecialchars($conf['traffic_anomaly_count'] ?? '50'); ?>" class="form-control" placeholder="默认50"/>
                                <span class="input-addon">次</span>
                            </div>
                            <span class="help-text">在时间窗口内访问超过此次数视为异常（0为不限制）</span>
                        </div>
                        <div class="form-group">
                            <label class="form-label">异常平均流量阈值</label>
                            <div class="input-group">
                                <input type="text" name="traffic_anomaly_avg_size" value="<?php echo htmlspecialchars($conf['traffic_anomaly_avg_size'] ?? '100'); ?>" class="form-control" placeholder="默认100"/>
                                <span class="input-addon">MB</span>
                            </div>
                            <span class="help-text">平均单次访问流量超过此值视为异常（0为不限制）</span>
                        </div>
                    </div>

                    <div class="form-row two-col">
                        <div class="form-group">
                            <label class="form-label">登录用户异常封禁</label>
                            <select class="form-control" name="user_anomaly_ban_enabled" default="<?php echo $conf['user_anomaly_ban_enabled'] ?? '0'?>">
                                <option value="0">关闭（仅记录日志）</option>
                                <option value="1">开启（封禁账号）</option>
                            </select>
                            <span class="help-text">登录用户恶意刷流量时是否封禁账号</span>
                        </div>
                        <div class="form-group">
                            <label class="form-label">用户异常封禁时长</label>
                            <div class="input-group">
                                <input type="text" name="user_anomaly_ban_duration" value="<?php echo htmlspecialchars($conf['user_anomaly_ban_duration'] ?? '86400'); ?>" class="form-control" placeholder="默认86400"/>
                                <span class="input-addon">秒</span>
                            </div>
                            <span class="help-text">用户被封禁的时间长度（86400秒=24小时）</span>
                        </div>
                    </div>

                    <div class="form-row">
                        <div class="form-group">
                            <label class="form-label">重置状态查看</label>
                            <div class="btn-group-inline">
                                <button type="button" class="btn btn-info" onclick="checkResetStatus()">
                                    <i class="fa fa-search"></i> 检查用户重置状态
                                </button>
                                <button type="button" class="btn btn-warning" onclick="fixResetIssues()">
                                    <i class="fa fa-wrench"></i> 修复重置问题
                                </button>
                            </div>
                            <div id="reset-status-info" class="status-info-box" style="display:none;"></div>
                        </div>
                    </div>

                    <div class="form-actions">
                        <button type="submit" class="btn btn-primary">
                            <i class="fa fa-save"></i> 保存设置
                        </button>
                    </div>
                </form>
            </div>
        </div>

        <?php elseif($mod=='file'): ?>
        <!-- 文件上传设置 -->
        <div class="card">
            <div class="card-header">
                <h3><i class="fa fa-file-o"></i> 文件类型设置</h3>
            </div>
            <div class="card-body">
                <form onsubmit="return saveSetting(this)" method="post" class="settings-form">
                    <div class="form-row">
                        <div class="form-group">
                            <label class="form-label">图片文件类型</label>
                            <input type="text" name="type_image" value="<?php echo htmlspecialchars($conf['type_image']); ?>" class="form-control" placeholder="jpg|jpeg|png|gif|bmp|webp"/>
                            <span class="help-text">在文件预览页面，以上文件类型将以图片的形式展示，多个类型用 | 分隔</span>
                        </div>
                    </div>

                    <div class="form-row">
                        <div class="form-group">
                            <label class="form-label">音频文件类型</label>
                            <input type="text" name="type_audio" value="<?php echo htmlspecialchars($conf['type_audio']); ?>" class="form-control" placeholder="mp3|wav|flac|aac|ogg"/>
                            <span class="help-text">在文件预览页面，以上文件类型将以音频的形式展示</span>
                        </div>
                    </div>

                    <div class="form-row">
                        <div class="form-group">
                            <label class="form-label">视频文件类型</label>
                            <input type="text" name="type_video" value="<?php echo htmlspecialchars($conf['type_video']); ?>" class="form-control" placeholder="mp4|avi|mkv|mov|wmv"/>
                            <span class="help-text">在文件预览页面，以上文件类型将以视频的形式展示</span>
                        </div>
                    </div>

                    <div class="form-row">
                        <div class="form-group">
                            <label class="form-label">禁止上传的文件类型</label>
                            <input type="text" name="type_block" value="<?php echo htmlspecialchars($conf['type_block']); ?>" class="form-control" placeholder="exe|bat|cmd|sh"/>
                            <span class="help-text">被列入的文件类型将无法上传，多个类型用 | 分隔</span>
                        </div>
                    </div>

                    <div class="form-row">
                        <div class="form-group">
                            <label class="form-label">文件名屏蔽关键词</label>
                            <input type="text" name="name_block" value="<?php echo htmlspecialchars($conf['name_block']); ?>" class="form-control" placeholder="多个关键词用|隔开"/>
                            <span class="help-text">包含这些关键词的文件名将无法上传</span>
                        </div>
                    </div>
            </div>
        </div>

        <div class="card">
            <div class="card-header">
                <h3><i class="fa fa-sliders"></i> 上传限制设置</h3>
            </div>
            <div class="card-body">
                <div class="form-row two-col">
                        <div class="form-group">
                            <label class="form-label">每IP每天限制上传数量</label>
                            <input type="text" name="upload_limit" value="<?php echo htmlspecialchars($conf['upload_limit']); ?>" class="form-control" placeholder="0或留空为不限制"/>
                            <span class="help-text">限制每个IP每天最多上传的文件数量</span>
                        </div>
                        <div class="form-group">
                            <label class="form-label">上传大小限制</label>
                            <div class="input-group">
                                <input type="text" name="upload_size" value="<?php echo htmlspecialchars($conf['upload_size']); ?>" class="form-control" placeholder="不填写则不限制"/>
                                <span class="input-addon">MB</span>
                            </div>
                            <span class="help-text">单个文件的最大上传大小</span>
                        </div>
                    </div>

                    <div class="form-row two-col">
                        <div class="form-group">
                            <label class="form-label">视频文件需要审核</label>
                            <select class="form-control" name="videoreview" default="<?php echo $conf['videoreview']?>">
                                <option value="0">关闭</option>
                                <option value="1">开启</option>
                            </select>
                            <span class="help-text">开启后上传的视频需要管理员审核后才能显示</span>
                        </div>
                        <div class="form-group">
                            <label class="form-label">仅限登录用户上传</label>
                            <select class="form-control" name="forcelogin" default="<?php echo $conf['forcelogin']?>">
                                <option value="0">否</option>
                                <option value="1">是</option>
                            </select>
                            <span class="help-text">开启后只有登录用户才能上传文件</span>
                        </div>
                    </div>

                    <div class="form-actions">
                        <button type="submit" class="btn btn-primary">
                            <i class="fa fa-save"></i> 保存设置
                        </button>
                    </div>
                </form>
            </div>
        </div>

        <?php elseif($mod=='green'): 
            $green_label_porn = explode(',', $conf['green_label_porn']);
            $green_label_terrorism = explode(',', $conf['green_label_terrorism']);
        ?>
        <!-- 图片检测设置 -->
        <div class="card">
            <div class="card-header">
                <h3><i class="fa fa-shield"></i> 内容安全检测配置</h3>
            </div>
            <div class="card-body">
                <form onsubmit="return saveSetting(this)" method="post" class="settings-form">
                    <div class="form-row two-col">
                        <div class="form-group">
                            <label class="form-label">图片违规检测</label>
                            <select class="form-control" name="green_check" id="green_check" default="<?php echo $conf['green_check']?>">
                                <option value="0">关闭</option>
                                <option value="1">阿里云内容安全接口</option>
                                <option value="2">腾讯云内容安全接口</option>
                            </select>
                            <span class="help-text">选择内容安全检测服务提供商</span>
                        </div>
                        <div class="form-group">
                            <label class="form-label">图片检测访问网址</label>
                            <input type="text" name="apiurl" value="<?php echo htmlspecialchars($conf['apiurl']); ?>" class="form-control" placeholder="不填写则默认使用当前网址"/>
                            <span class="help-text">图片检测时云服务商访问本站的网址，不填写则默认使用当前网址</span>
                        </div>
                    </div>

                    <!-- 阿里云配置 -->
                    <div id="green_aliyun" class="provider-config" style="<?php echo $conf['green_check']!='1'?'display:none;':''; ?>">
                        <div class="config-section">
                            <h4><i class="fa fa-cloud"></i> 阿里云配置</h4>
                            <div class="form-row two-col">
                                <div class="form-group">
                                    <label class="form-label">阿里云AccessKey Id</label>
                                    <input type="text" name="aliyun_ak" value="<?php echo htmlspecialchars($conf['aliyun_ak']); ?>" class="form-control"/>
                                </div>
                                <div class="form-group">
                                    <label class="form-label">阿里云AccessKey Secret</label>
                                    <input type="text" name="aliyun_sk" value="<?php echo htmlspecialchars($conf['aliyun_sk']); ?>" class="form-control"/>
                                </div>
                            </div>
                            <div class="form-row">
                                <div class="form-group">
                                    <label class="form-label">图片检测接入区域</label>
                                    <select class="form-control" name="green_check_region" default="<?php echo $conf['green_check_region']?>">
                                        <option value="cn-beijing">华北2（北京）</option>
                                        <option value="cn-shanghai">华东2（上海）</option>
                                        <option value="cn-shenzhen">华南1（深圳）</option>
                                        <option value="ap-southeast-1">新加坡</option>
                                        <option value="us-west-1">美西</option>
                                    </select>
                                    <span class="help-text">选择一个离本站服务器最近的区域以提升检测速度</span>
                                </div>
                            </div>

                            <div class="form-row two-col">
                                <div class="form-group">
                                    <label class="form-label">图片智能鉴黄</label>
                                    <select class="form-control" name="green_check_porn" id="green_check_porn" default="<?php echo $conf['green_check_porn']?>">
                                        <option value="0">关闭</option>
                                        <option value="1">开启</option>
                                    </select>
                                </div>
                            </div>

                            <div id="green_check_porn_" class="checkbox-config" style="<?php echo $conf['green_check_porn']!=1?'display:none;':''; ?>">
                                <label class="form-label">图片智能鉴黄屏蔽类型</label>
                                <div class="checkbox-group">
                                    <label class="checkbox-item">
                                        <input type="checkbox" name="green_label_porn[]" value="porn" <?php echo in_array('porn',$green_label_porn)?'checked':null;?>>
                                        <span class="checkmark">色情图片（porn）</span>
                                    </label>
                                    <label class="checkbox-item">
                                        <input type="checkbox" name="green_label_porn[]" value="sexy" <?php echo in_array('sexy',$green_label_porn)?'checked':null;?>>
                                        <span class="checkmark">性感图片（sexy）</span>
                                    </label>
                                </div>
                            </div>

                            <div class="form-row two-col">
                                <div class="form-group">
                                    <label class="form-label">图片暴恐涉政识别</label>
                                    <select class="form-control" name="green_check_terrorism" id="green_check_terrorism" default="<?php echo $conf['green_check_terrorism']?>">
                                        <option value="0">关闭</option>
                                        <option value="1">开启</option>
                                    </select>
                                </div>
                            </div>

                            <div id="green_check_terrorism_" class="checkbox-config" style="<?php echo $conf['green_check_terrorism']!=1?'display:none;':''; ?>">
                                <label class="form-label">图片暴恐涉政识别屏蔽类型</label>
                                <div class="checkbox-group">
                                    <label class="checkbox-item"><input type="checkbox" name="green_label_terrorism[]" value="bloody" <?php echo in_array('bloody',$green_label_terrorism)?'checked':null;?>> <span class="checkmark">血腥（bloody）</span></label>
                                    <label class="checkbox-item"><input type="checkbox" name="green_label_terrorism[]" value="explosion" <?php echo in_array('explosion',$green_label_terrorism)?'checked':null;?>> <span class="checkmark">爆炸烟光（explosion）</span></label>
                                    <label class="checkbox-item"><input type="checkbox" name="green_label_terrorism[]" value="outfit" <?php echo in_array('outfit',$green_label_terrorism)?'checked':null;?>> <span class="checkmark">特殊装束（outfit）</span></label>
                                    <label class="checkbox-item"><input type="checkbox" name="green_label_terrorism[]" value="logo" <?php echo in_array('logo',$green_label_terrorism)?'checked':null;?>> <span class="checkmark">特殊标识（logo）</span></label>
                                    <label class="checkbox-item"><input type="checkbox" name="green_label_terrorism[]" value="weapon" <?php echo in_array('weapon',$green_label_terrorism)?'checked':null;?>> <span class="checkmark">武器（weapon）</span></label>
                                    <label class="checkbox-item"><input type="checkbox" name="green_label_terrorism[]" value="politics" <?php echo in_array('politics',$green_label_terrorism)?'checked':null;?>> <span class="checkmark">涉政（politics）</span></label>
                                    <label class="checkbox-item"><input type="checkbox" name="green_label_terrorism[]" value="violence" <?php echo in_array('violence',$green_label_terrorism)?'checked':null;?>> <span class="checkmark">打斗（violence）</span></label>
                                    <label class="checkbox-item"><input type="checkbox" name="green_label_terrorism[]" value="crowd" <?php echo in_array('crowd',$green_label_terrorism)?'checked':null;?>> <span class="checkmark">聚众（crowd）</span></label>
                                    <label class="checkbox-item"><input type="checkbox" name="green_label_terrorism[]" value="parade" <?php echo in_array('parade',$green_label_terrorism)?'checked':null;?>> <span class="checkmark">游行（parade）</span></label>
                                    <label class="checkbox-item"><input type="checkbox" name="green_label_terrorism[]" value="carcrash" <?php echo in_array('carcrash',$green_label_terrorism)?'checked':null;?>> <span class="checkmark">车祸现场（carcrash）</span></label>
                                    <label class="checkbox-item"><input type="checkbox" name="green_label_terrorism[]" value="flag" <?php echo in_array('flag',$green_label_terrorism)?'checked':null;?>> <span class="checkmark">旗帜（flag）</span></label>
                                    <label class="checkbox-item"><input type="checkbox" name="green_label_terrorism[]" value="location" <?php echo in_array('location',$green_label_terrorism)?'checked':null;?>> <span class="checkmark">地标（location）</span></label>
                                    <label class="checkbox-item"><input type="checkbox" name="green_label_terrorism[]" value="drug" <?php echo in_array('drug',$green_label_terrorism)?'checked':null;?>> <span class="checkmark">涉毒（drug）</span></label>
                                    <label class="checkbox-item"><input type="checkbox" name="green_label_terrorism[]" value="gamble" <?php echo in_array('gamble',$green_label_terrorism)?'checked':null;?>> <span class="checkmark">赌博（gamble）</span></label>
                                </div>
                            </div>
                        </div>
                    </div>

                    <!-- 腾讯云配置 -->
                    <div id="green_qcloud" class="provider-config" style="<?php echo $conf['green_check']!='2'?'display:none;':''; ?>">
                        <div class="config-section">
                            <h4><i class="fa fa-cloud"></i> 腾讯云配置</h4>
                            <div class="form-row two-col">
                                <div class="form-group">
                                    <label class="form-label">腾讯云SecretId</label>
                                    <input type="text" name="qcloud_green_id" value="<?php echo htmlspecialchars($conf['qcloud_green_id']); ?>" class="form-control"/>
                                </div>
                                <div class="form-group">
                                    <label class="form-label">腾讯云SecretKey</label>
                                    <input type="text" name="qcloud_green_key" value="<?php echo htmlspecialchars($conf['qcloud_green_key']); ?>" class="form-control"/>
                                </div>
                            </div>
                            <div class="form-row">
                                <div class="form-group">
                                    <label class="form-label">图片检测接入区域</label>
                                    <select class="form-control" name="green_check_region" default="<?php echo $conf['green_check_region']?>">
                                        <option value="ap-beijing">华北地区(北京)</option>
                                        <option value="ap-shanghai">华东地区(上海)</option>
                                        <option value="ap-guangzhou">华南地区(广州)</option>
                                        <option value="ap-mumbai">亚太南部(孟买)</option>
                                        <option value="ap-singapore">亚太东南(新加坡)</option>
                                        <option value="eu-frankfurt">欧洲地区(法兰克福)</option>
                                        <option value="na-ashburn">美国东部(弗吉尼亚)</option>
                                        <option value="na-siliconvalley">美国西部(硅谷)</option>
                                    </select>
                                    <span class="help-text">选择一个离本站服务器最近的区域以提升检测速度</span>
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="form-actions">
                        <button type="submit" class="btn btn-primary">
                            <i class="fa fa-save"></i> 保存设置
                        </button>
                    </div>
                </form>
            </div>
            <div class="card-footer info-footer">
                <i class="fa fa-info-circle"></i>
                <span>
                    阿里云内容安全接口：<a href="https://yundun.console.aliyun.com/?p=cts#/api/statistics" target="_blank" rel="noreferrer">点此进入</a> | 
                    <a href="https://usercenter.console.aliyun.com/#/manage/ak" target="_blank" rel="noreferrer">获取密钥</a> &nbsp;&nbsp;
                    腾讯云内容安全接口：<a href="https://cloud.tencent.com/product/ims" target="_blank" rel="noreferrer">点此进入</a> | 
                    <a href="https://console.cloud.tencent.com/cam/capi" target="_blank" rel="noreferrer">获取密钥</a>
                </span>
            </div>
        </div>

        <?php elseif($mod=='api'):
            $scriptpath=str_replace('\\','/',$_SERVER['SCRIPT_NAME']);
            $sitepath = substr($scriptpath, 0, strrpos($scriptpath, '/'));
            $admin_path = substr($sitepath, strrpos($sitepath, '/'));
            $siteurl = (is_https() ? 'https://' : 'http://').$_SERVER['HTTP_HOST'].str_replace($admin_path,'',$sitepath).'/'; 
        ?>
        <!-- 上传API设置 -->
        <div class="card">
            <div class="card-header">
                <h3><i class="fa fa-code"></i> API基础设置</h3>
            </div>
            <div class="card-body">
                <form onsubmit="return saveSetting(this)" method="post" class="settings-form">
                    <div class="form-row two-col">
                        <div class="form-group">
                            <label class="form-label">上传API开关</label>
                            <select class="form-control" name="api_open" default="<?php echo $conf['api_open']?>">
                                <option value="0">关闭</option>
                                <option value="1">开启</option>
                            </select>
                            <span class="help-text">开启后允许通过API接口上传文件</span>
                        </div>
                        <div class="form-group">
                            <label class="form-label">来源域名白名单</label>
                            <input type="text" name="api_referer" value="<?php echo htmlspecialchars($conf['api_referer']); ?>" class="form-control" placeholder="example.com|example.org"/>
                            <span class="help-text">多个域名用 | 隔开，不填写则不限制来源域名</span>
                        </div>
                    </div>

                    <div class="form-actions">
                        <button type="submit" class="btn btn-primary">
                            <i class="fa fa-save"></i> 保存设置
                        </button>
                    </div>
                </form>
            </div>
        </div>

        <!-- API接口文档引导提示 -->
        <div class="card">
            <div class="card-header">
                <h3><i class="fa fa-book"></i> API接口文档</h3>
            </div>
            <div class="card-body">
                <div class="api-redirect-notice">
                    <div class="redirect-content">
                        <h4>完整的API接口文档已迁移至独立文档页面</h4>
                        <p>为提供更专业、更全面的开发者体验，API接口文档已升级至独立页面，包含完整的接口说明、参数规范、请求示例和错误码说明。</p>
                        <div class="redirect-info-box">
                            <div class="redirect-info-item">
                                <span class="info-label"><i class="fa fa-check-circle"></i> 新版文档包含</span>
                                <ul class="info-list">
                                    <li>用户认证接口（登录/注册/资料管理）</li>
                                    <li>文件上传接口（简单上传/分块上传/预检查）</li>
                                    <li>文件管理接口（列表/详情/搜索/重命名/删除）</li>
                                    <li>文件夹管理接口（创建/重命名/删除/移动/排序）</li>
                                    <li>分享管理接口（创建/查询/撤销分享链接）</li>
                                    <li>统计查询接口（存储/流量/使用详情）</li>
                                    <li>卡密兑换接口（兑换/兑换记录查询）</li>
                                    <li>完整的错误码说明和认证指南</li>
                                </ul>
                            </div>
                        </div>
                        <div class="redirect-link-box">
                            <p><strong>请访问以下链接查看完整API文档：</strong></p>
                            <a href="[链接]" class="btn btn-primary redirect-btn" target="_blank">
                                <i class="fa fa-external-link"></i> 访问API接口文档
                            </a>
                            <p class="redirect-hint">
                                <i class="fa fa-info-circle"></i>
                                此链接为占位符，待文档网站搭建完成后，请将 <code>[链接]</code> 替换为实际的文档网站URL。
                            </p>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <?php elseif($mod=='iptype'): ?>
        <!-- IP地址设置 -->
        <div class="card">
            <div class="card-header">
                <h3><i class="fa fa-map-marker"></i> IP地址获取设置</h3>
            </div>
            <div class="card-body">
                <form onsubmit="return saveSetting(this)" method="post" class="settings-form">
                    <div class="form-row">
                        <div class="form-group">
                            <label class="form-label">用户IP地址获取方式</label>
                            <select class="form-control" name="ip_type" id="ip_type_select" default="<?php echo $conf['ip_type']?>">
                                <option value="0">0_X_FORWARDED_FOR</option>
                                <option value="1">1_X_REAL_IP</option>
                                <option value="2">2_REMOTE_ADDR</option>
                            </select>
                            <span class="help-text">此功能设置用于防止用户伪造IP请求</span>
                        </div>
                    </div>

                    <div class="form-row">
                        <div class="form-group">
                            <label class="form-label">IP获取方式说明</label>
                            <div class="ip-type-info">
                                <div class="ip-type-item">
                                    <span class="ip-type-name">X_FORWARDED_FOR</span>
                                    <p>之前的获取真实IP方式，极易被伪造IP</p>
                                </div>
                                <div class="ip-type-item">
                                    <span class="ip-type-name">X_REAL_IP</span>
                                    <p>在网站使用CDN的情况下选择此项，在不使用CDN的情况下也会被伪造</p>
                                </div>
                                <div class="ip-type-item">
                                    <span class="ip-type-name">REMOTE_ADDR</span>
                                    <p>直接获取真实请求IP，无法被伪造，但可能获取到的是CDN节点IP</p>
                                </div>
                            </div>
                            <div class="alert alert-warning">
                                <i class="fa fa-exclamation-triangle"></i>
                                <strong>建议：</strong>你可以从中选择一个能显示你真实地址的IP，优先选下方的选项。
                            </div>
                        </div>
                    </div>

                    <div class="form-actions">
                        <button type="submit" class="btn btn-primary">
                            <i class="fa fa-save"></i> 保存设置
                        </button>
                    </div>
                </form>
            </div>
        </div>
        <script>
        $(document).ready(function(){
            $.ajax({
                type : "GET",
                url : "ajax.php?act=iptype",
                dataType : 'json',
                async: true,
                success : function(data) {
                    $("#ip_type_select").empty();
                    var defaultv = "<?php echo $conf['ip_type']; ?>";
                    $.each(data, function(k, item){
                        $("#ip_type_select").append('<option value="'+k+'" '+(defaultv==k?'selected':'')+'>'+ item.name +' - '+ item.ip +' '+ item.city +'</option>');
                    })
                }
            });
        })
        </script>

        <?php elseif($mod=='announcement'):
            // 处理公告增删改查
            $action = isset($_GET['action']) ? $_GET['action'] : 'list';
            
            if($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['do'])){
                if(!checkRefererHost())exit;
                $do = $_POST['do'];
                
                if($do == 'add' || $do == 'edit'){
                    $id = isset($_POST['id']) ? intval($_POST['id']) : 0;
                    $title = trim($_POST['title']);
                    $content = $_POST['content'];
                    $type = intval($_POST['type']);
                    $status = intval($_POST['status']);
                    $sort = intval($_POST['sort']);
                    $start_time = !empty($_POST['start_time']) ? $_POST['start_time'] : null;
                    $end_time = !empty($_POST['end_time']) ? $_POST['end_time'] : null;
                    
                    if(empty($title)){
                        showmsg('公告标题不能为空！', 3);
                    }
                    
                    $data = [
                        ':title' => $title,
                        ':content' => $content,
                        ':type' => $type,
                        ':status' => $status,
                        ':sort' => $sort,
                        ':start_time' => $start_time,
                        ':end_time' => $end_time
                    ];
                    
                    if($do == 'add'){
                        $sql = "INSERT INTO pre_user_announcement (title, content, type, status, sort, start_time, end_time, create_time, update_time) VALUES (:title, :content, :type, :status, :sort, :start_time, :end_time, NOW(), NOW())";
                        $result = $DB->exec($sql, $data);
                        if($result){
                            showmsg('添加公告成功！', 1);
                        }else{
                            showmsg('添加失败：'.$DB->error(), 3);
                        }
                    }else{
                        $data[':id'] = $id;
                        $sql = "UPDATE pre_user_announcement SET title=:title, content=:content, type=:type, status=:status, sort=:sort, start_time=:start_time, end_time=:end_time, update_time=NOW() WHERE id=:id";
                        $result = $DB->exec($sql, $data);
                        if($result !== false){
                            showmsg('修改公告成功！', 1);
                        }else{
                            showmsg('修改失败：'.$DB->error(), 3);
                        }
                    }
                }elseif($do == 'delete'){
                    $id = intval($_POST['id']);
                    $result = $DB->exec("DELETE FROM pre_user_announcement WHERE id=:id", [':id'=>$id]);
                    if($result){
                        exit(json_encode(['code'=>0, 'msg'=>'删除成功']));
                    }else{
                        exit(json_encode(['code'=>-1, 'msg'=>'删除失败']));
                    }
                }elseif($do == 'toggle'){
                    $id = intval($_POST['id']);
                    $status = intval($_POST['status']);
                    $result = $DB->exec("UPDATE pre_user_announcement SET status=:status, update_time=NOW() WHERE id=:id", [':status'=>$status, ':id'=>$id]);
                    if($result !== false){
                        exit(json_encode(['code'=>0, 'msg'=>'操作成功']));
                    }else{
                        exit(json_encode(['code'=>-1, 'msg'=>'操作失败']));
                    }
                }
            }
            
            // 获取公告列表
            $announcements = $DB->getAll("SELECT * FROM pre_user_announcement ORDER BY sort DESC, id DESC");
            
            // 获取单条公告（编辑用）
            $edit_item = null;
            if($action == 'edit' && isset($_GET['id'])){
                $edit_id = intval($_GET['id']);
                $edit_item = $DB->getRow("SELECT * FROM pre_user_announcement WHERE id=:id", [':id'=>$edit_id]);
            }
        ?>
        <!-- 用户中心公告管理 -->
        <div class="card">
            <div class="card-header">
                <h3><i class="fa fa-bullhorn"></i> 公告列表</h3>
                <a href="./set.php?mod=announcement&action=add" class="btn btn-primary btn-sm">
                    <i class="fa fa-plus"></i> 添加公告
                </a>
            </div>
            <div class="card-body">
                <?php if($action == 'list'): ?>
                <div class="announcement-list">
                    <div class="table-responsive-wrapper">
                        <table class="data-table">
                        <thead>
                            <tr>
                                <th width="50">ID</th>
                                <th width="60">排序</th>
                                <th>标题</th>
                                <th width="100">类型</th>
                                <th width="80">状态</th>
                                <th width="160">创建时间</th>
                                <th width="160">操作</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach($announcements as $item): ?>
                            <tr>
                                <td><?php echo $item['id']; ?></td>
                                <td><?php echo $item['sort']; ?></td>
                                <td><?php echo htmlspecialchars($item['title']); ?></td>
                                <td>
                                    <?php if($item['type']==1): ?>
                                    <span class="badge badge-info">普通</span>
                                    <?php elseif($item['type']==2): ?>
                                    <span class="badge badge-warning">重要</span>
                                    <?php else: ?>
                                    <span class="badge badge-danger">紧急</span>
                                    <?php endif; ?>
                                </td>
                                <td>
                                    <?php if($item['status']==1): ?>
                                    <span class="badge badge-success">启用</span>
                                    <?php else: ?>
                                    <span class="badge badge-default">禁用</span>
                                    <?php endif; ?>
                                </td>
                                <td><?php echo $item['create_time']; ?></td>
                                <td>
                                    <a href="./set.php?mod=announcement&action=edit&id=<?php echo $item['id']; ?>" class="btn btn-sm btn-info">
                                        <i class="fa fa-edit"></i> 编辑
                                    </a>
                                    <button type="button" class="btn btn-sm <?php echo $item['status']==1?'btn-warning':'btn-success'; ?>" onclick="toggleAnnouncement(<?php echo $item['id']; ?>, <?php echo $item['status']==1?0:1; ?>)">
                                        <i class="fa fa-<?php echo $item['status']==1?'pause':'play'; ?>"></i> <?php echo $item['status']==1?'禁用':'启用'; ?>
                                    </button>
                                    <button type="button" class="btn btn-sm btn-danger" onclick="deleteAnnouncement(<?php echo $item['id']; ?>)">
                                        <i class="fa fa-trash"></i> 删除
                                    </button>
                                </td>
                            </tr>
                            <?php endforeach; ?>
                            <?php if(empty($announcements)): ?>
                            <tr>
                                <td colspan="7" class="text-center">暂无公告</td>
                            </tr>
                            <?php endif; ?>
                        </tbody>
                    </table>
                    </div>
                </div>
                <?php else: ?>
                <form action="./set.php?mod=announcement" method="post" class="settings-form">
                    <input type="hidden" name="do" value="<?php echo $action; ?>"/>
                    <?php if($edit_item): ?>
                    <input type="hidden" name="id" value="<?php echo $edit_item['id']; ?>"/>
                    <?php endif; ?>
                    
                    <div class="form-row">
                        <div class="form-group">
                            <label class="form-label">公告标题 <span class="required">*</span></label>
                            <input type="text" name="title" value="<?php echo $edit_item ? htmlspecialchars($edit_item['title']) : ''; ?>" class="form-control" required placeholder="请输入公告标题"/>
                        </div>
                    </div>
                    
                    <div class="form-row two-col">
                        <div class="form-group">
                            <label class="form-label">公告类型</label>
                            <select class="form-control" name="type">
                                <option value="1" <?php echo ($edit_item && $edit_item['type']==1)?'selected':''; ?>>普通公告</option>
                                <option value="2" <?php echo ($edit_item && $edit_item['type']==2)?'selected':''; ?>>重要公告</option>
                                <option value="3" <?php echo ($edit_item && $edit_item['type']==3)?'selected':''; ?>>紧急公告</option>
                            </select>
                        </div>
                        <div class="form-group">
                            <label class="form-label">排序权重</label>
                            <input type="number" name="sort" value="<?php echo $edit_item ? intval($edit_item['sort']) : '0'; ?>" class="form-control" placeholder="数值越大越靠前"/>
                            <span class="help-text">数值越大，公告显示越靠前</span>
                        </div>
                    </div>
                    
                    <div class="form-row two-col">
                        <div class="form-group">
                            <label class="form-label">开始时间</label>
                            <input type="datetime-local" name="start_time" value="<?php echo $edit_item && $edit_item['start_time'] ? date('Y-m-d\TH:i', strtotime($edit_item['start_time'])) : ''; ?>" class="form-control"/>
                            <span class="help-text">留空表示立即显示</span>
                        </div>
                        <div class="form-group">
                            <label class="form-label">结束时间</label>
                            <input type="datetime-local" name="end_time" value="<?php echo $edit_item && $edit_item['end_time'] ? date('Y-m-d\TH:i', strtotime($edit_item['end_time'])) : ''; ?>" class="form-control"/>
                            <span class="help-text">留空表示永不过期</span>
                        </div>
                    </div>
                    
                    <div class="form-row">
                        <div class="form-group">
                            <label class="form-label">公告内容（支持HTML）</label>
                            <textarea class="form-control" name="content" rows="8" placeholder="支持HTML代码"><?php echo $edit_item ? htmlspecialchars($edit_item['content']) : ''; ?></textarea>
                            <span class="help-text">支持HTML标签，可用于添加链接、样式等</span>
                        </div>
                    </div>
                    
                    <div class="form-row">
                        <div class="form-group">
                            <label class="form-label">状态</label>
                            <select class="form-control" name="status">
                                <option value="1" <?php echo (!$edit_item || $edit_item['status']==1)?'selected':''; ?>>启用</option>
                                <option value="0" <?php echo ($edit_item && $edit_item['status']==0)?'selected':''; ?>>禁用</option>
                            </select>
                        </div>
                    </div>

                    <div class="form-actions">
                        <button type="submit" class="btn btn-primary">
                            <i class="fa fa-save"></i> <?php echo $action=='add'?'添加公告':'保存修改'; ?>
                        </button>
                        <a href="./set.php?mod=announcement" class="btn btn-default">
                            <i class="fa fa-arrow-left"></i> 返回列表
                        </a>
                    </div>
                </form>
                <?php endif; ?>
            </div>
        </div>
        
        <style>
        .announcement-list {
            width: 100%;
        }
        .announcement-list .table-responsive-wrapper {
            width: 100%;
            overflow-x: auto;
            -webkit-overflow-scrolling: touch;
        }
        .announcement-list .data-table {
            width: 100%;
            min-width: 700px;
            border-collapse: collapse;
        }
        .announcement-list .data-table th,
        .announcement-list .data-table td {
            padding: 12px;
            text-align: left;
            border-bottom: 1px solid #e2e8f0;
        }
        .announcement-list .data-table th {
            background: #f8fafc;
            font-weight: 600;
            color: #475569;
        }
        .announcement-list .data-table tr:hover {
            background: #f8fafc;
        }
        .badge {
            display: inline-block;
            padding: 4px 8px;
            border-radius: 4px;
            font-size: 12px;
            font-weight: 500;
        }
        .badge-info { background: #e0f2fe; color: #0369a1; }
        .badge-warning { background: #fef3c7; color: #92400e; }
        .badge-danger { background: #fee2e2; color: #991b1b; }
        .badge-success { background: #dcfce7; color: #166534; }
        .badge-default { background: #f1f5f9; color: #64748b; }
        .btn-sm {
            padding: 4px 10px;
            font-size: 12px;
        }
        .btn-info { background: #0ea5e9; color: #fff; border: none; }
        .btn-warning { background: #f59e0b; color: #fff; border: none; }
        .btn-success { background: #22c55e; color: #fff; border: none; }
        .btn-danger { background: #ef4444; color: #fff; border: none; }
        .btn-default { background: #f1f5f9; color: #475569; border: 1px solid #e2e8f0; }
        .required { color: #ef4444; }
        .text-center { text-align: center; }
        </style>
        
        <script>
        function deleteAnnouncement(id){
            if(confirm('确定要删除这条公告吗？')){
                $.ajax({
                    type: 'POST',
                    url: './set.php?mod=announcement',
                    data: {do: 'delete', id: id},
                    dataType: 'json',
                    success: function(data){
                        if(data.code == 0){
                            alert('删除成功');
                            location.reload();
                        }else{
                            alert(data.msg);
                        }
                    }
                });
            }
        }
        function toggleAnnouncement(id, status){
            var msg = status == 1 ? '启用' : '禁用';
            if(confirm('确定要'+msg+'这条公告吗？')){
                $.ajax({
                    type: 'POST',
                    url: './set.php?mod=announcement',
                    data: {do: 'toggle', id: id, status: status},
                    dataType: 'json',
                    success: function(data){
                        if(data.code == 0){
                            alert('操作成功');
                            location.reload();
                        }else{
                            alert(data.msg);
                        }
                    }
                });
            }
        }
        </script>

        <?php elseif($mod=='account'):
            if(isset($_POST['do']) && $_POST['do']=='submit'){
                if(!checkRefererHost())exit;
                include_once(ROOT."security_password.php");
                $user=$_POST['user'];
                $oldpwd=$_POST['oldpwd'];
                $newpwd=$_POST['newpwd'];
                $newpwd2=$_POST['newpwd2'];
                if($user==null)showmsg('用户名不能为空！',3);
                saveSetting('admin_user',$user);
                if(!empty($newpwd) && !empty($newpwd2)){
                    $stored_password = $conf['admin_pwd'];
                    $password_valid = false;
                    if (is_password_hash($stored_password)) {
                        $password_valid = password_verify_bcrypt($oldpwd, $stored_password);
                    } else {
                        $password_valid = ($oldpwd === $stored_password);
                    }
                    if(!$password_valid)showmsg('旧密码不正确！',3);
                    if($newpwd!=$newpwd2)showmsg('两次输入的密码不一致！',3);
                    $new_hash = password_hash_bcrypt($newpwd);
                    saveSetting('admin_pwd',$new_hash);
                }
                showmsg('修改成功！请重新登录',1);
            }
        ?>
        <!-- 账号管理设置 -->
        <div class="card">
            <div class="card-header">
                <h3><i class="fa fa-lock"></i> 管理员账号设置</h3>
            </div>
            <div class="card-body">
                <form action="./set.php?mod=account" method="post" class="settings-form">
                    <input type="hidden" name="do" value="submit"/>
                    
                    <div class="form-row">
                        <div class="form-group">
                            <label class="form-label">用户名</label>
                            <input type="text" name="user" value="<?php echo htmlspecialchars($conf['admin_user']); ?>" class="form-control" required placeholder="请输入管理员用户名"/>
                            <span class="help-text">登录后台管理系统的用户名</span>
                        </div>
                    </div>

                    <div class="form-row">
                        <div class="form-group">
                            <label class="form-label">旧密码</label>
                            <input type="password" name="oldpwd" value="" class="form-control" placeholder="请输入当前的管理员密码"/>
                            <span class="help-text">修改密码时需要验证旧密码</span>
                        </div>
                    </div>

                    <div class="form-row two-col">
                        <div class="form-group">
                            <label class="form-label">新密码</label>
                            <input type="password" name="newpwd" value="" class="form-control" placeholder="不修改请留空"/>
                            <span class="help-text">设置新密码，至少6位字符</span>
                        </div>
                        <div class="form-group">
                            <label class="form-label">确认新密码</label>
                            <input type="password" name="newpwd2" value="" class="form-control" placeholder="不修改请留空"/>
                            <span class="help-text">再次输入新密码以确认</span>
                        </div>
                    </div>

                    <div class="form-actions">
                        <button type="submit" class="btn btn-primary">
                            <i class="fa fa-save"></i> 保存修改
                        </button>
                    </div>
                </form>
            </div>
            <div class="card-footer warning-footer">
                <i class="fa fa-exclamation-triangle"></i>
                <span>修改账号密码后需要重新登录，请牢记您的新密码！</span>
            </div>
        </div>
        <?php endif; ?>
    </div>
</div>

<script src="https://s4.zstatic.net/ajax/libs/layer/2.3/layer.js"></script>
<script>
// 初始化下拉框默认值
var items = $("select[default]");
for (i = 0; i < items.length; i++) {
    $(items[i]).val($(items[i]).attr("default")||0);
}

// 保存设置
function saveSetting(obj){
    var ii = layer.load(2, {shade:[0.1,'#fff']});
    $.ajax({
        type : 'POST',
        url : 'ajax.php?act=set',
        data : $(obj).serialize(),
        dataType : 'json',
        success : function(data) {
            layer.close(ii);
            if(data.code == 0){
                layer.alert('设置保存成功！', {
                    icon: 1,
                    closeBtn: false
                }, function(){
                  window.location.reload()
                });
            }else{
                layer.alert(data.msg, {icon: 2})
            }
        },
        error:function(data){
            layer.msg('服务器错误');
            return false;
        }
    });
    return false;
}

// 快捷登录开关切换
$("select[name='quicklogin']").change(function(){
    if($(this).val() == 1){
        $("#quicklogin_config").show();
    }else{
        $("#quicklogin_config").hide();
    }
});

// 图片检测切换
$("select[name='green_check']").change(function(){
    if($(this).val() == 1){
        $("#green_aliyun").show();
        $("#green_qcloud").hide();
    }else if($(this).val() == 2){
        $("#green_aliyun").hide();
        $("#green_qcloud").show();
    }else{
        $("#green_aliyun").hide();
        $("#green_qcloud").hide();
    }
});

$("select[name='green_check_porn']").change(function(){
    if($(this).val() == 1){
        $("#green_check_porn_").show();
    }else{
        $("#green_check_porn_").hide();
    }
});

$("select[name='green_check_terrorism']").change(function(){
    if($(this).val() == 1){
        $("#green_check_terrorism_").show();
    }else{
        $("#green_check_terrorism_").hide();
    }
});

// 检查重置状态
function checkResetStatus() {
    var ii = layer.load(2, {shade:[0.1,'#fff']});
    $.ajax({
        type : 'POST',
        url : 'ajax.php?act=check_traffic_reset_status',
        data : {
            csrf_token: $('[name="csrf_token"]').val()
        },
        dataType : 'json',
        success : function(data) {
            layer.close(ii);
            if(data.code === 0){
                var html = '<h5>流量重置状态检查结果</h5>';
                html += '<table class="data-table">';
                html += '<thead><tr><th>检查项</th><th>状态</th><th>说明</th></tr></thead><tbody>';
                html += '<tr><td>配置值</td><td>' + data.config.interval + '天</td><td>当前设置的重置周期</td></tr>';
                html += '<tr><td>用户总数</td><td>' + data.stats.total_users + '</td><td>系统中的用户数量</td></tr>';
                html += '<tr><td>有重置时间的用户</td><td>' + data.stats.users_with_reset + '</td><td>已设置重置基准的用户</td></tr>';
                html += '<tr><td>最近重置</td><td>' + data.stats.last_reset + '</td><td>最近一次重置时间</td></tr>';
                html += '</tbody></table>';
                
                if(data.issues.length > 0) {
                    html += '<h5 style="color:#f0ad4e;margin-top:15px;">发现的问题：</h5><ul style="color:#92400e;">';
                    for(var i = 0; i < data.issues.length; i++) {
                        html += '<li>' + data.issues[i] + '</li>';
                    }
                    html += '</ul>';
                } else {
                    html += '<h5 style="color:#10b981;margin-top:15px;"><i class="fa fa-check-circle"></i> 未发现问题</h5>';
                }
                
                document.getElementById('reset-status-info').innerHTML = html;
                document.getElementById('reset-status-info').style.display = 'block';
            }else{
                layer.alert('检查失败：' + data.msg, {icon: 2});
            }
        },
        error:function(){
            layer.close(ii);
            layer.msg('检查失败，请稍后重试');
        }
    });
}

// 修复重置问题
function fixResetIssues() {
    layer.confirm('确定要修复流量重置问题吗？<br>这将为所有没有重置时间的用户设置基准时间。', {
        btn: ['确定修复','取消']
    }, function(){
        var ii = layer.load(2, {shade:[0.1,'#fff']});
        $.ajax({
            type : 'POST',
            url : 'ajax.php?act=fix_traffic_reset',
            data : {
                csrf_token: $('[name="csrf_token"]').val()
            },
            dataType : 'json',
            success : function(data) {
                layer.close(ii);
                if(data.code === 0){
                    layer.alert('修复完成！修复了 ' + data.fixed_count + ' 个用户。', {
                        icon: 1,
                        closeBtn: false
                    }, function(){
                        checkResetStatus();
                    });
                }else{
                    layer.alert('修复失败：' + data.msg, {icon: 2});
                }
            },
            error:function(){
                layer.close(ii);
                layer.msg('修复失败，请稍后重试');
            }
        });
    });
}
</script>

<style>
/* 设置页面专用样式 */
.settings-content {
    animation: fadeIn 0.4s ease-out;
}

.settings-form {
    max-width: 900px;
}

.form-row {
    margin-bottom: 24px;
}

.form-row.two-col {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 24px;
}

.form-group {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.form-label {
    font-size: 14px;
    font-weight: 600;
    color: var(--text-primary);
}

.help-text {
    font-size: 12px;
    color: var(--text-muted);
    line-height: 1.5;
}

.input-group {
    display: flex;
}

.input-group .form-control {
    border-radius: var(--border-radius) 0 0 var(--border-radius);
    flex: 1;
}

.input-addon {
    height: 42px;
    padding: 0 16px;
    background: var(--gray-100);
    border: 1px solid var(--border-color);
    border-left: none;
    border-radius: 0 var(--border-radius) var(--border-radius) 0;
    display: flex;
    align-items: center;
    color: var(--text-secondary);
    font-size: 14px;
    font-weight: 500;
}

.form-actions {
    margin-top: 32px;
    padding-top: 24px;
    border-top: 1px solid var(--border-color);
}

.form-actions .btn {
    min-width: 140px;
}

/* 复选框组 */
.checkbox-group {
    display: flex;
    flex-wrap: wrap;
    gap: 16px;
}

.checkbox-item {
    display: flex;
    align-items: center;
    gap: 8px;
    cursor: pointer;
    padding: 8px 12px;
    background: var(--gray-50);
    border-radius: var(--border-radius);
    border: 1px solid var(--border-color);
    transition: all var(--transition-fast);
}

.checkbox-item:hover {
    background: var(--gray-100);
    border-color: var(--primary-color);
}

.checkbox-item input[type="checkbox"] {
    width: 18px;
    height: 18px;
    accent-color: var(--primary-color);
}

.checkbox-item .checkmark {
    font-size: 14px;
    color: var(--text-primary);
}

.checkbox-item .checkmark i {
    margin-right: 4px;
}

/* 提供商配置区域 */
.provider-config {
    margin-top: 24px;
    padding: 24px;
    background: var(--gray-50);
    border-radius: var(--border-radius-lg);
    border: 1px solid var(--border-color);
}

.config-section h4 {
    margin: 0 0 20px 0;
    font-size: 16px;
    color: var(--text-primary);
    display: flex;
    align-items: center;
    gap: 8px;
}

.config-section h4 i {
    color: var(--primary-color);
}

.checkbox-config {
    margin-top: 16px;
    padding: 16px;
    background: var(--bg-card);
    border-radius: var(--border-radius);
    border: 1px solid var(--border-color);
}

/* 按钮组 */
.btn-group-inline {
    display: flex;
    gap: 12px;
    flex-wrap: wrap;
}

.status-info-box {
    margin-top: 16px;
    padding: 16px;
    background: var(--bg-card);
    border-radius: var(--border-radius);
    border: 1px solid var(--border-color);
}

.status-info-box h5 {
    margin: 0 0 12px 0;
    font-size: 14px;
    color: var(--text-primary);
}

/* IP类型信息 */
.ip-type-info {
    display: flex;
    flex-direction: column;
    gap: 12px;
}

.ip-type-item {
    padding: 16px;
    background: var(--gray-50);
    border-radius: var(--border-radius);
    border-left: 3px solid var(--primary-color);
}

.ip-type-name {
    font-weight: 600;
    color: var(--primary-color);
    font-size: 14px;
}

.ip-type-item p {
    margin: 4px 0 0 0;
    font-size: 13px;
    color: var(--text-secondary);
}

/* API文档引导提示样式 */
.api-redirect-notice {
    display: flex;
    gap: 24px;
    padding: 10px 0;
}

.redirect-icon {
    flex-shrink: 0;
    width: 80px;
    height: 80px;
    background: #f8f9fb;
    border-radius: 16px;
    display: flex;
    align-items: center;
    justify-content: center;
}

.redirect-icon i {
    font-size: 36px;
    color: #e09145;
}

.redirect-content {
    flex: 1;
    min-width: 0;
}

.redirect-content h4 {
    margin: 0 0 10px;
    font-size: 17px;
    font-weight: 600;
    color: var(--text-primary);
}

.redirect-content p {
    color: var(--text-secondary);
    font-size: 14px;
    line-height: 1.7;
    margin: 0 0 16px;
}

.redirect-info-box {
    background: #f8f9fb;
    border-radius: 10px;
    padding: 18px 20px;
    margin-bottom: 20px;
    border: 1px solid #eef0f4;
}

.info-label {
    display: block;
    font-size: 14px;
    font-weight: 600;
    color: #10b981;
    margin-bottom: 10px;
}

.info-label i {
    margin-right: 6px;
}

.info-list {
    margin: 0;
    padding-left: 20px;
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 6px 20px;
}

.info-list li {
    font-size: 13px;
    color: #555;
    line-height: 1.6;
}

.redirect-link-box {
    text-align: center;
    padding: 20px;
    background: #fffbf0;
    border: 1px solid #fde68a;
    border-radius: 10px;
}

.redirect-link-box p {
    margin: 0 0 12px;
}

.redirect-link-box strong {
    color: #92400e;
    font-size: 14px;
}

.redirect-btn {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    padding: 12px 28px;
    font-size: 15px;
    font-weight: 600;
    border-radius: 8px;
    background: #e09145;
    border-color: #e09145;
    color: #fff;
    text-decoration: none;
    transition: all 0.2s;
}

.redirect-btn:hover {
    background: #d4843a;
    border-color: #d4843a;
    color: #fff;
    text-decoration: none;
    transform: translateY(-1px);
    box-shadow: 0 4px 12px rgba(224, 145, 69, 0.3);
}

.redirect-hint {
    margin-top: 12px !important;
    font-size: 12px !important;
    color: #92400e !important;
}

.redirect-hint i {
    margin-right: 4px;
}

.redirect-hint code {
    background: #fde68a;
    padding: 2px 6px;
    border-radius: 3px;
    font-size: 12px;
    color: #92400e;
}

/* 底部信息 */
.info-footer,
.warning-footer {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 16px 24px;
    background: #eff6ff;
    border-top: 1px solid #bfdbfe;
    font-size: 13px;
    color: #1e40af;
}

.info-footer i {
    color: #3b82f6;
    font-size: 16px;
}

.warning-footer {
    background: #fffbeb;
    border-top-color: #fcd34d;
    color: #92400e;
}

.warning-footer i {
    color: #f59e0b;
}

.info-footer a {
    color: #2563eb;
    text-decoration: underline;
}

/* 响应式 */
@media (max-width: 767px) {
    .form-row.two-col {
        grid-template-columns: 1fr;
        gap: 16px;
    }
    
    .api-redirect-notice {
        padding: 32px 20px;
    }

    .redirect-icon {
        width: 64px;
        height: 64px;
        font-size: 28px;
        margin-bottom: 16px;
    }

    .notice-title {
        font-size: 18px;
    }

    .notice-text {
        font-size: 14px;
        line-height: 1.7;
    }

    .info-list li {
        font-size: 13px;
        padding: 8px 12px;
    }

    .notice-action {
        flex-direction: column;
        gap: 12px;
        align-items: stretch;
    }

    .btn-doc-link {
        width: 100%;
        justify-content: center;
    }

    .link-placeholder-tip {
        text-align: center;
    }
}
</style>

</body>
</html>
