<?php
 $id_login = $_SESSION['id'];

include '../../function/connect.php';
include '../../play/game/config.php';
include '../../play/game/functions.php';

if ($id_login == '') {
    if (isset($_POST['submit'])) {
        $username = strtolower($_POST['username']);
        $password = md5($_POST['password']);
        $konfirmasi_password = md5($_POST['konfirmasi_pass']);
        $email = $_POST['email'];
        $no_whatsapp = $_POST['no_whatsapp'];
        
        // FIX: Sesuaikan name dengan yang ada di form HTML
        $bank = $_POST['nama_bank'];
        $pemilik_rekening = $_POST['nama_pemilik'];
        $norek = $_POST['nomor_rekening'];
        
        $refferal = $_POST['refferal'];
        $captcha_asli = $_POST['captcha_asli'];
        $captcha = $_POST['captcha'];
        $status = 'Active';
        $level = 'user';
        $user = str_replace(' ', '', $username);
        $karakter = 'ABCDEFGHJKLMNPQRSTUVWXYZ23456789';
        $kode = '';
        for ($i=0; $i<6; $i++) {
            $kode .= $karakter[random_int(0, strlen($karakter)-1)];
        }
        $extplayer = $user;

        $cek_username = mysqli_query($koneksi, "SELECT * FROM tb_user WHERE username = '$username' ");
        $liat_username = mysqli_num_rows($cek_username);

        // FIX: Tambah cek email biar nggak error undefined variable
        $cek_email = mysqli_query($koneksi, "SELECT * FROM tb_user WHERE email = '$email' ");
        $liat_email = mysqli_num_rows($cek_email);

        if ($liat_email == 1) {
            header("Location: ../index.php?page=daftar&pesan=1");
        } else if ($liat_username == 1) {
            header("Location: ../index.php?page=daftar&pesan=30");
        } else if ($captcha != $captcha_asli) {
            header("Location: ../index.php?page=daftar&pesan=2");
        } else {
            if ($password == $konfirmasi_password) {

                $createAccountMember = nexusCreateUser($extplayer);
                $query = mysqli_query($koneksi, "INSERT INTO tb_user (id, extplayer, username, password, nama_lengkap, email, no_hp, level, refferal, status, status_game) VALUES (NULL, '$extplayer', '$username', '$password', '$pemilik_rekening', '$email', '$no_whatsapp', '$level','$extplayer','$status','ongame') ");
                $query2 = mysqli_query($koneksi, "INSERT INTO tb_saldo (id, id_user, active, transfer, pending, payout) VALUES (NULL, '$extplayer', '0','0','0','0') ");
                $query3 = mysqli_query($koneksi, "INSERT INTO tb_bank (id, icon, nama_bank, nomor_rekening, nama_pemilik, id_user, level) VALUES (NULL,'', '$bank', '$norek', '$pemilik_rekening', '$extplayer','') ");

                $query5 = mysqli_query($koneksi, "SELECT * FROM tb_user WHERE refferal = '$refferal' ");
                $cek_reff = mysqli_num_rows($query5);

                if ($cek_reff > 0) {
                    if ($refferal != "") {
                        $query4 = mysqli_query($koneksi, "INSERT INTO tb_refferal (id, user_refferal, keterangan, bonus, id_user) VALUES (NULL, '$refferal','Downline','0','$extplayer') ");
                    }
                }

                if ($query) {
                    $query = mysqli_query($koneksi, "SELECT * FROM tb_user WHERE username = '$username' AND password = '$password' ");
                    if ($hitung = 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?pesan=3");
                        }
                    }
                }
            } else {
                header("Location: ../index.php?page=daftar&pesan=4");
            }
        }
    } else {
        header("Location: ../index.php?page=daftar&pesan=5");
    }
} else {
    header("Location:../index.php");
}
?>