<?php
session_start();
include('../config/koneksi.php');

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

if (empty($user) || empty($pass)) {
    header('location:/m/?notif=1');
    exit;
}

$q = mysqli_query($conn, "SELECT * FROM `tb_user` WHERE username = '$user'");
if (mysqli_num_rows($q) > 0) {
    $user_data = mysqli_fetch_array($q, MYSQLI_ASSOC);
    $statusnya = $user_data['status'];

    if ($user_data['level'] === 'Promotor' || $user_data['level'] === 'Staff') {
        header('location:/m/?notif=3');
        exit;
    }

    if ($statusnya == 1) {
        $password = $user_data['password'];

        if (password_verify($pass, $password)) {
            if (($user_data['level'] ?? 'user') === 'user' && empty($user_data['referral_code'])) {
                require_once __DIR__ . '/includes/member_referral.php';
                member_referral_ensure_code($conn, $user_data);
            }
            $_SESSION['user'] = $user;
            header('location:../?notif=4');
            exit;
        } else {
            header('location:/m/?notif=3');
            exit;
        }
    } elseif ($statusnya == 0) {
        // Jika status adalah 0, akun sedang terkunci
        header('location:/m/?notif=10'); // Notifikasi untuk akun terkunci
        exit;
    } else {
        header('location:/m/?notif=3');
        exit;
    }
} else {
    header('location:/m/?notif=3');
    exit;
}
?>
