<?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';
?>

<style>
    body {
        background: radial-gradient(circle at top right, #1a1f26, #0b0e11);
        min-height: 100vh;
    }
    .auth-card {
        background: #1e2329 !important;
        border: 1px solid rgba(255, 255, 255, 0.05) !important;
        border-radius: 24px !important;
        box-shadow: 0 40px 100px rgba(0,0,0,0.6) !important;
    }
    .form-control {
        background: rgba(0, 0, 0, 0.3) !important;
        border: 1px solid #2b3139 !important;
        color: #fff !important;
        font-size: 15px !important;
        transition: all 0.3s ease;
    }
    .form-control:focus {
        border-color: var(--primary) !important;
        box-shadow: 0 0 0 4px rgba(0, 98, 255, 0.1) !important;
        background: rgba(0, 0, 0, 0.5) !important;
        border-radius: 12px;
    }
    .form-label {
        font-size: 14px !important;
        color: #e5e7eb !important;
        margin-bottom: 8px !important;
        font-weight: 600 !important;
    }
    .text-muted {
        color: #9ba3af !important;
    }
    .logo-text {
        background: linear-gradient(135deg, #fff 0%, #9ba3af 100%);
        -webkit-background-clip: text;
        -webkit-text-fill-color: transparent;
        font-weight: 900 !important;
    }
</style>

<div class="container py-5">
    <div class="row justify-content-center align-items-center" style="min-height: 80vh;">
        <div class="col-md-5">
            <div class="card auth-card p-4 p-md-5">
                <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" style="font-size: 28px;"><?= __('login') ?></h2>
                    <p class="text-muted" style="font-size: 15px;"><?= __('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"><?= __('account') ?></label>
                        <input type="text" name="account" class="form-control py-3 px-4 rounded-4" placeholder="<?= __('account') ?>" required>
                    </div>

                    <div class="mb-4">
                        <div class="d-flex justify-content-between mb-2">
                            <label class="form-label m-0"><?= __('password') ?></label>
                            <a href="/auth/forgot.php" class="small text-primary text-decoration-none"><?= __('forgot_password') ?></a>
                        </div>
                        <input type="password" name="password" class="form-control py-3 px-4 rounded-4" 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'; ?>
