<?php
require_once __DIR__ . '/../includes/lang.php';
require_once __DIR__ . '/../db/config.php';

$error = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $account = $_POST['account'] ?? '';
    $password = $_POST['password'] ?? '';
    
    if (empty($account) || empty($password)) {
        $error = __('fill_full_info');
    } else {
        $stmt = db()->prepare("SELECT * FROM users WHERE username = ? OR email = ?");
        $stmt->execute([$account, $account]);
        $user = $stmt->fetch();
        
        if ($user && password_verify($password, $user['password_hash'])) {
            if ($user['role'] === 'admin') {
                $error = __('login_admin_error');
            } else {
                if (session_status() === PHP_SESSION_NONE) session_start();
                $_SESSION['user_id'] = $user['id'];
                $_SESSION['username'] = $user['username'];
                $_SESSION['uid'] = $user['uid'];
                $_SESSION['role'] = $user['role'];
                header('Location: /');
                exit;
            }
        } else {
            $error = __('invalid_account_pwd');
        }
    }
}

include __DIR__ . '/../includes/header.php';
?>

<div class="container py-5">
    <div class="row justify-content-center">
        <div class="col-md-5">
            <div class="card bg-dark border-0 shadow-lg p-4 p-md-5" style="border-radius: 30px; background: #161a1e !important; border: 1px solid var(--border) !important;">
                <div class="text-center mb-5">
                    <div class="logo-container d-inline-flex mb-4 align-items-center">
                        <img src="<?= $site_logo ?>?v=<?= $logo_v ?? time() ?>" height="40" alt="<?= $site_name ?>">
                        <span class="logo-text ms-3" style="font-size: 24px; font-weight: 900; color: #fff; letter-spacing: 1px; text-transform: uppercase;"><?= $site_name ?></span>
                    </div>
                    <h2 class="fw-bold text-white mb-2"><?= __('login') ?></h2>
                    <p class="text-muted"><?= __('welcome_back') ?></p>
                </div>
                
                <?php if ($error): ?>
                    <div class="alert alert-danger py-3 px-4 small border-0 bg-danger bg-opacity-10 text-danger rounded-4 mb-4">
                        <i class="bi bi-exclamation-triangle-fill me-2"></i><?= $error ?>
                    </div>
                <?php endif; ?>

                <form method="POST">
                    <div class="mb-4">
                        <label class="form-label text-muted small fw-bold"><?= __('account') ?></label>
                        <input type="text" name="account" class="form-control bg-black border-secondary text-white py-3 px-4 rounded-4" style="background: #0b0e11 !important; border-color: #2b3139 !important;" placeholder="<?= __('account') ?>" required>
                    </div>

                    <div class="mb-4">
                        <div class="d-flex justify-content-between">
                            <label class="form-label text-muted small fw-bold"><?= __('password') ?></label>
                            <a href="#" class="small text-primary text-decoration-none"><?= __('forgot_password') ?></a>
                        </div>
                        <input type="password" name="password" class="form-control bg-black border-secondary text-white py-3 px-4 rounded-4" style="background: #0b0e11 !important; border-color: #2b3139 !important;" placeholder="<?= __('password') ?>" required>
                    </div>

                    <div class="d-flex gap-3 mb-4">
                        <button type="submit" class="btn btn-primary flex-grow-1 py-3 fw-bold rounded-pill shadow-primary"><?= __('login') ?></button>
                        <a href="/auth/register.php" class="btn btn-outline-secondary flex-grow-1 py-3 fw-bold rounded-pill d-flex align-items-center justify-content-center" style="border-color: #2b3139; color: #fff;"><?= __('register') ?></a>
                    </div>
                    
                    <div class="text-center p-3 rounded-4 bg-black bg-opacity-25 border border-secondary border-opacity-25">
                        <span class="small text-muted"><?= __('no_account') ?></span>
                        <a href="/auth/register.php" class="ms-1 text-primary fw-bold text-decoration-none small"><?= __('register_now') ?></a>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

<?php include __DIR__ . '/../includes/footer.php'; ?>
