<?php
require("../mainconfig.php");

$check_token = mysqli_query($db, "SELECT * FROM akun_qris");
$cek_token = mysqli_fetch_assoc($check_token);
$check_deposit = mysqli_query($db, "SELECT * FROM topup WHERE status = 'Waiting'");

if (mysqli_num_rows($check_deposit) == 0) {
  $update_monitor = mysqli_query($db,"UPDATE monitor_cron SET status = 'Success', response = 'Deposit request not found', tanggal = '$date $time' WHERE judul = 'cek_deposit'");
  die("Deposit request not found.");
} else {
  while($data_order = mysqli_fetch_assoc($check_deposit)) {
    $rate = 1;
    $bonusnya = ((($data_order['jumlah'] - $data_order['kode_unik']) / $rate) * $rate) - (($data_order['jumlah'] - $data_order['kode_unik']) / $rate);
    $yang_dibayar = (($data_order['jumlah'] - $data_order['kode_unik']) / $rate) + $data_order['kode_unik'];
    $hadehh = $data_order['jumlah'];
    $depo_user = $data_order['username'];
    $apa = $yang_dibayar - ($yang_dibayar * 0.3 / 100);
    $check_user = mysqli_query($db, "SELECT * FROM users WHERE username = '$depo_user'");
    $data_user = mysqli_fetch_assoc($check_user);
    $no = 1;
    $tokennya = $cek_token['token'];
    $hari_ini = date('Y-m-d');
    $kmaren_kmaren = date('Y-m-d', strtotime('-4 days'));
    $outputnya = "tidak_ketemu"; // Mendeklarasikan variabel outputnya sebelum loop

    do {
      $url = "https://pos.indibizpay.id/api/v1/transaction/history?start_date=$kmaren_kmaren&end_date=$hari_ini&category_transaction=QRIS&page=$no";

      $curl = curl_init($url);
      curl_setopt($curl, CURLOPT_URL, $url);
      curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

      $headers = array(
         "Authorization: Bearer $tokennya",
      );
      curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
      // Untuk keperluan debug saja
      curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
      curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);

      $resp = curl_exec($curl);
      curl_close($curl);
      $tes = json_decode($resp, true)['data']['data'];
      $acuan = json_decode($resp, true)['data']['to'];
      foreach ($tes as $key) {
        $pisah_titik = explode('.', $key['total_price'])[0];
        if ($pisah_titik == ceil($apa)) {
          $outputnya = "ketemu";
        }
      }
      $no++;
    } while ($acuan != NULL && $outputnya != "ketemu");

    if ($outputnya == "ketemu") {
      $ubah_penulisan = number_format($hadehh, 0, ',', '.');
      $tambah_saldo = $data_user['balance'] + $hadehh;
      $updateSaldo = mysqli_query($db, "UPDATE users SET balance = '$tambah_saldo' WHERE username = '$depo_user'");
      $updatedbnya = mysqli_query($db, "UPDATE topup SET status = 'Completed' WHERE username = '$depo_user'");
      $send = mysqli_query($db, "INSERT INTO balance_history(username, action, quantity, msg, tanggal) VALUES ('$depo_user','Add Balance','$hadehh','Kamu telah berhasil melakukan Topup saldo sebesar Rp $ubah_penulisan','$date $time')");
      $insert_tf = mysqli_query($db, "INSERT INTO catatan (username, note, waktu) VALUES ('$depo_user', 'Kamu telah berhasil melakukan Topup saldo sebesar $hadehh', '$date $time')");
      $msg_type = "success";
      $msg_content = "Data Topup ditemukan, Username : $depo_user, Nominal : $hadehh<br/>";
      $update_monitor = mysqli_query($db, "UPDATE monitor_cron SET status = 'Success', response = '$msg_content', tanggal = '$date $time' WHERE judul = 'cek_deposit'");
    } else {
      $msg_type = "error";
      $msg_content = "Error, Data Topup tidak ditemukan, mohon segera transfer sesuai nominal, jika terkendala, hubungi Admin";
      $update_monitor = mysqli_query($db, "UPDATE monitor_cron SET status = 'Success', response = '$msg_content', tanggal = '$date $time' WHERE judul = 'cek_deposit'");
    }

    if ($msg_type == "success") {
      echo $msg_content;
    } else {
      echo "$msg_content";
    }
  }
}
?>