<?php
session_start();
include "../config/setting.php";

$error = '';

// Jika sudah login
if (!empty($_SESSION['login']) && $_SESSION['role'] == 'member') { header("Location: " . $http . "dashboard/"); exit; }
if (!empty($_SESSION['login']) && $_SESSION['role'] == 'admin') { header("Location: " . $http . "administrator/"); exit; }

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

    $login    = mysql_real_escape_string($_POST['username']);
    $password = sha1(mysql_real_escape_string($_POST['password']));
    $idweb    = mysql_real_escape_string($idweb);

    $q = mysql_query("SELECT * FROM user WHERE (email = '$login' OR telpon='$login') AND password = '$password' AND status = '1' LIMIT 1");
    

    if ($q && mysql_num_rows($q) == 1) {

        $u = mysql_fetch_assoc($q);

        $_SESSION['login']  = true;
        $_SESSION['email']  = $u['email'];
        $_SESSION['role']   = $u['role'];
        $_SESSION['nama']   = $u['nama'];
        $_SESSION['id_user']   = $u['id'];

        // Redirect berdasarkan role
        // if ($u['role'] == 'admin' or $u['role'] == 'member') {
        if ($u['role'] == 'member') { header("Location: " . $http . "dashboard/"); } 
        elseif ($u['role'] == 'admin') { header("Location: " . $http . "administrator/"); } exit;

    } else { $error = mysql_num_rows($q); }
}
?>

<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Login - <?= $set['nama'] ?></title>

    <!-- Bootstrap -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">

    <!-- Lucide -->
    <script src="https://unpkg.com/lucide@latest"></script>

    <!-- Font -->
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&display=swap" rel="stylesheet">

    <!-- Custom CSS -->
    <link rel="stylesheet" href="../assets/css/style.css">
</head>
<body class="d-flex flex-column min-vh-100">

<?= inc('navbar') ?>

<main class="flex-grow-1">
    <div class="container py-5 min-vh-100 d-flex align-items-center justify-content-center">
        <div class="card border-0 shadow-lg rounded-4" style="max-width:450px;width:100%;">
            <div class="card-body p-5">

                <div class="text-center mb-4">
                    <h3 class="fw-bold text-primary">Login</h3>
                    <p class="text-muted small">Masuk untuk mengakses sistem</p>
                </div>

                <?php if ($error): ?>
                    <div class="alert alert-danger small">
                        <?= $error ?>
                    </div>
                <?php endif; ?>

                <!-- FORM LOGIN -->
                <form method="post">
                    <div class="mb-3">
                        <label class="form-label fw-medium small">Username</label>
                        <div class="input-group">
                            <span class="input-group-text bg-light border-end-0">
                                <i data-lucide="user" width="18"></i>
                            </span>
                            <input type="text"
                                   name="username"
                                   class="form-control bg-light border-start-0 ps-0"
                                   placeholder="Masukan Username atau Email"
                                   required>
                        </div>
                    </div>

                    <div class="mb-4">
                        <label class="form-label fw-medium small">Password</label>
                        <div class="input-group">
                            <span class="input-group-text bg-light border-end-0">
                                <i data-lucide="lock" width="18"></i>
                            </span>
                            <input type="password"
                                   name="password"
                                   class="form-control bg-light border-start-0 ps-0"
                                   placeholder="Masukan Password"
                                   required>
                        </div>
                    </div>

                    <div class="d-grid mb-3">
                        <button type="submit" name="login" class="btn btn-primary py-2 fw-bold">
                            Masuk
                        </button>
                    </div>
                </form>

                <div class="text-center small">
                    <p class="text-muted m-0">
                        Belum punya akun?
                        <a href="./register/" class="text-decoration-none fw-bold">Daftar sekarang</a>
                    </p>
                </div>

            </div>
        </div>
    </div>
</main>

<?= inc('footer') ?>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
<script>
    lucide.createIcons();
</script>

</body>
</html>
