<?php
ob_start();
$user_id = $_SESSION['user_id'];
$user = currentUser();

$success = '';
$error = '';
// ========== 处理绑定成功的提示 ==========
if (isset($_GET['bind_success'])) {
    $type = $_GET['bind_success'];
    $type_names = [
        'qq' => 'QQ',
        'gitee' => 'Gitee',
        'wx' => '微信',
        'alipay' => '支付宝',
        'github' => 'GitHub',
        'google' => 'Google',
    ];
    $name = $type_names[$type] ?? $type;
    $success = "{$name} 绑定成功！";
}
// ========== 提示处理结束 ==========
// 获取用户绑定的快捷登录信息
$user_bindings = [];
$bindings = $db->fetchAll("SELECT * FROM user_oauth_bindings WHERE user_id = $user_id");
foreach ($bindings as $binding) {
    $user_bindings[$binding['oauth_type']] = $binding;
}

// 处理绑定/解绑
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $action = $_POST['action'] ?? '';
    
    if ($action === 'update_profile') {
        $email = trim($_POST['email'] ?? '');
        
        if (empty($email)) {
            $error = '邮箱不能为空';
        } elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
            $error = '邮箱格式不正确';
        } else {
            $check = $db->fetchOne("SELECT id FROM users WHERE email = '$email' AND id != $user_id");
            if ($check) {
                $error = '邮箱已被其他用户使用';
            } else {
                $db->query("UPDATE users SET email = '$email' WHERE id = $user_id");
                $success = '资料更新成功';
                $user = currentUser();
            }
        }
    }
    
    if ($action === 'unbind') {
        $oauth_type = $_POST['oauth_type'] ?? '';
        $db->query("DELETE FROM user_oauth_bindings WHERE user_id = $user_id AND oauth_type = '$oauth_type'");
        $success = '解绑成功';
        $user_bindings = [];
        $bindings = $db->fetchAll("SELECT * FROM user_oauth_bindings WHERE user_id = $user_id");
        foreach ($bindings as $binding) {
            $user_bindings[$binding['oauth_type']] = $binding;
        }
    }
}

// 获取用户当前会员组
$current_group = getUserCurrentGroup($user_id);

// 快捷登录配置
$oauth_configs = [
    'qq' => ['name' => 'QQ', 'icon' => 'qq.svg', 'color' => '#12B7F5', 'bind_url' => '/api/qq_bind.php'],
    'wx' => ['name' => '微信', 'icon' => 'wechat.svg', 'color' => '#07C160', 'bind_url' => '/api/oauth_bind.php?type=wx'],
    'alipay' => ['name' => '支付宝', 'icon' => 'alipay.svg', 'color' => '#1677FF', 'bind_url' => '/api/oauth_bind.php?type=alipay'],
    'gitee' => ['name' => 'Gitee', 'icon' => 'gitee.svg', 'color' => '#C71D23', 'bind_url' => '/api/gitee_bind.php'],
];
?>

<div class="profile-container">
    <!-- 页面头部 -->
    <div class="page-header">
        <h2>
            <img src="https://api.iconify.design/mdi:account.svg?color=%23667eea" width="26" height="26" style="vertical-align: middle; margin-right: 10px;" alt="">
            个人资料
        </h2>
    </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; ?>
    
    <div class="profile-card">
        <!-- 头像区域 -->
        <div class="profile-avatar-section">
            <img src="<?php echo htmlspecialchars($user['avatar'] ?? '/assets/uploads/default_avatar.png'); ?>" alt="Avatar" class="profile-avatar" onerror="this.src='/assets/uploads/default_avatar.png'">
            <a href="?page=avatar" class="btn-avatar">
                <img src="https://api.iconify.design/mdi:camera.svg?color=white" width="16" height="16" style="margin-right: 6px;" alt="">
                更换头像
            </a>
        </div>
        
        <form method="POST" class="profile-form">
            <input type="hidden" name="action" value="update_profile">
            
            <!-- 用户名 -->
            <div class="form-group">
                <label>
                    <img src="https://api.iconify.design/mdi:account.svg?color=%23667eea" width="16" height="16" style="margin-right: 6px;" alt="">
                    用户名
                </label>
                <input type="text" value="<?php echo htmlspecialchars($user['username']); ?>" disabled>
                <small>用户名不可修改</small>
            </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($user['email']); ?>" required>
            </div>
            
            <!-- 会员等级 -->
            <div class="form-group">
                <label>
                    <img src="https://api.iconify.design/mdi:shield-account.svg?color=%23667eea" width="16" height="16" style="margin-right: 6px;" alt="">
                    会员等级
                </label>
                <div class="input-with-badge">
                    <input type="text" value="<?php echo htmlspecialchars($current_group['name']); ?>" disabled>
                    <?php if ($current_group['discount'] < 100): ?>
                        <span class="discount-badge">
                            <img src="https://api.iconify.design/mdi:percent.svg?color=white" width="12" height="12" alt="">
                            <?php echo $current_group['discount']; ?>折
                        </span>
                    <?php endif; ?>
                </div>
            </div>
            
            <!-- 注册时间 -->
            <div class="form-group">
                <label>
                    <img src="https://api.iconify.design/mdi:calendar.svg?color=%23667eea" width="16" height="16" style="margin-right: 6px;" alt="">
                    注册时间
                </label>
                <input type="text" value="<?php echo $user['created_at']; ?>" disabled>
            </div>
            
            <!-- 账户余额 -->
            <div class="form-group">
                <label>
                    <img src="https://api.iconify.design/mdi:wallet.svg?color=%23667eea" width="16" height="16" style="margin-right: 6px;" alt="">
                    账户余额
                </label>
                <div class="balance-row">
                    <input type="text" value="¥<?php echo number_format($user['balance'], 2); ?>" disabled>
                    <a href="?page=recharge" class="btn-recharge">
                        <img src="https://api.iconify.design/mdi:plus.svg?color=white" width="14" height="14" style="margin-right: 4px;" alt="">
                        充值
                    </a>
                </div>
            </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>
                <a href="?page=changepwd" class="btn btn-secondary">
                    <img src="https://api.iconify.design/mdi:lock.svg?color=currentColor" width="18" height="18" style="margin-right: 6px;" alt="">
                    修改密码
                </a>
            </div>
        </form>
    </div>
    
    <!-- 快捷登录绑定 -->
    <div class="oauth-card">
        <div class="oauth-header">
            <h3>
                <img src="https://api.iconify.design/mdi:link-variant.svg?color=%23667eea" width="20" height="20" style="margin-right: 8px;" alt="">
                快捷登录绑定
            </h3>
            <p>绑定后可使用第三方账号快速登录</p>
        </div>
        
        <div class="oauth-bindings">
            <?php foreach ($oauth_configs as $type => $config): ?>
                <?php $is_bound = isset($user_bindings[$type]); ?>
                <div class="oauth-item">
                    <div class="oauth-info">
                        <div class="oauth-icon" style="background: <?php echo $config['color']; ?>20;">
                            <img src="/assets/svg/<?php echo $config['icon']; ?>" style="width: 24px; height: 24px;" alt="<?php echo $config['name']; ?>">
                        </div>
                        <span class="oauth-name"><?php echo $config['name']; ?></span>
                        <?php if ($is_bound): ?>
                            <span class="oauth-status bound">
                                <img src="https://api.iconify.design/mdi:check-circle.svg?color=%2310b981" width="12" height="12" alt="">
                                已绑定
                            </span>
                        <?php else: ?>
                            <span class="oauth-status unbind">
                                <img src="https://api.iconify.design/mdi:close-circle.svg?color=%23ef4444" width="12" height="12" alt="">
                                未绑定
                            </span>
                        <?php endif; ?>
                    </div>
                    <div class="oauth-action">
                        <?php if ($is_bound): ?>
                            <form method="POST" style="display: inline;" onsubmit="return confirm('确定解绑吗？解绑后该账号将无法用于登录');">
                                <input type="hidden" name="action" value="unbind">
                                <input type="hidden" name="oauth_type" value="<?php echo $type; ?>">
                                <button type="submit" class="btn-unbind">
                                    <img src="https://api.iconify.design/mdi:link-off.svg?color=%23ef4444" width="14" height="14" style="margin-right: 4px;" alt="">
                                    解绑
                                </button>
                            </form>
                        <?php else: ?>
                            <a href="<?php echo $config['bind_url']; ?>" class="btn-bind">
                                <img src="https://api.iconify.design/mdi:link.svg?color=white" width="14" height="14" style="margin-right: 4px;" alt="">
                                去绑定
                            </a>
                        <?php endif; ?>
                    </div>
                </div>
            <?php endforeach; ?>
        </div>
        
        <div class="bind-tip">
            <div class="tip-header">
                <img src="https://api.iconify.design/mdi:information.svg?color=%23667eea" width="18" height="18" style="margin-right: 6px;" alt="">
                温馨提示
            </div>
            <ul>
                <li>绑定后可使用第三方账号一键登录，无需输入密码</li>
                <li>一个账号可以绑定多个第三方登录方式</li>
                <li>解绑后该第三方账号将无法登录，但不影响账号正常使用</li>
            </ul>
        </div>
    </div>
</div>

<style>
/* 基础变量 */
:root {
    --primary-color: #667eea;
    --primary-hover: #5a67d8;
    --danger-color: #ef4444;
    --success-color: #10b981;
    --warning-color: #f59e0b;
    --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;
}

.profile-container {
    max-width: 560px;
    margin: 0 auto;
    padding: 4px;
}

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

/* 提示消息 */
.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;
}

/* 资料卡片 */
.profile-card {
    background: var(--bg-primary);
    border-radius: var(--radius-lg);
    padding: 32px;
    border: 1px solid var(--border-color);
    box-shadow: var(--shadow-sm);
    margin-bottom: 28px;
}

/* 头像区域 */
.profile-avatar-section {
    text-align: center;
    margin-bottom: 32px;
}
.profile-avatar {
    width: 100px;
    height: 100px;
    border-radius: 50%;
    object-fit: cover;
    margin-bottom: 16px;
    border: 3px solid var(--border-color);
}
.btn-avatar {
    display: inline-flex;
    align-items: center;
    padding: 8px 18px;
    background: var(--primary-color);
    color: white;
    border-radius: 30px;
    text-decoration: none;
    font-size: 13px;
    font-weight: 500;
    transition: all 0.2s;
}
.btn-avatar:hover {
    background: var(--primary-hover);
    transform: translateY(-1px);
}

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

.input-with-badge {
    position: relative;
}
.input-with-badge input {
    padding-right: 70px;
}
.discount-badge {
    position: absolute;
    right: 8px;
    top: 50%;
    transform: translateY(-50%);
    display: inline-flex;
    align-items: center;
    gap: 4px;
    padding: 4px 10px;
    background: var(--primary-color);
    color: white;
    border-radius: 30px;
    font-size: 12px;
    font-weight: 500;
}

.balance-row {
    display: flex;
    gap: 10px;
}
.balance-row input {
    flex: 1;
}
.btn-recharge {
    display: inline-flex;
    align-items: center;
    padding: 0 18px;
    background: var(--success-color);
    color: white;
    border-radius: var(--radius-md);
    text-decoration: none;
    font-size: 13px;
    font-weight: 500;
    transition: all 0.2s;
    white-space: nowrap;
}
.btn-recharge:hover {
    background: #059669;
    transform: translateY(-1px);
}

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

/* 快捷登录卡片 */
.oauth-card {
    background: var(--bg-primary);
    border-radius: var(--radius-lg);
    padding: 28px;
    border: 1px solid var(--border-color);
    box-shadow: var(--shadow-sm);
}
.oauth-header {
    margin-bottom: 24px;
}
.oauth-header h3 {
    display: flex;
    align-items: center;
    font-size: 18px;
    font-weight: 600;
    color: var(--text-primary);
    margin: 0 0 6px;
}
.oauth-header p {
    color: var(--text-secondary);
    font-size: 14px;
    margin: 0 0 0 28px;
}

.oauth-bindings {
    margin-bottom: 24px;
}
.oauth-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 14px 0;
    border-bottom: 1px solid var(--border-color);
}
.oauth-item:last-child {
    border-bottom: none;
}
.oauth-info {
    display: flex;
    align-items: center;
    gap: 14px;
}
.oauth-icon {
    width: 44px;
    height: 44px;
    border-radius: 12px;
    display: flex;
    align-items: center;
    justify-content: center;
}
.oauth-name {
    font-weight: 500;
    min-width: 60px;
}
.oauth-status {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    font-size: 12px;
    padding: 4px 10px;
    border-radius: 30px;
    font-weight: 500;
}
.oauth-status.bound {
    background: #d1fae5;
    color: #065f46;
}
.oauth-status.unbind {
    background: #fef2f2;
    color: #991b1b;
}

.btn-bind {
    display: inline-flex;
    align-items: center;
    padding: 8px 18px;
    background: linear-gradient(135deg, #667eea, #764ba2);
    color: white;
    border-radius: 30px;
    text-decoration: none;
    font-size: 13px;
    font-weight: 500;
    transition: all 0.2s;
}
.btn-bind:hover {
    transform: translateY(-1px);
    box-shadow: 0 4px 12px rgba(102, 126, 234, 0.4);
}
.btn-unbind {
    display: inline-flex;
    align-items: center;
    padding: 8px 18px;
    background: transparent;
    border: 1px solid #ef4444;
    color: #ef4444;
    border-radius: 30px;
    cursor: pointer;
    font-size: 13px;
    font-weight: 500;
    transition: all 0.2s;
}
.btn-unbind:hover {
    background: #ef4444;
    color: white;
}
.btn-unbind:hover img {
    filter: brightness(0) invert(1);
}

.bind-tip {
    margin-top: 20px;
    padding: 18px 20px;
    background: var(--bg-secondary);
    border-radius: var(--radius-md);
}
.tip-header {
    display: flex;
    align-items: center;
    font-weight: 600;
    color: var(--text-primary);
    margin-bottom: 12px;
}
.bind-tip ul {
    margin: 0 0 0 20px;
    padding: 0;
}
.bind-tip li {
    margin: 8px 0;
    font-size: 13px;
    color: var(--text-secondary);
}

/* 响应式 */
@media (max-width: 768px) {
    .profile-card {
        padding: 24px;
    }
    .oauth-card {
        padding: 20px;
    }
    .form-actions {
        flex-direction: column;
    }
    .oauth-item {
        flex-direction: column;
        align-items: flex-start;
        gap: 12px;
    }
    .oauth-action {
        width: 100%;
    }
    .btn-bind, .btn-unbind {
        width: 100%;
        justify-content: center;
    }
}
</style>