<?php
session_start();
require_once '../../config.php';
load('middleware', 'user');

if ($_SESSION['user']['level'] !== "Admin") {
    header('location:' . base_url('id'));
}

if (isset($_GET['hapus'])) {
    $hapus = addslashes(trim(htmlspecialchars(mysqli_real_escape_string($db, $_GET['hapus']))));
    
    mysqli_query($db, "DELETE FROM pembelian_guest WHERE id = '$hapus'");
    
    $_SESSION['alert'] = ['success', 'Success!', 'Data berhasil di hapus.'];
    exit(header('location: /admin/guest/transaksi.php'));
}

include_once '../../layouts/header-admin.php'; ?>
<div class="row justify-content-center">
    <div class="col-md-12 col-12">
        <div class="card bg-dark shadow">
            <div class="card-header">
                <h4 class="card-title">Kelola Transaksi</h4>
            </div>
            <div class="card-body card-dashboard">
                <div class="table-responsive">
                    <table class="table table-striped table-dark table-bordered table-hover" id="tbb">
                        <thead>
                        <tr>
                            <th width="10">No</th>
                            <th>Username</th>
                            <th>Order ID</th>
                            <th>Produk</th>
                            <th>Harga</th>
                            <th>Target</th>
                            <th>Nickname</th>
                            <th>Status</th>
                            <th width="10">Action</th>
                        </tr>
                        </thead>
                        <tbody>
                        <?php 
                        $no = 1;
                        $query_pembelian_guest = mysqli_query($db, "SELECT * FROM pembelian_guest ORDER BY id DESC");
                        while($fetch_pembelian_guest = mysqli_fetch_assoc($query_pembelian_guest)): 
                        ?>
                        <tr>
                            <td><?= $no++; ?></td>
                            <td><?= $fetch_pembelian_guest['username'];?></td>
                            <td><a href="<?= base_url('id/invoice?no='.$fetch_pembelian_guest['order_id']);?>">#<?= $fetch_pembelian_guest['order_id']; ?></a></td>
                            <td><?= $fetch_pembelian_guest['layanan']; ?></td>
                            <td style="white-space: nowrap;">Rp <?= number_format($fetch_pembelian_guest['harga']+$fetch_pembelian_guest['fee'],0,',','.'); ?></td>
                            <td><b><input type="text" class="form-control" name="target" value="<?= $fetch_pembelian_guest['target']; ?><?php if($fetch_pembelian_guest['additional_target'] != null): ?>(<?= $fetch_pembelian_guest['additional_target'] ?? null ;?>) <?php endif; ?>" disabled></td>
                            <td><b><input type="text" class="form-control" name="target" value="<?php if($fetch_pembelian_guest['nickname'] != null): ?><?= $fetch_pembelian_guest['nickname'] ?? null ;?> <?php endif; ?>" disabled></td>
                            <td>
                                <?php if ($fetch_pembelian_guest['status'] == 'Pending') {
                                    $badge_bg = 'warning';
                                    $stts = "PENDING";
                                }else if ($fetch_pembelian_guest['status'] == 'Success'){
                                    $badge_bg = 'success';
                                    $stts = "SUCCESS";
                                }else if ($fetch_pembelian_guest['status'] == 'Canceled'){
                                    $badge_bg = 'danger';
                                    $stts = "CANCELED";
                                }else if($fetch_pembelian_guest['status'] == 'Waiting'){
                                    $badge_bg = 'secondary';
                                    $stts = "WAITING";
                                }else if($fetch_pembelian_guest['status'] == 'Processing'){
                                    $badge_bg = 'primary';
                                    $stts = "PROCESSING";
                                }
                                ?>
                                <span class="badge p-2 bg-<?= $badge_bg;?>"><?=$stts;?></span>
                            </td>
                            <td style="white-space: nowrap;">
                                <a class="btn btn-warning btn-sm" href="/admin/guest/transaksi/edit?id=<?= $fetch_pembelian_guest['id'] ?>">Edit</a> <a class="btn btn-danger btn-sm" href="/admin/guest/transaksi.php?hapus=<?= $fetch_pembelian_guest['id']; ?>">Hapus</a>
                            </td>
                        </tr>
                        <?php endwhile; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>
<script>
$(document).ready(function() {
	$('#tbb').dataTable();
 });
</script>
<?php include_once '../../layouts/footer-admin.php'; ?>





























