@extends('layouts.app')

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

@section('content')
<div class="container-fluid">
    <!-- Page Header -->
    <div class="d-flex justify-content-between align-items-center mb-4">
        <div>
            <h4 class="mb-1">
                <i class="bi bi-calendar-range text-primary me-2"></i>
                Manajemen Tahun Ajaran
            </h4>
            <p class="text-muted mb-0">Kelola tahun ajaran dan status keaktifan</p>
        </div>
        <a href="{{ route('superadmin.academic-years.create') }}" class="btn btn-primary">
            <i class="bi bi-plus-circle me-1"></i>
            Tambah Tahun Ajaran
        </a>
    </div>

    @if(session('success'))
    <div class="alert alert-success alert-dismissible fade show" role="alert">
        <i class="bi bi-check-circle me-2"></i>{{ session('success') }}
        <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
    </div>
    @endif

    @if(session('error'))
    <div class="alert alert-danger alert-dismissible fade show" role="alert">
        <i class="bi bi-exclamation-circle me-2"></i>{{ session('error') }}
        <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
    </div>
    @endif

    <!-- Academic Years List -->
    <div class="card border-0 shadow-sm">
        <div class="card-body">
            <div class="table-responsive">
                <table class="table table-hover align-middle">
                    <thead class="table-light">
                        <tr>
                            <th width="5%">No</th>
                            <th width="20%">Tahun Ajaran</th>
                            <th width="15%">Periode</th>
                            <th width="30%">Keterangan</th>
                            <th width="15%" class="text-center">Status</th>
                            <th width="15%" class="text-center">Aksi</th>
                        </tr>
                    </thead>
                    <tbody>
                        @forelse($academicYears as $index => $year)
                        <tr class="{{ $year->is_active ? 'table-success' : '' }}">
                            <td>{{ $index + 1 }}</td>
                            <td>
                                <strong class="text-dark">{{ $year->name }}</strong>
                                @if($year->is_active)
                                <span class="badge bg-success ms-2">
                                    <i class="bi bi-check-circle-fill me-1"></i>Aktif
                                </span>
                                @endif
                            </td>
                            <td>
                                <small class="text-muted">
                                    <i class="bi bi-calendar-event me-1"></i>
                                    {{ $year->start_date->format('d M Y') }}
                                    <br>
                                    <i class="bi bi-calendar-check me-1"></i>
                                    {{ $year->end_date->format('d M Y') }}
                                </small>
                            </td>
                            <td>
                                <small class="text-muted">{{ $year->description ?? '-' }}</small>
                            </td>
                            <td class="text-center">
                                @if($year->is_active)
                                <span class="badge bg-success px-3 py-2">
                                    <i class="bi bi-toggle-on me-1"></i>Aktif
                                </span>
                                @else
                                <span class="badge bg-secondary px-3 py-2">
                                    <i class="bi bi-toggle-off me-1"></i>Nonaktif
                                </span>
                                @endif
                            </td>
                            <td class="text-center">
                                <div class="btn-group" role="group">
                                    @if(!$year->is_active)
                                    <form action="{{ route('superadmin.academic-years.activate', $year->id) }}" method="POST" class="d-inline">
                                        @csrf
                                        <button type="submit" class="btn btn-sm btn-outline-success" title="Aktifkan" onclick="return confirm('Aktifkan tahun ajaran {{ $year->name }}?')">
                                            <i class="bi bi-check-circle"></i>
                                        </button>
                                    </form>
                                    @endif
                                    
                                    <a href="{{ route('superadmin.academic-years.edit', $year->id) }}" class="btn btn-sm btn-outline-primary" title="Edit">
                                        <i class="bi bi-pencil"></i>
                                    </a>
                                    
                                    @if(!$year->is_active)
                                    <form action="{{ route('superadmin.academic-years.destroy', $year->id) }}" method="POST" class="d-inline" onsubmit="return confirm('Hapus tahun ajaran {{ $year->name }}?')">
                                        @csrf
                                        @method('DELETE')
                                        <button type="submit" class="btn btn-sm btn-outline-danger" title="Hapus">
                                            <i class="bi bi-trash"></i>
                                        </button>
                                    </form>
                                    @endif
                                </div>
                            </td>
                        </tr>
                        @empty
                        <tr>
                            <td colspan="6" class="text-center py-5">
                                <i class="bi bi-calendar-x text-muted" style="font-size: 3rem;"></i>
                                <p class="text-muted mt-3 mb-0">Belum ada tahun ajaran</p>
                                <a href="{{ route('superadmin.academic-years.create') }}" class="btn btn-primary btn-sm mt-2">
                                    <i class="bi bi-plus-circle me-1"></i>Tambah Tahun Ajaran
                                </a>
                            </td>
                        </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>
        </div>
    </div>

    <!-- Info Card -->
    <div class="row mt-4">
        <div class="col-md-12">
            <div class="card border-0 border-start border-primary border-4 bg-light">
                <div class="card-body">
                    <h6 class="text-primary mb-2">
                        <i class="bi bi-info-circle-fill me-1"></i>Informasi
                    </h6>
                    <ul class="mb-0 small text-muted">
                        <li>Hanya satu tahun ajaran yang dapat aktif dalam satu waktu</li>
                        <li>Tahun ajaran yang aktif akan mempengaruhi data yang ditampilkan di seluruh sistem</li>
                        <li>Tahun ajaran yang sedang aktif tidak dapat dihapus</li>
                        <li>Pastikan periode tanggal sudah sesuai sebelum mengaktifkan tahun ajaran</li>
                    </ul>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection
