<?php $__env->startSection('title', 'Manajemen Tahun Ajaran'); ?>

<?php $__env->startSection('content'); ?>
<div class="row">
    <div class="col-12">
        <div class="card">
            <div class="card-header">
                <h3 class="card-title">Data Tahun Ajaran</h3>
                <div class="card-tools">
                    <button type="button" class="btn btn-primary btn-sm" data-toggle="modal" data-target="#modal-add">
                        <i class="fas fa-plus"></i> Tambah Data
                    </button>
                </div>
            </div>
            <!-- /.card-header -->
            <div class="card-body table-responsive p-0">
                <table class="table table-hover text-nowrap">
                    <thead>
                        <tr>
                            <th style="width: 50px;">No</th>
                            <th>Tahun Ajaran</th>
                            <th>Periode SPP</th>
                            <th>Tgl. Jatuh Tempo</th>
                            <th>Status</th>
                            <th style="width: 150px;">Aksi</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php $__empty_1 = true; $__currentLoopData = $tahunAjarans; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $index => $ta): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); $__empty_1 = false; ?>
                            <tr>
                                <td><?php echo e($index + 1); ?></td>
                                <td><?php echo e($ta->tahun); ?></td>
                                <td>
                                    <span class="badge badge-info">
                                        <?php echo e(App\Models\TahunAjaran::namaBulan($ta->bulan_mulai_spp ?: 7)); ?> &ndash; <?php echo e(App\Models\TahunAjaran::namaBulan($ta->bulan_selesai_spp ?: 6)); ?>

                                    </span>
                                </td>
                                <td>Tgl <?php echo e($ta->tanggal_jatuh_tempo_spp ?: 10); ?></td>
                                <td>
                                    <?php if($ta->status === 'aktif'): ?>
                                        <span class="badge badge-success">Aktif</span>
                                    <?php else: ?>
                                        <span class="badge badge-secondary">Nonaktif</span>
                                    <?php endif; ?>
                                </td>
                                <td>
                                    <button type="button" class="btn btn-warning btn-sm" 
                                            data-toggle="modal" 
                                            data-target="#modal-edit-<?php echo e($ta->id); ?>">
                                        <i class="fas fa-edit"></i>
                                    </button>
                                    <?php if($ta->status !== 'aktif'): ?>
                                    <button type="button" class="btn btn-danger btn-sm m-0" onclick="confirmDelete(<?php echo e($ta->id); ?>)">
                                        <i class="fas fa-trash"></i>
                                    </button>
                                    <form id="delete-form-<?php echo e($ta->id); ?>" action="<?php echo e(route('tahun-ajaran.destroy', $ta->id)); ?>" method="POST" class="d-none">
                                        <?php echo csrf_field(); ?>
                                        <?php echo method_field('DELETE'); ?>
                                    </form>
                                    <?php endif; ?>
                                </td>
                            </tr>

                            <!-- Edit Modal -->
                            <div class="modal fade" id="modal-edit-<?php echo e($ta->id); ?>">
                                <div class="modal-dialog">
                                    <div class="modal-content">
                                        <form action="<?php echo e(route('tahun-ajaran.update', $ta->id)); ?>" method="POST">
                                            <?php echo csrf_field(); ?>
                                            <?php echo method_field('PUT'); ?>
                                            <div class="modal-header">
                                                <h4 class="modal-title">Edit Tahun Ajaran</h4>
                                                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                                    <span aria-hidden="true">&times;</span>
                                                </button>
                                            </div>
                                            <div class="modal-body">
                                                <div class="form-group">
                                                    <label for="tahun">Tahun Ajaran</label>
                                                    <input type="text" name="tahun" class="form-control" value="<?php echo e($ta->tahun); ?>" required placeholder="YYYY/YYYY">
                                                </div>
                                                <div class="row">
                                                    <div class="col-md-6">
                                                        <div class="form-group">
                                                            <label>Bulan Mulai SPP</label>
                                                            <select name="bulan_mulai_spp" class="form-control" required>
                                                                <?php for($b = 1; $b <= 12; $b++): ?>
                                                                    <option value="<?php echo e($b); ?>" <?php echo e(($ta->bulan_mulai_spp ?: 7) == $b ? 'selected' : ''); ?>><?php echo e(App\Models\TahunAjaran::namaBulan($b)); ?></option>
                                                                <?php endfor; ?>
                                                            </select>
                                                        </div>
                                                    </div>
                                                    <div class="col-md-6">
                                                        <div class="form-group">
                                                            <label>Bulan Selesai SPP</label>
                                                            <select name="bulan_selesai_spp" class="form-control" required>
                                                                <?php for($b = 1; $b <= 12; $b++): ?>
                                                                    <option value="<?php echo e($b); ?>" <?php echo e(($ta->bulan_selesai_spp ?: 6) == $b ? 'selected' : ''); ?>><?php echo e(App\Models\TahunAjaran::namaBulan($b)); ?></option>
                                                                <?php endfor; ?>
                                                            </select>
                                                        </div>
                                                    </div>
                                                </div>
                                                <div class="form-group">
                                                    <label for="tanggal_jatuh_tempo_spp">Tanggal Jatuh Tempo SPP</label>
                                                    <input type="number" name="tanggal_jatuh_tempo_spp" class="form-control" value="<?php echo e($ta->tanggal_jatuh_tempo_spp ?: 10); ?>" min="1" max="31" required>
                                                    <small class="text-muted">Setiap tanggal berapakah SPP bulan berjalan jatuh tempo? (1-31)</small>
                                                </div>
                                                <div class="form-group">
                                                    <label for="status">Status</label>
                                                    <select name="status" class="form-control" required>
                                                        <option value="nonaktif" <?php echo e($ta->status === 'nonaktif' ? 'selected' : ''); ?>>Nonaktif</option>
                                                        <option value="aktif" <?php echo e($ta->status === 'aktif' ? 'selected' : ''); ?>>Aktif</option>
                                                    </select>
                                                </div>
                                            </div>
                                            <div class="modal-footer justify-content-between">
                                                <button type="button" class="btn btn-default" data-dismiss="modal">Tutup</button>
                                                <button type="submit" class="btn btn-primary">Simpan Perubahan</button>
                                            </div>
                                        </form>
                                    </div>
                                </div>
                            </div>
                            <!-- /.modal -->
                        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); if ($__empty_1): ?>
                            <tr>
                                <td colspan="6" class="text-center">Belum ada data tahun ajaran.</td>
                            </tr>
                        <?php endif; ?>
                    </tbody>
                </table>
            </div>
            <!-- /.card-body -->
        </div>
        <!-- /.card -->
    </div>
</div>

<!-- Add Modal -->
<div class="modal fade" id="modal-add">
    <div class="modal-dialog">
        <div class="modal-content">
            <form action="<?php echo e(route('tahun-ajaran.store')); ?>" method="POST">
                <?php echo csrf_field(); ?>
                <div class="modal-header">
                    <h4 class="modal-title">Tambah Tahun Ajaran</h4>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>
                <div class="modal-body">
                    <div class="form-group">
                        <label for="tahun">Tahun Ajaran</label>
                        <input type="text" name="tahun" class="form-control" required placeholder="Contoh: 2026/2027">
                    </div>
                    <div class="row">
                        <div class="col-md-6">
                            <div class="form-group">
                                <label>Bulan Mulai SPP</label>
                                <select name="bulan_mulai_spp" class="form-control" required>
                                    <?php for($b = 1; $b <= 12; $b++): ?>
                                        <option value="<?php echo e($b); ?>" <?php echo e($b == 7 ? 'selected' : ''); ?>><?php echo e(App\Models\TahunAjaran::namaBulan($b)); ?></option>
                                    <?php endfor; ?>
                                </select>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="form-group">
                                <label>Bulan Selesai SPP</label>
                                <select name="bulan_selesai_spp" class="form-control" required>
                                    <?php for($b = 1; $b <= 12; $b++): ?>
                                        <option value="<?php echo e($b); ?>" <?php echo e($b == 6 ? 'selected' : ''); ?>><?php echo e(App\Models\TahunAjaran::namaBulan($b)); ?></option>
                                    <?php endfor; ?>
                                </select>
                            </div>
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="tanggal_jatuh_tempo_spp">Tanggal Jatuh Tempo SPP</label>
                        <input type="number" name="tanggal_jatuh_tempo_spp" class="form-control" value="10" min="1" max="31" required>
                        <small class="text-muted">Setiap tanggal berapakah SPP bulan berjalan jatuh tempo? (1-31)</small>
                    </div>
                    <div class="alert alert-info">
                        <i class="fas fa-info-circle"></i> Contoh: Jika tahun ajaran dimulai Juli, pilih <strong>Juli</strong> s/d <strong>Juni</strong>.
                    </div>
                    <div class="form-group">
                        <label for="status">Status</label>
                        <select name="status" class="form-control" required>
                            <option value="nonaktif" selected>Nonaktif</option>
                            <option value="aktif">Aktif</option>
                        </select>
                    </div>
                </div>
                <div class="modal-footer justify-content-between">
                    <button type="button" class="btn btn-default" data-dismiss="modal">Tutup</button>
                    <button type="submit" class="btn btn-primary">Simpan</button>
                </div>
            </form>
        </div>
    </div>
</div>
<!-- /.modal -->

<?php $__env->stopSection(); ?>

<?php $__env->startPush('scripts'); ?>
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
<script>
    // Menampilkan pesan sukses atau error
    <?php if(session('success')): ?>
        Swal.fire({
            icon: 'success',
            title: 'Berhasil',
            text: '<?php echo e(session('success')); ?>',
            timer: 3000,
            showConfirmButton: false
        });
    <?php endif; ?>

    <?php if(session('error')): ?>
        Swal.fire({
            icon: 'error',
            title: 'Gagal',
            text: '<?php echo e(session('error')); ?>',
            timer: 3000,
            showConfirmButton: false
        });
    <?php endif; ?>

    <?php if($errors->any()): ?>
        Swal.fire({
            icon: 'error',
            title: 'Terdapat Kesalahan',
            html: '<?php echo implode("<br>", $errors->all()); ?>'
        });
    <?php endif; ?>

    function confirmDelete(id) {
        Swal.fire({
            title: 'Apakah Anda yakin?',
            text: "Data ini akan dihapus secara permanen!",
            icon: 'warning',
            showCancelButton: true,
            confirmButtonColor: '#d33',
            cancelButtonColor: '#3085d6',
            confirmButtonText: 'Ya, hapus!',
            cancelButtonText: 'Batal'
        }).then((result) => {
            if (result.isConfirmed) {
                document.getElementById('delete-form-' + id).submit();
            }
        });
    }
</script>
<?php $__env->stopPush(); ?>

<?php echo $__env->make('layouts.app', array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?><?php /**PATH C:\Herd\spp_app\resources\views/tahun-ajaran/index.blade.php ENDPATH**/ ?>