<?php
// File ini berisi struktur modal untuk semua jenis transaksi nasabah
// Disertakan (include) di bagian bawah dashboard.php
?>

<div class="modal fade" id="modalSetoran" tabindex="-1" aria-hidden="true">
    <div class="modal-dialog modal-dialog-centered">
        <form class="modal-content" action="setoran.php" method="POST" style="border-radius: 20px;">
            <div class="modal-header border-0">
                <h5 class="modal-title fw-bold">Input Setoran Baru</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <div class="modal-body">
                <input type="hidden" name="action" value="simpan_setoran">
                <input type="hidden" name="id_siswa" value="<?= $id_siswa ?>">
                
                <div class="mb-3">
                    <label class="form-label small fw-bold">Besar Setoran (Rp)</label>
                    <input type="number" name="jumlah" class="form-control" placeholder="0" required>
                </div>
                <div class="mb-3">
                    <label class="form-label small fw-bold">Keterangan</label>
                    <input type="text" name="keterangan" class="form-control" value="Setoran Tunai">
                </div>
                <div class="mb-3">
                    <label class="form-label small fw-bold">Tanggal</label>
                    <input type="datetime-local" name="tanggal_transaksi" class="form-control" value="<?= date('Y-m-d\TH:i') ?>" required>
                </div>
            </div>
            <div class="modal-footer border-0">
                <button type="submit" class="btn btn-primary w-100 rounded-pill py-2">Kirim Pengajuan Setoran</button>
            </div>
        </form>
    </div>
</div>

<div class="modal fade" id="modalPenarikan" tabindex="-1" aria-hidden="true">
    <div class="modal-dialog modal-dialog-centered">
        <form class="modal-content" action="penarikan.php" method="POST" style="border-radius: 20px;">
            <div class="modal-header border-0">
                <h5 class="modal-title fw-bold text-danger">Tarik Saldo</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <div class="modal-body">
                <input type="hidden" name="action" value="simpan_penarikan">
                <input type="hidden" name="id_siswa" value="<?= $id_siswa ?>">
                
                <div class="alert alert-info small border-0">
                    Saldo Tersedia: <strong>Rp <?= number_format($siswa['saldo'] ?? 0, 0, ',', '.') ?></strong>
                </div>
                
                <div class="mb-3">
                    <label class="form-label small fw-bold">Jumlah Penarikan (Rp)</label>
                    <input type="number" name="jumlah" class="form-control text-danger fw-bold" max="<?= $siswa['saldo'] ?>" required>
                </div>
                <div class="mb-3">
                    <label class="form-label small fw-bold">PIN Transaksi</label>
                    <input type="password" name="pin_verifikasi" class="form-control text-center" maxlength="6" placeholder="Masukkan 6 Digit PIN" required>
                </div>
            </div>
            <div class="modal-footer border-0">
                <button type="submit" class="btn btn-danger w-100 rounded-pill py-2">Tarik Tunai</button>
            </div>
        </form>
    </div>
</div>

<div class="modal fade" id="modalTransfer" tabindex="-1" aria-hidden="true">
    <div class="modal-dialog modal-dialog-centered">
        <form class="modal-content" action="transfer.php" method="POST" style="border-radius: 20px;">
            <div class="modal-header border-0">
                <h5 class="modal-title fw-bold text-primary">Transfer Saldo</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <div class="modal-body">
                <input type="hidden" name="action" value="simpan_transfer">
                <input type="hidden" name="id_pengirim" value="<?= $id_siswa ?>">
                
                <div class="mb-3">
                    <label class="form-label small fw-bold">Penerima (Cari NIS/Nama)</label>
                    <select name="id_penerima" class="form-select select2-nasabah" style="width: 100%;" required>
                        <option value="">-- Pilih Penerima --</option>
                        <?php foreach($siswa_list_all as $s): ?>
                            <?php if($s['id_siswa'] != $id_siswa): ?>
                                <option value="<?= $s['id_siswa'] ?>"><?= $s['nis'] ?> - <?= $s['nama_siswa'] ?></option>
                            <?php endif; ?>
                        <?php endforeach; ?>
                    </select>
                </div>
                <div class="mb-3">
                    <label class="form-label small fw-bold">Jumlah Transfer (Rp)</label>
                    <input type="number" name="jumlah" class="form-control fw-bold" required>
                </div>
                <div class="mb-3">
                    <label class="form-label small fw-bold">PIN Transaksi</label>
                    <input type="password" name="pin_verifikasi" class="form-control text-center" maxlength="6" required>
                </div>
            </div>
            <div class="modal-footer border-0">
                <button type="submit" class="btn btn-primary w-100 rounded-pill py-2">Kirim Sekarang</button>
            </div>
        </form>
    </div>
</div>

<div class="modal fade" id="modalPayment" tabindex="-1" aria-hidden="true">
    <div class="modal-dialog modal-dialog-centered">
        <form class="modal-content" action="pembayaran.php" method="POST" style="border-radius: 20px;">
            <div class="modal-header border-0">
                <h5 class="modal-title fw-bold text-warning">Bayar Tagihan</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <div class="modal-body">
                <input type="hidden" name="action" value="simpan_bayar">
                <input type="hidden" name="id_siswa" value="<?= $id_siswa ?>">
                
                <div class="mb-3">
                    <label class="form-label small fw-bold">Jenis Pembayaran</label>
                    <select name="id_pembayaran" id="nasabahIdPay" class="form-select" required>
                        <option value="">-- Pilih Jenis --</option>
                        <?php foreach($pembayaran_list as $p): ?>
                            <option value="<?= $p['id_pembayaran'] ?>" data-satuan="<?= $p['nilai_satuan'] ?>">
                                <?= $p['nama_pembayaran'] ?>
                            </option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <div class="mb-3">
                    <label class="form-label small fw-bold">Jumlah (Rp)</label>
                    <input type="number" name="jumlah" id="nasabahJumlahBayar" class="form-control fw-bold" required>
                </div>
            </div>
            <div class="modal-footer border-0">
                <button type="submit" class="btn btn-warning w-100 rounded-pill py-2 fw-bold">Bayar Sekarang</button>
            </div>
        </form>
    </div>
</div>

<script>
$(document).ready(function() {
    // Inisialisasi Select2 untuk modal Transfer
    if ($.fn.select2) {
        $('.select2-nasabah').select2({
            theme: 'bootstrap-5',
            dropdownParent: $('#modalTransfer')
        });
    }

    // Otomatisasi jumlah pembayaran saat jenis dipilih (dari logika pembayaran.php)
    $('#nasabahIdPay').on('change', function() {
        const selected = $(this).find('option:selected');
        const satuan = parseFloat(selected.data('satuan'));
        if (satuan > 0) {
            $('#nasabahJumlahBayar').val(satuan).attr('readonly', true);
        } else {
            $('#nasabahJumlahBayar').val('').attr('readonly', false);
        }
    });
});
</script>