<?php
include '../../function/connect.php';

$id = $_GET['id'] ?? '';

$get = mysqli_query($koneksi, "
    SELECT * FROM tb_bonus 
    WHERE id='$id' AND status='Pending'
");

$data = mysqli_fetch_assoc($get);

if (!$data) {
    echo "<script>alert('Bonus tidak valid');history.back();</script>";
    exit;
}

$id_user = $data['id_user'];
$nominal = $data['nominal'];
$tanggal = date('Y-m-d H:i:s');

// Tambah saldo user
mysqli_query($koneksi, "
    UPDATE tb_saldo 
    SET active = active + $nominal 
    WHERE id_user='$id_user'
");

// Log transaksi
mysqli_query($koneksi, "
    INSERT INTO tb_transaksi
    (id,gambar,tanggal,transaksi,total,dari_bank,metode,bonus,keterangan,status,id_user)
    VALUES
    (NULL,'','$tanggal','Bonus Masuk Saldo','$nominal','','System','','Konfirmasi Bonus','Sukses','$id_user')
");

// Update status bonus
mysqli_query($koneksi, "
    UPDATE tb_bonus 
    SET status='Selesai' 
    WHERE id='$id'
");

echo "
<script>
alert('Bonus berhasil dikonfirmasi & masuk saldo user');
window.location='?page=daftar_permintaan_bonus';
</script>";