<?php
session_start();
require 'function.php';
require 'config.php';

if (isset($_SESSION['login'])) {
    header('location:dashboard');
    exit;
}

$koneksi = mysqli_connect($dbhost,$dbuser,$dbpassword,$dbname);

// Get App Info
$result = mysqli_query($koneksi, "SELECT * FROM users_id" );
$whatsapp = mysqli_fetch_assoc($result);
$logo = $whatsapp['logo'];

$event = mysqli_query($koneksi, "SELECT * FROM event_id" );
$checkevent = mysqli_fetch_assoc($event);
$acara = $checkevent['acara'];

$error = false;
if (isset($_POST['login'])) {
    $username = $_POST['username'];
    $password = $_POST['password'];
    $new = md5($password);
    
    $result = mysqli_query($koneksi, "SELECT * FROM users_id WHERE username='$username' AND password='$new'");
    $cek = mysqli_num_rows($result);
    $data = mysqli_fetch_array($result);

    if ($cek > 0) {
        $_SESSION['login'] = true;
        $_SESSION['role'] = $data['role'];
        $_SESSION['id'] = $data['id'];
        
        // Initialize Active Event
        $user_id = $data['id'];
        $res_ev = mysqli_query($koneksi, "SELECT event_id FROM user_events WHERE user_id = '$user_id' LIMIT 1");
        if ($res_ev && $ev_data = mysqli_fetch_assoc($res_ev)) {
            $_SESSION['active_event_id'] = $ev_data['event_id'];
        }
        
        header('location:dashboard');
        exit;
    }
    $error = true;
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?= $acara;?></title>
    <!-- CSS -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.0/font/bootstrap-icons.css">
    <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;500;600;700&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="css/modern.css">
    <script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>
    <link rel="icon" type="image/png" href="<?=$logo;?>">
</head>

<body class="d-flex align-items-center min-vh-100">

    <div class="container">
        <div class="row justify-content-center">
            <div class="col-md-5">
                <div class="glass-card text-center p-5">
                    
                    <div class="mb-4">
                        <img src="<?php echo $whatsapp['logo'];?>" alt="Logo" class="img-fluid" style="max-height: 80px;">
                    </div>
                    
                    <h3 class="fw-bold mb-1 text-gradient">Welcome Back</h3>
                    <p class="text-muted mb-4">Login to Access Dashboard</p>

                    <?php if ($error) : ?>
                        <script>swal("Oops!", "Username/Password Salah", "error");</script>
                        <div class="alert alert-danger py-2">Invalid Credentials</div>
                    <?php endif; ?>

                    <form action="" method="post">
                        <div class="form-floating mb-3 text-start">
                            <input type="text" class="form-control" id="username" name="username" placeholder="Username" required>
                            <label for="username">Username</label>
                        </div>
                        
                        <div class="form-floating mb-4 text-start position-relative">
                            <input type="password" class="form-control" id="password" name="password" placeholder="Password" required style="padding-right: 45px;">
                            <label for="password">Password</label>
                            <span class="position-absolute top-50 end-0 translate-middle-y me-3 text-muted" style="cursor: pointer; z-index: 10;" onclick="togglePasswordVisibility('password', 'eyeIcon')">
                                <i class="bi bi-eye-slash" id="eyeIcon"></i>
                            </span>
                        </div>
                        
                        <button type="submit" name="login" class="btn btn-primary w-100 py-3 mb-3">
                            <i class="bi bi-box-arrow-in-right me-2"></i> Login
                        </button>
                    </form>

                    <div class="text-center mt-3">
                        <a href="reset-password" class="text-decoration-none text-muted small hover-text-primary">
                            Forgot Password?
                        </a>
                    </div>

                </div>
            </div>
        </div>
    </div>
    
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
    <script>
        function togglePasswordVisibility(inputId, iconId) {
            const passwordInput = document.getElementById(inputId);
            const icon = document.getElementById(iconId);
            if (passwordInput.type === 'password') {
                passwordInput.type = 'text';
                icon.classList.remove('bi-eye-slash');
                icon.classList.add('bi-eye');
            } else {
                passwordInput.type = 'password';
                icon.classList.remove('bi-eye');
                icon.classList.add('bi-eye-slash');
            }
        }
    </script>
</body>
</html>
