@extends('layouts.app')

@section('title', 'Manajemen Tahun Ajaran')

@section('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>
                        @forelse ($tahunAjarans as $index => $ta)
                            <tr>
                                <td>{{ $index + 1 }}</td>
                                <td>{{ $ta->tahun }}</td>
                                <td>
                                    <span class="badge badge-info">
                                        {{ App\Models\TahunAjaran::namaBulan($ta->bulan_mulai_spp ?: 7) }} &ndash; {{ App\Models\TahunAjaran::namaBulan($ta->bulan_selesai_spp ?: 6) }}
                                    </span>
                                </td>
                                <td>Tgl {{ $ta->tanggal_jatuh_tempo_spp ?: 10 }}</td>
                                <td>
                                    @if ($ta->status === 'aktif')
                                        <span class="badge badge-success">Aktif</span>
                                    @else
                                        <span class="badge badge-secondary">Nonaktif</span>
                                    @endif
                                </td>
                                <td>
                                    <button type="button" class="btn btn-warning btn-sm" 
                                            data-toggle="modal" 
                                            data-target="#modal-edit-{{ $ta->id }}">
                                        <i class="fas fa-edit"></i>
                                    </button>
                                    @if ($ta->status !== 'aktif')
                                    <button type="button" class="btn btn-danger btn-sm m-0" onclick="confirmDelete({{ $ta->id }})">
                                        <i class="fas fa-trash"></i>
                                    </button>
                                    <form id="delete-form-{{ $ta->id }}" action="{{ route('tahun-ajaran.destroy', $ta->id) }}" method="POST" class="d-none">
                                        @csrf
                                        @method('DELETE')
                                    </form>
                                    @endif
                                </td>
                            </tr>

                            <!-- Edit Modal -->
                            <div class="modal fade" id="modal-edit-{{ $ta->id }}">
                                <div class="modal-dialog">
                                    <div class="modal-content">
                                        <form action="{{ route('tahun-ajaran.update', $ta->id) }}" method="POST">
                                            @csrf
                                            @method('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="{{ $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>
                                                                @for($b = 1; $b <= 12; $b++)
                                                                    <option value="{{ $b }}" {{ ($ta->bulan_mulai_spp ?: 7) == $b ? 'selected' : '' }}>{{ App\Models\TahunAjaran::namaBulan($b) }}</option>
                                                                @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>
                                                                @for($b = 1; $b <= 12; $b++)
                                                                    <option value="{{ $b }}" {{ ($ta->bulan_selesai_spp ?: 6) == $b ? 'selected' : '' }}>{{ App\Models\TahunAjaran::namaBulan($b) }}</option>
                                                                @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="{{ $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" {{ $ta->status === 'nonaktif' ? 'selected' : '' }}>Nonaktif</option>
                                                        <option value="aktif" {{ $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 -->
                        @empty
                            <tr>
                                <td colspan="6" class="text-center">Belum ada data tahun ajaran.</td>
                            </tr>
                        @endforelse
                    </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="{{ route('tahun-ajaran.store') }}" method="POST">
                @csrf
                <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>
                                    @for($b = 1; $b <= 12; $b++)
                                        <option value="{{ $b }}" {{ $b == 7 ? 'selected' : '' }}>{{ App\Models\TahunAjaran::namaBulan($b) }}</option>
                                    @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>
                                    @for($b = 1; $b <= 12; $b++)
                                        <option value="{{ $b }}" {{ $b == 6 ? 'selected' : '' }}>{{ App\Models\TahunAjaran::namaBulan($b) }}</option>
                                    @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 -->

@endsection

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

    @if(session('error'))
        Swal.fire({
            icon: 'error',
            title: 'Gagal',
            text: '{{ session('error') }}',
            timer: 3000,
            showConfirmButton: false
        });
    @endif

    @if($errors->any())
        Swal.fire({
            icon: 'error',
            title: 'Terdapat Kesalahan',
            html: '{!! implode("<br>", $errors->all()) !!}'
        });
    @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>
@endpush
