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

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

$id_user = $_SESSION['id'];
$user = mysqli_fetch_assoc(mysqli_query($conn, "SELECT * FROM users WHERE id='$id_user'"));
$withdrawals = mysqli_query($conn, "SELECT * FROM withdrawals WHERE user_id='$id_user' ORDER BY id DESC");

$title = "Riwayat 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">
                    <div class="d-flex justify-content-between align-items-center mb-4 flex-wrap gap-2">
                        <h4 class="section-title mb-0 d-flex align-items-center gap-2">
                            <svg class="menu-icon" xmlns="http://www.w3.org/2000/svg" width="26" height="26" fill="none" viewBox="0 0 24 24" stroke="#dc3545" stroke-width="2">
                                <path stroke-linecap="round" stroke-linejoin="round" d="M12 21V9m0 0l4 4m-4-4l-4 4M5 3h14"/>
                            </svg>
                            Riwayat Penarikan
                        </h4>

                    </div>

                    <div class="table-responsive table-modern">
                        <table class="table table-bordered align-middle">
                            <thead>
                                <tr>
                                    <th>No</th>
                                    <th>Tanggal</th>
                                    <th>Nominal</th>
                                    <th>Bank Tujuan</th>
                                    <th>No. Rekening</th>
                                    <th>Nama Rekening</th>
                                    <th>Status</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php if ($withdrawals && mysqli_num_rows($withdrawals) > 0): ?>
                                    <?php $no = 1; while($w = mysqli_fetch_assoc($withdrawals)) : ?>
                                    <tr>
                                        <td><?= $no++; ?></td>

                                        <td>
                                            <?= !empty($w['created_at']) ? date('d-m-Y H:i', strtotime($w['created_at'])) : '-'; ?>
                                        </td>

                                        <td>
                                            <strong><?= rupiah($w['nominal']); ?></strong>
                                        </td>

                                        <td><?= htmlspecialchars($w['bank_tujuan']); ?></td>
                                        <td><?= htmlspecialchars($w['rekening_tujuan']); ?></td>
                                        <td><?= htmlspecialchars($w['nama_rekening']); ?></td>

                                        <td>
                                            <?php if ($w['status'] == 'pending'): ?>
                                                <span class="badge bg-warning text-dark">Pending</span>
                                            <?php elseif ($w['status'] == 'confirmed'): ?>
                                                <span class="badge bg-success">Confirmed</span>
                                            <?php else: ?>
                                                <span class="badge bg-danger">Rejected</span>
                                            <?php endif; ?>
                                        </td>
                                    </tr>
                                    <?php endwhile; ?>
                                <?php else: ?>
                                    <tr>
                                        <td colspan="7" class="text-center text-muted py-4">
                                            Belum ada riwayat penarikan.
                                        </td>
                                    </tr>
                                <?php endif; ?>
                            </tbody>
                        </table>
                    </div>
                </div>

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

<style>
.section-title {
    font-weight: 700;
    color: #1f2d3d;
}

.menu-icon {
    width: 22px;
    height: 22px;
    flex-shrink: 0;
}
</style>

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