<?php
include '../../function/connect.php';

if (isset($_POST['submit'])) {
    // ================= VALIDASI CAPTCHA (SAMA KAYA DAFTAR) =================
    $captcha_asli = $_POST['captcha_asli'];
    $captcha      = $_POST['captcha'];

    if ($captcha != $captcha_asli) {
        header("Location: ../index.php?pesan=2"); // Kode verifikasi salah
        exit;
    }
    // ========================================================================

    $username = mysqli_real_escape_string($koneksi, $_POST['username']);
    $password = md5($_POST['password']);

    $query = mysqli_query($koneksi, "SELECT * FROM tb_user WHERE username = '$username' AND password = '$password' ");
    
    if (mysqli_num_rows($query) == 1) {
        $data = mysqli_fetch_array($query);
        $status = $data['status'];

        if ($status == 'Active') {
            $id = $data['id'];
            $level = $data['level'];
            $_SESSION['username'] = $data['username'];
            $_SESSION['extplayer'] = $data['extplayer'];
            $_SESSION['id'] = $id;

            header("Location: ../index.php");
            exit;
        } else {
            header("Location: ../index.php?pesan=6"); // Akun tidak aktif
            exit;
        }
    } else {
        header("Location: ../index.php?pesan=7"); // Username / password salah
        exit;
    }
}
?>