<?php
/**
 * 公众号登录页面 — 显示后台设置的站点logo和微信登录按钮
 */
$pageTitle = '登录';
$pageCss = '/css/login.css';
require_once '../includes/header.php';

// 获取公众号配置和站点配置
$db = getDB();
$wechatConfig = [];
$stmt = $db->query("SELECT key_name, value FROM settings WHERE group_name = 'wechat'");
foreach ($stmt->fetchAll() as $row) {
    $wechatConfig[$row['key_name']] = $row['value'];
}

$siteInfo = [];
$stmt = $db->query("SELECT key_name, value FROM settings WHERE group_name = 'site' AND key_name IN ('site_logo', 'site_title')");
foreach ($stmt->fetchAll() as $row) {
    $siteInfo[$row['key_name']] = $row['value'];
}

$wechatEnabled = ($wechatConfig['wechat_enabled'] ?? '0') === '1';
$wechatAppid = $wechatConfig['wechat_appid'] ?? '';
$wechatForceFollow = ($wechatConfig['wechat_force_follow'] ?? '0') === '1';
$wechatQrcode = $wechatConfig['wechat_qrcode_url'] ?? '';
?>

<div class="login-container">
    <!-- 站点Logo -->
    <div class="login-header">
        <?php if ($siteInfo['site_logo']): ?>
        <div class="login-logo">
            <img src="<?php echo htmlspecialchars($siteInfo['site_logo']); ?>" alt="Logo">
        </div>
        <?php else: ?>
        <div class="login-logo-placeholder">
            <i class="fas fa-shield-alt"></i>
        </div>
        <?php endif; ?>
        <div class="login-site-name"><?php echo htmlspecialchars($siteInfo['site_title'] ?? SITE_NAME); ?></div>
        <div class="login-desc">校园生活服务平台</div>
    </div>

    <?php if ($wechatEnabled && $wechatAppid): ?>
    <!-- 微信登录按钮 -->
    <div class="login-actions">
        <button class="wechat-login-btn" onclick="wechatLogin()">
            <i class="fab fa-weixin"></i> 微信公众号登录
        </button>
        <div class="login-hint">使用微信公众号授权登录，获取头像和昵称</div>
    </div>

    <?php if ($wechatForceFollow && $wechatQrcode): ?>
    <!-- 强关提示 -->
    <div class="force-follow-tip">
        <div class="tip-icon"><i class="fas fa-info-circle"></i></div>
        <div class="tip-text">首次登录需关注公众号完成绑定</div>
    </div>

    <!-- 公众号二维码（仅在强关模式下显示） -->
    <div class="qrcode-section">
        <div class="qrcode-title">扫码关注公众号</div>
        <div class="qrcode-box">
            <img src="<?php echo htmlspecialchars($wechatQrcode); ?>" alt="公众号二维码">
        </div>
        <div class="qrcode-hint">长按识别二维码关注公众号后登录</div>
    </div>
    <?php endif; ?>
    <?php else: ?>
    <!-- 未配置公众号登录 -->
    <div class="login-unavailable">
        <div class="unavailable-icon"><i class="fas fa-exclamation-circle"></i></div>
        <div class="unavailable-text">公众号登录未启用</div>
        <div class="unavailable-hint">请联系管理员开启公众号登录功能</div>
    </div>
    <?php endif; ?>

    <!-- 底部 -->
    <div class="login-footer">
        <a href="<?php echo SITE_URL; ?>/pages/profile.php">返回我的</a>
    </div>
</div>

<script>
function wechatLogin() {
    var appid = '<?php echo addslashes($wechatAppid); ?>';
    var redirectUri = encodeURIComponent('<?php echo SITE_URL; ?>/pages/wechat-callback.php');
    var state = 'login_' + Date.now();

    // 微信网页授权跳转
    var url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' + appid +
        '&redirect_uri=' + redirectUri +
        '&response_type=code' +
        '&scope=snsapi_userinfo' +
        '&state=' + state +
        '#wechat_redirect';

    location.href = url;
}
</script>

<?php require_once '../includes/footer.php'; ?>
