<?php
include("./includes/common.php");

if(!$conf['userlogin']){
    @header('Content-Type: text/html; charset=UTF-8');
	exit("<script language='javascript'>alert('未开启用户系统');window.location.href='./';</script>");
}

if(!$islogin2){
    @header('Content-Type: text/html; charset=UTF-8');
	exit("<script language='javascript'>alert('请先登录');window.location.href='login.php';</script>");
}

// 加载邮件验证类
$emailVerify = new \lib\EmailVerify();
$emailSender = new \lib\EmailSender();
$emailEnabled = $emailSender->isReady();

if($_SERVER['REQUEST_METHOD'] == 'POST'){
    @header('Content-Type: application/json; charset=UTF-8');
    
    if(!checkRefererHost())exit(json_encode(['code'=>-1, 'msg'=>'请求来源非法']));
    
    $action = isset($_POST['action'])?$_POST['action']:exit('{"code":-1,"msg":"缺少操作参数"}');
    $csrf_token = $_POST['csrf_token'];
    
    if(!$csrf_token || $csrf_token != $_SESSION['csrf_token']){
        exit(json_encode(['code'=>-1, 'msg'=>'请求验证失败，请刷新页面重试']));
    }
    
    switch($action){
        case 'update_profile':
            $nickname = trim($_POST['nickname']);
            $email = trim($_POST['email']);
            
            if(empty($nickname)){
                exit(json_encode(['code'=>-1, 'msg'=>'昵称不能为空']));
            }
            if(mb_strlen($nickname, 'UTF-8') > 20){
                exit(json_encode(['code'=>-1, 'msg'=>'昵称不能超过20个字符']));
            }
            
            if(!empty($email) && !filter_var($email, FILTER_VALIDATE_EMAIL)){
                exit(json_encode(['code'=>-1, 'msg'=>'邮箱格式不正确']));
            }
            
            if(!empty($email) && $email != $userrow['email']){
                if($DB->getRow("SELECT * FROM pre_user WHERE email=:email AND uid!=:uid", [':email'=>$email, ':uid'=>$uid])){
                    exit(json_encode(['code'=>-1, 'msg'=>'邮箱已被其他用户使用']));
                }
            }
            
            $result = $DB->exec("UPDATE pre_user SET nickname=:nickname, email=:email WHERE uid=:uid", [
                ':nickname' => $nickname,
                ':email' => $email,
                ':uid' => $uid
            ]);
            
            if($result){
                $new_csrf = md5(mt_rand(0,999).time());
                $_SESSION['csrf_token'] = $new_csrf;
                exit(json_encode(['code'=>0, 'msg'=>'资料更新成功', 'new_csrf'=>$new_csrf]));
            } else {
                exit(json_encode(['code'=>-1, 'msg'=>'更新失败：'.$DB->error()]));
            }
            break;
            
        case 'change_password':
            $old_password = $_POST['old_password'];
            $new_password = $_POST['new_password'];
            $confirm_password = $_POST['confirm_password'];
            
            if(empty($old_password) || empty($new_password) || empty($confirm_password)){
                exit(json_encode(['code'=>-1, 'msg'=>'请填写完整密码信息']));
            }
            
            if(!password_verify($old_password, $userrow['password'])){
                exit(json_encode(['code'=>-1, 'msg'=>'原密码错误']));
            }
            
            if(strlen($new_password) < 6){
                exit(json_encode(['code'=>-1, 'msg'=>'新密码长度不能少于6位']));
            }
            
            if($new_password != $confirm_password){
                exit(json_encode(['code'=>-1, 'msg'=>'两次输入的新密码不一致']));
            }
            
            $new_password_hash = password_hash($new_password, PASSWORD_DEFAULT);
            $result = $DB->exec("UPDATE pre_user SET password=:password WHERE uid=:uid", [
                ':password' => $new_password_hash,
                ':uid' => $uid
            ]);
            
            if($result){
                $new_csrf = bin2hex(random_bytes(16));
                $_SESSION['csrf_token'] = $new_csrf;
                setcookie("user_token", "", [
                    'expires' => time() - 1,
                    'path' => '/',
                    'secure' => is_https(),
                    'httponly' => true,
                    'samesite' => 'Lax',
                ]);
                exit(json_encode(['code'=>0, 'msg'=>'密码修改成功，请重新登录', 'redirect'=>'login.php', 'new_csrf'=>$new_csrf]));
            } else {
                exit(json_encode(['code'=>-1, 'msg'=>'密码修改失败：'.$DB->error()]));
            }
            break;
            
        case 'bind_social':
            $type = $_POST['social_type'];
            if(!in_array($type, ['qq', 'wx'])){
                exit(json_encode(['code'=>-1, 'msg'=>'无效的第三方类型']));
            }
            $field = $type.'_openid';
            if(!empty($userrow[$field])){
                exit(json_encode(['code'=>-1, 'msg'=>'您已经绑定了该账号']));
            }
            $_SESSION['bind_mode'] = 'bind';
            exit(json_encode(['code'=>0, 'msg'=>'准备绑定', 'action'=>'redirect', 'type'=>$type]));
            break;
            
        case 'unbind_social':
            $type = $_POST['social_type'];
            if(!in_array($type, ['qq', 'wx'])){
                exit(json_encode(['code'=>-1, 'msg'=>'无效的第三方类型']));
            }
            $field = $type.'_openid';
            if(empty($userrow[$field])){
                exit(json_encode(['code'=>-1, 'msg'=>'您尚未绑定该账号']));
            }
            $result = $DB->exec("UPDATE pre_user SET {$field}=NULL WHERE uid=:uid", [':uid'=>$uid]);
            if($result){
                $new_csrf = md5(mt_rand(0,999).time());
                $_SESSION['csrf_token'] = $new_csrf;
                exit(json_encode(['code'=>0, 'msg'=>'解绑成功', 'new_csrf'=>$new_csrf]));
            } else {
                exit(json_encode(['code'=>-1, 'msg'=>'解绑失败：'.$DB->error()]));
            }
            break;

        case 'send_bind_code':
            // 检查邮件功能是否启用
            if(!$emailEnabled){
                exit(json_encode(['code'=>-1, 'msg'=>'邮件功能未启用']));
            }
            
            $email = trim($_POST['email']);
            
            if(empty($email)){
                exit(json_encode(['code'=>-1, 'msg'=>'请输入邮箱地址']));
            }
            
            if(!filter_var($email, FILTER_VALIDATE_EMAIL)){
                exit(json_encode(['code'=>-1, 'msg'=>'邮箱格式不正确']));
            }
            
            // 检查邮箱是否已被其他用户绑定
            if($emailVerify->isEmailBoundByOther($email, $uid)){
                exit(json_encode(['code'=>-1, 'msg'=>'该邮箱已被其他用户绑定']));
            }
            
            // 发送验证码
            $result = $emailVerify->sendCode($email, 'bind', $uid);
            exit(json_encode($result));
            break;
            
        case 'bind_email':
            // 检查邮件功能是否启用
            if(!$emailEnabled){
                exit(json_encode(['code'=>-1, 'msg'=>'邮件功能未启用']));
            }
            
            $email = trim($_POST['email']);
            $code = trim($_POST['code']);
            
            if(empty($email) || empty($code)){
                exit(json_encode(['code'=>-1, 'msg'=>'请填写完整信息']));
            }
            
            if(!filter_var($email, FILTER_VALIDATE_EMAIL)){
                exit(json_encode(['code'=>-1, 'msg'=>'邮箱格式不正确']));
            }
            
            // 检查邮箱是否已被其他用户绑定
            if($emailVerify->isEmailBoundByOther($email, $uid)){
                exit(json_encode(['code'=>-1, 'msg'=>'该邮箱已被其他用户绑定']));
            }
            
            // 验证验证码
            $verifyResult = $emailVerify->verifyCode($email, $code, 'bind', $uid);
            if($verifyResult['code'] !== 0){
                exit(json_encode(['code'=>-1, 'msg'=>'验证码错误或已过期']));
            }
            
            // 更新用户邮箱
            $result = $DB->exec("UPDATE pre_user SET email=:email WHERE uid=:uid", [
                ':email' => $email,
                ':uid' => $uid
            ]);
            
            if($result){
                $new_csrf = md5(mt_rand(0,999).time());
                $_SESSION['csrf_token'] = $new_csrf;
                exit(json_encode(['code'=>0, 'msg'=>'邮箱绑定成功', 'new_csrf'=>$new_csrf]));
            } else {
                exit(json_encode(['code'=>-1, 'msg'=>'邮箱绑定失败：'.$DB->error()]));
            }
            break;
            
        case 'send_unbind_code':
            // 检查邮件功能是否启用
            if(!$emailEnabled){
                exit(json_encode(['code'=>-1, 'msg'=>'邮件功能未启用']));
            }
            
            $email = trim($_POST['email']);
            
            if(empty($email)){
                exit(json_encode(['code'=>-1, 'msg'=>'请输入邮箱地址']));
            }
            
            if(!filter_var($email, FILTER_VALIDATE_EMAIL)){
                exit(json_encode(['code'=>-1, 'msg'=>'邮箱格式不正确']));
            }
            
            // 检查邮箱是否属于当前用户
            if($email != $userrow['email']){
                exit(json_encode(['code'=>-1, 'msg'=>'请输入您当前绑定的邮箱地址']));
            }
            
            // 发送验证码
            $result = $emailVerify->sendCode($email, 'unbind', $uid);
            exit(json_encode($result));
            break;
            
        case 'unbind_email':
            // 检查邮件功能是否启用
            if(!$emailEnabled){
                exit(json_encode(['code'=>-1, 'msg'=>'邮件功能未启用']));
            }
            
            $email = trim($_POST['email']);
            $code = trim($_POST['code']);
            
            if(empty($email) || empty($code)){
                exit(json_encode(['code'=>-1, 'msg'=>'请填写完整信息']));
            }
            
            if(!filter_var($email, FILTER_VALIDATE_EMAIL)){
                exit(json_encode(['code'=>-1, 'msg'=>'邮箱格式不正确']));
            }
            
            // 检查邮箱是否属于当前用户
            if($email != $userrow['email']){
                exit(json_encode(['code'=>-1, 'msg'=>'请输入您当前绑定的邮箱地址']));
            }
            
            // 验证验证码
            $verifyResult = $emailVerify->verifyCode($email, $code, 'unbind', $uid);
            if($verifyResult['code'] !== 0){
                exit(json_encode(['code'=>-1, 'msg'=>'验证码错误或已过期']));
            }
            
            // 清空用户邮箱
            $result = $DB->exec("UPDATE pre_user SET email='' WHERE uid=:uid", [
                ':uid' => $uid
            ]);
            
            if($result){
                $new_csrf = md5(mt_rand(0,999).time());
                $_SESSION['csrf_token'] = $new_csrf;
                exit(json_encode(['code'=>0, 'msg'=>'邮箱解绑成功', 'new_csrf'=>$new_csrf]));
            } else {
                exit(json_encode(['code'=>-1, 'msg'=>'邮箱解绑失败：'.$DB->error()]));
            }
            break;

        case 'redeem_card':
            $card_code = trim($_POST['card_code']);
            if(empty($card_code)){
                exit(json_encode(['code'=>-1, 'msg'=>'请输入卡密代码']));
            }
            $result = redeem_card($card_code, $uid);
            if(isset($result['code']) && $result['code'] === 0){
                $new_csrf = md5(mt_rand(0,999).time());
                $_SESSION['csrf_token'] = $new_csrf;
                $result['new_csrf'] = $new_csrf;
                $info = get_user_resource_info($uid);
                if($info){
                    $result['resource_info'] = $info;
                }
            }
            exit(json_encode($result));
            break;
        
        case 'get_resource':
            $info = get_user_resource_info($uid);
            if($info){
                exit(json_encode(['code'=>0, 'data'=>$info]));
            }else{
                exit(json_encode(['code'=>-1, 'msg'=>'无法获取资源信息']));
            }
            break;
    }
}

$title = '个人中心 - ' . $conf['title'];
include SYSTEM_ROOT.'header.php';

$csrf_token = md5(mt_rand(0,999).time());
$_SESSION['csrf_token'] = $csrf_token;

$qq_bound = !empty($userrow['qq_openid']);
$wx_bound = !empty($userrow['wx_openid']);
$email_bound = !empty($userrow['email']);
$resource_info = get_user_resource_info($uid);

// 获取用户中心公告
$announcements = [];
try {
    $announcements = $DB->getAll("SELECT * FROM pre_user_announcement WHERE status=1 AND (start_time IS NULL OR start_time <= NOW()) AND (end_time IS NULL OR end_time >= NOW()) ORDER BY sort DESC, id DESC LIMIT 10");
} catch (Exception $e) {
    $announcements = [];
}

$default_avatar = 'data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100"><circle cx="50" cy="35" r="20" fill="%23ccc"/><circle cx="50" cy="90" r="35" fill="%23ccc"/></svg>';
$default_avatar_png = './assets/img/avatar.png';

$has_avatar = false;
if (!empty($userrow['faceimg'])) {
    $faceimg = $userrow['faceimg'];
    $is_default = false;
    if ($faceimg === './assets/img/avatar.png' || 
        $faceimg === 'assets/img/avatar.png' ||
        $faceimg === '/assets/img/avatar.png' ||
        $faceimg === $siteurl . 'assets/img/avatar.png' ||
        strpos($faceimg, 'avatar.png') !== false && (strpos($faceimg, 'assets/img/') !== false || strpos($faceimg, "assets\\img\\") !== false)) {
        $is_default = true;
    }
    $has_avatar = !$is_default;
}
?>
<link rel="stylesheet" href="./assets/css/user_center.css?v=2">
<link rel="stylesheet" href="./assets/css/user_announcement.css?v=1">

<div class="user-center-page">
    <!-- 公告栏 -->
    <?php if(!empty($announcements)): ?>
    <div class="user-announcement-bar" id="announcementBar">
        <div class="announcement-icon">
            <i class="fa fa-bullhorn"></i>
        </div>
        <div class="announcement-scroll-wrapper" id="announcementWrapper">
            <div class="announcement-scroll-content" id="announcementScroll">
                <?php foreach($announcements as $index => $ann): ?>
                <div class="announcement-item" data-index="<?php echo $index; ?>" data-id="<?php echo $ann['id']; ?>" data-title="<?php echo htmlspecialchars($ann['title']); ?>" data-content="<?php echo htmlspecialchars($ann['content']); ?>" data-type="<?php echo $ann['type']; ?>">
                    <?php if($ann['type'] == 3): ?>
                    <span class="announcement-tag tag-urgent">紧急</span>
                    <?php elseif($ann['type'] == 2): ?>
                    <span class="announcement-tag tag-important">重要</span>
                    <?php endif; ?>
                    <span class="announcement-title-text"><?php echo htmlspecialchars($ann['title']); ?></span>
                    <span class="announcement-content-html"><?php echo $ann['content']; ?></span>
                    <span class="announcement-click-hint"><i class="fa fa-hand-o-up"></i> 点击查看详情</span>
                </div>
                <?php endforeach; ?>
            </div>
        </div>
        <div class="announcement-nav">
            <button class="nav-btn nav-prev" id="annPrev" title="上一条"><i class="fa fa-chevron-up"></i></button>
            <span class="nav-indicator" id="annIndicator">1/<?php echo count($announcements); ?></span>
            <button class="nav-btn nav-next" id="annNext" title="下一条"><i class="fa fa-chevron-down"></i></button>
        </div>
    </div>

    <!-- 公告详情弹窗 -->
    <div class="announcement-modal-overlay" id="annModalOverlay">
        <div class="announcement-modal">
            <div class="announcement-modal-header">
                <div class="modal-header-icon">
                    <i class="fa fa-bullhorn"></i>
                </div>
                <h3 class="modal-title" id="annModalTitle">公告标题</h3>
                <button class="modal-close" id="annModalClose" title="关闭">
                    <i class="fa fa-times"></i>
                </button>
            </div>
            <div class="announcement-modal-body">
                <div class="modal-type-badge" id="annModalBadge"></div>
                <div class="modal-content" id="annModalContent"></div>
            </div>
            <div class="announcement-modal-footer">
                <button class="modal-btn modal-btn-primary" id="annModalOk">我知道了</button>
            </div>
        </div>
    </div>
    <?php endif; ?>

    <!-- 用户资料卡片 -->
    <div class="user-profile-card">
        <div class="profile-header">
            <img class="profile-avatar" src="<?php echo $has_avatar ? htmlspecialchars($userrow['faceimg']) : $default_avatar_png; ?>" alt="头像" onerror="this.src='<?php echo htmlspecialchars($default_avatar); ?>'">
            <div class="profile-info">
                <h2 class="profile-name"><?php echo htmlspecialchars($userrow['nickname']); ?> <?php if(intval($userrow['level']) == 1): ?><span class="user-level-badge vip">VIP</span><?php else: ?><span class="user-level-badge normal">普通</span><?php endif; ?></h2>
                <p class="profile-meta">@<?php echo htmlspecialchars($userrow['username']); ?> · 注册于 <?php echo date('Y-m-d', strtotime($userrow['addtime'])); ?></p>
            </div>
            <a href="login.php?logout=1" class="profile-logout" onclick="return confirm('确定退出登录？')"><i class="fa fa-sign-out"></i> 退出</a>
        </div>
    </div>

    <div class="row">
        <!-- 左侧列 -->
        <div class="col-md-6">
            <!-- 资源配额卡片 -->
            <div class="section-card">
                <div class="section-header">
                    <i class="fa fa-hdd-o"></i>
                    <h3>资源配额</h3>
                </div>
                <div class="section-body">
                    <?php if($resource_info): ?>
                    <div class="resource-grid">
                        <div class="resource-item">
                            <i class="fa fa-database"></i>
                            <span class="resource-text">
                                <span class="resource-label">已用存储</span>
                                <span class="value"><?php echo size_format($resource_info['storage_used']); ?></span>
                            </span>
                        </div>
                        <div class="resource-item">
                            <i class="fa fa-hdd-o"></i>
                            <span class="resource-text">
                                <span class="resource-label">可用存储</span>
                                <span class="value"><?php echo $resource_info['storage_unlimited'] ? '<span class="unlimited">无限制</span>' : size_format($resource_info['storage_remaining']); ?></span>
                            </span>
                        </div>
                        <div class="resource-item">
                            <i class="fa fa-exchange"></i>
                            <span class="resource-text">
                                <span class="resource-label">已用流量</span>
                                <span class="value"><?php echo size_format($resource_info['traffic_used']); ?></span>
                            </span>
                        </div>
                        <div class="resource-item">
                            <i class="fa fa-signal"></i>
                            <span class="resource-text">
                                <span class="resource-label">可用流量</span>
                                <span class="value"><?php echo $resource_info['traffic_unlimited'] ? '<span class="unlimited">无限制</span>' : size_format($resource_info['traffic_remaining']); ?></span>
                            </span>
                        </div>
                    </div>
                    <?php if(!$resource_info['storage_unlimited'] || !$resource_info['traffic_unlimited']): ?>
                    <div class="progress-section">
                        <?php if(!$resource_info['storage_unlimited']): ?>
                        <div class="progress-item">
                            <div class="progress-label">
                                <span>存储使用率</span>
                                <span><?php echo round($resource_info['storage_percentage'], 2); ?>%</span>
                            </div>
                            <div class="progress-bar-custom">
                                <div class="bar bar-storage" style="width: <?php echo min(100, $resource_info['storage_percentage']); ?>%"></div>
                            </div>
                        </div>
                        <?php endif; ?>
                        <?php if(!$resource_info['traffic_unlimited']): ?>
                        <div class="progress-item">
                            <div class="progress-label">
                                <span>流量使用率</span>
                                <span><?php echo round($resource_info['traffic_percentage'], 2); ?>%</span>
                            </div>
                            <div class="progress-bar-custom">
                                <div class="bar bar-traffic" style="width: <?php echo min(100, $resource_info['traffic_percentage']); ?>%"></div>
                            </div>
                        </div>
                        <?php endif; ?>
                    </div>
                    <?php endif; ?>
                    <?php else: ?>
                    <div class="alert alert-warning">
                        <i class="fa fa-exclamation-triangle"></i> 无法获取资源信息
                    </div>
                    <?php endif; ?>
                </div>
            </div>

            <!-- 卡密兑换卡片 -->
            <div class="section-card">
                <div class="section-header">
                    <i class="fa fa-gift"></i>
                    <h3>卡密兑换</h3>
                </div>
                <div class="section-body">
                    <form id="redeemForm" method="post">
                        <input type="hidden" name="action" value="redeem_card">
                        <input type="hidden" name="csrf_token" value="<?php echo $csrf_token; ?>">
                        <div class="redeem-form">
                            <input type="text" name="card_code" placeholder="请输入卡密代码" required>
                            <button type="submit">兑换</button>
                        </div>
                        <p class="redeem-hint"><i class="fa fa-info-circle"></i> 兑换成功后，存储空间和流量配额会立即增加</p>
                    </form>
                </div>
            </div>
        </div>

        <!-- 右侧列 -->
        <div class="col-md-6">
            <!-- 个人资料卡片 -->
            <div class="section-card">
                <div class="section-header">
                    <i class="fa fa-user"></i>
                    <h3>个人资料</h3>
                </div>
                <div class="section-body">
                    <form id="profileForm" method="post">
                        <input type="hidden" name="action" value="update_profile">
                        <input type="hidden" name="csrf_token" value="<?php echo $csrf_token; ?>">
                        <div class="form-grid">
                            <div class="form-card">
                                <label>用户名</label>
                                <input type="text" value="<?php echo htmlspecialchars($userrow['username']); ?>" readonly>
                            </div>
                            <div class="form-card">
                                <label>昵称</label>
                                <input type="text" name="nickname" value="<?php echo htmlspecialchars($userrow['nickname']); ?>" required>
                            </div>
                            <?php if($emailEnabled): ?>
                            <div class="form-card form-card-full">
                                <label>邮箱</label>
                                <input type="email" name="email" value="<?php echo htmlspecialchars($userrow['email']); ?>" placeholder="用于找回密码" readonly>
                                <div class="help-text">请在"账号安全"-"邮箱绑定"中管理邮箱</div>
                            </div>
                            <?php endif; ?>
                            <div class="form-actions">
                                <button type="submit" class="btn-save"><i class="fa fa-check"></i> 保存修改</button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>

            <!-- 账号安全卡片 -->
            <div class="section-card">
                <div class="section-header">
                    <i class="fa fa-lock"></i>
                    <h3>账号安全</h3>
                </div>
                <div class="section-body">
                    <div class="security-tabs">
                        <button class="active" data-tab="pwd"><i class="fa fa-key"></i> 修改密码</button>
                        <button data-tab="social"><i class="fa fa-plug"></i> 账号绑定</button>
                        <?php if($emailEnabled): ?>
                        <button data-tab="email"><i class="fa fa-envelope"></i> 邮箱绑定</button>
                        <?php endif; ?>
                    </div>
                    
                    <!-- 修改密码 -->
                    <div class="security-tab-content active" id="pwd">
                        <form id="passwordForm" class="password-form" method="post">
                            <input type="hidden" name="action" value="change_password">
                            <input type="hidden" name="csrf_token" value="<?php echo $csrf_token; ?>">
                            <div class="form-group">
                                <label>原密码</label>
                                <input type="password" name="old_password" placeholder="请输入当前密码" required>
                            </div>
                            <div class="form-group">
                                <label>新密码</label>
                                <input type="password" name="new_password" placeholder="新密码（至少6位）" required>
                            </div>
                            <div class="form-group">
                                <label>确认密码</label>
                                <input type="password" name="confirm_password" placeholder="再次输入新密码" required>
                            </div>
                            <button type="submit" class="btn-submit"><i class="fa fa-lock"></i> 修改密码</button>
                        </form>
                    </div>
                    
                    <!-- 账号绑定 -->
                    <div class="security-tab-content" id="social">
                        <div class="social-bind-list">
<?php if(!$conf['login_apiurl'] || !$conf['login_appid'] || !$conf['login_appkey']):?>
                            <div class="text-center text-muted" style="padding:20px;">
                                <i class="fa fa-info-circle" style="font-size:24px;color:#ccc;margin-bottom:10px;display:block;"></i>
                                管理员暂未开启快捷登录
                            </div>
<?php else:?>
<?php if($conf['login_qq']):?>
                            <div class="social-item">
                                <div class="social-info">
                                    <div class="social-icon qq"><i class="fa fa-qq"></i></div>
                                    <div>
                                        <div class="social-name">QQ账号</div>
                                        <div class="social-status"><?php echo $qq_bound ? '<span style="color:#14b8a6;"><i class="fa fa-check-circle"></i> 已绑定</span>' : '未绑定'; ?></div>
                                    </div>
                                </div>
                                <button class="social-btn <?php echo $qq_bound ? 'unbind' : 'bind'; ?>" onclick="<?php echo $qq_bound ? "unbindSocial('qq')" : "bindSocial('qq')"; ?>">
                                    <?php echo $qq_bound ? '解绑' : '绑定'; ?>
                                </button>
                            </div>
<?php endif;?>
<?php if($conf['login_wx']):?>
                            <div class="social-item">
                                <div class="social-info">
                                    <div class="social-icon wx"><i class="fa fa-weixin"></i></div>
                                    <div>
                                        <div class="social-name">微信账号</div>
                                        <div class="social-status"><?php echo $wx_bound ? '<span style="color:#14b8a6;"><i class="fa fa-check-circle"></i> 已绑定</span>' : '未绑定'; ?></div>
                                    </div>
                                </div>
                                <button class="social-btn <?php echo $wx_bound ? 'unbind' : 'bind'; ?>" onclick="<?php echo $wx_bound ? "unbindSocial('wx')" : "bindSocial('wx')"; ?>">
                                    <?php echo $wx_bound ? '解绑' : '绑定'; ?>
                                </button>
                            </div>
<?php endif;?>
<?php if(!$conf['login_qq'] && !$conf['login_wx']):?>
                            <div class="text-center text-muted" style="padding:20px;">
                                <i class="fa fa-info-circle" style="font-size:24px;color:#ccc;margin-bottom:10px;display:block;"></i>
                                管理员暂未开启任何快捷登录
                            </div>
<?php endif;?>
<?php endif;?>
                        </div>
                    </div>
                    
                    <?php if($emailEnabled): ?>
                    <!-- 邮箱绑定 -->
                    <div class="security-tab-content" id="email">
                        <?php if($email_bound): ?>
                        <!-- 已绑定邮箱，显示解绑界面 -->
                        <form id="unbindEmailForm" class="email-bind-form" method="post">
                            <input type="hidden" name="action" value="unbind_email">
                            <input type="hidden" name="csrf_token" value="<?php echo $csrf_token; ?>">
                            <div class="current-email">
                                <div class="email-text"><i class="fa fa-envelope"></i> <?php echo htmlspecialchars($userrow['email']); ?></div>
                                <div class="email-hint">当前已绑定邮箱，解绑需要验证</div>
                            </div>
                            <div class="form-group">
                                <label>确认解绑邮箱</label>
                                <div class="input-group">
                                    <input type="email" id="unbindEmail" name="email" value="<?php echo htmlspecialchars($userrow['email']); ?>" placeholder="请输入当前绑定邮箱" required>
                                </div>
                            </div>
                            <div class="form-group">
                                <label>验证码</label>
                                <div class="input-group">
                                    <input type="text" name="code" class="code-input" placeholder="请输入验证码" required maxlength="6">
                                    <button type="button" class="btn-send-code" id="btnSendUnbindCode" onclick="sendUnbindCode()">获取验证码</button>
                                </div>
                            </div>
                            <button type="submit" class="btn-submit" style="background: linear-gradient(135deg, #dc2626 0%, #ef4444 100%);">
                                <i class="fa fa-unlink"></i> 确认解绑
                            </button>
                        </form>
                        <?php else: ?>
                        <!-- 未绑定邮箱，显示绑定界面 -->
                        <form id="bindEmailForm" class="email-bind-form" method="post">
                            <input type="hidden" name="action" value="bind_email">
                            <input type="hidden" name="csrf_token" value="<?php echo $csrf_token; ?>">
                            <div class="form-group">
                                <label>绑定新邮箱</label>
                                <div class="input-group">
                                    <input type="email" id="bindEmail" name="email" placeholder="请输入要绑定的邮箱" required>
                                </div>
                            </div>
                            <div class="form-group">
                                <label>验证码</label>
                                <div class="input-group">
                                    <input type="text" name="code" class="code-input" placeholder="请输入验证码" required maxlength="6">
                                    <button type="button" class="btn-send-code" id="btnSendBindCode" onclick="sendBindCode()">获取验证码</button>
                                </div>
                            </div>
                            <button type="submit" class="btn-submit">
                                <i class="fa fa-link"></i> 确认绑定
                            </button>
                        </form>
                        <?php endif; ?>
                    </div>
                    <?php endif; ?>
                </div>
            </div>
        </div>
    </div>
</div>

<script src="https://s4.zstatic.net/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="https://s4.zstatic.net/ajax/libs/layer/2.3/layer.js"></script>
<script src="./assets/js/user_center.js"></script>
<?php if(!empty($announcements)): ?>
<script src="./assets/js/user_announcement.js?v=1"></script>
<?php endif; ?>
<?php include SYSTEM_ROOT.'footer.php'; ?>
