<?php
ob_start();
requireAdmin();
require_once __DIR__ . '/../../includes/LicenseClient.php';

$success = '';
$error = '';

// 处理表单提交
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $action = $_POST['action'] ?? '';
    
    if ($action === 'save_site') {
        // 保存网站基本配置
        $site_name = trim($_POST['site_name'] ?? '');
        $site_keywords = trim($_POST['site_keywords'] ?? '');
        $site_icp = trim($_POST['site_icp'] ?? '');
        $site_copyright = trim($_POST['site_copyright'] ?? '');
        $sys_version = trim($_POST['sys_version'] ?? '1.0.0');
        $register_enabled = isset($_POST['register_enabled']) ? '1' : '0';
        
        updateSiteConfig('site_name', $site_name);
        updateSiteConfig('site_keywords', $site_keywords);
        updateSiteConfig('site_icp', $site_icp);
        updateSiteConfig('site_copyright', $site_copyright);
        updateSiteConfig('sys_version', $sys_version);
        updateSiteConfig('register_enabled', $register_enabled);
        
        // 处理网站LOGO上传
        if (isset($_FILES['site_logo']) && $_FILES['site_logo']['error'] === UPLOAD_ERR_OK) {
            $file = $_FILES['site_logo'];
            $allowed_types = ['image/jpeg', 'image/png', 'image/gif', 'image/webp'];
            $max_size = 2 * 1024 * 1024;
            
            if (in_array($file['type'], $allowed_types) && $file['size'] <= $max_size) {
                $ext = pathinfo($file['name'], PATHINFO_EXTENSION);
                $filename = 'logo_' . time() . '.' . $ext;
                $target = UPLOAD_DIR . $filename;
                
                if (move_uploaded_file($file['tmp_name'], $target)) {
                    updateSiteConfig('site_logo', UPLOAD_URL . $filename);
                }
            }
        }
        
        $success = '网站配置已保存';
    }
    
    if ($action === 'save_contact') {
        // 保存联系配置
        $contact_config = [
            'qq_group' => trim($_POST['qq_group'] ?? ''),
            'email' => trim($_POST['email'] ?? ''),
            'service_time' => trim($_POST['service_time'] ?? ''),
            'company_address' => trim($_POST['company_address'] ?? ''),
            'business_tip' => trim($_POST['business_tip'] ?? ''),
        ];
        
        // 处理微信公众号二维码上传
        if (isset($_FILES['wechat_qrcode']) && $_FILES['wechat_qrcode']['error'] === UPLOAD_ERR_OK) {
            $file = $_FILES['wechat_qrcode'];
            $allowed_types = ['image/jpeg', 'image/png', 'image/gif', 'image/webp'];
            $max_size = 2 * 1024 * 1024;
            
            if (in_array($file['type'], $allowed_types) && $file['size'] <= $max_size) {
                $ext = pathinfo($file['name'], PATHINFO_EXTENSION);
                $filename = 'wechat_qrcode.' . $ext;
                $target = UPLOAD_DIR . $filename;
                
                if (move_uploaded_file($file['tmp_name'], $target)) {
                    $contact_config['wechat_qrcode'] = UPLOAD_URL . $filename;
                }
            }
        } else {
            // 保留原有二维码
            $old_config = json_decode(getSiteConfig('contact_config') ?? '{}', true);
            $contact_config['wechat_qrcode'] = $old_config['wechat_qrcode'] ?? '';
        }
        
        updateSiteConfig('contact_config', json_encode($contact_config));
        $success = '联系配置已保存';
    }
    
    if ($action === 'save_payment') {
        // 保存支付配置
        $api_url = trim($_POST['api_url'] ?? '');
        $pid = trim($_POST['pid'] ?? '');
        $key = trim($_POST['key'] ?? '');
        $pay_alipay = isset($_POST['pay_alipay']) ? 1 : 0;
        $pay_wxpay = isset($_POST['pay_wxpay']) ? 1 : 0;
        
        $payment_config = [
            'api_url' => $api_url,
            'pid' => $pid,
            'key' => $key,
            'pay_alipay' => $pay_alipay,
            'pay_wxpay' => $pay_wxpay
        ];
        
        updateSiteConfig('payment_config', json_encode($payment_config));
        $success = '支付配置已保存';
    }
    
    if ($action === 'save_upload') {
        // 处理网站LOGO上传
        if (isset($_FILES['site_logo']) && $_FILES['site_logo']['error'] === UPLOAD_ERR_OK) {
            $file = $_FILES['site_logo'];
            $allowed_types = ['image/jpeg', 'image/png', 'image/gif', 'image/webp'];
            $max_size = 2 * 1024 * 1024;
            
            if (in_array($file['type'], $allowed_types) && $file['size'] <= $max_size) {
                $ext = pathinfo($file['name'], PATHINFO_EXTENSION);
                $filename = 'logo_' . time() . '.' . $ext;
                $target = UPLOAD_DIR . $filename;
                
                if (move_uploaded_file($file['tmp_name'], $target)) {
                    updateSiteConfig('site_logo', UPLOAD_URL . $filename);
                }
            }
        }
        
        // 处理侧边栏小图标上传
        if (isset($_FILES['site_sidebar_icon']) && $_FILES['site_sidebar_icon']['error'] === UPLOAD_ERR_OK) {
            $file = $_FILES['site_sidebar_icon'];
            $allowed_types = ['image/jpeg', 'image/png', 'image/gif', 'image/webp'];
            $max_size = 2 * 1024 * 1024;
            
            if (in_array($file['type'], $allowed_types) && $file['size'] <= $max_size) {
                $ext = pathinfo($file['name'], PATHINFO_EXTENSION);
                $filename = 'sidebar_icon_' . time() . '.' . $ext;
                $target = UPLOAD_DIR . $filename;
                
                if (move_uploaded_file($file['tmp_name'], $target)) {
                    updateSiteConfig('site_sidebar_icon', UPLOAD_URL . $filename);
                }
            }
        }
        
        // 处理微信公众号二维码上传
        if (isset($_FILES['wechat_qrcode']) && $_FILES['wechat_qrcode']['error'] === UPLOAD_ERR_OK) {
            $file = $_FILES['wechat_qrcode'];
            $allowed_types = ['image/jpeg', 'image/png', 'image/gif', 'image/webp'];
            $max_size = 2 * 1024 * 1024;
            
            if (in_array($file['type'], $allowed_types) && $file['size'] <= $max_size) {
                $ext = pathinfo($file['name'], PATHINFO_EXTENSION);
                $filename = 'wechat_qrcode.' . $ext;
                $target = UPLOAD_DIR . $filename;
                
                if (move_uploaded_file($file['tmp_name'], $target)) {
                    $old_config = json_decode(getSiteConfig('contact_config') ?? '{}', true);
                    $old_config['wechat_qrcode'] = UPLOAD_URL . $filename;
                    updateSiteConfig('contact_config', json_encode($old_config));
                }
            }
        }
        
        $success = '上传设置已保存';
    }
}

// 获取当前配置
$config = getSiteConfig();
$contact_config = json_decode(getSiteConfig('contact_config') ?? '{}', true);
$payment_config = json_decode(getSiteConfig('payment_config') ?? '{}', true);

// 获取当前激活的 Tab
$active_tab = $_GET['tab'] ?? 'site';
?>

<div class="config-container">
    <!-- 页面头部 -->
    <div class="page-header">
        <h2>
            <img src="https://api.iconify.design/mdi:cog.svg?color=%23667eea" width="26" height="26" style="vertical-align: middle; margin-right: 10px;" alt="">
            网站配置
        </h2>
        <p class="text-muted">
            <img src="https://api.iconify.design/mdi:shield.svg?color=%239ca3af" width="14" height="14" style="margin-right: 4px;" alt="">
            仅超级管理员可修改
        </p>
    </div>
    
    <?php if ($success): ?>
        <div class="alert alert-success">
            <img src="https://api.iconify.design/mdi:check-circle.svg?color=%2310b981" width="20" height="20" style="margin-right: 8px;" alt="">
            <?php echo $success; ?>
        </div>
    <?php endif; ?>
    <?php if ($error): ?>
        <div class="alert alert-error">
            <img src="https://api.iconify.design/mdi:alert-circle.svg?color=%23ef4444" width="20" height="20" style="margin-right: 8px;" alt="">
            <?php echo $error; ?>
        </div>
    <?php endif; ?>
    
    <!-- Tab 切换 -->
    <div class="config-tabs">
        <a href="?page=config&tab=site" class="tab-item <?php echo $active_tab == 'site' ? 'active' : ''; ?>">
            <img src="https://api.iconify.design/mdi:web.svg?color=<?php echo $active_tab == 'site' ? 'white' : '%236b7280'; ?>" width="16" height="16" style="margin-right: 6px;" alt="">
            网站配置
        </a>
        <a href="?page=config&tab=contact" class="tab-item <?php echo $active_tab == 'contact' ? 'active' : ''; ?>">
            <img src="https://api.iconify.design/mdi:card-account-phone.svg?color=<?php echo $active_tab == 'contact' ? 'white' : '%236b7280'; ?>" width="16" height="16" style="margin-right: 6px;" alt="">
            联系配置
        </a>
        <a href="?page=config&tab=payment" class="tab-item <?php echo $active_tab == 'payment' ? 'active' : ''; ?>">
            <img src="https://api.iconify.design/mdi:credit-card.svg?color=<?php echo $active_tab == 'payment' ? 'white' : '%236b7280'; ?>" width="16" height="16" style="margin-right: 6px;" alt="">
            支付配置
        </a>
        <a href="?page=config&tab=upload" class="tab-item <?php echo $active_tab == 'upload' ? 'active' : ''; ?>">
            <img src="https://api.iconify.design/mdi:cloud-upload.svg?color=<?php echo $active_tab == 'upload' ? 'white' : '%236b7280'; ?>" width="16" height="16" style="margin-right: 6px;" alt="">
            上传设置
        </a>
    </div>
    
    <div class="form-card">
        <?php if ($active_tab == 'site'): ?>
        <!-- 网站配置 -->
        <form method="POST">
            <input type="hidden" name="action" value="save_site">
            
            <div class="section-title">
                <img src="https://api.iconify.design/mdi:information.svg?color=%23667eea" width="18" height="18" style="margin-right: 8px;" alt="">
                基本信息
            </div>
            
            <div class="form-group">
                <label>
                    <img src="https://api.iconify.design/mdi:web.svg?color=%23667eea" width="16" height="16" style="margin-right: 6px;" alt="">
                    网站名称
                </label>
                <input type="text" name="site_name" value="<?php echo htmlspecialchars($config['site_name'] ?? '麟泓授权系统'); ?>" placeholder="请输入网站名称">
            </div>
            
            <div class="form-group">
                <label>
                    <img src="https://api.iconify.design/mdi:tag-multiple.svg?color=%23667eea" width="16" height="16" style="margin-right: 6px;" alt="">
                    网站关键词 (SEO)
                </label>
                <input type="text" name="site_keywords" value="<?php echo htmlspecialchars($config['site_keywords'] ?? ''); ?>" placeholder="例如：授权系统,软件授权,PHP加密">
                <small>多个关键词用英文逗号分隔</small>
            </div>
            
            <div class="form-group">
                <label>
                    <img src="https://api.iconify.design/mdi:certificate.svg?color=%23667eea" width="16" height="16" style="margin-right: 6px;" alt="">
                    备案号
                </label>
                <input type="text" name="site_icp" value="<?php echo htmlspecialchars($config['site_icp'] ?? ''); ?>" placeholder="例如：京ICP备12345678号">
            </div>
            
            <div class="form-group">
                <label>
                    <img src="https://api.iconify.design/mdi:copyright.svg?color=%23667eea" width="16" height="16" style="margin-right: 6px;" alt="">
                    版权声明
                </label>
                <input type="text" name="site_copyright" value="<?php echo htmlspecialchars($config['site_copyright'] ?? ''); ?>" placeholder="例如：© 2024 麟泓授权系统">
                <small>显示在页脚</small>
            </div>
            
            <div class="form-group">
                <label>
                    <img src="https://api.iconify.design/mdi:tag.svg?color=%23667eea" width="16" height="16" style="margin-right: 6px;" alt="">
                    程序版本
                </label>
                <input type="text" name="sys_version" value="<?php echo htmlspecialchars($config['sys_version'] ?? '1.0.0'); ?>" placeholder="例如：1.0.0">
                <small>显示在用户端版本信息页面</small>
            </div>
            
            <div class="section-title">
                <img src="https://api.iconify.design/mdi:tune.svg?color=%23667eea" width="18" height="18" style="margin-right: 8px;" alt="">
                其他设置
            </div>
            
            <div class="form-group">
                <label class="checkbox-label">
                    <input type="checkbox" name="register_enabled" value="1" <?php echo ($config['register_enabled'] ?? '1') == '1' ? 'checked' : ''; ?>>
                    <span class="checkbox-custom"></span>
                    <span class="checkbox-text">
                        <img src="https://api.iconify.design/mdi:account-plus.svg?color=%23667eea" width="16" height="16" style="margin-right: 6px;" alt="">
                        开启用户注册
                    </span>
                </label>
            </div>
            
            <div class="form-actions">
                <button type="submit" class="btn btn-primary">
                    <img src="https://api.iconify.design/mdi:content-save.svg?color=white" width="18" height="18" style="margin-right: 6px;" alt="">
                    保存网站配置
                </button>
            </div>
        </form>
        
        <?php elseif ($active_tab == 'contact'): ?>
        <!-- 联系配置 -->
        <form method="POST">
            <input type="hidden" name="action" value="save_contact">
            
            <div class="section-title">
                <img src="https://api.iconify.design/mdi:card-account-phone.svg?color=%23667eea" width="18" height="18" style="margin-right: 8px;" alt="">
                联系方式
            </div>
            
            <div class="form-group">
                <label>
                    <img src="https://api.iconify.design/mdi:qqchat.svg?color=%23667eea" width="16" height="16" style="margin-right: 6px;" alt="">
                    QQ群号
                </label>
                <input type="text" name="qq_group" value="<?php echo htmlspecialchars($contact_config['qq_group'] ?? '709503830'); ?>" placeholder="请输入QQ群号">
            </div>
            
            <div class="form-group">
                <label>
                    <img src="https://api.iconify.design/mdi:email.svg?color=%23667eea" width="16" height="16" style="margin-right: 6px;" alt="">
                    官方邮箱
                </label>
                <input type="email" name="email" value="<?php echo htmlspecialchars($contact_config['email'] ?? 'admin@lhwks.top'); ?>" placeholder="请输入官方邮箱">
            </div>
            
            <div class="form-group">
                <label>
                    <img src="https://api.iconify.design/mdi:clock-outline.svg?color=%23667eea" width="16" height="16" style="margin-right: 6px;" alt="">
                    服务时间
                </label>
                <input type="text" name="service_time" value="<?php echo htmlspecialchars($contact_config['service_time'] ?? '周一至周五 9:00-18:00'); ?>" placeholder="例如：周一至周五 9:00-18:00">
            </div>
            
            <div class="form-group">
                <label>
                    <img src="https://api.iconify.design/mdi:map-marker.svg?color=%23667eea" width="16" height="16" style="margin-right: 6px;" alt="">
                    公司地址
                </label>
                <input type="text" name="company_address" value="<?php echo htmlspecialchars($contact_config['company_address'] ?? '中国 · 江苏'); ?>" placeholder="例如：中国 · 江苏">
            </div>
            
            <div class="form-group">
                <label>
                    <img src="https://api.iconify.design/mdi:message-text.svg?color=%23667eea" width="16" height="16" style="margin-right: 6px;" alt="">
                    温馨提示
                </label>
                <textarea name="business_tip" rows="3" placeholder="例如：非工作时间提交的问题，我们会在下一个工作日第一时间处理"><?php echo htmlspecialchars($contact_config['business_tip'] ?? '非工作时间提交的问题，我们会在下一个工作日第一时间处理'); ?></textarea>
            </div>
            
            <div class="form-actions">
                <button type="submit" class="btn btn-primary">
                    <img src="https://api.iconify.design/mdi:content-save.svg?color=white" width="18" height="18" style="margin-right: 6px;" alt="">
                    保存联系配置
                </button>
            </div>
        </form>
        
        <?php elseif ($active_tab == 'payment'): ?>
        <!-- 支付配置 -->
        <form method="POST">
            <input type="hidden" name="action" value="save_payment">
            
            <div class="section-title">
                <img src="https://api.iconify.design/mdi:credit-card.svg?color=%23667eea" width="18" height="18" style="margin-right: 8px;" alt="">
                支付接口配置
            </div>
            
            <div class="form-group">
                <label>
                    <img src="https://api.iconify.design/mdi:web.svg?color=%23667eea" width="16" height="16" style="margin-right: 6px;" alt="">
                    支付接口地址
                </label>
                <input type="url" name="api_url" value="<?php echo htmlspecialchars($payment_config['api_url'] ?? ''); ?>" placeholder="https://your-pay.com/">
                <small>易支付接口地址，以 / 结尾</small>
            </div>
            
            <div class="form-group">
                <label>
                    <img src="https://api.iconify.design/mdi:identifier.svg?color=%23667eea" width="16" height="16" style="margin-right: 6px;" alt="">
                    商户ID (PID)
                </label>
                <input type="text" name="pid" value="<?php echo htmlspecialchars($payment_config['pid'] ?? ''); ?>" placeholder="请输入商户ID">
            </div>
            
            <div class="form-group">
                <label>
                    <img src="https://api.iconify.design/mdi:key.svg?color=%23667eea" width="16" height="16" style="margin-right: 6px;" alt="">
                    商户密钥 (KEY)
                </label>
                <input type="text" name="key" value="<?php echo htmlspecialchars($payment_config['key'] ?? ''); ?>" placeholder="请输入商户密钥">
            </div>
            
            <div class="form-group">
                <label>
                    <img src="https://api.iconify.design/mdi:toggle-switch.svg?color=%23667eea" width="16" height="16" style="margin-right: 6px;" alt="">
                    启用支付方式
                </label>
                <div class="checkbox-group">
                    <label class="checkbox-label">
                        <input type="checkbox" name="pay_alipay" value="1" <?php echo ($payment_config['pay_alipay'] ?? 1) == 1 ? 'checked' : ''; ?>>
                        <span class="checkbox-custom"></span>
                        <span class="checkbox-text">
                            <img src="/assets/svg/alipay-ys.svg" width="18" height="18" style="margin-right: 6px;" alt="">
                            支付宝
                        </span>
                    </label>
                    <label class="checkbox-label">
                        <input type="checkbox" name="pay_wxpay" value="1" <?php echo ($payment_config['pay_wxpay'] ?? 1) == 1 ? 'checked' : ''; ?>>
                        <span class="checkbox-custom"></span>
                        <span class="checkbox-text">
                            <img src="/assets/svg/wxpay-ys.svg" width="18" height="18" style="margin-right: 6px;" alt="">
                            微信支付
                        </span>
                    </label>
                </div>
            </div>
            
            <div class="info-note">
                <div class="info-note-header">
                    <img src="https://api.iconify.design/mdi:information.svg?color=%23667eea" width="20" height="20" style="margin-right: 8px;" alt="">
                    <h4>支付回调地址配置</h4>
                </div>
                <div class="callback-item">
                    <span class="callback-label">
                        <img src="https://api.iconify.design/mdi:arrow-right-circle.svg?color=%236b7280" width="14" height="14" style="margin-right: 6px;" alt="">
                        异步回调地址：
                    </span>
                    <code><?php echo PAY_NOTIFY_URL; ?></code>
                    <button type="button" class="copy-url-btn" onclick="copyToClipboard('<?php echo PAY_NOTIFY_URL; ?>')" title="复制">
                        <img src="https://api.iconify.design/mdi:content-copy.svg?color=%23667eea" width="14" height="14" alt="">
                    </button>
                </div>
                <div class="callback-item">
                    <span class="callback-label">
                        <img src="https://api.iconify.design/mdi:check-circle.svg?color=%236b7280" width="14" height="14" style="margin-right: 6px;" alt="">
                        同步回调地址：
                    </span>
                    <code><?php echo PAY_RETURN_URL; ?></code>
                    <button type="button" class="copy-url-btn" onclick="copyToClipboard('<?php echo PAY_RETURN_URL; ?>')" title="复制">
                        <img src="https://api.iconify.design/mdi:content-copy.svg?color=%23667eea" width="14" height="14" alt="">
                    </button>
                </div>
                <p class="text-muted">
                    <img src="https://api.iconify.design/mdi:alert-circle.svg?color=%23f59e0b" width="14" height="14" style="margin-right: 4px;" alt="">
                    请在支付平台后台配置上述回调地址
                </p>
            </div>
            
            <div class="form-actions">
                <button type="submit" class="btn btn-primary">
                    <img src="https://api.iconify.design/mdi:content-save.svg?color=white" width="18" height="18" style="margin-right: 6px;" alt="">
                    保存支付配置
                </button>
            </div>
        </form>
        
        <?php elseif ($active_tab == 'upload'): ?>
        <!-- 上传设置 -->
        <form method="POST" enctype="multipart/form-data">
            <input type="hidden" name="action" value="save_upload">
            
            <div class="section-title">
                <img src="https://api.iconify.design/mdi:image.svg?color=%23667eea" width="18" height="18" style="margin-right: 8px;" alt="">
                网站LOGO
            </div>
            
            <div class="form-group">
                <label>
                    <img src="https://api.iconify.design/mdi:image.svg?color=%23667eea" width="16" height="16" style="margin-right: 6px;" alt="">
                    网站LOGO
                </label>
                <div class="logo-preview">
                    <img src="<?php echo htmlspecialchars($config['site_logo'] ?? '/assets/uploads/logo.png'); ?>" id="logoPreview" alt="Logo Preview">
                </div>
                <div class="file-input-wrapper">
                    <input type="file" name="site_logo" accept="image/*" id="logoInput">
                    <label for="logoInput" class="file-input-label">
                        <img src="https://api.iconify.design/mdi:cloud-upload.svg?color=%23667eea" width="16" height="16" style="margin-right: 6px;" alt="">
                        选择图片
                    </label>
                    <span class="file-name" id="fileNameDisplay">未选择文件</span>
                </div>
                <small>支持 JPG、PNG、GIF、WEBP，大小不超过2MB</small>
            </div>
            
            <!-- 侧边栏小图标 -->
            <div class="section-title" style="margin-top: 32px;">
                <img src="https://api.iconify.design/mdi:image-size-select-actual.svg?color=%23667eea" width="18" height="18" style="margin-right: 8px;" alt="">
                侧边栏小图标
            </div>

            <div class="form-group">
                <label>
                    <img src="https://api.iconify.design/mdi:image.svg?color=%23667eea" width="16" height="16" style="margin-right: 6px;" alt="">
                    侧边栏小图标
                </label>
                <div class="icon-preview">
                    <img src="<?php echo htmlspecialchars($config['site_sidebar_icon'] ?? '/assets/uploads/logo.png'); ?>" id="sidebarIconPreview" alt="侧边栏图标预览">
                </div>
                <div class="file-input-wrapper">
                    <input type="file" name="site_sidebar_icon" accept="image/*" id="sidebarIconInput">
                    <label for="sidebarIconInput" class="file-input-label">
                        <img src="https://api.iconify.design/mdi:cloud-upload.svg?color=%23667eea" width="16" height="16" style="margin-right: 6px;" alt="">
                        选择图标
                    </label>
                    <span class="file-name" id="sidebarIconFileName">未选择文件</span>
                </div>
                <small>支持 JPG、PNG、GIF、WEBP，大小不超过2MB。建议使用正方形小图标</small>
            </div>
            
            <div class="section-title" style="margin-top: 32px;">
                <img src="https://api.iconify.design/mdi:qrcode.svg?color=%23667eea" width="18" height="18" style="margin-right: 8px;" alt="">
                公众号二维码
            </div>
            
            <div class="form-group">
                <label>
                    <img src="https://api.iconify.design/mdi:wechat.svg?color=%23667eea" width="16" height="16" style="margin-right: 6px;" alt="">
                    微信公众号二维码
                </label>
                <div class="qr-preview">
                    <img src="<?php echo htmlspecialchars($contact_config['wechat_qrcode'] ?? '/assets/uploads/wechat.jpg'); ?>" id="qrPreview" alt="二维码预览">
                </div>
                <div class="file-input-wrapper">
                    <input type="file" name="wechat_qrcode" accept="image/*" id="qrInput">
                    <label for="qrInput" class="file-input-label">
                        <img src="https://api.iconify.design/mdi:cloud-upload.svg?color=%23667eea" width="16" height="16" style="margin-right: 6px;" alt="">
                        选择图片
                    </label>
                    <span class="file-name" id="qrFileName">未选择文件</span>
                </div>
                <small>支持 JPG、PNG、GIF、WEBP，大小不超过2MB</small>
            </div>
            
            <div class="form-actions">
                <button type="submit" class="btn btn-primary">
                    <img src="https://api.iconify.design/mdi:content-save.svg?color=white" width="18" height="18" style="margin-right: 6px;" alt="">
                    保存上传设置
                </button>
            </div>
        </form>
        <?php endif; ?>
    </div>
</div>

<style>
/* 基础变量 */
:root {
    --primary-color: #667eea;
    --primary-hover: #5a67d8;
    --danger-color: #ef4444;
    --success-color: #10b981;
    --bg-primary: #ffffff;
    --bg-secondary: #f9fafb;
    --text-primary: #1f2937;
    --text-secondary: #6b7280;
    --text-muted: #9ca3af;
    --border-color: #e5e7eb;
    --shadow-sm: 0 1px 2px 0 rgba(0, 0, 0, 0.05);
    --shadow-md: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
    --radius-sm: 8px;
    --radius-md: 12px;
    --radius-lg: 16px;
}

.config-container {
    max-width: 720px;
    margin: 0 auto;
    padding: 4px;
}

/* 页面头部 */
.page-header {
    margin-bottom: 24px;
}
.page-header h2 {
    display: flex;
    align-items: center;
    font-size: 24px;
    font-weight: 600;
    color: var(--text-primary);
    margin: 0 0 6px;
}
.text-muted {
    display: flex;
    align-items: center;
    color: var(--text-muted);
    font-size: 13px;
}

/* 提示消息 */
.alert {
    display: flex;
    align-items: center;
    padding: 14px 20px;
    border-radius: var(--radius-md);
    margin-bottom: 24px;
    font-size: 14px;
}
.alert-success {
    background: #d1fae5;
    color: #065f46;
    border: 1px solid #a7f3d0;
}
.alert-error {
    background: #fef2f2;
    border: 1px solid #fecaca;
    color: #991b1b;
}

/* Tab 切换 */
.config-tabs {
    display: flex;
    gap: 4px;
    margin-bottom: 20px;
    background: var(--bg-secondary);
    padding: 4px;
    border-radius: var(--radius-lg);
}
.tab-item {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 12px 16px;
    font-size: 14px;
    font-weight: 500;
    color: var(--text-secondary);
    text-decoration: none;
    border-radius: var(--radius-md);
    transition: all 0.2s;
}
.tab-item:hover {
    color: var(--primary-color);
}
.tab-item.active {
    background: var(--primary-color);
    color: white;
}

/* 表单卡片 */
.form-card {
    background: var(--bg-primary);
    border-radius: var(--radius-lg);
    padding: 32px;
    border: 1px solid var(--border-color);
    box-shadow: var(--shadow-sm);
}

/* 分区标题 */
.section-title {
    display: flex;
    align-items: center;
    font-size: 16px;
    font-weight: 600;
    color: var(--text-primary);
    margin-bottom: 20px;
    padding-bottom: 12px;
    border-bottom: 1px solid var(--border-color);
}
.section-title:not(:first-child) {
    margin-top: 28px;
}

/* 表单组 */
.form-group {
    margin-bottom: 22px;
}
.form-group label {
    display: flex;
    align-items: center;
    margin-bottom: 8px;
    font-weight: 500;
    font-size: 14px;
    color: var(--text-primary);
}
.form-group input[type="text"],
.form-group input[type="email"],
.form-group input[type="url"],
.form-group textarea {
    width: 100%;
    padding: 12px 16px;
    border: 1.5px solid var(--border-color);
    border-radius: var(--radius-md);
    font-size: 14px;
    background: var(--bg-primary);
    color: var(--text-primary);
    transition: all 0.2s;
}
.form-group input:focus,
.form-group textarea:focus {
    outline: none;
    border-color: var(--primary-color);
    box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
}
.form-group textarea {
    resize: vertical;
    font-family: inherit;
}
.form-group small {
    display: block;
    margin-top: 6px;
    font-size: 12px;
    color: var(--text-muted);
}

/* 复选框 */
.checkbox-label {
    display: flex;
    align-items: center;
    cursor: pointer;
}
.checkbox-label input[type="checkbox"] {
    width: 18px;
    height: 18px;
    margin-right: 8px;
    cursor: pointer;
    accent-color: var(--primary-color);
}
.checkbox-text {
    display: flex;
    align-items: center;
}
.checkbox-group {
    display: flex;
    gap: 24px;
    margin-top: 8px;
}

/* 信息提示区 */
.info-note {
    background: var(--bg-secondary);
    padding: 20px;
    border-radius: var(--radius-md);
    margin: 24px 0;
    border: 1px solid var(--border-color);
}
.info-note-header {
    display: flex;
    align-items: center;
    margin-bottom: 16px;
}
.info-note-header h4 {
    font-size: 15px;
    font-weight: 600;
    color: var(--text-primary);
    margin: 0;
}
.callback-item {
    display: flex;
    align-items: center;
    margin-bottom: 12px;
    flex-wrap: wrap;
    gap: 8px;
}
.callback-label {
    display: flex;
    align-items: center;
    font-size: 13px;
    color: var(--text-secondary);
    min-width: 120px;
}
.callback-item code {
    background: var(--bg-primary);
    padding: 6px 12px;
    border-radius: var(--radius-sm);
    font-size: 12px;
    font-family: 'Monaco', 'Menlo', 'Courier New', monospace;
    color: var(--primary-color);
    border: 1px solid var(--border-color);
    word-break: break-all;
}
.copy-url-btn {
    background: none;
    border: 1px solid var(--border-color);
    border-radius: var(--radius-sm);
    padding: 6px 10px;
    cursor: pointer;
    transition: all 0.2s;
    display: inline-flex;
    align-items: center;
    justify-content: center;
}
.copy-url-btn:hover {
    background: var(--bg-primary);
    border-color: var(--primary-color);
}
.info-note .text-muted {
    margin-top: 16px;
    padding-top: 12px;
    border-top: 1px dashed var(--border-color);
}

/* 图标预览 */
.icon-preview {
    margin-bottom: 12px;
}
.icon-preview img {
    width: 48px;
    height: 48px;
    border-radius: 10px;
    border: 1px solid var(--border-color);
    padding: 6px;
    background: var(--bg-secondary);
    object-fit: cover;
}

/* 预览 */
.logo-preview, .qr-preview {
    margin-bottom: 12px;
}
.logo-preview img {
    max-height: 60px;
    border-radius: var(--radius-sm);
    border: 1px solid var(--border-color);
    padding: 8px;
    background: var(--bg-secondary);
}
.qr-preview img {
    width: 120px;
    height: 120px;
    object-fit: cover;
    border-radius: var(--radius-sm);
    border: 1px solid var(--border-color);
    padding: 8px;
    background: var(--bg-secondary);
}

/* 文件上传 */
.file-input-wrapper {
    display: flex;
    align-items: center;
    gap: 12px;
    margin-top: 8px;
}
.file-input-wrapper input[type="file"] {
    display: none;
}
.file-input-label {
    display: inline-flex;
    align-items: center;
    padding: 10px 18px;
    background: var(--bg-secondary);
    border: 1.5px dashed var(--border-color);
    border-radius: var(--radius-md);
    cursor: pointer;
    font-size: 14px;
    color: var(--text-primary);
    transition: all 0.2s;
    white-space: nowrap;
}
.file-input-label:hover {
    border-color: var(--primary-color);
    background: rgba(102, 126, 234, 0.05);
}
.file-name {
    font-size: 13px;
    color: var(--text-muted);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

/* 按钮 */
.form-actions {
    margin-top: 32px;
    padding-top: 20px;
    border-top: 1px solid var(--border-color);
}
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 12px 28px;
    border-radius: var(--radius-md);
    font-size: 15px;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.2s;
    border: 1px solid transparent;
}
.btn-primary {
    background: var(--primary-color);
    color: white;
    box-shadow: 0 2px 4px rgba(102, 126, 234, 0.2);
    width: 100%;
}
.btn-primary:hover {
    background: var(--primary-hover);
    transform: translateY(-1px);
    box-shadow: 0 4px 8px rgba(102, 126, 234, 0.3);
}

/* 响应式 */
@media (max-width: 768px) {
    .form-card {
        padding: 20px;
    }
    .config-tabs {
        flex-wrap: wrap;
    }
    .tab-item {
        flex: none;
        width: calc(50% - 2px);
    }
    .file-input-wrapper {
        flex-direction: column;
        align-items: flex-start;
    }
    .file-name {
        width: 100%;
    }
    .callback-item {
        flex-direction: column;
        align-items: flex-start;
    }
    .callback-label {
        min-width: auto;
    }
    .callback-item code {
        width: 100%;
    }
}
</style>

<script>
// 侧边栏图标预览
document.getElementById('sidebarIconInput')?.addEventListener('change', function(e) {
    const file = e.target.files[0];
    if (file) {
        document.getElementById('sidebarIconFileName').textContent = file.name;
        const reader = new FileReader();
        reader.onload = function(e) {
            document.getElementById('sidebarIconPreview').src = e.target.result;
        };
        reader.readAsDataURL(file);
    } else {
        document.getElementById('sidebarIconFileName').textContent = '未选择文件';
    }
});

// 文件选择显示文件名
document.getElementById('logoInput')?.addEventListener('change', function(e) {
    const file = e.target.files[0];
    if (file) {
        document.getElementById('fileNameDisplay').textContent = file.name;
        const reader = new FileReader();
        reader.onload = function(e) {
            document.getElementById('logoPreview').src = e.target.result;
        };
        reader.readAsDataURL(file);
    } else {
        document.getElementById('fileNameDisplay').textContent = '未选择文件';
    }
});

document.getElementById('qrInput')?.addEventListener('change', function(e) {
    const file = e.target.files[0];
    if (file) {
        document.getElementById('qrFileName').textContent = file.name;
        const reader = new FileReader();
        reader.onload = function(e) {
            document.getElementById('qrPreview').src = e.target.result;
        };
        reader.readAsDataURL(file);
    } else {
        document.getElementById('qrFileName').textContent = '未选择文件';
    }
});

function copyToClipboard(text) {
    navigator.clipboard?.writeText(text).then(() => {
        window.showToast && window.showToast('回调地址已复制', 'success');
    }).catch(() => {
        const ta = document.createElement('textarea');
        ta.value = text;
        document.body.appendChild(ta);
        ta.select();
        document.execCommand('copy');
        document.body.removeChild(ta);
        window.showToast && window.showToast('回调地址已复制', 'success');
    });
}
</script>