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

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

$transaksi = mysqli_query($conn, "
    SELECT transactions.*, users.nama 
    FROM transactions
    JOIN users ON transactions.user_id = users.id
    ORDER BY transactions.id DESC
");

$title = "Data Setoran";
include '../layouts/header.php';
?>

<div class="container-fluid">
    <div id="overlay" class="overlay"></div>

    <div class="row">
        <div class="col-lg-2 p-0">
            <?php include '../layouts/sidebar.php'; ?>
        </div>

        <div class="col-lg-10 p-0">
            <?php include '../layouts/navbar.php'; ?>

            <div class="content-wrap">
                <?php showFlash(); ?>

                <div class="card p-4">
                    <h4 class="section-title">Data Setoran</h4>

                        <div class="table-responsive table-modern">                   
                        <table class="table table-bordered align-middle">
                            <thead>
                                <tr>
                                    <th>No</th>
                                    <th>Nama User</th>
                                    <th>Tanggal</th>
                                    <th>Nominal</th>
                                    <th>Bukti</th>
                                    <th>Status</th>
                                    <th>Aksi</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php $no=1; while($t = mysqli_fetch_assoc($transaksi)) : ?>
                                <tr>
                                    <td><?= $no++; ?></td>
                                    <td><?= htmlspecialchars($t['nama']); ?></td>
                                    <td><?= $t['created_at']; ?></td>
                                    <td><?= rupiah($t['nominal']); ?></td>
                                    <td>
                                        <?php if ($t['bukti_transfer']) : ?>
                                            <a href="../uploads/<?= $t['bukti_transfer']; ?>" target="_blank">
                                                <img src="../uploads/<?= $t['bukti_transfer']; ?>" class="preview-img">
                                            </a>
                                        <?php endif; ?>
                                    </td>
                                    <td>
                                        <span class="badge bg-<?= $t['status']=='pending'?'warning':($t['status']=='confirmed'?'success':'danger'); ?>">
                                            <?= ucfirst($t['status']); ?>
                                        </span>
                                    </td>
                                    <td>
                                        <?php if ($t['status']=='pending'): ?>
                                            <a href="transaksi_konfirmasi.php?id=<?= $t['id']; ?>" class="btn btn-success btn-sm">Konfirmasi</a>
                                            <a href="transaksi_tolak.php?id=<?= $t['id']; ?>" class="btn btn-danger btn-sm">Tolak</a>
                                        <?php else: ?>
                                            <span class="text-muted">Selesai</span>
                                        <?php endif; ?>
                                    </td>
                                </tr>
                                <?php endwhile; ?>
                            </tbody>
                        </table>
                    </div>
                </div>

            </div>
        </div>
    </div>
</div>

<?php include '../layouts/footer.php'; ?>