<?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'"));
$transaksi = mysqli_query($conn, "SELECT * FROM transactions WHERE user_id='$id_user' ORDER BY id DESC");

$title = "Riwayat 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">
                    <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="#0d6efd" stroke-width="2">
                                <path stroke-linecap="round" stroke-linejoin="round" d="M12 3v12m0 0l-4-4m4 4l4-4M5 21h14"/>
                            </svg>
                            Riwayat Setoran
                        </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>Bukti</th>
                                    <th>Status</th>
                                    <th>Catatan Admin</th>
                                    <th>Dikonfirmasi</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php if ($transaksi && mysqli_num_rows($transaksi) > 0): ?>
                                    <?php $no = 1; while($row = mysqli_fetch_assoc($transaksi)) : ?>
                                    <tr>
                                        <td><?= $no++; ?></td>

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

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

                                        <td>
                                            <?php if (!empty($row['bukti_transfer'])) : ?>
                                                <a href="/tabungan-app/uploads/<?= htmlspecialchars($row['bukti_transfer']); ?>" target="_blank">
                                                    <img src="/tabungan-app/uploads/<?= htmlspecialchars($row['bukti_transfer']); ?>" class="preview-img">
                                                </a>
                                            <?php else : ?>
                                                <span class="text-muted">Tidak ada</span>
                                            <?php endif; ?>
                                        </td>

                                        <td>
                                            <?php if ($row['status'] == 'pending'): ?>
                                                <span class="badge bg-warning text-dark">Pending</span>
                                            <?php elseif ($row['status'] == 'confirmed'): ?>
                                                <span class="badge bg-success">Confirmed</span>
                                            <?php else: ?>
                                                <span class="badge bg-danger">Rejected</span>
                                            <?php endif; ?>
                                        </td>

                                        <td>
                                            <?= !empty($row['catatan_admin']) ? htmlspecialchars($row['catatan_admin']) : '<span class="text-muted">-</span>'; ?>
                                        </td>

                                        <td>
                                            <?= !empty($row['confirmed_at']) ? date('d-m-Y H:i', strtotime($row['confirmed_at'])) : '<span class="text-muted">Belum</span>'; ?>
                                        </td>
                                    </tr>
                                    <?php endwhile; ?>
                                <?php else: ?>
                                    <tr>
                                        <td colspan="7" class="text-center text-muted py-4">
                                            Belum ada riwayat setoran.
                                        </td>
                                    </tr>
                                <?php endif; ?>
                            </tbody>
                        </table>
                    </div>
                </div>

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

<style>
.preview-img {
    width: 70px;
    height: 70px;
    object-fit: cover;
    border-radius: 12px;
    border: 2px solid #eee;
    transition: 0.3s;
}

.preview-img:hover {
    transform: scale(1.05);
}

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

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

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