<?php

session_start();

include_once '../config/database.php';
include_once '../template/lang.php';

$error = '';

/* =========================================================
   LOGIN
========================================================= */

if(isset($_POST['login'])){

    $username = mysqli_real_escape_string(
        $conn,
        $_POST['username']
    );

    $password = $_POST['password'];

    /* =====================================================
       GET ADMIN
    ===================================================== */

    $query = mysqli_query(
        $conn,
        "SELECT * FROM admins
         WHERE username='$username'"
    );

    if(!$query){

        die(mysqli_error($conn));

    }

    $data = mysqli_fetch_assoc($query);

    /* =====================================================
       VERIFY PASSWORD
    ===================================================== */

    if(
        $data &&
        password_verify(
            $password,
            $data['password']
        )
    ){

        $_SESSION['admin_login'] = true;

        $_SESSION['admin_username'] = $username;

        header("Location: index.php");
        exit;

    }else{

        $error = tr(
            'Invalid username or password.',
            'Username atau password salah.'
        );
    }
}

?>
<!DOCTYPE html>
<html lang="<?php echo h(current_lang()); ?>">

<head>

    <meta charset="UTF-8">

    <meta name="viewport"
          content="width=device-width, initial-scale=1.0">

    <title>

        <?php echo tr(
            'Admin Login — UK Trail Run 2026',
            'Masuk Admin — UK Trail Run 2026'
        ); ?>

    </title>

    <link rel="stylesheet"
          href="./assets/css/admin.css?v=<?php echo time(); ?>">

    <link rel="icon"
          type="image/webp"
          href="../assets/img/logo.webp">

</head>

<body>

    <div class="login-bg">

        <!-- BRAND -->
        <div class="login-brand">

            <div class="login-brand__logo">

                <img src="../assets/img/logo.png"
                     alt="">

            </div>

            <span class="login-brand__name">
                UK Trail Run 2026
            </span>

            <span class="login-brand__sub">

                <?php echo tr(
                    'Event Administration Portal',
                    'Portal Administrasi Acara'
                ); ?>

            </span>

        </div>

        <!-- CARD -->
        <div class="login-card">

            <div class="login-card__accent"></div>

            <h1 class="login-card__title">

                <?php echo tr(
                    'Welcome Back',
                    'Selamat Datang'
                ); ?>

            </h1>

            <p class="login-card__desc">

                <?php echo tr(
                    'Sign in to access the admin dashboard.',
                    'Masuk untuk mengakses dashboard admin.'
                ); ?>

            </p>

            <!-- ERROR -->
            <?php if($error != ''){ ?>

                <div class="login-error is-visible">

                    <?php echo $error; ?>

                </div>

            <?php } ?>

            <!-- FORM -->
            <form method="POST">

                <!-- USERNAME -->
                <div class="login-field">

                    <label class="login-label">

                        <?php echo tr(
                            'Username',
                            'Username'
                        ); ?>

                    </label>

                    <div class="login-input-wrap">

                        <input type="text"
                               name="username"
                               class="login-input"
                               required>

                    </div>

                </div>

                <!-- PASSWORD -->
                <div class="login-field">

                    <label class="login-label">

                        <?php echo tr(
                            'Password',
                            'Password'
                        ); ?>

                    </label>

                    <div class="login-input-wrap">

                        <input type="password"
                               name="password"
                               class="login-input"
                               required>

                    </div>

                </div>

                <!-- BUTTON -->
                <button type="submit"
                        name="login"
                        class="login-btn">

                    <?php echo tr(
                        'Sign In',
                        'Masuk'
                    ); ?>

                </button>

            </form>

        </div>

        <p class="login-footer">

            &copy; 2026 UK TRAIL RUN

        </p>

    </div>

</body>
</html>