<?php
require_once __DIR__ . '/../MPHX/common.php';

if (!is_admin_login()) {
    header('Location: /admin/login.php');
    exit;
}

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

/**
 * 调整颜色亮度
 */
function adjustBrightness($hex, $percent) {
    $hex = ltrim($hex, '#');
    
    if (strlen($hex) == 3) {
        $hex = $hex[0] . $hex[0] . $hex[1] . $hex[1] . $hex[2] . $hex[2];
    }
    
    $r = max(0, min(255, hexdec(substr($hex, 0, 2)) + $percent));
    $g = max(0, min(255, hexdec(substr($hex, 2, 2)) + $percent));
    $b = max(0, min(255, hexdec(substr($hex, 4, 2)) + $percent));
    
    return '#' . str_pad(dechex($r), 2, '0', STR_PAD_LEFT) . 
                 str_pad(dechex($g), 2, '0', STR_PAD_LEFT) . 
                 str_pad(dechex($b), 2, '0', STR_PAD_LEFT);
}
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <title>系统设置 - 宝塔主机管理系统</title>
    <link rel="stylesheet" href="/static/css/bootstrap.min.css">
    <link rel="stylesheet" href="/static/css/all.min.css">
    <link rel="stylesheet" href="css/style.css?v=2026042402">
</head>
<body>
    <div class="content-area">
        <?php if ($gn == 'wz'): ?>
        <div class="card">
            <h3><i class="fas fa-cog"></i> 网站设置</h3>
            <form id="wzForm">
                <div class="form-group">
                    <label for="gg">网站公告</label>
                    <textarea name="gg" rows="5" id="gg" class="form-control" placeholder="请填写网站公告"><?php echo $conf['gg']; ?></textarea>
                    <small>请填写网站公告内容</small>
                </div>
                <div class="form-group">
                    <label for="qqh">站长QQ</label>
                    <input type="text" name="qqh" id="qqh" value="<?php echo $conf['qqh']; ?>" class="form-control" placeholder="请填写您的QQ号">
                    <small>请填写站长QQ号</small>
                </div>
                <div class="form-group">
                    <label>后台验证码开关</label>
                    <div style="display: flex; align-items: center; gap: 8px;">
                        <input type="checkbox" id="yzm" name="yzm" <?php if($conf['yzm']=='true')echo 'checked';?> style="width: auto;">
                        <label for="yzm" style="margin: 0; font-weight: 400;">开启验证码</label>
                    </div>
                </div>
                <div class="form-group">
                    <label>主机邮箱绑定开关</label>
                    <div style="display: flex; align-items: center; gap: 8px;">
                        <input type="checkbox" id="zjyxbd" name="zjyxbd" <?php if($conf['zjyxbd']=='true')echo 'checked';?> style="width: auto;">
                        <label for="zjyxbd" style="margin: 0; font-weight: 400;">开启邮箱绑定</label>
                    </div>
                </div>
                <button type="button" class="btn btn-primary" onclick="setwz(this)">
                    <i class="fas fa-check"></i> 确认修改
                </button>
            </form>
        </div>
        
        <script>
        function setwz(btn) {
            var $btn = $(btn);
            var originalText = $btn.html();
            $btn.addClass('btn-loading').prop('disabled', true);
            
            $.ajax({
                type: 'POST',
                url: 'ajax.php',
                data: $('#wzForm').serialize() + '&egn=setwz',
                dataType: 'json',
                success: function(data) {
                    $btn.removeClass('btn-loading').prop('disabled', false).html(originalText);
                    alert(data.code);
                    if(data.code == '修改成功') {
                        location.reload();
                    }
                },
                error: function() {
                    $btn.removeClass('btn-loading').prop('disabled', false).html(originalText);
                    alert('操作失败，请重试');
                }
            });
        }
        
        function verifyAdminEmail(btn) {
            var $btn = $(btn);
            var originalText = $btn.html();
            var adminEmail = $('#admin_email').val().trim();
            
            if(!adminEmail) {
                showAlert('请先填写管理员邮箱', 'error');
                return;
            }
            
            if(!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(adminEmail)) {
                showAlert('请输入有效的邮箱地址', 'error');
                return;
            }
            
            $btn.addClass('btn-loading').prop('disabled', true);
            
            $.ajax({
                type: 'POST',
                url: 'ajax.php',
                data: {
                    egn: 'verify_admin_email',
                    admin_email: adminEmail
                },
                dataType: 'json',
                success: function(data) {
                    $btn.removeClass('btn-loading').prop('disabled', false).html(originalText);
                    if(data.code === 'success') {
                        showAlert(data.message, 'success');
                    } else {
                        showAlert(data.message, 'error');
                    }
                },
                error: function() {
                    $btn.removeClass('btn-loading').prop('disabled', false).html(originalText);
                    showAlert('发送失败，请检查网络连接', 'error');
                }
            });
        }
        
        function showAlert(message, type) {
            var alertBox = $('#emailVerifyAlert');
            alertBox.removeClass('alert-success alert-danger').addClass('alert-' + type);
            alertBox.text(message);
            alertBox.show();
            setTimeout(() => {
                alertBox.hide();
            }, 8000);
        }
        </script>
        
        <?php elseif ($gn == 'api'): ?>
        <div class="card">
            <h3><i class="fas fa-plug"></i> API设置</h3>
            <form id="apiForm">
                <div class="form-group">
                    <label for="api">API密钥</label>
                    <div style="display: flex; gap: 10px;">
                        <input type="text" name="api" id="api" value="<?php echo $conf['api']; ?>" class="form-control" placeholder="请输入API密钥" style="flex: 1;">
                        <button type="button" class="btn btn-secondary" onclick="generateApiKey()" style="white-space: nowrap;">
                            <i class="fas fa-random"></i> 生成密钥
                        </button>
                    </div>
                    <small>请输入API密钥</small>
                </div>
                <div class="form-group">
                    <label>API开关</label>
                    <div style="display: flex; align-items: center; gap: 8px;">
                        <input type="checkbox" id="apiqk" name="apiqk" <?php if($conf['apiqk']=='true')echo 'checked';?> style="width: auto;">
                        <label for="apiqk" style="margin: 0; font-weight: 400;">开启API</label>
                    </div>
                </div>
                <div class="form-group">
                    <label for="hxu">默认PHP版本</label>
                    <select name="hxu" id="hxu" class="form-control">
                        <option value="56" <?php if($conf['hxu']=='56')echo 'selected';?>>PHP 5.6</option>
                        <option value="70" <?php if($conf['hxu']=='70')echo 'selected';?>>PHP 7.0</option>
                        <option value="71" <?php if($conf['hxu']=='71')echo 'selected';?>>PHP 7.1</option>
                        <option value="72" <?php if($conf['hxu']=='72')echo 'selected';?>>PHP 7.2</option>
                        <option value="73" <?php if($conf['hxu']=='73')echo 'selected';?>>PHP 7.3</option>
                        <option value="74" <?php if($conf['hxu']=='74')echo 'selected';?>>PHP 7.4</option>
                        <option value="80" <?php if($conf['hxu']=='80')echo 'selected';?>>PHP 8.0</option>
                    </select>
                </div>
                <div class="form-group">
                    <label for="hxi">Linux建站目录</label>
                    <input type="text" name="hxi" id="hxi" value="<?php echo $conf['hxi']; ?>" class="form-control" placeholder="/www/wwwroot">
                    <small>请输入Linux建站目录</small>
                </div>
                <div class="form-group">
                    <label for="hxo">Windows建站目录</label>
                    <input type="text" name="hxo" id="hxo" value="<?php echo $conf['hxo']; ?>" class="form-control" placeholder="D:/wwwroot">
                    <small>请输入Windows建站目录</small>
                </div>
                <button type="button" class="btn btn-primary" onclick="setapi(this)">
                    <i class="fas fa-check"></i> 确认修改
                </button>
            </form>
        </div>
        
        <script>
        // 生成随机API密钥
        function generateApiKey() {
            var chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
            var length = 32;
            var result = '';
            for (var i = 0; i < length; i++) {
                result += chars.charAt(Math.floor(Math.random() * chars.length));
            }
            $('#api').val(result);
        }
        
        function setapi(btn) {
            var $btn = $(btn);
            var originalText = $btn.html();
            $btn.addClass('btn-loading').prop('disabled', true);
            
            $.ajax({
                type: 'POST',
                url: 'ajax.php',
                data: $('#apiForm').serialize() + '&egn=setapi',
                dataType: 'json',
                success: function(data) {
                    $btn.removeClass('btn-loading').prop('disabled', false).html(originalText);
                    alert(data.code);
                    if(data.code == '修改成功') {
                        location.reload();
                    }
                },
                error: function() {
                    $btn.removeClass('btn-loading').prop('disabled', false).html(originalText);
                    alert('操作失败，请重试');
                }
            });
        }
        </script>
        
        <?php elseif ($gn == 'kzmb'): ?>
        <style>
            .switch-group { padding: 20px 0; border-bottom: 1px solid #e8eaed; margin-bottom: 0; }
            .switch-group:first-of-type { padding-top: 10px; }
            .switch-group:last-of-type { border-bottom: none; padding-bottom: 10px; }
            .switch-label { font-size: 16px; font-weight: 600; color: #202124; margin-bottom: 12px; display: block; }
            .switch-item { display: flex; align-items: center; gap: 12px; cursor: pointer; }
            .switch-item input[type="checkbox"] { width: 20px; height: 20px; margin: 0; flex-shrink: 0; }
            .switch-text { font-size: 15px; color: #5f6368; line-height: 1.5; font-weight: 400; }
            .switch-text small { display: inline; font-size: 13px; color: #80868b; margin-left: 6px; }
            @media (max-width: 768px) {
                .switch-group { padding: 18px 0; }
                .switch-label { font-size: 15px; margin-bottom: 10px; }
                .switch-item { gap: 14px; }
                .switch-item input[type="checkbox"] { width: 24px; height: 24px; min-width: 24px; }
                .switch-text { font-size: 14px; }
                .switch-text small { display: block; margin-top: 6px; margin-left: 0; font-size: 12px; }
            }
        </style>
        <div class="card">
            <h3><i class="fas fa-desktop"></i> 控制面板设置</h3>
            <form id="kzmbForm" enctype="multipart/form-data">
                <div class="form-group">
                    <label for="name">控制面板名称</label>
                    <input type="text" name="name" id="name" value="<?php echo $conf['name']; ?>" class="form-control" placeholder="用户控制面板">
                    <small>请输入控制面板名称</small>
                </div>
                
                <div class="form-group switch-group">
                    <label class="switch-label">控制面板验证码开关</label>
                    <label class="switch-item">
                        <input type="checkbox" id="yzme" name="yzme" <?php if($conf['yzme']=='true')echo 'checked';?>>
                        <span class="switch-text">开启验证码</span>
                    </label>
                </div>
                
                <div class="form-group switch-group">
                    <label class="switch-label">控制面板开关</label>
                    <label class="switch-item">
                        <input type="checkbox" id="kzmbqk" name="kzmbqk" <?php if($conf['kzmbqk']=='true')echo 'checked';?>>
                        <span class="switch-text">开启控制面板</span>
                    </label>
                </div>
                
                <div class="form-group switch-group">
                    <label class="switch-label">用户登录邮箱验证开关</label>
                    <label class="switch-item">
                        <input type="checkbox" id="login_email_verify" name="login_email_verify" <?php if(isset($conf['login_email_verify']) && $conf['login_email_verify']=='true')echo 'checked';?>>
                        <span class="switch-text">开启登录邮箱验证<small>（未绑定邮箱的用户登录后需要验证邮箱）</small></span>
                    </label>
                </div>
                
                <button type="button" class="btn btn-primary" onclick="setkzmb(this)">
                    <i class="fas fa-check"></i> 确认修改
                </button>
            </form>
        </div>
        
        <script>
        function setkzmb(btn) {
            var $btn = $(btn);
            var originalText = $btn.html();
            $btn.addClass('btn-loading').prop('disabled', true);
            
            var formData = new FormData($('#kzmbForm')[0]);
            formData.append('egn', 'setkzmb');
            
            $.ajax({
                type: 'POST',
                url: 'ajax.php',
                data: formData,
                processData: false,
                contentType: false,
                dataType: 'json',
                success: function(data) {
                    $btn.removeClass('btn-loading').prop('disabled', false).html(originalText);
                    alert(data.code);
                    if(data.code == '修改成功') {
                        location.reload();
                    }
                },
                error: function() {
                    $btn.removeClass('btn-loading').prop('disabled', false).html(originalText);
                    alert('操作失败，请重试');
                }
            });
        }
        </script>
        
        <?php elseif ($gn == 'theme'): ?>
        <?php
        // 使用新的模板系统
        require_once __DIR__ . '/../user/TemplateManager.php';
        $templates = TemplateManager::getAllTemplates();
        $current_theme = isset($conf['theme']) && !empty($conf['theme']) ? $conf['theme'] : 'default';
        ?>
        <style>
            .theme-list {
                display: flex;
                flex-wrap: wrap;
                gap: 15px;
                margin-top: 15px;
            }
            .theme-item {
                border: 2px solid #ddd;
                border-radius: 10px;
                padding: 0;
                cursor: pointer;
                width: 160px;
                text-align: center;
                background: #fff;
                overflow: hidden;
                transition: all 0.3s ease;
                box-shadow: 0 2px 6px rgba(0,0,0,0.06);
            }
            .theme-item:hover {
                transform: translateY(-3px);
                box-shadow: 0 6px 18px rgba(0,0,0,0.1);
            }
            .theme-item.active {
                border-color: #188038;
                background: #f0f9f0;
            }
            .theme-preview-img {
                width: 100%;
                height: 90px;
                object-fit: cover;
                border-bottom: 1px solid #eee;
                display: block;
            }
            .theme-preview-placeholder {
                width: 100%;
                height: 90px;
                display: flex;
                align-items: center;
                justify-content: center;
                font-size: 36px;
                border-bottom: 1px solid #eee;
            }
            .theme-info {
                padding: 8px;
            }
            .theme-name {
                font-weight: 600;
                font-size: 13px;
                margin-bottom: 3px;
                color: #333;
            }
            .theme-desc {
                font-size: 10px;
                color: #666;
                margin-bottom: 4px;
                height: 28px;
                overflow: hidden;
                line-height: 1.4;
            }
            .theme-author {
                font-size: 10px;
                color: #999;
                margin-bottom: 8px;
            }
            .theme-badge {
                display: inline-block;
                padding: 3px 10px;
                border-radius: 10px;
                font-size: 10px;
                font-weight: 500;
            }
            .theme-radio {
                margin-top: 8px;
                padding-top: 8px;
                border-top: 1px solid #eee;
            }
            .theme-radio input[type="radio"] {
                width: 16px;
                height: 16px;
                cursor: pointer;
            }
            .theme-preview-img {
                cursor: pointer;
                transition: transform 0.2s;
            }
            .theme-preview-img:hover {
                transform: scale(1.02);
            }
            /* 图片预览模态框 */
            .image-preview-modal {
                display: none;
                position: fixed;
                top: 0;
                left: 0;
                right: 0;
                bottom: 0;
                background: rgba(0,0,0,0.85);
                z-index: 10000;
                justify-content: center;
                align-items: center;
                padding: 40px;
            }
            .image-preview-modal.active {
                display: flex;
            }
            .image-preview-modal img {
                max-width: 90%;
                max-height: 90%;
                border-radius: 8px;
                box-shadow: 0 20px 60px rgba(0,0,0,0.5);
            }
            .image-preview-modal .close-btn {
                position: absolute;
                top: 20px;
                right: 30px;
                color: #fff;
                font-size: 36px;
                cursor: pointer;
                opacity: 0.8;
                transition: opacity 0.2s;
            }
            .image-preview-modal .close-btn:hover {
                opacity: 1;
            }
            .image-preview-modal .image-title {
                position: absolute;
                bottom: 20px;
                left: 50%;
                transform: translateX(-50%);
                color: #fff;
                font-size: 16px;
                background: rgba(0,0,0,0.6);
                padding: 8px 20px;
                border-radius: 20px;
            }
        </style>
        <div class="card">
            <h3><i class="fas fa-palette"></i> 主题模板设置</h3>
            <form id="themeForm">
                <div class="form-group">
                    <label>选择主题模板</label>
                    <div class="theme-list">
                        <?php foreach ($templates as $id => $template): 
                            $primaryColor = $template['options']['primary_color'] ?? '#188038';
                            $isActive = ($current_theme == $id);
                            $previewImg = isset($template['preview']) ? $template['preview'] : '';
                            $previewPath = '/templates/' . $id . '/' . $previewImg;
                            $hasPreview = !empty($previewImg) && file_exists(__DIR__ . '/../templates/' . $id . '/' . $previewImg);
                            // 添加文件修改时间戳，防止缓存
                            if ($hasPreview) {
                                $previewFullPath = __DIR__ . '/../templates/' . $id . '/' . $previewImg;
                                $previewPath .= '?v=' . filemtime($previewFullPath);
                            }
                        ?>
                        <div class="theme-item <?php echo $isActive ? 'active' : ''; ?>" data-theme="<?php echo $id; ?>" onclick="selectTheme('<?php echo $id; ?>')">
                            <?php if ($hasPreview): ?>
                            <img src="<?php echo $previewPath; ?>" alt="<?php echo htmlspecialchars($template['name'] ?? $id); ?>" class="theme-preview-img" onclick="event.stopPropagation(); showThemePreview('<?php echo $previewPath; ?>', '<?php echo htmlspecialchars($template['name'] ?? $id); ?>')">
                            <?php else: ?>
                            <div class="theme-preview-placeholder" style="background: linear-gradient(135deg, <?php echo $primaryColor; ?> 0%, <?php echo adjustBrightness($primaryColor, 30); ?> 100%); color: white;">
                                <i class="fas fa-palette"></i>
                            </div>
                            <?php endif; ?>
                            <div class="theme-info">
                                <div class="theme-name"><?php echo htmlspecialchars($template['name'] ?? $id); ?></div>
                                <div class="theme-desc"><?php echo htmlspecialchars($template['description'] ?? ''); ?></div>
                                <div class="theme-author">作者: <?php echo htmlspecialchars($template['author'] ?? 'Unknown'); ?> | v<?php echo htmlspecialchars($template['version'] ?? '1.0.0'); ?></div>
                                <?php if ($isActive): ?>
                                <span class="theme-badge" style="background: <?php echo $primaryColor; ?>; color: #fff;">当前使用</span>
                                <?php endif; ?>
                                <div class="theme-radio">
                                    <input type="radio" name="theme" value="<?php echo $id; ?>" <?php echo $isActive ? 'checked' : ''; ?>>
                                </div>
                            </div>
                        </div>
                        <?php endforeach; ?>
                    </div>
                    <small style="margin-top: 15px; display: block; color: #666;"><i class="fas fa-info-circle"></i> 点击选择要使用的主题模板，然后点击确认修改保存。切换后所有用户界面将更新。</small>
                </div>
                <button type="button" class="btn btn-primary" onclick="settheme(this)">
                    <i class="fas fa-check"></i> 确认修改
                </button>
            </form>
        </div>
        
        <!-- 图片预览模态框 -->
        <div class="image-preview-modal" id="themePreviewModal" onclick="closeThemePreview()">
            <span class="close-btn">&times;</span>
            <img src="" alt="" id="themePreviewImg">
            <div class="image-title" id="themePreviewTitle"></div>
        </div>
        
        <script>
        function selectTheme(theme) {
            $('input[name="theme"][value="' + theme + '"]').prop('checked', true);
            $('.theme-item').removeClass('active').css('border-color', '#ddd');
            $('.theme-item[data-theme="' + theme + '"]').addClass('active');
        }
        
        // 显示主题预览图片
        function showThemePreview(imgPath, themeName) {
            $('#themePreviewImg').attr('src', imgPath);
            $('#themePreviewTitle').text(themeName);
            $('#themePreviewModal').addClass('active');
        }
        
        // 关闭主题预览
        function closeThemePreview() {
            $('#themePreviewModal').removeClass('active');
        }
        
        // ESC键关闭预览
        $(document).keydown(function(e) {
            if (e.key === 'Escape') {
                closeThemePreview();
            }
        });
        
        function settheme(btn) {
            var $btn = $(btn);
            var originalText = $btn.html();
            $btn.addClass('btn-loading').prop('disabled', true);
            
            $.ajax({
                type: 'POST',
                url: 'ajax.php',
                data: $('#themeForm').serialize() + '&egn=settheme',
                dataType: 'json',
                success: function(data) {
                    $btn.removeClass('btn-loading').prop('disabled', false).html(originalText);
                    alert(data.code);
                    if(data.code == '修改成功') {
                        location.reload();
                    }
                },
                error: function() {
                    $btn.removeClass('btn-loading').prop('disabled', false).html(originalText);
                    alert('操作失败，请重试');
                }
            });
        }
        </script>
        
        <?php elseif ($gn == 'yzf'): ?>
        <div class="card">
            <h3><i class="fas fa-credit-card"></i> 支付设置</h3>
            <form id="zfForm">
                <div class="form-group">
                    <label for="hxe">易支付地址</label>
                    <input type="text" name="hxe" id="hxe" value="<?php echo $conf['hxe']; ?>" class="form-control" placeholder="https://pay.example.com">
                    <small>请输入易支付地址</small>
                </div>
                <div class="form-group">
                    <label for="hxr">易支付ID</label>
                    <input type="text" name="hxr" id="hxr" value="<?php echo $conf['hxr']; ?>" class="form-control" placeholder="商户ID">
                    <small>请输入易支付商户ID</small>
                </div>
                <div class="form-group">
                    <label for="hxt">易支付Key</label>
                    <input type="text" name="hxt" id="hxt" value="<?php echo $conf['hxt']; ?>" class="form-control" placeholder="商户Key">
                    <small>请输入易支付商户Key</small>
                </div>
                <div class="form-group">
                    <label>启用支付方式</label>
                    <div class="d-flex flex-wrap gap-3">
                        <label class="d-flex align-items-center mr-3" style="cursor: pointer;">
                            <input type="checkbox" name="pay_alipay" id="pay_alipay" value="1" <?php if(!isset($conf['pay_alipay']) || $conf['pay_alipay'] !== 'false')echo 'checked';?> style="margin-right: 8px;">
                            <i class="fab fa-alipay text-primary mr-1"></i> 支付宝
                        </label>
                        <label class="d-flex align-items-center mr-3" style="cursor: pointer;">
                            <input type="checkbox" name="pay_wechat" id="pay_wechat" value="1" <?php if(!isset($conf['pay_wechat']) || $conf['pay_wechat'] !== 'false')echo 'checked';?> style="margin-right: 8px;">
                            <i class="fab fa-weixin text-success mr-1"></i> 微信支付
                        </label>
                    </div>
                    <small>勾选要启用的支付方式，至少启用一种</small>
                </div>

                <button type="button" class="btn btn-primary" onclick="setzf(this)">
                    <i class="fas fa-check"></i> 确认修改
                </button>
            </form>
        </div>
        
        <script>
        // 简单的MD5实现
        var MD5 = function(string) {
            function RotateLeft(lValue, iShiftBits) {
                return (lValue << iShiftBits) | (lValue >>> (32 - iShiftBits));
            }
            function AddUnsigned(lX, lY) {
                var lX4, lY4, lX8, lY8, lResult;
                lX8 = (lX & 0x80000000);
                lY8 = (lY & 0x80000000);
                lX4 = (lX & 0x40000000);
                lY4 = (lY & 0x40000000);
                lResult = (lX & 0x3FFFFFFF) + (lY & 0x3FFFFFFF);
                if (lX4 & lY4) {
                    return (lResult ^ 0x80000000 ^ lX8 ^ lY8);
                }
                if (lX4 | lY4) {
                    if (lResult & 0x40000000) {
                        return (lResult ^ 0xC0000000 ^ lX8 ^ lY8);
                    } else {
                        return (lResult ^ 0x40000000 ^ lX8 ^ lY8);
                    }
                } else {
                    return (lResult ^ lX8 ^ lY8);
                }
            }
            function F(x, y, z) { return (x & y) | ((~x) & z); }
            function G(x, y, z) { return (x & z) | (y & (~z)); }
            function H(x, y, z) { return (x ^ y ^ z); }
            function I(x, y, z) { return (y ^ (x | (~z))); }
            function FF(a, b, c, d, x, s, ac) {
                a = AddUnsigned(a, AddUnsigned(AddUnsigned(F(b, c, d), x), ac));
                return AddUnsigned(RotateLeft(a, s), b);
            }
            function GG(a, b, c, d, x, s, ac) {
                a = AddUnsigned(a, AddUnsigned(AddUnsigned(G(b, c, d), x), ac));
                return AddUnsigned(RotateLeft(a, s), b);
            }
            function HH(a, b, c, d, x, s, ac) {
                a = AddUnsigned(a, AddUnsigned(AddUnsigned(H(b, c, d), x), ac));
                return AddUnsigned(RotateLeft(a, s), b);
            }
            function II(a, b, c, d, x, s, ac) {
                a = AddUnsigned(a, AddUnsigned(AddUnsigned(I(b, c, d), x), ac));
                return AddUnsigned(RotateLeft(a, s), b);
            }
            function ConvertToWordArray(string) {
                var lWordCount;
                var lMessageLength = string.length;
                var lNumberOfWords_temp1 = lMessageLength + 8;
                var lNumberOfWords_temp2 = (lNumberOfWords_temp1 - (lNumberOfWords_temp1 % 64)) / 64;
                var lNumberOfWords = (lNumberOfWords_temp2 + 1) * 16;
                var lWordArray = Array(lNumberOfWords - 1);
                var lBytePosition = 0;
                var lByteCount = 0;
                while (lByteCount < lMessageLength) {
                    lWordCount = (lByteCount - (lByteCount % 4)) / 4;
                    lBytePosition = (lByteCount % 4) * 8;
                    lWordArray[lWordCount] = (lWordArray[lWordCount] | (string.charCodeAt(lByteCount) << lBytePosition));
                    lByteCount++;
                }
                lWordCount = (lByteCount - (lByteCount % 4)) / 4;
                lBytePosition = (lByteCount % 4) * 8;
                lWordArray[lWordCount] = lWordArray[lWordCount] | (0x80 << lBytePosition);
                lWordArray[lNumberOfWords - 2] = lMessageLength << 3;
                lWordArray[lNumberOfWords - 1] = lMessageLength >>> 29;
                return lWordArray;
            }
            function WordToHex(lValue) {
                var WordToHexValue = "", WordToHexValue_temp = "", lByte, lCount;
                for (lCount = 0; lCount <= 3; lCount++) {
                    lByte = (lValue >>> (lCount * 8)) & 255;
                    WordToHexValue_temp = "0" + lByte.toString(16);
                    WordToHexValue = WordToHexValue + WordToHexValue_temp.substr(WordToHexValue_temp.length - 2, 2);
                }
                return WordToHexValue;
            }
            var x = Array();
            var k, AA, BB, CC, DD, a, b, c, d;
            var S11 = 7, S12 = 12, S13 = 17, S14 = 22;
            var S21 = 5, S22 = 9, S23 = 14, S24 = 20;
            var S31 = 4, S32 = 11, S33 = 16, S34 = 23;
            var S41 = 6, S42 = 10, S43 = 15, S44 = 21;
            x = ConvertToWordArray(string);
            a = 0x67452301; b = 0xEFCDAB89; c = 0x98BADCFE; d = 0x10325476;
            for (k = 0; k < x.length; k += 16) {
                AA = a; BB = b; CC = c; DD = d;
                a = FF(a, b, c, d, x[k + 0], S11, 0xD76AA478);
                d = FF(d, a, b, c, x[k + 1], S12, 0xE8C7B756);
                c = FF(c, d, a, b, x[k + 2], S13, 0x242070DB);
                b = FF(b, c, d, a, x[k + 3], S14, 0xC1BDCEEE);
                a = FF(a, b, c, d, x[k + 4], S11, 0xF57C0FAF);
                d = FF(d, a, b, c, x[k + 5], S12, 0x4787C62A);
                c = FF(c, d, a, b, x[k + 6], S13, 0xA8304613);
                b = FF(b, c, d, a, x[k + 7], S14, 0xFD469501);
                a = FF(a, b, c, d, x[k + 8], S11, 0x698098D8);
                d = FF(d, a, b, c, x[k + 9], S12, 0x8B44F7AF);
                c = FF(c, d, a, b, x[k + 10], S13, 0xFFFF5BB1);
                b = FF(b, c, d, a, x[k + 11], S14, 0x895CD7BE);
                a = FF(a, b, c, d, x[k + 12], S11, 0x6B901122);
                d = FF(d, a, b, c, x[k + 13], S12, 0xFD987193);
                c = FF(c, d, a, b, x[k + 14], S13, 0xA679438E);
                b = FF(b, c, d, a, x[k + 15], S14, 0x49B40821);
                a = GG(a, b, c, d, x[k + 1], S21, 0xF61E2562);
                d = GG(d, a, b, c, x[k + 6], S22, 0xC040B340);
                c = GG(c, d, a, b, x[k + 11], S23, 0x265E5A51);
                b = GG(b, c, d, a, x[k + 0], S24, 0xE9B6C7AA);
                a = GG(a, b, c, d, x[k + 5], S21, 0xD62F105D);
                d = GG(d, a, b, c, x[k + 10], S22, 0x2441453);
                c = GG(c, d, a, b, x[k + 15], S23, 0xD8A1E681);
                b = GG(b, c, d, a, x[k + 4], S24, 0xE7D3FBC8);
                a = GG(a, b, c, d, x[k + 9], S21, 0x21E1CDE6);
                d = GG(d, a, b, c, x[k + 14], S22, 0xC33707D6);
                c = GG(c, d, a, b, x[k + 3], S23, 0xF4D50D87);
                b = GG(b, c, d, a, x[k + 8], S24, 0x455A14ED);
                a = GG(a, b, c, d, x[k + 13], S21, 0xA9E3E905);
                d = GG(d, a, b, c, x[k + 2], S22, 0xFCEFA3F8);
                c = GG(c, d, a, b, x[k + 7], S23, 0x676F02D9);
                b = GG(b, c, d, a, x[k + 12], S24, 0x8D2A4C8A);
                a = HH(a, b, c, d, x[k + 5], S31, 0xFFFA3942);
                d = HH(d, a, b, c, x[k + 8], S32, 0x8771F681);
                c = HH(c, d, a, b, x[k + 11], S33, 0x6D9D6122);
                b = HH(b, c, d, a, x[k + 14], S34, 0xFDE5380C);
                a = HH(a, b, c, d, x[k + 1], S31, 0xA4BEEA44);
                d = HH(d, a, b, c, x[k + 4], S32, 0x4BDECFA9);
                c = HH(c, d, a, b, x[k + 7], S33, 0xF6BB4B60);
                b = HH(b, c, d, a, x[k + 10], S34, 0xBEBFBC70);
                a = HH(a, b, c, d, x[k + 13], S31, 0x289B7EC6);
                d = HH(d, a, b, c, x[k + 0], S32, 0xEAA127FA);
                c = HH(c, d, a, b, x[k + 3], S33, 0xD4EF3085);
                b = HH(b, c, d, a, x[k + 6], S34, 0x4881D05);
                a = HH(a, b, c, d, x[k + 9], S31, 0xD9D4D039);
                d = HH(d, a, b, c, x[k + 12], S32, 0xE6DB99E5);
                c = HH(c, d, a, b, x[k + 15], S33, 0x1FA27CF8);
                b = HH(b, c, d, a, x[k + 2], S34, 0xC4AC5665);
                a = II(a, b, c, d, x[k + 0], S41, 0xF4292244);
                d = II(d, a, b, c, x[k + 7], S42, 0x432AFF97);
                c = II(c, d, a, b, x[k + 14], S43, 0xAB9423A7);
                b = II(b, c, d, a, x[k + 5], S44, 0xFC93A039);
                a = II(a, b, c, d, x[k + 12], S41, 0x655B59C3);
                d = II(d, a, b, c, x[k + 3], S42, 0x8F0CCC92);
                c = II(c, d, a, b, x[k + 10], S43, 0xFFEFF47D);
                b = II(b, c, d, a, x[k + 1], S44, 0x85845DD1);
                a = II(a, b, c, d, x[k + 8], S41, 0x6FA87E4F);
                d = II(d, a, b, c, x[k + 15], S42, 0xFE2CE6E0);
                c = II(c, d, a, b, x[k + 6], S43, 0xA3014314);
                b = II(b, c, d, a, x[k + 13], S44, 0x4E0811A1);
                a = II(a, b, c, d, x[k + 4], S41, 0xF7537E82);
                d = II(d, a, b, c, x[k + 11], S42, 0xBD3AF235);
                c = II(c, d, a, b, x[k + 2], S43, 0x2AD7D2BB);
                b = II(b, c, d, a, x[k + 9], S44, 0xEB86D391);
                a = AddUnsigned(a, AA);
                b = AddUnsigned(b, BB);
                c = AddUnsigned(c, CC);
                d = AddUnsigned(d, DD);
            }
            return (WordToHex(a) + WordToHex(b) + WordToHex(c) + WordToHex(d)).toLowerCase();
        }
        
        function setzf(btn) {
            var $btn = $(btn);
            var originalText = $btn.html();
            $btn.addClass('btn-loading').prop('disabled', true);
            
            $.ajax({
                type: 'POST',
                url: 'ajax.php',
                data: $('#zfForm').serialize() + '&egn=setzf',
                dataType: 'json',
                success: function(data) {
                    $btn.removeClass('btn-loading').prop('disabled', false).html(originalText);
                    alert(data.code);
                    if(data.code == '修改成功') {
                        location.reload();
                    }
                },
                error: function() {
                    $btn.removeClass('btn-loading').prop('disabled', false).html(originalText);
                    alert('操作失败，请重试');
                }
            });
        }
        
        </script>
        
        <?php elseif ($gn == 'gl'): ?>
        <div style="display: grid; grid-template-columns: 1fr 1fr; gap: 20px;">
            <!-- 左侧：管理员账号 -->
            <div class="card">
                <h3><i class="fas fa-user-shield"></i> 管理员账号</h3>
                <p style="color: #5f6368; font-size: 13px; margin-bottom: 16px;">修改后台登录账号和密码</p>
                <form id="glForm">
                    <div style="display: grid; grid-template-columns: 1fr 1fr; gap: 15px;">
                        <div class="form-group" style="margin-bottom: 15px;">
                            <label for="olduser">原账号</label>
                            <input type="text" name="olduser" id="olduser" class="form-control" placeholder="当前账号" required>
                        </div>
                        <div class="form-group" style="margin-bottom: 15px;">
                            <label for="oldpass">原密码</label>
                            <input type="password" name="oldpass" id="oldpass" class="form-control" placeholder="当前密码" required>
                        </div>
                    </div>
                    <div style="display: grid; grid-template-columns: 1fr 1fr; gap: 15px;">
                        <div class="form-group" style="margin-bottom: 15px;">
                            <label for="newuser">新账号</label>
                            <input type="text" name="newuser" id="newuser" class="form-control" placeholder="新账号" required>
                        </div>
                        <div class="form-group" style="margin-bottom: 15px;">
                            <label for="newpass">新密码</label>
                            <input type="password" name="newpass" id="newpass" class="form-control" placeholder="新密码" required>
                        </div>
                    </div>
                    <button type="button" class="btn btn-primary" onclick="setgl(this)" style="width: 100%;">
                        <i class="fas fa-check"></i> 保存账号密码
                    </button>
                </form>
            </div>
            
            <!-- 右侧：后台邮箱验证设置 -->
            <div class="card">
                <h3><i class="fas fa-envelope-shield"></i> 后台访问邮箱验证</h3>
                <p style="color: #5f6368; font-size: 13px; margin-bottom: 16px;">开启后，访问后台任何页面都需要先验证邮箱验证码</p>
                <form id="adminEmailVerifyForm">
                    <div class="form-group" style="margin-bottom: 15px;">
                        <label for="admin_email">管理员邮箱</label>
                        <input type="email" name="admin_email" id="admin_email" class="form-control" value="<?php echo isset($conf['admin_email']) ? $conf['admin_email'] : ''; ?>" placeholder="admin@example.com">
                        <small style="color: #5f6368;">用于接收验证码的邮箱地址</small>
                    </div>
                    <div id="emailVerifyAlert" class="alert" style="display: none; margin-bottom: 15px;"></div>
                    <button type="button" class="btn btn-success" onclick="verifyAdminEmail(this)" style="width: 100%; margin-bottom: 15px;">
                        <i class="fas fa-paper-plane"></i> 验证邮箱
                    </button>
                    <div class="form-group" style="margin-bottom: 15px;">
                        <label>邮箱验证开关</label>
                        <div style="display: flex; align-items: center; gap: 8px;">
                            <input type="checkbox" id="admin_email_verify" name="admin_email_verify" <?php if(isset($conf['admin_email_verify']) && $conf['admin_email_verify']=='true')echo 'checked';?> style="width: auto;">
                            <label for="admin_email_verify" style="margin: 0; font-weight: 400;">开启后台访问邮箱验证</label>
                        </div>
                        <small style="color: #5f6368;">开启后每次访问后台都需要输入邮箱验证码</small>
                    </div>
                    <button type="button" class="btn btn-primary" onclick="setAdminEmailVerify(this)" style="width: 100%;">
                        <i class="fas fa-check"></i> 保存邮箱设置
                    </button>
                </form>
            </div>
        </div>
        
        <div style="display: grid; grid-template-columns: 1fr; gap: 20px; margin-top: 20px;">
            <!-- 后台目录设置 -->
            <div class="card">
                <h3><i class="fas fa-folder-cog"></i> 后台目录设置</h3>
                <p style="color: #5f6368; font-size: 13px; margin-bottom: 16px;">修改后台访问路径可提升安全性，修改后将自动跳转到新地址，请妥善保存新地址。如有定时任务（如 cleanup_temp.php），请同步更新任务中的路径。</p>
                <form id="adminDirForm">
                    <div class="form-group" style="margin-bottom: 15px;">
                        <label>当前目录</label>
                        <input type="text" class="form-control" value="<?php echo isset($conf['admin_dir']) ? $conf['admin_dir'] : 'admin'; ?>" readonly style="background: #f8f9fa;">
                        <small style="color: #5f6368;">当前后台访问路径: /<?php echo isset($conf['admin_dir']) ? $conf['admin_dir'] : 'admin'; ?>/</small>
                    </div>
                    <div class="form-group" style="margin-bottom: 15px;">
                        <label for="admin_dir">新目录名</label>
                        <input type="text" name="admin_dir" id="admin_dir" class="form-control" placeholder="如: manage、backend" required>
                        <small style="color: #5f6368;">2-30位字母、数字或下划线，不可使用: user、api、install 等保留名</small>
                    </div>
                    <button type="button" class="btn btn-success" onclick="setAdminDir(this)" style="width: 100%;">
                        <i class="fas fa-folder-open"></i> 修改后台目录
                    </button>
                </form>
            </div>
        </div>
        
        <script>
        function setgl(btn) {
            var $btn = $(btn);
            var originalText = $btn.html();
            $btn.addClass('btn-loading').prop('disabled', true);
            
            $.ajax({
                type: 'POST',
                url: 'ajax.php',
                data: $('#glForm').serialize() + '&egn=gl',
                dataType: 'json',
                success: function(data) {
                    $btn.removeClass('btn-loading').prop('disabled', false).html(originalText);
                    alert(data.code);
                    if(data.code == '修改成功') {
                        location.reload();
                    }
                },
                error: function() {
                    $btn.removeClass('btn-loading').prop('disabled', false).html(originalText);
                    alert('操作失败，请重试');
                }
            });
        }
        
        function setAdminDir(btn) {
            var $btn = $(btn);
            var originalText = $btn.html();
            var adminDir = $('#admin_dir').val().trim();
            
            if(!adminDir) {
                alert('请输入后台目录名称');
                return;
            }
            
            if(!/^[a-zA-Z0-9_-]+$/.test(adminDir)) {
                alert('目录名称只能包含字母、数字、下划线和横线');
                return;
            }
            
            if(adminDir == 'user' || adminDir == 'static' || adminDir == 'MPHX' || adminDir == 'api' || adminDir == 'install') {
                alert('该目录名称已被系统使用，请选择其他名称');
                return;
            }
            
            if(!confirm('确定要修改后台目录为 "' + adminDir + '" 吗？\n修改后将跳转到新地址登录！')) {
                return;
            }
            
            $btn.addClass('btn-loading').prop('disabled', true);
            
            $.ajax({
                type: 'POST',
                url: 'ajax.php',
                data: {egn: 'setadmin_dir', admin_dir: adminDir},
                dataType: 'json',
                success: function(data) {
                    $btn.removeClass('btn-loading').prop('disabled', false).html(originalText);
                    alert(data.message);
                    if(data.code == 200) {
                        // 跳转到新的后台地址
                        window.location.href = '/' + adminDir + '/login.php';
                    }
                },
                error: function() {
                    $btn.removeClass('btn-loading').prop('disabled', false).html(originalText);
                    alert('操作失败，请重试');
                }
            });
        }
        
        function setAdminEmailVerify(btn) {
            var $btn = $(btn);
            var originalText = $btn.html();
            var adminEmail = $('#admin_email').val().trim();
            var verifyEnabled = $('#admin_email_verify').is(':checked');
            
            if(verifyEnabled && !adminEmail) {
                alert('开启邮箱验证前请先填写管理员邮箱');
                return;
            }
            
            if(adminEmail && !/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(adminEmail)) {
                alert('请输入有效的邮箱地址');
                return;
            }
            
            $btn.addClass('btn-loading').prop('disabled', true);
            
            $.ajax({
                type: 'POST',
                url: 'ajax.php',
                data: {
                    egn: 'setadmin_email_verify',
                    admin_email: adminEmail,
                    admin_email_verify: verifyEnabled ? 'true' : 'false'
                },
                dataType: 'json',
                success: function(data) {
                    $btn.removeClass('btn-loading').prop('disabled', false).html(originalText);
                    alert(data.code);
                    if(data.code == '修改成功') {
                        location.reload();
                    }
                },
                error: function() {
                    $btn.removeClass('btn-loading').prop('disabled', false).html(originalText);
                    alert('操作失败，请重试');
                }
            });
        }
        </script>
        
        <?php elseif ($gn == 'mail'): ?>
        <style>
            /* PC端：水平排列 */
            .btn-group-vertical { display: flex; flex-direction: row; gap: 12px; }
            .btn-group-vertical .btn { min-height: 40px; font-size: 14px; border-radius: 6px; padding: 10px 20px; }
            /* 移动端：垂直排列 */
            @media (max-width: 768px) {
                .btn-group-vertical { flex-direction: column; gap: 10px; }
                .btn-group-vertical .btn { width: 100%; min-height: 48px; font-size: 16px; }
            }
        </style>
        <div class="card">
            <h3><i class="fas fa-envelope"></i> 邮箱设置</h3>
            <form id="mailForm">
                <div class="form-group">
                    <label for="mailhost">邮箱服务器</label>
                    <input type="text" name="mailhost" id="mailhost" value="<?php echo $conf['mailhost']; ?>" class="form-control" placeholder="smtp.example.com">
                    <small>请输入邮箱服务器地址</small>
                </div>
                <div class="form-group">
                    <label for="mailuser">邮箱账号</label>
                    <input type="text" name="mailuser" id="mailuser" value="<?php echo $conf['mailuser']; ?>" class="form-control" placeholder="noreply@example.com">
                    <small>请输入邮箱账号</small>
                </div>
                <div class="form-group">
                    <label for="mailpassword">邮箱密码</label>
                    <input type="password" name="mailpassword" id="mailpassword" value="" class="form-control" placeholder="邮箱密码或授权码（留空则保持原密码不变）" autocomplete="new-password" readonly onfocus="this.removeAttribute('readonly')">
                    <small>请输入邮箱密码或授权码，留空则保持原密码不变</small>
                </div>
                <div class="form-group">
                    <label for="mailport">邮箱端口</label>
                    <input type="text" name="mailport" id="mailport" value="<?php echo $conf['mailport']; ?>" class="form-control" placeholder="465">
                    <small>请输入邮箱端口</small>
                </div>
                <div class="form-group">
                    <label for="test_email">测试收件邮箱</label>
                    <input type="email" name="test_email" id="test_email" class="form-control" placeholder="test@example.com">
                    <small>请输入接收测试邮件的邮箱地址</small>
                </div>
                <div class="btn-group-vertical">
                    <button type="button" class="btn btn-primary" onclick="setmail(this)">
                        <i class="fas fa-check"></i> 确认修改
                    </button>
                    <button type="button" class="btn btn-success" onclick="testMailConfig(this)">
                        <i class="fas fa-paper-plane"></i> 发送测试邮件
                    </button>
                </div>
            </form>
        </div>
        
        <script>
        function setmail(btn) {
            var $btn = $(btn);
            var originalText = $btn.html();
            $btn.addClass('btn-loading').prop('disabled', true);
            
            $.ajax({
                type: 'POST',
                url: 'ajax.php',
                data: $('#mailForm').serialize() + '&egn=setmail',
                dataType: 'json',
                success: function(data) {
                    $btn.removeClass('btn-loading').prop('disabled', false).html(originalText);
                    alert(data.code);
                    if(data.code == '修改成功') {
                        location.reload();
                    }
                },
                error: function() {
                    $btn.removeClass('btn-loading').prop('disabled', false).html(originalText);
                    alert('操作失败，请重试');
                }
            });
        }
        
        function testMailConfig(btn) {
            var $btn = $(btn);
            var originalText = $btn.html();
            var testEmail = $('#test_email').val().trim();
            var mailpassword = $('#mailpassword').val().trim();
            
            if (!testEmail) {
                alert('请输入测试收件邮箱地址');
                $('#test_email').focus();
                return;
            }
            
            // 简单的邮箱格式验证
            var emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
            if (!emailRegex.test(testEmail)) {
                alert('请输入正确的邮箱地址');
                $('#test_email').focus();
                return;
            }
            
            $btn.addClass('btn-loading').prop('disabled', true);
            
            // 如果密码框为空，从序列化数据中移除密码字段，让后端使用数据库中的密码
            var formData = $('#mailForm').serialize();
            if (!mailpassword) {
                formData = formData.replace(/&?mailpassword=[^&]*/g, '');
            }
            
            $.ajax({
                type: 'POST',
                url: 'ajax.php',
                data: formData + '&egn=testmailconfig&test_email=' + encodeURIComponent(testEmail),
                dataType: 'json',
                success: function(data) {
                    $btn.removeClass('btn-loading').prop('disabled', false).html(originalText);
                    alert(data.code);
                },
                error: function() {
                    $btn.removeClass('btn-loading').prop('disabled', false).html(originalText);
                    alert('操作失败，请重试');
                }
            });
        }
        </script>
        
        <?php elseif ($gn == 'jk'): ?>
        <div class="card">
            <h3><i class="fas fa-chart-line"></i> 监控设置</h3>
            <form id="jkForm">
                <div class="form-group">
                    <label>域名监控开关</label>
                    <div style="display: flex; align-items: center; gap: 8px;">
                        <input type="checkbox" id="ymjkkg" name="ymjkkg" <?php if($conf['ymjkkg']=='true')echo 'checked';?> style="width: auto;">
                        <label for="ymjkkg" style="margin: 0; font-weight: 400;">开启域名监控</label>
                    </div>
                </div>
                <div class="form-group">
                    <label>文件监控开关</label>
                    <div style="display: flex; align-items: center; gap: 8px;">
                        <input type="checkbox" id="wjjkkg" name="wjjkkg" <?php if($conf['wjjkkg']=='true')echo 'checked';?> style="width: auto;">
                        <label for="wjjkkg" style="margin: 0; font-weight: 400;">开启文件监控</label>
                    </div>
                </div>
                <div class="form-group">
                    <label>每天邮箱发送开关</label>
                    <div style="display: flex; align-items: center; gap: 8px;">
                        <input type="checkbox" id="mtyxfskg" name="mtyxfskg" <?php if($conf['mtyxfskg']=='true')echo 'checked';?> style="width: auto;">
                        <label for="mtyxfskg" style="margin: 0; font-weight: 400;">开启邮件通知</label>
                    </div>
                </div>
                <div class="form-group">
                    <label for="ymjktsyz">域名监控天数阈值</label>
                    <input type="text" name="ymjktsyz" id="ymjktsyz" value="<?php echo $conf['ymjktsyz']; ?>" class="form-control" placeholder="30">
                    <small>请输入域名监控天数阈值</small>
                </div>
                <div class="form-group">
                    <label for="wjjktsyz">文件监控天数阈值</label>
                    <input type="text" name="wjjktsyz" id="wjjktsyz" value="<?php echo $conf['wjjktsyz']; ?>" class="form-control" placeholder="7">
                    <small>请输入文件监控天数阈值</small>
                </div>
                <button type="button" class="btn btn-primary" onclick="setjk(this)">
                    <i class="fas fa-check"></i> 确认修改
                </button>
            </form>
        </div>
        
        <script>
        function setjk(btn) {
            var $btn = $(btn);
            var originalText = $btn.html();
            $btn.addClass('btn-loading').prop('disabled', true);
            
            $.ajax({
                type: 'POST',
                url: 'ajax.php',
                data: $('#jkForm').serialize() + '&egn=setjk',
                dataType: 'json',
                success: function(data) {
                    $btn.removeClass('btn-loading').prop('disabled', false).html(originalText);
                    alert(data.code);
                    if(data.code == '修改成功') {
                        location.reload();
                    }
                },
                error: function() {
                    $btn.removeClass('btn-loading').prop('disabled', false).html(originalText);
                    alert('操作失败，请重试');
                }
            });
        }
        </script>
        
        <?php elseif ($gn == 'parse_domain'): ?>
        <?php
        // 获取所有宝塔服务器列表
        $bt_servers = $DB->get_all("SELECT id, ip, btmc as name FROM `bt` ORDER BY id ASC");
        // 获取已设置的解析域名
        $parse_domains = array();
        $domain_rows = $DB->get_all("SELECT bt_id, parse_domain FROM `server_parse_domain`");
        if ($domain_rows) {
            foreach ($domain_rows as $row) {
                $parse_domains[$row['bt_id']] = $row['parse_domain'];
            }
        }
        ?>
        <div class="card">
            <h3><i class="fas fa-globe"></i> 用户解析域名设置</h3>
            <p style="color: #666; margin-bottom: 20px;">为每个服务器设置用户绑定的域名需要解析到的目标地址。留空则显示服务器IP。</p>
            
            <form id="parseDomainForm">
                <div class="table-responsive">
                    <table class="table table-bordered table-hover">
                        <thead>
                            <tr>
                                <th style="width: 60px;">ID</th>
                                <th style="width: 150px;">服务器名称</th>
                                <th style="width: 150px;">服务器IP</th>
                                <th>用户解析域名</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($bt_servers as $server): ?>
                            <tr>
                                <td><?php echo $server['id']; ?></td>
                                <td><?php echo htmlspecialchars($server['name'] ?? '未命名'); ?></td>
                                <td><?php echo htmlspecialchars($server['ip']); ?></td>
                                <td>
                                    <input type="text" 
                                           name="parse_domain[<?php echo $server['id']; ?>]" 
                                           value="<?php echo htmlspecialchars($parse_domains[$server['id']] ?? ''); ?>" 
                                           class="form-control" 
                                           placeholder="例如: www.ert.com">
                                </td>
                            </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>
                
                <div class="alert alert-info" style="margin-top: 20px;">
                    <i class="fas fa-info-circle"></i> <strong>说明：</strong>
                    <ul style="margin: 10px 0 0 20px; padding: 0;">
                        <li>设置后，对应服务器下的用户在域名管理页面将看到设置的解析地址</li>
                        <li>留空则显示该服务器的IP地址</li>
                        <li>用户将看到 "请将域名解析到: [此地址]"</li>
                    </ul>
                </div>
                
                <button type="button" class="btn btn-primary" onclick="saveParseDomain(this)" style="margin-top: 15px;">
                    <i class="fas fa-check"></i> 保存设置
                </button>
            </form>
        </div>
        
        <script>
        function saveParseDomain(btn) {
            var $btn = $(btn);
            var originalText = $btn.html();
            $btn.addClass('btn-loading').prop('disabled', true);
            
            $.ajax({
                type: 'POST',
                url: 'ajax.php',
                data: $('#parseDomainForm').serialize() + '&egn=save_server_parse_domain',
                dataType: 'json',
                success: function(data) {
                    $btn.removeClass('btn-loading').prop('disabled', false).html(originalText);
                    alert(data.code);
                    if(data.code == '保存成功') {
                        location.reload();
                    }
                },
                error: function() {
                    $btn.removeClass('btn-loading').prop('disabled', false).html(originalText);
                    alert('操作失败，请重试');
                }
            });
        }
        </script>
        
        <?php endif; ?>
    </div>
    
    <script src="/static/js/jquery.min.js"></script>
</body>
</html>