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

<!-- Widget Total Biaya -->
<div class="widget-container" style="margin-bottom: 20px;">
    <div class="widget warning" style="max-width: 350px;">
        <div class="widget-info">
            <h3>Rp <?= number_format($total_biaya, 0, ',', '.') ?></h3>
            <p>Total Biaya Peminjaman</p>
        </div>
        <div class="widget-icon"><i class="fas fa-hand-holding-usd"></i></div>
    </div>
</div>

<div class="glass-card" style="width: 100%; overflow-x: auto;">
    <div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 20px;">
        <h3 class="card-title" style="margin-bottom: 0;">Data Peminjaman</h3>
        <?php if(session()->get('role') == 'admin'): ?>
        <div>
            <a href="<?= base_url('transaksi/hapus-semua-peminjaman') ?>" class="btn btn-danger" onclick="return confirm('Apakah Anda yakin ingin menghapus SEMUA data peminjaman? Tindakan ini tidak dapat dibatalkan.')"><i class="fas fa-trash"></i> Hapus Semua</a>
            <button class="btn btn-primary" onclick="openModal('modalForm')"><i class="fas fa-plus"></i> Tambah</button>
        </div>
        <?php endif; ?>
    </div>
    <table id="tableData" class="display nowrap" style="width:100%">
        <thead>
            <tr>
                <th>No</th>
                <th>Nama Barang</th>
                <th>Ruangan</th>
                <th>Tgl Peminjaman</th>
                <th>Tgl Pengembalian</th>
                <th>Biaya</th>
                <th>Bukti Bayar</th>
                <th>Foto</th>
                <th>Keterangan</th>
                <?php if(session()->get('role') == 'admin'): ?>
                <th>Aksi</th>
                <?php endif; ?>
            </tr>
        </thead>
        <tbody>
            <?php $no=1; foreach($data as $d): ?>
            <tr>
                <td><?= $no++ ?></td>
                <td><?= $d['nama_barang'] ?></td>
                <td><?= $d['nama_ruangan'] ?></td>
                <td><?= $d['tgl_peminjaman'] ?></td>
                <td><?= $d['tgl_pengembalian'] ?></td>
                <td>Rp <?= number_format($d['biaya'],0,',','.') ?></td>
                <td>
                    <?php if(!empty($d['bukti_bayar'])): ?>
                        <a href="<?= base_url('uploads/'.$d['bukti_bayar']) ?>" target="_blank" class="btn btn-sm btn-success"><i class="fas fa-image"></i> Lihat</a>
                    <?php else: ?>
                        <span style="color:#aaa;">-</span>
                    <?php endif; ?>
                </td>
                <td>
                    <?php if($d['foto']): ?>
                        <a href="<?= base_url('uploads/'.$d['foto']) ?>" target="_blank" class="btn btn-sm btn-primary"><i class="fas fa-image"></i>Lihat</a>
                    <?php else: ?>-<?php endif; ?>
                </td>
                <td><?= $d['keterangan'] ?></td>
                <?php if(session()->get('role') == 'admin'): ?>
                <td>
                    <button class="btn btn-sm btn-success" onclick='editPeminjaman(<?= json_encode($d) ?>)'><i class="fas fa-edit"></i></button>
                    <a href="<?= base_url('transaksi/hapus-peminjaman/'.$d['id']) ?>" class="btn btn-sm btn-danger" onclick="return confirm('Yakin?')"><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" style="max-width: 700px;">
        <div class="modal-header">
            <h3>Form Peminjaman</h3>
            <button class="close-btn" onclick="closeModal('modalForm')">&times;</button>
        </div>
        <form action="<?= base_url('transaksi/simpan-peminjaman') ?>" method="post" enctype="multipart/form-data">
            <div class="modal-body" style="display: grid; grid-template-columns: 1fr 1fr; gap: 15px;">
                <input type="hidden" name="id" id="peminjaman_id">
                <div class="form-group">
                    <label>Barang</label>
                    <select name="id_barang" class="form-control" required>
                        <option value="">Pilih Barang</option>
                        <?php foreach($barang as $b): ?>
                            <option value="<?= $b['id'] ?>"><?= $b['nama_barang'] ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <div class="form-group">
                    <label>Ruangan</label>
                    <select name="id_ruangan" class="form-control" required>
                        <option value="">Pilih Ruangan</option>
                        <?php foreach($ruangan as $r): ?>
                            <option value="<?= $r['id'] ?>"><?= $r['nama_ruangan'] ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <div class="form-group">
                    <label>Tgl Peminjaman</label>
                    <input type="date" name="tgl_peminjaman" class="form-control" required>
                </div>
                <div class="form-group">
                    <label>Tgl Pengembalian</label>
                    <input type="date" name="tgl_pengembalian" class="form-control">
                </div>
                <div class="form-group">
                    <label>Biaya</label>
                    <input type="number" name="biaya" class="form-control">
                </div>
                <div class="form-group">
                    <label>Keterangan</label>
                    <input type="text" name="keterangan" class="form-control">
                </div>
                <div class="form-group">
                    <label>Foto Barang</label>
                    <input type="file" name="foto" class="form-control" accept="image/*">
                </div>
                <div class="form-group">
                    <label>Bukti Bayar</label>
                    <input type="file" name="bukti_bayar" class="form-control" accept="image/*">
                </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('#tableData'); });
function editPeminjaman(data) {
    document.getElementById('peminjaman_id').value = data.id;
    document.querySelector('#modalForm select[name="id_barang"]').value = data.id_barang;
    document.querySelector('#modalForm select[name="id_ruangan"]').value = data.id_ruangan;
    document.querySelector('#modalForm input[name="tgl_peminjaman"]').value = data.tgl_peminjaman;
    document.querySelector('#modalForm input[name="tgl_pengembalian"]').value = data.tgl_pengembalian;
    document.querySelector('#modalForm input[name="biaya"]').value = data.biaya;
    document.querySelector('#modalForm input[name="keterangan"]').value = data.keterangan;
    openModal('modalForm');
}
</script>
<?= $this->endSection() ?>
