<?php
include_once '../db/koneksi.php';
include_once '../api/sgx.php';
include_once '../api/nexus.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_deposit'])) {
    $id = $_GET['id_deposit'];
    $deposit = mysqli_query($koneksi, "SELECT * FROM tb_deposit WHERE id = '$id'");
    $data = mysqli_fetch_array($deposit);

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

    $extplayer = $data['extplayer'];
    $username = $data['username'];
    $jumlah = $data['jumlah'];
    $status = $data['status'];
    $ref = $data['ref'];
} else {
    echo '
      <script>
        alert("Pilih deposit yang ingin diubah!");
        window.location.replace("'.$alamat_admin.'deposit");
      </script>
    ';
    exit;
}

// --- JIKA TOMBOL SIMPAN DITEKAN ---
if (isset($_POST['ubah_data'])) {
    $status_baru = $_POST['status'];

    if ($status_baru == 'Sukses') {
        $jumlah = floatval($jumlah);

        // --- PROSES DEPOSIT KE API ---
        $res_nexus = json_decode($nx->deposit_member($extplayer, $jumlah));
        $res_sgx   = json_decode($SGX->deposit_member($extplayer, $jumlah));

        if ($res_nexus->status != 1 || $res_sgx->status != 200) {
            mysqli_query($koneksi, "UPDATE tb_user SET saldo = saldo + $jumlah WHERE extplayer = '$extplayer'");
            echo '
              <script>
                alert("Gagal proses ke provider: '. addslashes($res_nexus->msg ?? $res_sgx->message ?? 'Unknown error') .'");
                window.location.replace("'.$alamat_admin.'deposit");
              </script>
            ';
            exit;
        }
    }

    // --- UPDATE STATUS DI DATABASE ---
    $update = mysqli_query($koneksi, "UPDATE tb_deposit SET status = '$status_baru', updated_at = NOW() WHERE id = '$id'");
    if ($update) {
        echo '
          <script>
            alert("Berhasil ubah data.");
            window.location.replace("'.$alamat_admin.'deposit");
          </script>
        ';
    } else {
        echo "Proses Gagal<br>Error : ".mysqli_error($koneksi);
    }
}

// --- HAPUS DATA ---
if (isset($_POST['hapus_data'])) {
    $hapus = mysqli_query($koneksi, "DELETE FROM tb_deposit WHERE id = '$id'");
    if ($hapus) {
        echo '
          <script>
            alert("Berhasil hapus data.");
            window.location.replace("'.$alamat_admin.'deposit");
          </script>
        ';
    } else {
        echo "Proses Gagal<br>Error : ".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">Deposit</div>
    </div>
    <div class="col-md-6">
      <div class="text-center text-md-end">
        <a href="<?php echo $alamat_admin.'deposit'; ?>" 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 Data Deposit</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 htmlspecialchars($username); ?>" readonly disabled>
            <label>Username</label>
          </div>
        </div>
        <div class="col-md-6">
          <div class="form-floating form-floating-outline">
            <input type="text" class="form-control" value="<?php echo number_format($jumlah,0,',','.'); ?>" readonly disabled>
            <label>Jumlah Deposit</label>
          </div>
        </div>

        <div class="col-md-12">
          <div class="form-floating form-floating-outline mb-4">
            <?php if ($status == "Pending") { ?>
              <select name="status" class="form-select select2" required>
            <?php } else { ?>
              <select name="status" class="form-select select2" required disabled>
            <?php } ?>

              <option value="Pending"   <?= $status=='Pending' ? 'selected' : '' ?>>Diproses</option>
              <option value="Rejected" <?= $status=='Rejected' ? 'selected' : '' ?>>Dibatalkan</option>
              <option value="Sukses"  <?= $status=='Sukses' ? 'selected' : '' ?>>Disetujui</option>
            </select>
            <label>Status</label>
          </div>
        </div>
      </div>

      <div class="pt-4 text-end">
        <button type="button" class="btn btn-danger waves-effect waves-light me-sm-3 me-1" 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 waves-effect waves-light">
          <span class="tf-icons mdi mdi-content-save me-1"></span>
          Simpan
        </button>
      </div>
    </form>
  </div>
</div>

<!-- Modal Hapus Data -->
<div class="modal fade" id="hapus_data" data-bs-backdrop="static" data-bs-keyboard="false" tabindex="-1">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <h1 class="modal-title fs-5">Hapus Data</h1>
        <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>
