<?php
declare(strict_types=1);

$lockFile = dirname(__DIR__) . DIRECTORY_SEPARATOR . 'install' . DIRECTORY_SEPARATOR . 'install.lock';
if (!is_file($lockFile)) {
    header('Location: ../install/', true, 302);
    exit;
}
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>登录 - 香橙个人网盘系统</title>
    <script src="../assets/vendor/tailwind/tailwindcss-3.4.17.js"></script>
    <link href="../assets/vendor/font-awesome/css/font-awesome.min.css" rel="stylesheet">
    <script>
        tailwind.config = {
            theme: {
                extend: {
                    colors: {
                        primary: '#165DFF',
                    }
                }
            }
        }
    </script>
</head>
<body class="bg-gray-50 min-h-screen flex items-center justify-center p-4">
    <div class="w-full max-w-sm">
        <!-- Logo -->
        <div class="text-center mb-8">
            <div class="w-16 h-16 bg-primary rounded-2xl flex items-center justify-center mx-auto mb-4">
                <i class="fa fa-cloud text-white text-2xl"></i>
            </div>
            <h1 class="text-2xl font-bold text-gray-800">香橙个人网盘系统</h1>
            <p class="text-gray-500 mt-1">个人网盘</p>
        </div>

        <!-- 登录表单 -->
        <div class="bg-white rounded-2xl shadow-sm p-6">
            <form onsubmit="login(event)">
                <div class="mb-4">
                    <input 
                        type="text" 
                        id="username"
                        placeholder="邮箱 / 手机号" 
                        class="w-full px-4 py-3 border border-gray-200 rounded-xl focus:outline-none focus:border-primary focus:ring-2 focus:ring-primary/20"
                    >
                </div>
                <div class="mb-4">
                    <input 
                        type="password" 
                        id="password"
                        placeholder="密码" 
                        class="w-full px-4 py-3 border border-gray-200 rounded-xl focus:outline-none focus:border-primary focus:ring-2 focus:ring-primary/20"
                    >
                </div>
                <div class="flex items-center justify-between mb-6 text-sm">
                    <label class="flex items-center gap-2 text-gray-500">
                        <input type="checkbox" class="rounded text-primary">
                        <span>记住我</span>
                    </label>
                    <a href="#" class="text-primary">忘记密码</a>
                </div>
                <button 
                    id="loginBtn"
                    type="submit" 
                    class="w-full bg-primary text-white py-3 rounded-xl font-medium hover:bg-primary/90 transition-colors"
                >
                    登录
                </button>
            </form>
            <p id="loginErr" class="hidden text-red-500 text-sm mt-4"></p>

        </div>
    </div>

    <script>
        async function login(e) {
            e.preventDefault();
            const username = document.getElementById('username').value.trim();
            const password = document.getElementById('password').value;
            const err = document.getElementById('loginErr');
            const btn = document.getElementById('loginBtn');

            if (!username || !password) {
                err.textContent = '请输入账号和密码';
                err.classList.remove('hidden');
                return;
            }

            err.classList.add('hidden');
            btn.disabled = true;
            btn.textContent = '登录中...';

            try {
                const response = await fetch('ajax.php?action=login', {
                    method: 'POST',
                    headers: { 'Content-Type': 'application/json' },
                    body: JSON.stringify({ username, password })
                });
                const result = await response.json();

                if (!result.success) {
                    err.textContent = result.message || '登录失败';
                    err.classList.remove('hidden');
                    return;
                }

                window.location.href = './';
            } catch (e) {
                err.textContent = '登录接口请求失败';
                err.classList.remove('hidden');
            } finally {
                btn.disabled = false;
                btn.textContent = '登录';
            }
        }
    </script>
</body>
</html>
