<?php
/**
 * Login Page
 * Secure login with role-based redirect
 */
require_once __DIR__ . '/auth/functions.php';

// Redirect if already logged in
if (isLoggedIn()) {
    $role = getUserRole();
    if ($role === 'superadmin') {
        header('Location: /muanjai/superadmin/index.php');
    } elseif (in_array($role, ['manager', 'staff'])) {
        header('Location: /muanjai/system/dashboard.php');
    } elseif ($role === 'packer') {
        header('Location: /muanjai/system/pos/packer.php');
    } else {
        header('Location: /muanjai/user/dashboard.php');
    }
    exit;
}

$error = '';
$success = '';

// Check for timeout message
if (isset($_GET['timeout'])) {
    $error = 'ເຊສຊັນໝົດອາຍຸ ກະລຸນາເຂົ້າສູ່ລະບົບໃໝ່';
}

// Check for success message from registration
if (isset($_GET['registered'])) {
    $success = 'ລົງທະບຽນສຳເລັດແລ້ວ! ກະລຸນາເຂົ້າສູ່ລະບົບ';
}

// Process login form
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    // Validate CSRF token
    if (!validateCSRFToken($_POST['csrf_token'] ?? '')) {
        $error = 'ຄຳຂໍບໍ່ຖືກຕ້ອງ ກະລຸນາລອງໃໝ່';
    } else {
        $username = $_POST['username'] ?? '';
        $password = $_POST['password'] ?? '';

        $result = loginUser($pdo, $username, $password);

        if ($result['success']) {
            regenerateCSRFToken();

            // Redirect based on role
            if ($result['role'] === 'superadmin') {
                header('Location: /muanjai/superadmin/index.php');
            } elseif (in_array($result['role'], ['manager', 'staff'])) {
                header('Location: /muanjai/system/dashboard.php');
            } elseif ($result['role'] === 'packer') {
                header('Location: /muanjai/system/pos/packer.php');
            } else {
                header('Location: /muanjai/staff/dashboard.php');
            }
            exit;
        } else {
            $error = $result['error'];
        }
    }
}

$csrfToken = generateCSRFToken();
?>
<!DOCTYPE html>
<html lang="lo">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="ເຂົ້າສູ່ລະບົບ - Muanjai">
    <title>ເຂົ້າສູ່ລະບົບ | Muanjai</title>
    <link href="https://fonts.googleapis.com/css2?family=Noto+Sans+Lao:wght@300;400;500;600;700&display=swap"
        rel="stylesheet">
    <link rel="icon" type="image/png" href="img/logo/muanjai_logo.png">
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Noto Sans Lao', sans-serif;
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
            background: linear-gradient(135deg, #1a1a2e 0%, #16213e 50%, #0f0f23 100%);
            padding: 20px;
        }

        .login-container {
            width: 100%;
            max-width: 420px;
        }

        .login-card {
            background: rgba(255, 255, 255, 0.05);
            backdrop-filter: blur(20px);
            border-radius: 24px;
            padding: 48px 40px;
            border: 1px solid rgba(193, 153, 42, 0.2);
            box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.5);
        }

        .logo {
            text-align: center;
            margin-bottom: 32px;
        }

        .logo-icon {
            width: 120px;
            height: 120px;
            /* background: linear-gradient(135deg, #c1992a 0%, #e8c547 100%); */
            /* border-radius: 20px; */
            display: inline-flex;
            align-items: center;
            justify-content: center;
            margin-bottom: 16px;
            /* box-shadow: 0 10px 40px rgba(193, 153, 42, 0.3); */
        }

        .logo-icon img {
            width: 100%;
            height: 100%;
            object-fit: contain;
            /* fill: #ffffff; */
        }

        .logo h1 {
            color: #ffffff;
            font-size: 28px;
            font-weight: 600;
            margin-bottom: 8px;
        }

        .logo p {
            color: rgba(255, 255, 255, 0.6);
            font-size: 14px;
        }

        .form-group {
            margin-bottom: 24px;
        }

        .form-group label {
            display: block;
            color: rgba(255, 255, 255, 0.8);
            font-size: 14px;
            font-weight: 500;
            margin-bottom: 8px;
        }

        .input-wrapper {
            position: relative;
        }

        .input-wrapper svg {
            position: absolute;
            left: 16px;
            top: 50%;
            transform: translateY(-50%);
            width: 20px;
            height: 20px;
            fill: rgba(255, 255, 255, 0.4);
            transition: fill 0.3s ease;
        }

        .form-group input {
            width: 100%;
            padding: 16px 16px 16px 48px;
            background: rgba(255, 255, 255, 0.08);
            border: 1px solid rgba(255, 255, 255, 0.1);
            border-radius: 12px;
            font-size: 16px;
            color: #ffffff;
            font-family: 'Noto Sans Lao', sans-serif;
            transition: all 0.3s ease;
        }

        .form-group input:focus {
            outline: none;
            border-color: #c1992a;
            background: rgba(255, 255, 255, 0.1);
            box-shadow: 0 0 0 4px rgba(193, 153, 42, 0.15);
        }

        .form-group input:focus+svg,
        .input-wrapper:focus-within svg {
            fill: #c1992a;
        }

        .form-group input::placeholder {
            color: rgba(255, 255, 255, 0.4);
        }

        .alert {
            padding: 14px 16px;
            border-radius: 12px;
            margin-bottom: 24px;
            font-size: 14px;
            display: flex;
            align-items: center;
            gap: 10px;
        }

        .alert-error {
            background: rgba(239, 68, 68, 0.15);
            border: 1px solid rgba(239, 68, 68, 0.3);
            color: #fca5a5;
        }

        .alert-success {
            background: rgba(34, 197, 94, 0.15);
            border: 1px solid rgba(34, 197, 94, 0.3);
            color: #86efac;
        }

        .btn-login {
            width: 100%;
            padding: 16px;
            background: linear-gradient(135deg, #c1992a 0%, #e8c547 100%);
            border: none;
            border-radius: 12px;
            color: #ffffff;
            font-size: 16px;
            font-weight: 600;
            font-family: 'Noto Sans Lao', sans-serif;
            cursor: pointer;
            transition: all 0.3s ease;
            box-shadow: 0 4px 15px rgba(193, 153, 42, 0.3);
        }

        .btn-login:hover {
            transform: translateY(-2px);
            box-shadow: 0 8px 25px rgba(193, 153, 42, 0.4);
        }

        .btn-login:active {
            transform: translateY(0);
        }

        .register-link {
            text-align: center;
            margin-top: 24px;
            color: rgba(255, 255, 255, 0.6);
            font-size: 14px;
        }

        .register-link a {
            color: #c1992a;
            text-decoration: none;
            font-weight: 500;
            transition: color 0.3s ease;
        }

        .register-link a:hover {
            color: #e8c547;
        }

        @media (max-width: 480px) {
            .login-card {
                padding: 32px 24px;
            }

            .logo h1 {
                font-size: 24px;
            }
        }
    </style>
</head>

<body>
    <div class="login-container">
        <div class="login-card">
            <div class="logo">
                <div class="logo-icon">
                    <img src="img/logo/muanjai_logo.png" alt="Muanjai Logo">
                </div>
                <h1>Muanjai</h1>
                <p>ເຂົ້າສູ່ລະບົບເພື່ອດຳເນີນການຕໍ່</p>
            </div>

            <?php if (!empty($error)): ?>
                <div class="alert alert-error">
                    <svg width="20" height="20" viewBox="0 0 24 24" fill="currentColor">
                        <path
                            d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm1 15h-2v-2h2v2zm0-4h-2V7h2v6z" />
                    </svg>
                    <?= e($error) ?>
                </div>
            <?php endif; ?>

            <?php if (!empty($success)): ?>
                <div class="alert alert-success">
                    <svg width="20" height="20" viewBox="0 0 24 24" fill="currentColor">
                        <path
                            d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm-2 15l-5-5 1.41-1.41L10 14.17l7.59-7.59L19 8l-9 9z" />
                    </svg>
                    <?= e($success) ?>
                </div>
            <?php endif; ?>

            <form method="POST" action="" id="loginForm">
                <input type="hidden" name="csrf_token" value="<?= e($csrfToken) ?>">

                <div class="form-group">
                    <label for="username">ຊື່ຜູ້ໃຊ້ ຫຼື ອີເມວ</label>
                    <div class="input-wrapper">
                        <input type="text" id="username" name="username" placeholder="username ຫຼື email@example.com"
                            required autocomplete="username">
                        <svg viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg">
                            <path
                                d="M12 12c2.21 0 4-1.79 4-4s-1.79-4-4-4-4 1.79-4 4 1.79 4 4 4zm0 2c-2.67 0-8 1.34-8 4v2h16v-2c0-2.66-5.33-4-8-4z" />
                        </svg>
                    </div>
                </div>

                <div class="form-group">
                    <label for="password">ລະຫັດຜ່ານ</label>
                    <div class="input-wrapper">
                        <input type="password" id="password" name="password" placeholder="••••••••" required
                            autocomplete="current-password">
                        <svg viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg">
                            <path
                                d="M18 8h-1V6c0-2.76-2.24-5-5-5S7 3.24 7 6v2H6c-1.1 0-2 .9-2 2v10c0 1.1.9 2 2 2h12c1.1 0 2-.9 2-2V10c0-1.1-.9-2-2-2zm-6 9c-1.1 0-2-.9-2-2s.9-2 2-2 2 .9 2 2-.9 2-2 2zm3.1-9H8.9V6c0-1.71 1.39-3.1 3.1-3.1 1.71 0 3.1 1.39 3.1 3.1v2z" />
                        </svg>
                    </div>
                </div>

                <button type="submit" class="btn-login">ເຂົ້າສູ່ລະບົບ</button>
            </form>

            <div class="register-link">
                ຍັງບໍ່ມີບັນຊີ? <a href="register.php">ລົງທະບຽນ</a>
            </div>
        </div>
    </div>
</body>

</html>