<?php
include dirname(__DIR__) . '/includes/common.php';

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

// 处理登录
if (isset($_POST['user']) && isset($_POST['pass'])) {
    $user = daddslashes($_POST['user']);
    $pass = daddslashes($_POST['pass']);
    
    if ($_SESSION["login_key"] != $_POST['key']) {
        exit("<script>alert('验证失败，请刷新网页');history.go(-1);</script>");
    }
    
    if ($user == '' || $pass == '') {
        exit("<script>alert('用户名或密码不能为空！');history.go(-1);</script>");
    }
    
    $row = $DB->getRow("SELECT * FROM pre_user WHERE user='{$user}' LIMIT 1");
    if ($row && $user == $row['user'] && $pass == $row['pwd']) {
        // 检查封禁状态：显示封禁页（与 member.php 一致）
        if ($row['status'] == 1) {
            $ban_reason = isset($row['reason']) && $row['reason'] !== '' ? htmlspecialchars($row['reason']) : '违反平台相关规定';
            header('Content-Type: text/html; charset=UTF-8');
            echo '<!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>账户已封禁 - '.htmlspecialchars($conf['sitename'] ?? '').'</title>
	<script src="../assets/js/tailwind.js"></script>
	<link rel="stylesheet" href="../assets/css/all.min.css">
	<style>.ban-card{animation:fadeInUp .4s ease-out}@keyframes fadeInUp{from{opacity:0;transform:translateY(20px)}to{opacity:1;transform:translateY(0)}}</style>
</head>
<body class="min-h-screen bg-gradient-to-br from-indigo-600 via-purple-600 to-pink-600 flex items-center justify-center p-4">
	<div class="w-full max-w-sm sm:max-w-md ban-card">
		<div class="bg-white rounded-xl sm:rounded-2xl shadow-2xl p-6 sm:p-8 text-center">
			<div class="inline-flex items-center justify-center w-14 h-14 sm:w-16 sm:h-16 bg-red-100 rounded-full mb-4">
				<i class="fas fa-user-lock text-red-500 text-2xl sm:text-3xl"></i>
			</div>
			<h1 class="text-lg sm:text-xl font-bold text-gray-800 mb-1">账户已被封禁</h1>
			<p class="text-gray-500 text-sm mb-4">您的账号因以下原因被限制使用</p>
			<div class="bg-gray-50 rounded-lg px-4 py-3 text-left text-gray-700 text-sm mb-6 border border-gray-100">'.$ban_reason.'</div>
			<p class="text-gray-400 text-xs mb-5">如有疑问请联系客服</p>
			<a href="login.php" class="inline-flex items-center justify-center gap-2 w-full sm:w-auto px-5 py-2.5 rounded-lg font-medium text-white bg-gradient-to-r from-indigo-500 to-purple-500 hover:from-indigo-600 hover:to-purple-600 shadow-lg hover:shadow-xl transition-all">
				<i class="fas fa-arrow-left"></i> 返回登录
			</a>
		</div>
	</div>
</body>
</html>';
            exit;
        }
        
        $session = md5($user . $pass . $password_hash);
        $expiretime = time() + 604800;
        $token = authcode("{$user}\t{$session}\t{$expiretime}", 'ENCODE', SYS_KEY);
        setcookie("user_token", $token, time() + 604800, '/');
        exit("<script>window.location.href='./index.php';</script>");
    } else {
        exit("<script>alert('用户名或密码不正确！');history.go(-1);</script>");
    }
}

// 处理注销
if (isset($_GET['logout'])) {
    setcookie("user_token", "", time() - 604800, '/');
    exit("<script>window.location.href='./login.php';</script>");
}

// 已登录则跳转
if ($islogin2 == 1) {
    exit("<script>window.location.href='./index.php';</script>");
}

// 生成验证key
$key = md5(mt_rand(0, 999) . time());
$_SESSION["login_key"] = $key;
?>
<!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>登录 - <?php echo $conf['sitename'] ?? '管理系统'; ?></title>
    <script src="../assets/js/tailwind.js"></script>
    <link rel="stylesheet" href="https://cdn.bootcdn.net/ajax/libs/font-awesome/6.4.0/css/all.min.css">
</head>
<body class="min-h-screen bg-gradient-to-br from-indigo-600 via-purple-600 to-pink-600 flex items-center justify-center p-4">
    
    <div class="w-full max-w-xs sm:max-w-sm md:max-w-md">
        <!-- Logo -->
        <div class="text-center mb-5">
            <div class="inline-flex items-center justify-center w-12 h-12 sm:w-14 sm:h-14 bg-white/20 backdrop-blur-xl rounded-xl mb-2 shadow-lg">
                <i class="fas fa-user-shield text-xl sm:text-2xl text-white"></i>
            </div>
            <h1 class="text-lg sm:text-xl font-bold text-white"><?php echo $conf['sitename'] ?? '管理系统'; ?></h1>
            <p class="text-purple-200 text-xs sm:text-sm mt-0.5">安全可靠的云端管理平台</p>
        </div>
        
        <!-- 登录卡片 -->
        <div class="bg-white rounded-xl sm:rounded-2xl shadow-2xl p-4 sm:p-6 hover:shadow-2xl/80 transition-shadow">
            <form action="login.php" method="post" class="space-y-3 sm:space-y-4">
                <input type="hidden" name="key" value="<?php echo $key; ?>">
                
                <div>
                    <label class="block text-xs sm:text-sm font-medium text-gray-700 mb-0.5">用户名</label>
                    <div class="relative">
                        <span class="absolute left-3 sm:left-4 top-1/2 -translate-y-1/2 text-gray-400">
                            <i class="fas fa-user text-sm sm:text-base"></i>
                        </span>
                        <input type="text" name="user" value="<?php echo htmlspecialchars($user); ?>" 
                               class="w-full pl-9 sm:pl-11 pr-3 sm:pr-4 py-2 sm:py-2.5 md:py-3 text-xs sm:text-sm border border-gray-200 rounded-lg sm:rounded-xl focus:ring-2 focus:ring-indigo-500 focus:border-indigo-500 transition-all"
                               placeholder="请输入用户名" required>
                    </div>
                </div>
                
                <div>
                    <label class="block text-xs sm:text-sm font-medium text-gray-700 mb-0.5">密码</label>
                    <div class="relative">
                        <span class="absolute left-3 sm:left-4 top-1/2 -translate-y-1/2 text-gray-400">
                            <i class="fas fa-lock text-sm sm:text-base"></i>
                        </span>
                        <input type="password" name="pass" 
                               class="w-full pl-9 sm:pl-11 pr-3 sm:pr-4 py-2 sm:py-2.5 md:py-3 text-xs sm:text-sm border border-gray-200 rounded-lg sm:rounded-xl focus:ring-2 focus:ring-indigo-500 focus:border-indigo-500 transition-all"
                               placeholder="请输入密码" required>
                    </div>
                </div>
                
                <button type="submit" 
                        class="w-full py-2 sm:py-2.5 md:py-3 text-xs sm:text-sm md:text-base bg-gradient-to-r from-blue-600 to-indigo-600 hover:from-blue-700 hover:to-indigo-700 text-white font-medium sm:font-semibold rounded-lg sm:rounded-xl shadow transition-all">
                    <i class="fas fa-sign-in-alt mr-1"></i>登 录
                </button>
            </form>
            
            <?php if (($conf['quick_open'] ?? 0) == 1): ?>
            <!-- 快捷登录 -->
            <div class="mt-4 sm:mt-6">
                <div class="relative">
                    <div class="absolute inset-0 flex items-center">
                        <div class="w-full border-t border-gray-200"></div>
                    </div>
                    <div class="relative flex justify-center text-xs sm:text-sm">
                        <span class="px-2 bg-white text-gray-400">快捷登录</span>
                    </div>
                </div>
                <div class="mt-3 flex justify-center gap-4">
                    <?php if (($conf['quick_qq'] ?? 0) == 1): ?>
                    <a href="./connect.php?type=qq" class="w-9 h-9 sm:w-10 sm:h-10 flex items-center justify-center bg-blue-500 hover:bg-blue-600 text-white rounded-full shadow transition-all">
                        <i class="fab fa-qq text-base sm:text-lg"></i>
                    </a>
                    <?php endif; ?>
                    <?php if (($conf['quick_wx'] ?? 0) == 1): ?>
                    <a href="./connect.php?type=wx" class="w-9 h-9 sm:w-10 sm:h-10 flex items-center justify-center bg-green-500 hover:bg-green-600 text-white rounded-full shadow transition-all">
                        <i class="fab fa-weixin text-base sm:text-lg"></i>
                    </a>
                    <?php endif; ?>
                    <?php if (($conf['quick_alipay'] ?? 0) == 1): ?>
                    <a href="./connect.php?type=alipay" class="w-9 h-9 sm:w-10 sm:h-10 flex items-center justify-center bg-blue-400 hover:bg-blue-500 text-white rounded-full shadow transition-all">
                        <i class="fab fa-alipay text-base sm:text-lg"></i>
                    </a>
                    <?php endif; ?>
                </div>
            </div>
            <?php endif; ?>
            
            <!-- 底部链接 -->
            <div class="mt-4 sm:mt-6 text-center text-xs sm:text-sm text-gray-500 space-x-4">
                <a href="./retrieve.php" class="hover:text-indigo-600 transition-colors">
                    <i class="fas fa-key mr-0.5"></i>忘记密码
                </a>
                <?php if (($conf['user_open'] ?? 0) == 1): ?>
                <a href="./reg.php" class="hover:text-indigo-600 transition-colors">
                    <i class="fas fa-user-plus mr-0.5"></i>注册账号
                </a>
                <?php endif; ?>
            </div>
        </div>
        
        <!-- 版权 -->
        <div class="text-center mt-5 text-white/70 text-xs sm:text-sm hover:text-white/90 transition-colors">
            &copy; <?php echo date('Y'); ?> <?php echo $conf['sitename'] ?? '管理系统'; ?>
        </div>
    </div>
    
    <!-- Toast 提示 -->
    <div id="toastWrap" class="fixed top-4 left-1/2 -translate-x-1/2 z-[60] flex flex-col items-center gap-2"></div>
    
    <script>
    // Toast 提示函数
    function showToast(message, type = 'info', duration = 2000) {
        const toastWrap = document.getElementById('toastWrap');
        const toast = document.createElement('div');
        
        let bgClass = 'bg-gray-800';
        if (type === 'success') bgClass = 'bg-green-600';
        if (type === 'error') bgClass = 'bg-red-600';
        if (type === 'warning') bgClass = 'bg-yellow-600';
        
        toast.className = `${bgClass} text-white px-3 py-1.5 text-sm rounded-lg shadow-lg flex items-center gap-1.5 transform transition-all duration-300 ease-in-out opacity-0 translate-y-[-10px]`;
        toast.innerHTML = `<i class="fas fa-${type === 'success' ? 'check' : type === 'error' ? 'exclamation' : type === 'warning' ? 'exclamation-triangle' : 'info'}"></i><span>${message}</span>`;
        
        toastWrap.appendChild(toast);
        
        setTimeout(() => {
            toast.classList.remove('opacity-0', 'translate-y-[-10px]');
        }, 10);
        
        setTimeout(() => {
            toast.classList.add('opacity-0', 'translate-y-[-10px]');
            setTimeout(() => {
                toastWrap.removeChild(toast);
            }, 300);
        }, duration);
    }
    </script>
</body>
</html>
