<?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>");
    }
    
    if ($user == $conf['admin_user'] && $pass == $conf['admin_pwd']) {
        $session = md5($user . $pass . $password_hash);
        $expiretime = time() + 604800;
        $token = authcode("{$user}\t{$session}\t{$expiretime}", 'ENCODE', SYS_KEY);
        setcookie("admin_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("admin_token", "", time() - 604800, '/');
    exit("<script>alert('您已成功注销！');window.location.href='./login.php';</script>");
}

// 已登录则跳转
if ($islogin == 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="../assets/css/all.min.css">
</head>
<body class="min-h-screen bg-gradient-to-br from-slate-900 via-purple-900 to-slate-900 flex items-center justify-center p-3 sm:p-4">
    
    <div class="w-full max-w-xs sm:max-w-sm md:max-w-md">
        <!-- Logo区域 -->
        <div class="text-center mb-4 sm:mb-6 md:mb-8">
            <div class="inline-flex items-center justify-center w-12 h-12 sm:w-14 sm:h-14 md:w-16 md:h-16 bg-purple-600 rounded-xl sm:rounded-2xl mb-2 sm:mb-3 md:mb-4 shadow-lg">
                <i class="fas fa-shield-halved text-xl sm:text-2xl md:text-3xl text-white"></i>
            </div>
            <h1 class="text-lg sm:text-xl md:text-2xl font-bold text-white"><?php echo $conf['sitename'] ?? '管理系统'; ?></h1>
            <p class="text-slate-400 text-xs sm:text-sm mt-1 sm:mt-2">管理员登录</p>
        </div>
        
        <!-- 登录卡片 -->
        <div class="bg-white/10 backdrop-blur-lg rounded-xl sm:rounded-2xl shadow-2xl border border-white/20 p-4 sm:p-6 md:p-8">
            <form action="login.php" method="post" class="space-y-3 sm:space-y-4 md:space-y-6">
                <input type="hidden" name="key" value="<?php echo $key; ?>">
                
                <!-- 用户名 -->
                <div>
                    <label class="block text-xs sm:text-sm font-medium text-slate-300 mb-1 sm:mb-2">
                        <i class="fas fa-user mr-1 sm:mr-2"></i>用户名
                    </label>
                    <input type="text" name="user" value="<?php echo htmlspecialchars($user); ?>" 
                           class="w-full px-3 sm:px-4 py-2 sm:py-2.5 md:py-3 text-xs sm:text-sm bg-white/10 border border-white/20 rounded-lg sm:rounded-xl text-white placeholder-slate-400 focus:outline-none focus:ring-2 focus:ring-purple-500 focus:border-transparent transition"
                           placeholder="请输入管理员账号" required>
                </div>
                
                <!-- 密码 -->
                <div>
                    <label class="block text-xs sm:text-sm font-medium text-slate-300 mb-1 sm:mb-2">
                        <i class="fas fa-lock mr-1 sm:mr-2"></i>密码
                    </label>
                    <input type="password" name="pass" 
                           class="w-full px-3 sm:px-4 py-2 sm:py-2.5 md:py-3 text-xs sm:text-sm bg-white/10 border border-white/20 rounded-lg sm:rounded-xl text-white placeholder-slate-400 focus:outline-none focus:ring-2 focus:ring-purple-500 focus:border-transparent transition"
                           placeholder="请输入登录密码" required>
                </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-purple-600 to-blue-600 hover:from-purple-700 hover:to-blue-700 text-white font-medium sm:font-semibold rounded-lg sm:rounded-xl shadow-lg transition">
                    <i class="fas fa-sign-in-alt mr-1 sm:mr-2"></i>登 录
                </button>
            </form>
        </div>
        
        <!-- 底部信息 -->
        <div class="text-center mt-4 sm:mt-6 text-slate-500 text-xs sm:text-sm">
            <p>&copy; <?php echo date('Y'); ?> <?php echo $conf['sitename'] ?? '管理系统'; ?></p>
        </div>
    </div>
    
</body>
</html>
