<?php
require_once('session.php');

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $id = $_POST['cuid'];
    $note = mysqli_real_escape_string($conn, $_POST['note']); // Hindari SQL Injection
    $currentDate = date('Y-m-d H:i:s');

    // Ambil data transaksi
    $sql_1 = mysqli_query($conn, "SELECT * FROM `tb_transaksi` WHERE cuid = '$id'") or die(mysqli_error($conn));
    $s1 = mysqli_fetch_array($sql_1);
    $usersID = $s1['userID'];
    $amounts = $s1['total'];
    $trxID = $s1['kd_transaksi'];

    if ($s1['status'] == 0) {
        // Update status transaksi menjadi reject (2)
        $update = mysqli_query($conn, "UPDATE `tb_transaksi` SET `status` = 2, `note` = '$note' WHERE `cuid` = '$id'") 
                  or die(mysqli_error($conn));
        
        // Update status turnover menjadi 0 jika ada
        $updates = mysqli_query($conn, "UPDATE `tb_turnover` SET `status` = 0 WHERE `trxID` = '$trxID'") 
                  or die(mysqli_error($conn));

        // Update status transaksi terkait lainnya (jenis 7)
        $updateTrb = mysqli_query($conn, "UPDATE `tb_transaksi` SET `status` = 2 WHERE `userID` = '$usersID' AND `jenis` = 7 AND `kd_transaksi` = '$trxID'")
                     or die(mysqli_error($conn));
    }

    // Redirect kembali ke halaman request deposit dengan pesan sukses
    header('Location: ../request_depo.php?status=rejected');
    exit;
} else {
    // Jika bukan metode POST, redirect kembali tanpa perubahan
    header('Location: ../request_depo.php');
    exit;
}
?>
