<div class="container-fluid">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <div>
            <h4 class="mb-0">Iuran Saya</h4>
            <small class="text-muted">Daftar tagihan dan riwayat pembayaran iuran</small>
        </div>
    </div>

    <!-- Ringkasan Iuran -->
    <div class="row mb-4">
        <div class="col-md-4">
            <div class="card border-0 shadow-sm">
                <div class="card-body">
                    <div class="d-flex justify-content-between align-items-center">
                        <div>
                            <small class="text-muted">Total Tagihan Belum Lunas</small>
                            <h5 class="mb-0 text-danger">
                                <?php 
                                $belum_lunas = array_filter($iuran, function($i){ return $i->status_bayar == 'belum'; });
                                echo count($belum_lunas); 
                                ?>
                            </h5>
                        </div>
                        <i class="bi bi-exclamation-triangle fs-1 text-danger"></i>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-md-4">
            <div class="card border-0 shadow-sm">
                <div class="card-body">
                    <div class="d-flex justify-content-between align-items-center">
                        <div>
                            <small class="text-muted">Total Sudah Dibayar</small>
                            <h5 class="mb-0 text-success">
                                <?php 
                                $lunas = array_filter($iuran, function($i){ return $i->status_bayar == 'lunas'; });
                                echo count($lunas); 
                                ?>
                            </h5>
                        </div>
                        <i class="bi bi-check-circle fs-1 text-success"></i>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-md-4">
            <div class="card border-0 shadow-sm">
                <div class="card-body">
                    <div class="d-flex justify-content-between align-items-center">
                        <div>
                            <small class="text-muted">Total Tunggakan</small>
                            <h5 class="mb-0 text-warning">
                                Rp <?php 
                                $total_tunggakan = 0;
                                foreach($belum_lunas as $b) {
                                    $total_tunggakan += $b->nominal;
                                }
                                echo number_format($total_tunggakan, 0, ',', '.'); 
                                ?>
                            </h5>
                        </div>
                        <i class="bi bi-cash-stack fs-1 text-warning"></i>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Filter -->
    <div class="card mb-4">
        <div class="card-body">
            <form method="get" class="row g-3">
                <div class="col-md-3">
                    <label class="form-label">Status</label>
                    <select name="status" class="form-select">
                        <option value="">Semua Status</option>
                        <option value="belum" <?php echo ($this->input->get('status') == 'belum') ? 'selected' : ''; ?>>Belum Dibayar</option>
                        <option value="lunas" <?php echo ($this->input->get('status') == 'lunas') ? 'selected' : ''; ?>>Lunas</option>
                    </select>
                </div>
                <div class="col-md-3">
                    <label class="form-label">Tahun</label>
                    <select name="tahun" class="form-select">
                        <option value="">Semua Tahun</option>
                        <?php 
                        $tahun_sekarang = date('Y');
                        for($i=$tahun_sekarang-2; $i<=$tahun_sekarang; $i++): 
                        ?>
                            <option value="<?php echo $i; ?>" <?php echo ($this->input->get('tahun') == $i) ? 'selected' : ''; ?>>
                                <?php echo $i; ?>
                            </option>
                        <?php endfor; ?>
                    </select>
                </div>
                <div class="col-md-3">
                    <label class="form-label">&nbsp;</label>
                    <button type="submit" class="btn btn-primary d-block">
                        <i class="bi bi-filter me-2"></i>Filter
                    </button>
                </div>
            </form>
        </div>
    </div>

    <!-- Tabel Iuran -->
    <div class="table-container">
        <table class="table table-hover">
            <thead class="table-light">
                <tr>
                    <th>No</th>
                    <th>Jenis Iuran</th>
                    <th>Periode</th>
                    <th class="text-end">Nominal</th>
                    <th>Status</th>
                    <th>Tanggal Bayar</th>
                    <th>Aksi</th>
                </tr>
            </thead>
            <tbody>
                <?php 
                $no = 1; 
                if(isset($iuran) && !empty($iuran)):
                    foreach($iuran as $i): 
                ?>
                <tr>
                    <td><?php echo $no++; ?></td>
                    <td><?php echo $i->nama_iuran; ?></td>
                    <td><?php echo date('F Y', strtotime($i->tahun . '-' . $i->bulan . '-01')); ?></td>
                    <td class="text-end">Rp <?php echo number_format($i->nominal, 0, ',', '.'); ?></td>
                    <td>
                        <?php if($i->status_bayar == 'lunas'): ?>
                            <span class="badge bg-success">Lunas</span>
                        <?php else: ?>
                            <span class="badge bg-danger">Belum Dibayar</span>
                        <?php endif; ?>
                    </td>
                    <td>
                        <?php echo $i->tanggal_bayar ? date('d/m/Y', strtotime($i->tanggal_bayar)) : '-'; ?>
                    </td>
                    <td>
                        <?php if($i->status_bayar == 'belum'): ?>
                            <a href="<?php echo base_url('iuran/bayar/'.$i->id); ?>" class="btn btn-sm btn-success">
                                <i class="bi bi-credit-card me-1"></i>Bayar
                            </a>
                        <?php else: ?>
                            <span class="badge bg-success">
                                <i class="bi bi-check-circle me-1"></i>Sudah Dibayar
                            </span>
                        <?php endif; ?>
                    </td>
                </tr>
                <?php 
                    endforeach; 
                endif;
                ?>
            </tbody>
        </table>
        
        <?php if(!isset($iuran) || empty($iuran)): ?>
        <div class="text-center py-4">
            <i class="bi bi-inbox fs-1 text-muted"></i>
            <p class="text-muted mt-2">Belum ada data iuran</p>
        </div>
        <?php endif; ?>
    </div>

    <!-- Info -->
    <div class="alert alert-info mt-4" role="alert">
        <i class="bi bi-info-circle-fill me-2"></i>
        <strong>Informasi:</strong> Anda dapat melakukan pembayaran iuran dengan klik tombol "Bayar" pada tagihan yang belum lunas.
    </div>
</div>