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

<!-- Widget Total Pemasukan -->
<div class="widget-container" style="margin-bottom: 20px;">
    <div class="widget success" style="max-width: 350px;">
        <div class="widget-info">
            <h3>Rp <?= number_format($total_pemasukan, 0, ',', '.') ?></h3>
            <p>Total Pemasukan Penghapusan</p>
        </div>
        <div class="widget-icon"><i class="fas fa-coins"></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 Penghapusan</h3>
        <?php if(session()->get('role') == 'admin'): ?>
        <div>
            <a href="<?= base_url('transaksi/hapus-semua-penghapusan') ?>" class="btn btn-danger" onclick="return confirm('Apakah Anda yakin ingin menghapus SEMUA data penghapusan? 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 Kerusakan</th>
                <th>Tgl Persetujuan</th>
                <th>Status Realisasi</th>
                <th>Pemasukan</th>
                <th>Bukti Realisasi</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_kerusakan'] ?></td>
                <td><?= $d['tgl_persetujuan'] ?></td>
                <td>
                    <?php
                        $status = $d['status_realisasi'] ?? '-';
                        $sbadge = 'btn-secondary';
                        if($status == 'Dijual')     $sbadge = 'btn-success';
                        if($status == 'Musnahkan')  $sbadge = 'btn-danger';
                        if($status == 'Hibahkan')   $sbadge = 'btn-warning';
                    ?>
                    <?php if($status && $status != '-'): ?>
                    <span class="btn btn-sm <?= $sbadge ?>" style="pointer-events:none; padding:2px 8px;"><?= $status ?></span>
                    <?php else: ?><span style="color:#aaa;">-</span><?php endif; ?>
                </td>
                <td>Rp <?= number_format($d['pemasukan'] ?? 0, 0, ',', '.') ?></td>
                <td>
                    <?php if(!empty($d['bukti_realisasi'])): ?>
                        <a href="<?= base_url('uploads/'.$d['bukti_realisasi']) ?>" 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='editPenghapusan(<?= json_encode($d) ?>)'><i class="fas fa-edit"></i></button>
                    <a href="<?= base_url('transaksi/hapus-penghapusan/'.$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 Penghapusan</h3>
            <button class="close-btn" onclick="closeModal('modalForm')">&times;</button>
        </div>
        <form action="<?= base_url('transaksi/simpan-penghapusan') ?>" 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="penghapusan_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 Kerusakan</label>
                    <input type="date" name="tgl_kerusakan" class="form-control">
                </div>
                <div class="form-group">
                    <label>Tgl Persetujuan</label>
                    <input type="date" name="tgl_persetujuan" class="form-control">
                </div>
                <div class="form-group">
                    <label>Status Realisasi</label>
                    <select name="status_realisasi" class="form-control">
                        <option value="">-- Pilih --</option>
                        <option value="Dijual">Dijual</option>
                        <option value="Musnahkan">Musnahkan</option>
                        <option value="Hibahkan">Hibahkan</option>
                    </select>
                </div>
                <div class="form-group">
                    <label>Pemasukan (Rp)</label>
                    <input type="number" name="pemasukan" class="form-control" placeholder="Isi jika ada pemasukan">
                </div>
                <div class="form-group" style="grid-column: span 2;">
                    <label>Keterangan</label>
                    <textarea name="keterangan" class="form-control" rows="2"></textarea>
                </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 Realisasi</label>
                    <input type="file" name="bukti_realisasi" 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 editPenghapusan(data) {
    document.getElementById('penghapusan_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_kerusakan"]').value = data.tgl_kerusakan;
    document.querySelector('#modalForm input[name="tgl_persetujuan"]').value = data.tgl_persetujuan;
    document.querySelector('#modalForm select[name="status_realisasi"]').value = data.status_realisasi || '';
    document.querySelector('#modalForm input[name="pemasukan"]').value = data.pemasukan || 0;
    document.querySelector('#modalForm textarea[name="keterangan"]').value = data.keterangan;
    openModal('modalForm');
}
</script>
<?= $this->endSection() ?>
