

<?php $__env->startSection('title', 'Daftar Buku'); ?>

<?php $__env->startSection('content_header'); ?>
    <div class="d-flex justify-content-between align-items-center">
        <h1><i class="fas fa-book mr-2"></i>Daftar Buku</h1>
        <div>
            <button type="button" class="btn btn-success mr-2" data-toggle="modal" data-target="#modalPukal">
                <i class="fas fa-file-excel mr-1"></i> Daftar Pukal
            </button>
            <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#modalDaftar">
                <i class="fas fa-plus mr-1"></i> Daftar Buku
            </button>
        </div>
    </div>
<?php $__env->stopSection(); ?>

<?php $__env->startSection('content'); ?>
    <?php if(session('success')): ?>
        <div class="alert alert-success alert-dismissible">
            <button type="button" class="close" data-dismiss="alert">&times;</button>
            <?php echo e(session('success')); ?>

        </div>
    <?php endif; ?>

    
    <?php
        $bgColors = [
            'bg-gradient-primary',
            'bg-gradient-success',
            'bg-gradient-warning',
            'bg-gradient-danger',
            'bg-gradient-info',
            'bg-gradient-secondary',
        ];
    ?>
    <div class="row">
        <div class="col-6 col-sm-4 col-md-2">
            <div class="small-box bg-gradient-dark card-filter" data-darjah="" style="cursor:pointer">
                <div class="inner">
                    <h3><?php echo e($jumlahTotal); ?></h3>
                    <p>Jumlah Semua Darjah</p>
                </div>
                <div class="icon"><i class="fas fa-book"></i></div>
                <a class="small-box-footer">Bilangan Kod Buku</a>
            </div>
        </div>
        <?php $__currentLoopData = $statDarjah->sortKeys(); $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $drj => $bil): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
            <div class="col-6 col-sm-4 col-md-2">
                <div class="small-box <?php echo e($bgColors[$loop->index % count($bgColors)]); ?> card-filter"
                    data-darjah="<?php echo e($drj); ?>" style="cursor:pointer">
                    <div class="inner">
                        <h3><?php echo e($bil); ?></h3>
                        <p><?php echo e($drj); ?></p>
                    </div>
                    <div class="icon"><i class="fas fa-book-open"></i></div>
                    <a class="small-box-footer">Bilangan Kod Buku</a>
                </div>
            </div>
        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
    </div>

    <div class="card card-outline card-primary">
        <div class="card-header">
            <h3 class="card-title"><i class="fas fa-list mr-1"></i> Senarai Buku</h3>
            <div class="card-tools">
                <span id="filterBadge" class="badge badge-info" style="display:none"></span>
                <button id="btnResetFilter" class="btn btn-sm btn-outline-secondary ml-1" style="display:none">
                    <i class="fas fa-times mr-1"></i> Papar Semua
                </button>
            </div>
        </div>
        <div class="card-body table-responsive p-0">
            <table class="table table-hover table-striped">
                <thead class="thead-dark">
                    <tr>
                        <th width="60">Bil</th>
                        <th>Kod Buku</th>
                        <th>Judul</th>
                        <th>Darjah</th>
                        <th>Jenis Sekolah</th>
                        <th width="120">Tindakan</th>
                    </tr>
                </thead>
                <tbody id="tableBuku">
                    <?php $__empty_1 = true; $__currentLoopData = $bukus; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $index => $buku): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); $__empty_1 = false; ?>
                        <tr data-darjah="<?php echo e($buku->darjah); ?>">
                            <td class="row-bil"><?php echo e($index + 1); ?></td>
                            <td><span class="badge badge-primary"><?php echo e($buku->kod_buku); ?></span></td>
                            <td><?php echo e($buku->judul); ?></td>
                            <td><?php echo e($buku->darjah); ?></td>
                            <td>
                                <?php if($buku->jenis_sekolah): ?>
                                    <span class="badge badge-info"><?php echo e($buku->jenis_sekolah); ?></span>
                                <?php else: ?>
                                    <span class="text-muted">Semua</span>
                                <?php endif; ?>
                            </td>
                            <td>
                                <div class="d-flex">
                                    <button type="button" class="btn btn-warning btn-sm mr-1 btn-edit"
                                        data-kod="<?php echo e($buku->kod_buku); ?>" data-judul="<?php echo e($buku->judul); ?>"
                                        data-darjah="<?php echo e($buku->darjah); ?>" data-jenis-sekolah="<?php echo e($buku->jenis_sekolah); ?>"
                                        data-toggle="modal" data-target="#modalEdit">
                                        <i class="fas fa-edit"></i>
                                    </button>
                                    <form action="<?php echo e(route('admin.buku.destroy', $buku->kod_buku)); ?>" method="POST"
                                        onsubmit="return confirm('Padam buku ini?')">
                                        <?php echo csrf_field(); ?>
                                        <?php echo method_field('DELETE'); ?>
                                        <button type="submit" class="btn btn-danger btn-sm">
                                            <i class="fas fa-trash"></i>
                                        </button>
                                    </form>
                                </div>
                            </td>
                        </tr>
                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); if ($__empty_1): ?>
                        <tr>
                            <td colspan="6" class="text-center text-muted py-3">Tiada data buku.</td>
                        </tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>

    <!-- Modal Daftar Buku -->
    <div class="modal fade" id="modalDaftar" tabindex="-1" role="dialog">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header bg-primary text-white">
                    <h5 class="modal-title"><i class="fas fa-plus-circle mr-1"></i> Daftar Buku Baharu</h5>
                    <button type="button" class="close text-white" data-dismiss="modal">&times;</button>
                </div>
                <form action="<?php echo e(route('admin.buku.store')); ?>" method="POST">
                    <?php echo csrf_field(); ?>
                    <div class="modal-body">
                        <?php if($errors->any()): ?>
                            <div class="alert alert-danger alert-dismissible">
                                <button type="button" class="close" data-dismiss="alert">&times;</button>
                                <ul class="mb-0">
                                    <?php $__currentLoopData = $errors->all(); $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $error): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                        <li><?php echo e($error); ?></li>
                                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                </ul>
                            </div>
                        <?php endif; ?>
                        <div class="form-group">
                            <label>Kod Buku <span class="text-danger">*</span></label>
                            <input type="text" name="kod_buku"
                                class="form-control <?php $__errorArgs = ['kod_buku'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?> is-invalid <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>" value="<?php echo e(old('kod_buku')); ?>"
                                placeholder="Contoh: BK001" style="text-transform: uppercase">
                            <?php $__errorArgs = ['kod_buku'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?>
                                <div class="invalid-feedback"><?php echo e($message); ?></div>
                            <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>
                        </div>
                        <div class="form-group">
                            <label>Judul <span class="text-danger">*</span></label>
                            <input type="text" name="judul"
                                class="form-control <?php $__errorArgs = ['judul'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?> is-invalid <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>" value="<?php echo e(old('judul')); ?>"
                                placeholder="Masukkan judul buku">
                            <?php $__errorArgs = ['judul'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?>
                                <div class="invalid-feedback"><?php echo e($message); ?></div>
                            <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>
                        </div>
                        <div class="form-group">
                            <label>Darjah <span class="text-danger">*</span></label>
                            <select name="darjah" class="form-control <?php $__errorArgs = ['darjah'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?> is-invalid <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>">
                                <option value="">-- Pilih Darjah --</option>
                                <?php $__currentLoopData = $darjahs; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $drj): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                    <option value="<?php echo e($drj->penerangan); ?>"
                                        <?php echo e(old('darjah') == $drj->penerangan ? 'selected' : ''); ?>>
                                        <?php echo e($drj->penerangan); ?>

                                    </option>
                                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                            </select>
                            <?php $__errorArgs = ['darjah'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?>
                                <div class="invalid-feedback"><?php echo e($message); ?></div>
                            <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>
                        </div>
                        <div class="form-group">
                            <label>Jenis Sekolah <small class="text-muted">(kosongkan jika untuk semua)</small></label>
                            <select name="jenis_sekolah" class="form-control">
                                <option value="">-- Semua Jenis Sekolah --</option>
                                <?php $__currentLoopData = $jenisSekolahs; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $js): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                    <option value="<?php echo e($js->nama_jenis); ?>"
                                        <?php echo e(old('jenis_sekolah') == $js->nama_jenis ? 'selected' : ''); ?>>
                                        <?php echo e($js->kod_jenis); ?> (<?php echo e($js->nama_jenis); ?>)
                                    </option>
                                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                            </select>
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-secondary" data-dismiss="modal">
                            <i class="fas fa-times mr-1"></i> Batal
                        </button>
                        <button type="submit" class="btn btn-primary">
                            <i class="fas fa-save mr-1"></i> Simpan
                        </button>
                    </div>
                </form>
            </div>
        </div>
    </div>

    <!-- Modal Daftar Pukal -->
    <div class="modal fade" id="modalPukal" tabindex="-1" role="dialog">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header bg-success text-white">
                    <h5 class="modal-title"><i class="fas fa-file-excel mr-1"></i> Daftar Pukal Buku</h5>
                    <button type="button" class="close text-white" data-dismiss="modal">&times;</button>
                </div>
                <form action="<?php echo e(route('admin.buku.import-pukal')); ?>" method="POST" enctype="multipart/form-data">
                    <?php echo csrf_field(); ?>
                    <div class="modal-body">
                        <div class="alert alert-info">
                            <i class="fas fa-info-circle mr-1"></i>
                            Muat turun <a href="<?php echo e(route('admin.buku.template-excel')); ?>" class="alert-link">template
                                Excel</a> sebelum mengisi data.
                        </div>
                        <p class="font-weight-bold mb-1">Format lajur yang diperlukan:</p>
                        <table class="table table-sm table-bordered mb-3">
                            <thead class="thead-light">
                                <tr>
                                    <th>Bil</th>
                                    <th>Kod Buku</th>
                                    <th>Judul</th>
                                    <th>Darjah</th>
                                </tr>
                            </thead>
                            <tbody>
                                <tr class="text-muted">
                                    <td>1</td>
                                    <td>BK001</td>
                                    <td>Bahasa Melayu Jilid 1</td>
                                    <td>Darjah 1</td>
                                </tr>
                            </tbody>
                        </table>
                        <div class="form-group">
                            <label>Fail Excel <span class="text-danger">*</span></label>
                            <div class="custom-file">
                                <input type="file" class="custom-file-input" id="failExcelBuku" name="fail_excel"
                                    accept=".xlsx,.xls,.csv">
                                <label class="custom-file-label" for="failExcelBuku">Pilih fail...</label>
                            </div>
                            <small class="text-muted">Format: .xlsx, .xls, .csv (Maks: 5MB)</small>
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-secondary" data-dismiss="modal">
                            <i class="fas fa-times mr-1"></i> Batal
                        </button>
                        <button type="submit" class="btn btn-success">
                            <i class="fas fa-upload mr-1"></i> Import
                        </button>
                    </div>
                </form>
            </div>
        </div>
    </div>

    <!-- Modal Edit Buku -->
    <div class="modal fade" id="modalEdit" tabindex="-1" role="dialog">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header bg-warning">
                    <h5 class="modal-title"><i class="fas fa-edit mr-1"></i> Kemaskini Buku</h5>
                    <button type="button" class="close" data-dismiss="modal">&times;</button>
                </div>
                <form id="formEdit" method="POST">
                    <?php echo csrf_field(); ?>
                    <?php echo method_field('PUT'); ?>
                    <div class="modal-body">
                        <div class="form-group">
                            <label>Kod Buku</label>
                            <input type="text" id="edit_kod" class="form-control" disabled>
                        </div>
                        <div class="form-group">
                            <label>Judul <span class="text-danger">*</span></label>
                            <input type="text" name="judul" id="edit_judul" class="form-control" required
                                placeholder="Masukkan judul buku">
                        </div>
                        <div class="form-group">
                            <label>Darjah <span class="text-danger">*</span></label>
                            <select name="darjah" id="edit_darjah" class="form-control" required>
                                <option value="">-- Pilih Darjah --</option>
                                <?php $__currentLoopData = $darjahs; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $drj): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                    <option value="<?php echo e($drj->penerangan); ?>"><?php echo e($drj->penerangan); ?></option>
                                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                            </select>
                        </div>
                        <div class="form-group">
                            <label>Jenis Sekolah <small class="text-muted">(kosongkan jika untuk semua)</small></label>
                            <select name="jenis_sekolah" id="edit_jenis_sekolah" class="form-control">
                                <option value="">-- Semua Jenis Sekolah --</option>
                                <?php $__currentLoopData = $jenisSekolahs; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $js): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                    <option value="<?php echo e($js->nama_jenis); ?>"><?php echo e($js->kod_jenis); ?> (<?php echo e($js->nama_jenis); ?>)
                                    </option>
                                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                            </select>
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-secondary" data-dismiss="modal">
                            <i class="fas fa-times mr-1"></i> Batal
                        </button>
                        <button type="submit" class="btn btn-warning">
                            <i class="fas fa-save mr-1"></i> Kemaskini
                        </button>
                    </div>
                </form>
            </div>
        </div>
    </div>
<?php $__env->stopSection(); ?>

<?php $__env->startPush('js'); ?>
    <script>
        // Auto buka modal daftar jika ada validation error
        <?php if($errors->any()): ?>
            $('#modalDaftar').modal('show');
        <?php endif; ?>

        // Isi data ke modal edit
        $('.btn-edit').on('click', function() {
            var kod = $(this).data('kod');
            var judul = $(this).data('judul');
            var darjah = $(this).data('darjah');
            var jenisSekolah = $(this).data('jenis-sekolah') || '';

            $('#edit_kod').val(kod);
            $('#edit_judul').val(judul);
            $('#edit_darjah').val(darjah);
            $('#edit_jenis_sekolah').val(jenisSekolah);

            var url = '<?php echo e(url('admin/buku')); ?>/' + kod;
            $('#formEdit').attr('action', url);
        });

        // Update label for custom file input
        $('#failExcelBuku').on('change', function() {
            var fileName = $(this).val().split('\\').pop();
            $(this).siblings('.custom-file-label').text(fileName || 'Pilih fail...');
        });

        // Filter table by darjah on card click
        $('.card-filter').on('click', function() {
            var darjah = $(this).data('darjah');
            filterTable(darjah);
        });

        $('#btnResetFilter').on('click', function() {
            filterTable('');
        });

        function filterTable(darjah) {
            var rows = $('#tableBuku tr[data-darjah]');
            var bil = 1;

            rows.each(function() {
                if (darjah === '' || $(this).data('darjah') == darjah) {
                    $(this).show();
                    $(this).find('.row-bil').text(bil++);
                } else {
                    $(this).hide();
                }
            });

            // highlight active card
            $('.card-filter').css('opacity', darjah === '' ? '1' : '0.5');
            if (darjah !== '') {
                $('.card-filter[data-darjah="' + darjah + '"]').css('opacity', '1');
                $('#filterBadge').text('Ditapis: ' + darjah).show();
                $('#btnResetFilter').show();
            } else {
                $('.card-filter').css('opacity', '1');
                $('#filterBadge').hide();
                $('#btnResetFilter').hide();
            }
        }
    </script>
<?php $__env->stopPush(); ?>

<?php echo $__env->make('adminlte::page', array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?><?php /**PATH C:\laragon\www\myspbt\resources\views/admin/buku/index.blade.php ENDPATH**/ ?>