<?php
session_start();
require '../../../config.php';
load('middleware', 'user');
$date = date('Y-m-d H:i:s');
if ($_SESSION['user']['level'] !== "Admin") {
    header('location:' . base_url('id'));

}
$dataaa = $db->query("SELECT * FROM deposit");
if (isset($_GET['tolak'])) {
    $tolak = $_GET['tolak'];
    mysqli_query($db, "UPDATE deposit SET status='Canceled' WHERE id = '$tolak'");
    
    $_SESSION['alert'] = ['success', 'Success!', 'Data deposit berhasil di Tolak.'];
    exit(header("Location: ".base_url('admin/user/deposit/')));
}
if (isset($_GET['terima'])) {
    $terima = $_GET['terima'];
    $wapi = $db->query("SELECT * FROM options WHERE opt_name='tp-key'")->fetch_assoc();
    $wap = $db->query("SELECT * FROM options WHERE opt_name='tp-phone'")->fetch_assoc();
    $ch = $db->query("SELECT * FROM deposit WHERE id='$terima'")->fetch_assoc();
    mysqli_query($db, "UPDATE deposit SET status='Success' WHERE id = '$terima'");
    $db->query("UPDATE users SET saldo=saldo+{$ch['get_saldo']} WHERE username='{$ch['username']}'");
    $db->query("INSERT INTO history_balance VALUES ('','{$ch['username']}','{$ch['get_saldo']}','+','$date')");
    $WhatsApp->sendMessage('https://wabot.myrigaming.my.id/',$wapi['opt_value'],$wap['opt_value'],$ch['nomor'],"*Status Deposit Success*\n- Username : *".$ch['username']."*\n- Status : *Deposit Di Setujui*\nSaldo Didapat : *".$ch['get_saldo']."*\n\n*BY ".config('web','title')."*");
    $_SESSION['alert'] = ['success', 'Success!', 'Data deposit berhasil di Terima.'];
    exit(header("Location: ".base_url('admin/user/deposit/')));
}
if (isset($_GET['hapus'])) {
    $hapus = $_GET['hapus'];
    
    mysqli_query($db, "DELETE FROM deposit WHERE id = '$hapus'");
    
    $_SESSION['alert'] = ['success', 'Success!', 'Data deposit berhasil di Hapus.'];
    exit(header("Location: ".base_url('admin/user/deposit/')));
}
require '../../../layouts/header-admin.php'; 
?>
<div class="row">
        <div class="col-12">
            <div class="card bg-dark shadow">
                <div class="card-header">
                    <h4 class="card-title">Kelola Deposit</h4>
                </div>
                <div class="card-body card-dashboard">
                    <div class="table-responsive">
                        	<table class="table table-striped table-dark table-bordered table-hover dataTable no-footer" id="tbb">
                        	    <thead>
                                <tr>
                                    <th>Username</th>
                                    <th>Metode</th>
                                    <th>Amount</th>
                                    <th>Status</th>
                                    <th>Act.</th>
                                </tr>
                                </thead>
                                <tbody>
                            <?php
                            while($dataa = $dataaa->fetch_assoc()):?>
                            <?php if($dataa['status'] == "Success") {
                                $label = "success";
                                $icon = "fa-solid fa-square-check";
                            } else if($dataa['status'] == "Canceled") {
                                $label = "danger";
                                $icon = "fa-solid fa-square-xmark";
                            }else if($dataa['status'] == "Pending"){
                                $label= "warning";
                                $icon = "fa-solid fa-triangle-exclamation";
                            };?>
                            <tr>
                                <td><?= $dataa['username'];?></td>
                                <td><?= $dataa['metode'];?></td>
                                <td><?= $dataa['amount'];?></td>
                                <td><span class="text-<?= $label; ?>"><i class="<?= $icon; ?>"></i> <?= $dataa['status']; ?></td>
                                <td style="white-space: nowrap;">
                                <?php if($dataa['status'] !== "Success" && $dataa['status'] !== "Canceled"):?>
                                    <a class="btn btn-success btn-sm" href="/admin/user/deposit/?terima=<?= $dataa['id'];?>">Terima</a>
                                <?php endif;?>
                                <?php if($dataa['status'] !== "Canceled" && $dataa['status'] !== "Success"):?>
                                    <a class="btn btn-warning btn-sm" href="/admin/user/deposit/?tolak=<?= $dataa['id']; ?>">Tolak</a>
                                <?php endif;?>
                                    <a class="btn btn-danger btn-sm" href="/admin/user/deposit/?hapus=<?= $dataa['id']; ?>">Hapus</a>
                                </td>
                            </tr>
                            <?php endwhile;?>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
<?php require '../../../layouts/footer-admin.php'; ?>
<script>
    $(document).ready(function() {
	$('#tbb').dataTable();
 });
</script>