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

<!-- Widget Total Biaya -->
<div class="widget-container" style="margin-bottom: 20px;">
    <div class="widget danger" style="max-width: 350px;">
        <div class="widget-info">
            <h3>Rp <?= number_format($total_biaya, 0, ',', '.') ?></h3>
            <p>Total Biaya Perbaikan</p>
        </div>
        <div class="widget-icon"><i class="fas fa-tools"></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 Perbaikan</h3>
        <?php if(session()->get('role') == 'admin'): ?>
        <div>
            <a href="<?= base_url('transaksi/hapus-semua-perbaikan') ?>" class="btn btn-danger" onclick="return confirm('Apakah Anda yakin ingin menghapus SEMUA data perbaikan? 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>Jenis Perbaikan</th>
                <th>Tgl Kerusakan</th>
                <th>Tgl Perbaikan</th>
                <th>Biaya</th>
                <th>Bukti Bayar</th>
                <th>Foto</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['jenis_perbaikan'] ?></td>
                <td><?= $d['tgl_kerusakan'] ?></td>
                <td><?= $d['tgl_perbaikan'] ?></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>
                <?php if(session()->get('role') == 'admin'): ?>
                <td>
                    <button class="btn btn-sm btn-success" onclick='editPerbaikan(<?= json_encode($d) ?>)'><i class="fas fa-edit"></i></button>
                    <a href="<?= base_url('transaksi/hapus-perbaikan/'.$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 Perbaikan</h3>
            <button class="close-btn" onclick="closeModal('modalForm')">&times;</button>
        </div>
        <form action="<?= base_url('transaksi/simpan-perbaikan') ?>" 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="perbaikan_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" style="grid-column: span 2;">
                    <label>Jenis Perbaikan</label>
                    <input type="text" name="jenis_perbaikan" class="form-control" required>
                </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 Perbaikan</label>
                    <input type="date" name="tgl_perbaikan" class="form-control">
                </div>
                <div class="form-group">
                    <label>Biaya</label>
                    <input type="number" name="biaya" class="form-control">
                </div>
                <div class="form-group">
                    <!-- spacer -->
                </div>
                <div class="form-group">
                    <label>Foto Kerusakan</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 editPerbaikan(data) {
    document.getElementById('perbaikan_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="jenis_perbaikan"]').value = data.jenis_perbaikan;
    document.querySelector('#modalForm input[name="tgl_kerusakan"]').value = data.tgl_kerusakan;
    document.querySelector('#modalForm input[name="tgl_perbaikan"]').value = data.tgl_perbaikan;
    document.querySelector('#modalForm input[name="biaya"]').value = data.biaya;
    openModal('modalForm');
}
</script>
<?= $this->endSection() ?>
