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

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

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

$title = "Data Penarikan";
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 Penarikan</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>Bank</th>
                                    <th>Rekening</th>
                                    <th>Nama Rekening</th>
                                    <th>Status</th>
                                    <th>Aksi</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php $no=1; while($w = mysqli_fetch_assoc($data)) : ?>
                                <tr>
                                    <td><?= $no++; ?></td>
                                    <td><?= htmlspecialchars($w['nama']); ?></td>
                                    <td><?= $w['created_at']; ?></td>
                                    <td><?= rupiah($w['nominal']); ?></td>
                                    <td><?= htmlspecialchars($w['bank_tujuan']); ?></td>
                                    <td><?= htmlspecialchars($w['rekening_tujuan']); ?></td>
                                    <td><?= htmlspecialchars($w['nama_rekening']); ?></td>
                                    <td>
                                        <span class="badge bg-<?= $w['status']=='pending'?'warning':($w['status']=='confirmed'?'success':'danger'); ?>">
                                            <?= ucfirst($w['status']); ?>
                                        </span>
                                    </td>
                                    <td>
                                        <?php if ($w['status']=='pending'): ?>
                                            <a href="withdrawal_konfirmasi.php?id=<?= $w['id']; ?>" class="btn btn-success btn-sm">Konfirmasi</a>
                                            <a href="withdrawal_tolak.php?id=<?= $w['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'; ?>