<?php
include '../config/koneksi.php';
include '../helpers/functions.php';

if (!isLogin() || !isAdmin()) redirect('../auth/login.php');

$id = (int) $_GET['id'];
$trx = mysqli_fetch_assoc(mysqli_query($conn, "SELECT * FROM transactions WHERE id=$id"));

if ($trx && $trx['status'] == 'pending') {
    mysqli_begin_transaction($conn);

    try {
        mysqli_query($conn, "UPDATE transactions SET status='confirmed', confirmed_at=NOW(), catatan_admin='Dikonfirmasi admin' WHERE id=$id");
        mysqli_query($conn, "UPDATE users SET saldo = saldo + {$trx['nominal']} WHERE id={$trx['user_id']}");
        mysqli_commit($conn);
        setFlash('success', 'Setoran berhasil dikonfirmasi');
    } catch (Exception $e) {
        mysqli_rollback($conn);
        setFlash('danger', 'Gagal konfirmasi setoran');
    }
}

redirect('transaksi.php');
?>