<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
ob_start();
include_once '../db/koneksi.php';
include_once '../api/nexus.php';
include_once '../api/sgx.php';
	
if (!isset($_SESSION['kode_admin'])) {
    echo '
        <script>
            alert("Terjadi kesalahan, harap masuk kembali!");
            window.location.replace("' . $alamat_admin . 'keluar.php");
        </script>
    ';
    exit();
}

if (isset($_GET['id_anggota'])) {
    $id_anggota = $_GET['id_anggota'];
    $anggota = mysqli_query($koneksi, "SELECT * FROM tb_user WHERE id = '$id_anggota'");
    $data_anggota = mysqli_fetch_array($anggota);

    $saldo_anggota = $data_anggota['saldo'];
    $nama_pengguna_anggota = $data_anggota['username'];
    $extplayer_anggota = $data_anggota['extplayer']; 
} else {
    echo '
        <script>
            alert("Pilih anggota yang ingin diubah!");
            window.location.replace("' . $alamat_admin . 'saldo");
        </script>
    ';
    exit();
}

if (isset($_POST['ubah_data'])) {
    $saldo_anggota_2 = $_POST['saldo_anggota'];
    $status_transaksi = $_POST['status_transaksi'];
    
    if ($status_transaksi == 'deposit') {
        $jumlah_deposit = $saldo_anggota_2;
        $proses_api = $nx->deposit_member($extplayer_anggota, $jumlah_deposit);
        $nxsApi = $SGX->deposit_member($extplayer_anggota, $jumlah_deposit);

        $proses_api = json_decode($proses_api);
        $nxsApi = json_decode($nxsApi);

        if ($proses_api->status == 1) {
            $saldo_baru = $saldo_anggota + $jumlah_deposit;
            $ubah_saldo = mysqli_query($koneksi, "UPDATE tb_user SET saldo = '$saldo_baru' WHERE id = '$id_anggota'");
            
            if ($ubah_saldo) {
                echo '
                    <script>
                        alert("Berhasil tambah saldo.");
                        window.location.replace("' . $alamat_admin . 'saldo");
                    </script>
                ';
            } else {
                echo "Proses Gagal<br>Error : " . mysqli_error($koneksi);
            }

            
        } else {
            echo '
                <script>
                    alert("Proses deposit gagal: ' . htmlspecialchars($proses_api->msg) . '");
                    window.location.replace("' . $alamat_admin . 'ubah_saldo/' . $id_anggota . '");
                </script>
            ';
        }
    } elseif ($status_transaksi == 'withdraw') {
        // Menggunakan API untuk melakukan withdraw
        $jumlah_withdraw = $saldo_anggota_2;
        $proses_api = $nx->withdraw_member($extplayer_anggota, $jumlah_withdraw);
        $nxsApi = $SGX->withdraw_member($extplayer_anggota, $jumlah_withdraw);

        $proses_api = json_decode($proses_api);
        $nxsApi = json_decode($nxsApi);

        if ($proses_api->status == 1 && $nxsApi->status == 200) {
            $saldo_baru = $saldo_anggota - $jumlah_withdraw;
            $ubah_saldo = mysqli_query($koneksi, "UPDATE tb_user SET saldo = '$saldo_baru' WHERE extplayer = '$extplayer_anggota'");
            
            if ($ubah_saldo) {
                echo '
                    <script>
                        alert("Berhasil kurangi saldo.");
                        window.location.replace("' . $alamat_admin . 'saldo");
                    </script>
                ';
            } else {
                echo "Proses Gagal<br>Error : " . mysqli_error($koneksi);
            }
        } else {
            echo '
                <script>
                    alert("Proses withdraw gagal: ' . htmlspecialchars($proses_api->msg) . '");
                    window.location.replace("' . $alamat_admin . 'ubah_saldo/' . $id_anggota . '");
                </script>
            ';
        }
    } else {
        echo '
            <script>
                alert("Status tidak valid!");
                window.location.replace("' . $alamat_admin . 'ubah_saldo/' . $id_anggota . '");
            </script>
        ';
    }
}
?>

<div class="container-xxl flex-grow-1 container-p-y">
    <div class="row gy-4 mb-4">
        <div class="col-md-6">
            <div class="fw-bold fs-4 text-center text-md-start">Anggota</div>
        </div>
        <div class="col-md-6">
            <div class="text-center text-md-end">
                <a href="<?php echo $alamat_admin . 'anggota'; ?>" class="btn btn-sm btn-primary waves-effect waves-light">
                    <span class="tf-icons mdi mdi-chevron-double-left me-1"></span>
                    Kembali
                </a>
            </div>
        </div>
    </div>

    <div class="card mb-4">
        <h5 class="card-header">Ubah Saldo Anggota</h5>
        <form method="post" class="card-body">
            <hr class="my-4 mx-n4">
            <h6>Tambah Saldo</h6>
            <div class="row g-3">
                <div class="col-md-4">
                    <div class="form-floating form-floating-outline">
                        <input type="text" name="saldo_anggota" class="form-control" value="<?php echo htmlspecialchars($saldo_anggota); ?>" required>
                        <label>Saldo</label>
                    </div>
                </div>
                <div class="col-md-4">
                    <div class="form-floating form-floating-outline mb-4">
                        <select name="status_transaksi" class="form-select select2" required>
                            <option value="deposit">Tambah Saldo</option>
                            <option value="withdraw">Kurangi Saldo</option>
                        </select>
                        <label>Status Transaksi</label>
                    </div>
                </div>
            </div>
            <div class="pt-4 text-end">
                <button type="submit" name="ubah_data" class="btn btn-primary waves-effect waves-light">
                    <span class="tf-icons mdi mdi-content-save me-1"></span>
                    Simpan
                </button>
            </div>
        </form>
    </div>
</div>
