<?= $this->extend('layout/main') ?>

<?= $this->section('content') ?>
<div class="glass-card">
    <div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 20px;">
        <h3 class="card-title" style="margin-bottom: 0;">Data Sumber Dana</h3>
        <?php if(session()->get('role') == 'admin'): ?>
        <button class="btn btn-primary" onclick="tambahData()">
            <i class="fas fa-plus"></i> Tambah Data
        </button>
        <?php endif; ?>
    </div>

    <table id="tableSumberDana" class="display nowrap">
        <thead>
            <tr>
                <th>No</th>
                <th>Sumber Dana</th>
                <?php if(session()->get('role') == 'admin'): ?>
                <th>Aksi</th>
                <?php endif; ?>
            </tr>
        </thead>
        <tbody>
            <?php $no = 1; foreach($sumber as $s): ?>
            <tr>
                <td><?= $no++ ?></td>
                <td><?= $s['nama_sumber'] ?></td>
                <?php if(session()->get('role') == 'admin'): ?>
                <td>
                    <button class="btn btn-sm btn-success" onclick="editData(<?= $s['id'] ?>, '<?= addslashes($s['nama_sumber']) ?>')">
                        <i class="fas fa-edit"></i>
                    </button>
                    <a href="<?= base_url('master/hapus-sumber-dana/'.$s['id']) ?>" class="btn btn-sm btn-danger" onclick="return confirm('Yakin ingin menghapus?')">
                        <i class="fas fa-trash"></i>
                    </a>
                </td>
                <?php endif; ?>
            </tr>
            <?php endforeach; ?>
        </tbody>
    </table>
</div>

<!-- Modal Form -->
<div class="modal-overlay" id="modalForm">
    <div class="modal">
        <div class="modal-header">
            <h3 id="modalTitle">Tambah Sumber Dana</h3>
            <button class="close-btn" onclick="closeModal('modalForm')">&times;</button>
        </div>
        <form action="<?= base_url('master/simpan-sumber-dana') ?>" method="post">
            <div class="modal-body">
                <input type="hidden" name="id" id="id">
                <div class="form-group">
                    <label class="form-label">Nama Sumber Dana</label>
                    <input type="text" name="nama_sumber" id="nama_sumber" class="form-control" required>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-danger" onclick="closeModal('modalForm')">Batal</button>
                <button type="submit" class="btn btn-primary">Simpan</button>
            </div>
        </form>
    </div>
</div>
<?= $this->endSection() ?>

<?= $this->section('scripts') ?>
<script>
    $(document).ready(function() {
        initDataTable('#tableSumberDana');
    });

    function tambahData() {
        document.getElementById('modalTitle').innerText = 'Tambah Sumber Dana';
        document.getElementById('id').value = '';
        document.getElementById('nama_sumber').value = '';
        openModal('modalForm');
    }

    function editData(id, nama) {
        document.getElementById('modalTitle').innerText = 'Edit Sumber Dana';
        document.getElementById('id').value = id;
        document.getElementById('nama_sumber').value = nama;
        openModal('modalForm');
    }
</script>
<?= $this->endSection() ?>
