<?php
include_once '../db/koneksi.php';
include_once '../api/sgx.php';
include_once '../api/nexus.php';
error_reporting(E_ALL);
ini_set("display_errors", 1);

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_withdraw'])) {
    $id_withdraw = $_GET['id_withdraw'];
    $query = mysqli_query($koneksi, "SELECT * FROM tb_withdraw WHERE id = '$id_withdraw'");
    $data = mysqli_fetch_assoc($query);

    if (!$data) {
        echo '
        <script>
        alert("Data withdraw tidak ditemukan!");
        window.location.replace("'.$alamat_admin.'withdraw");
        </script>
        ';
        exit;
    }

    $extplayer  = $data['extplayer'];
    $username_player   = $data['username'];
    $ref        = $data['ref'];
    $jumlah     = $data['jumlah'];
    $bank_asal  = $data['bank_asal'];
    $status     = $data['status'];
    $created_at = $data['created_at'];

} else {
    echo '
    <script>
    alert("Pilih withdraw yang ingin diubah!");
    window.location.replace("'.$alamat_admin.'withdraw");
    </script>
    ';
    exit;
}

// === Proses ubah data ===
if (isset($_POST['ubah_data'])) {
    $status_baru = $_POST['status_withdraw'];

    if ($status_baru == "Rejected") {
        $proses_api = json_decode($nx->deposit_member($extplayer, $jumlah));
        $nxsApi     = json_decode($SGX->deposit_member($extplayer, $jumlah));
        mysqli_query($koneksi, "UPDATE tb_user set saldo = $jumlah WHERE username = '$username_player'");

        if ($proses_api->status != 1 && $nxsApi->status != 200) {
            echo '
            <script>
            alert("Pastikan saldo API mencukupi untuk mengembalikan coin!");
            window.location.replace("'.$alamat_admin.'withdraw");
            </script>
            ';
            exit;
        }
    }

    // if ($status_baru == "Sukses") {
    //     $proses_api = json_decode($nx->withdraw_member($extplayer, $jumlah));
    //     $nxsApi     = json_decode($SGX->withdraw_member($extplayer, $jumlah));

    //     if ($proses_api->status != 1 && $nxsApi->status != 200) {
    //         echo '
    //         <script>
    //         alert("Proses withdraw gagal! Pastikan API aktif dan saldo mencukupi.");
    //         window.location.replace("'.$alamat_admin.'withdraw");
    //         </script>
    //         ';
    //         exit;
    //     }
    // }

    // Update status withdraw
    $update = mysqli_query($koneksi, "UPDATE tb_withdraw SET status = '$status_baru', updated_at = NOW() WHERE id = '$id_withdraw'");

    if ($update) {
        echo '
        <script>
        alert("Berhasil mengubah data withdraw!");
        window.location.replace("'.$alamat_admin.'withdraw");
        </script>
        ';
        exit;
    } else {
        echo "Gagal memperbarui data: " . mysqli_error($koneksi);
    }
}

if (isset($_POST['hapus_data'])) {
    $hapus = mysqli_query($koneksi, "DELETE FROM tb_withdraw WHERE id = '$id_withdraw'");
    if ($hapus) {
        echo '
        <script>
        alert("Berhasil menghapus data withdraw!");
        window.location.replace("'.$alamat_admin.'withdraw");
        </script>
        ';
        exit;
    } else {
        echo "Gagal menghapus data: " . mysqli_error($koneksi);
    }
}
?>

<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">Withdraw</div>
    </div>
    <div class="col-md-6 text-end">
      <a href="<?php echo $alamat_admin.'withdraw'; ?>" class="btn btn-sm btn-primary">
        <span class="tf-icons mdi mdi-chevron-double-left me-1"></span>Kembali
      </a>
    </div>
  </div>

  <div class="card mb-4">
    <h5 class="card-header">Ubah Data Withdraw</h5>
    <form method="post" class="card-body">
      <div class="row g-3">
        <div class="col-md-6">
          <div class="form-floating form-floating-outline">
            <input type="text" class="form-control" value="<?php echo $jumlah; ?>" readonly disabled>
            <label>Jumlah</label>
          </div>
        </div>

        <div class="col-md-6">
          <div class="form-floating form-floating-outline mb-4">
            <select name="status_withdraw" class="form-select select2" required <?php echo ($status == 'Sukses' ? 'disabled' : ''); ?>>
              <option value="Pending" <?php echo ($status == 'Pending') ? 'selected' : ''; ?>>Pending</option>
              <option value="Rejected" <?php echo ($status == 'Rejected') ? 'selected' : ''; ?>>Rejected</option>
              <option value="Sukses" <?php echo ($status == 'Sukses') ? 'selected' : ''; ?>>Sukses</option>
            </select>
            <label>Status</label>
          </div>
        </div>
      </div>

      <div class="pt-4 text-end">
        <button type="button" class="btn btn-danger me-2" data-bs-toggle="modal" data-bs-target="#hapus_data">
          <span class="tf-icons mdi mdi-delete me-1"></span>Hapus
        </button>
        <button type="submit" name="ubah_data" class="btn btn-primary">
          <span class="tf-icons mdi mdi-content-save me-1"></span>Simpan
        </button>
      </div>
    </form>
  </div>
</div>

<!-- Modal hapus -->
<div class="modal fade" id="hapus_data" tabindex="-1">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title">Hapus Data Withdraw</h5>
        <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
      </div>
      <form method="post">
        <div class="modal-body">Yakin ingin menghapus data ini?</div>
        <div class="modal-footer">
          <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Batal</button>
          <button type="submit" name="hapus_data" class="btn btn-danger">Hapus</button>
        </div>
      </form>
    </div>
  </div>
</div>
