<?php
session_start();
require_once 'includes/config.php';
require_once 'includes/db.php';
require_once 'includes/functions.php';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if (!validate_csrf($_POST['csrf_token'] ?? '')) {
        die('Invalid request');
    }

    $email = trim($_POST['email'] ?? '');
    $password = trim($_POST['password'] ?? '');

    if (!$email || !$password) {
        set_flash('error', 'Email dan password harus diisi');
    } else {
        $stmt = $conn->prepare('SELECT id, password_hash, role FROM users WHERE email = ? AND is_active = 1');
        $stmt->bind_param('s', $email);
        $stmt->execute();
        $result = $stmt->get_result();
        $user = $result->fetch_assoc();
        $stmt->close();

        if ($user && password_verify($password, $user['password_hash'])) {
            session_regenerate_id(true);
            $_SESSION['user_id'] = $user['id'];
            $_SESSION['role'] = $user['role'];
            $_SESSION['lang'] = 'id';

            $redirect = $_SESSION['redirect_after_login'] ?? BASE_URL . '/' . $user['role'] . '/index.php';
            unset($_SESSION['redirect_after_login']);

            header('Location: ' . $redirect);
            exit;
        } else {
            set_flash('error', 'Email atau password salah');
        }
    }

    if ($flash = get_flash()) {
        $_SESSION['flash'] = $flash;
    }
    header('Location: ' . BASE_URL . '/login.php');
    exit;
}

$page_title = 'Login - ANATL Portal';
?>
<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?= $page_title ?></title>
    <link rel="stylesheet" href="<?= BASE_URL ?>/assets/css/style.css">
</head>
<body style="background: linear-gradient(135deg, #003A70 0%, #005BAA 100%); display: flex; align-items: center; justify-content: center; min-height: 100vh;">

<div style="background: white; padding: 48px; border-radius: 12px; box-shadow: 0 8px 24px rgba(0,0,0,.12); width: 100%; max-width: 400px;">
    <h1 style="text-align: center; color: #003A70; margin-bottom: 32px; font-size: 28px;">ANATL Portal</h1>

    <?php if ($flash = get_flash()): ?>
    <div class="alert alert--<?= esc($flash['type']) ?>">
        <?= esc($flash['msg']) ?>
    </div>
    <?php endif; ?>

    <form method="POST" class="form">
        <input type="hidden" name="csrf_token" value="<?= csrf_token() ?>">

        <div class="form-group">
            <label for="email">Email</label>
            <input type="email" id="email" name="email" required>
        </div>

        <div class="form-group">
            <label for="password">Password</label>
            <input type="password" id="password" name="password" required>
        </div>

        <button type="submit" class="btn btn--primary" style="width: 100%;">Masuk</button>
    </form>

    <hr style="margin: 24px 0; border: none; border-top: 1px solid #D1D9E0;">

    <p style="text-align: center; font-size: 12px; color: #5A6A7A; margin-bottom: 16px;">
        Demo Credentials (untuk testing):
    </p>
    <table style="width: 100%; font-size: 12px;">
        <tr><td>Username</td><td><strong>admin@anatl.tl</strong></td></tr>
        <tr><td>Password</td><td><strong>password</strong></td></tr>
        <tr><td colspan="2" style="padding-top: 8px; border-top: 1px solid #D1D9E0; text-align: center; color: #5A6A7A; padding-top: 8px;">
            Default password: <strong>password</strong>
        </td></tr>
    </table>
</div>

</body>
</html>
