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

<div class="container-fluid">

    <!-- HEADER -->
    <div class="d-flex justify-content-between align-items-center mb-3">
        <div>
            <h4 class="font-weight-bold mb-1">
                <i class="fas fa-edit text-warning mr-2"></i>
                Edit Ruang Baca
            </h4>
            <small class="text-muted">
                Ubah data buku / artikel di ruang baca
            </small>
        </div>

        <a href="<?= base_url('ruang_baca') ?>" class="btn btn-secondary shadow-sm">
            <i class="fas fa-arrow-left mr-1"></i>
            Kembali
        </a>
    </div>

    <!-- FORM -->
    <div class="card shadow-sm border-0">
        <div class="card-body">

            <form action="<?= base_url('ruang_baca/update/' . $ruang_baca['id']) ?>" method="post" enctype="multipart/form-data">

                <?= csrf_field(); ?>

                <div class="row">

                    <div class="col-md-6">

                        <div class="form-group">
                            <label>Jenis</label>
                            <select name="jenis" id="jenis" class="form-control" required>
                                <option value="buku" <?= ($ruang_baca['jenis'] == 'buku') ? 'selected' : '' ?>>Buku</option>
                                <option value="artikel" <?= ($ruang_baca['jenis'] == 'artikel') ? 'selected' : '' ?>>Artikel</option>
                            </select>
                        </div>

                        <div class="form-group">
                            <label>Judul</label>
                            <input type="text" name="judul" class="form-control" value="<?= esc($ruang_baca['judul']) ?>" required>
                        </div>

                        <div class="form-group">
                            <label>Penulis</label>
                            <input type="text" name="penulis" class="form-control" value="<?= esc($ruang_baca['penulis']) ?>" required>
                        </div>

                    </div>

                    <div class="col-md-6">

                        <div class="form-group">
                            <label>Kategori</label>
                            <select name="kategori" class="form-control">
                                <?php
                                $kategori = [
                                    'Pelajaran',
                                    'Novel',
                                    'Agama',
                                    'Komputer',
                                    'Ilmiah',
                                    'Ensiklopedia',
                                    'Lainnya'
                                ];
                                ?>
                                <option value="">-- Pilih Kategori --</option>
                                <?php foreach ($kategori as $k) : ?>
                                    <option value="<?= $k ?>" <?= ($ruang_baca['kategori'] == $k) ? 'selected' : '' ?>><?= $k ?></option>
                                <?php endforeach; ?>
                            </select>
                        </div>

                        <div class="form-group">
                            <label>File PDF</label>
                            <div class="custom-file">
                                <input type="file" name="file_pdf" class="custom-file-input" id="file_pdf" accept="application/pdf">
                                <label class="custom-file-label" for="file_pdf">
                                    <?= !empty($ruang_baca['file_pdf']) ? esc($ruang_baca['file_pdf']) : 'Pilih file PDF' ?>
                                </label>
                            </div>
                            <small class="text-muted">
                                <i class="fas fa-info-circle mr-1"></i>
                                Kosongkan jika file tidak ingin diganti.
                            </small>
                        </div>

                        <?php if (!empty($ruang_baca['file_pdf'])) : ?>
                            <div class="alert alert-info py-2">
                                <i class="fas fa-file-pdf mr-1"></i>
                                File saat ini:
                                <a href="<?= base_url('ruang_baca/download/' . $ruang_baca['id']) ?>" target="_blank" class="alert-link">
                                    <?= esc($ruang_baca['file_pdf']) ?>
                                </a>
                            </div>
                        <?php endif; ?>

                    </div>

                </div>

                <div class="border-top pt-3">

                    <button class="btn btn-success">
                        <i class="fas fa-save mr-1"></i>
                        Update
                    </button>

                    <a href="<?= base_url('ruang_baca') ?>" class="btn btn-secondary">
                        Batal
                    </a>

                </div>

            </form>

        </div>
    </div>

</div>

<script src="<?= base_url('plugins/bs-custom-file-input/bs-custom-file-input.min.js') ?>"></script>
<script>
$(document).ready(function () {
    bsCustomFileInput.init();
});
</script>

<?= $this->endSection() ?>
