<?php
require __DIR__ . '/../layout/header.php';
require __DIR__ . '/../layout/sidebar.php';

$data = $data ?? [];
$tab = $_GET['tab'] ?? 'approve';
$search = $_GET['search'] ?? '';
?>

<div class="main">

<div class="header">Data Iuran Member</div>

<div class="card">

<!-- 🔥 NOTIFIKASI -->
<?php if(isset($_GET['ok'])): ?>
<div style="background:#d4edda;color:#155724;padding:10px;margin-bottom:15px;border-radius:5px;">
    ✅ Berhasil kirim: <?= (int)$_GET['ok'] ?> email |
    ❌ Gagal: <?= (int)$_GET['fail'] ?>
</div>
<?php endif; ?>

<div style="margin-bottom:20px;">
    <a href="?tab=approve" class="btn btn-primary">Perlu Approve</a>
    <a href="?tab=lunas" class="btn btn-success">Sudah Bayar</a>
    <a href="?tab=tunggakan" class="btn btn-danger">Belum Bayar</a>
</div>

<form method="GET" style="margin-bottom:15px;">
    <input type="hidden" name="tab" value="<?= $tab ?>">
    <input type="text" name="search" placeholder="Cari nama..." 
           value="<?= htmlspecialchars($search) ?>"
           style="padding:8px; width:250px;">
    <button class="btn btn-primary">Cari</button>
</form>

<table class="table">

<thead>
<?php if ($tab == 'tunggakan'): ?>

    <!-- 🔥 BUTTON REMINDER -->
    <a href="/membership/iuran-reminder" 
       class="btn btn-warning"
       style="margin-bottom:10px; display:inline-block;"
       onclick="return confirm('Kirim reminder ke semua member yang belum bayar bulan ini?')">
       📧 Kirim Reminder
    </a>

    <tr>
        <th>No</th>
        <th>Nama</th>
        <th>Tunggakan</th>
    </tr>

<?php else: ?>

    <tr>
        <th>No</th>
        <th>Nama</th>
        <th>Bulan</th>
        <th>Tahun</th>
        <th>Jumlah</th>
        <th>Tanggal</th>
        <th>Bukti</th>
        <th>Status</th>
        <?php if ($tab == 'approve'): ?>
        <th>Aksi</th>
        <?php endif; ?>
    </tr>

<?php endif; ?>
</thead>

<tbody>

<?php if ($tab == 'approve'): ?>

    <?php $no=1; $ada=false; ?>
    <?php foreach ($data as $d): ?>
        <?php
        if ($d['status']!='Menunggu Verifikasi') continue;
        if ($search && stripos($d['nama'],$search)===false) continue;
        $ada=true;
        ?>
        <tr>
            <td><?= $no++ ?></td>
            <td><?= htmlspecialchars($d['nama']) ?></td>
            <td><?= $d['bulan'] ?></td>
            <td><?= $d['tahun'] ?></td>
            <td>Rp <?= number_format($d['jumlah']) ?></td>
            <td><?= $d['tanggal_bayar'] ?></td>
            <td>
                <?php if($d['bukti_bayar']): ?>
                <img src="/membership/public/assets/uploads/<?= $d['bukti_bayar'] ?>" width="60" onclick="window.open(this.src)">
                <?php endif; ?>
            </td>
            <td><span class="badge warning">Menunggu</span></td>
            <td>
                <a href="/membership/iuran-approve?id=<?= $d['id'] ?>" class="btn btn-success">Approve</a>
                <a href="/membership/iuran-reject?id=<?= $d['id'] ?>" class="btn btn-danger">Reject</a>
            </td>
        </tr>
    <?php endforeach; ?>
    <?php if(!$ada): ?>
    <tr><td colspan="9" class="text-center">Tidak ada data</td></tr>
    <?php endif; ?>


<?php elseif ($tab == 'lunas'): ?>

    <?php $no=1; $ada=false; ?>
    <?php foreach ($data as $d): ?>
        <?php
        if ($d['status']!='Lunas') continue;
        if ($search && stripos($d['nama'],$search)===false) continue;
        $ada=true;
        ?>
        <tr>
            <td><?= $no++ ?></td>
            <td><?= htmlspecialchars($d['nama']) ?></td>
            <td><?= $d['bulan'] ?></td>
            <td><?= $d['tahun'] ?></td>
            <td>Rp <?= number_format($d['jumlah']) ?></td>
            <td><?= $d['tanggal_bayar'] ?></td>
            <td>
                <?php if($d['bukti_bayar']): ?>
                <img src="/membership/public/assets/uploads/<?= $d['bukti_bayar'] ?>" width="60">
                <?php endif; ?>
            </td>
            <td><span class="badge success">Lunas</span></td>
        </tr>
    <?php endforeach; ?>
    <?php if(!$ada): ?>
    <tr><td colspan="8" class="text-center">Belum ada yang lunas</td></tr>
    <?php endif; ?>


<?php else: ?>

    <?php
    $members=[];

    foreach($data as $d){
        $members[$d['member_id']]['nama']=$d['nama'];
        $members[$d['member_id']]['created']=$d['member_created_at'];
        $members[$d['member_id']]['data'][]=$d;
    }

    $no=1;
    $ada=false;
    ?>

    <?php foreach($members as $m): ?>

        <?php
        if ($search && stripos($m['nama'],$search)===false) continue;

        $start=new DateTime($m['created']);
        $start->modify('first day of this month');

        $end=new DateTime();

        $sudah=[];
        foreach($m['data'] as $i){
            if($i['status']=='Lunas'){
                $sudah[]=$i['bulan'].'-'.$i['tahun'];
            }
        }

        $belum=[];

        while($start <= $end){

            $bulan=$start->format('F');
            $tahun=$start->format('Y');

            if(!in_array($bulan.'-'.$tahun,$sudah)){
                $belum[]=$bulan.' '.$tahun;
            }

            $start->modify('+1 month');
        }
        ?>

        <?php if(!empty($belum)): $ada=true; ?>
        <tr>
            <td><?= $no++ ?></td>
            <td><?= htmlspecialchars($m['nama']) ?></td>
            <td>
                <?php foreach($belum as $b): ?>
                <span class="badge danger"><?= $b ?></span>
                <?php endforeach; ?>
            </td>
        </tr>
        <?php endif; ?>

    <?php endforeach; ?>

    <?php if(!$ada): ?>
    <tr><td colspan="3" class="text-center">Semua sudah bayar</td></tr>
    <?php endif; ?>

<?php endif; ?>

</tbody>
</table>

</div>
</div>

<?php require __DIR__ . '/../layout/footer.php'; ?>