@extends('layouts.app')

@section('content')
<style>
    /* --- Global Dark Mode & Modern Overrides --- */
    .modern-card {
        background-color: var(--bg-card, #1e293b);
        border: 1px solid var(--border-color, #334155);
        border-radius: 20px;
    }
    
    [data-theme="dark"] .card { background-color: var(--bg-card); border: 1px solid var(--border-color); color: var(--text-main); }
    [data-theme="dark"] .card-header { border-color: var(--border-color); background-color: rgba(255,255,255,0.02) !important; color: var(--text-main) !important; }
    
    [data-theme="dark"] .form-control {
        background-color: var(--hover-bg); border-color: var(--border-color); color: var(--text-main); border-radius: 12px;
    }
    [data-theme="dark"] .form-control:focus { border-color: var(--primary-color); box-shadow: 0 0 0 0.25rem rgba(59, 130, 246, 0.25); }
    [data-theme="dark"] .text-muted { color: #cbd5e1 !important; }
    [data-theme="dark"] label { color: var(--text-main); }
    
    [data-theme="dark"] .table { color: var(--text-main); border-color: var(--border-color); }
    [data-theme="dark"] .table-light th { background-color: rgba(255,255,255,0.03) !important; color: var(--text-main); border-color: var(--border-color) !important; }
    [data-theme="dark"] .table-striped > tbody > tr:nth-of-type(odd) > * { background-color: rgba(255,255,255,0.02); color: var(--text-main); }
    
    .btn-pill { border-radius: 50rem !important; }
    .form-control { border-radius: 12px; }
</style>

<div class="container py-4">
    <div class="d-flex align-items-center mb-4">
        <h2 class="fw-bold text-white mb-0"><i class="bi bi-building me-2 text-primary"></i>Master Data Sekolah</h2>
    </div>

    {{-- Alert Success/Error --}}
    @if(session('success'))
        <div class="alert alert-success alert-dismissible fade show rounded-4 bg-success bg-opacity-25 border-0 border-start border-success border-4 text-white shadow-sm">
            <i class="bi bi-check-circle-fill me-2"></i> {{ session('success') }}
            <button type="button" class="btn-close btn-close-white" data-bs-dismiss="alert"></button>
        </div>
    @endif
    @if($errors->any())
        <div class="alert alert-danger alert-dismissible fade show rounded-4 bg-danger bg-opacity-25 border-0 border-start border-danger border-4 text-white shadow-sm">
            <ul class="mb-0">
                @foreach($errors->all() as $e) <li><i class="bi bi-exclamation-triangle me-1"></i> {{$e}}</li> @endforeach
            </ul>
            <button type="button" class="btn-close btn-close-white" data-bs-dismiss="alert" style="position: absolute; top: 50%; right: 1rem; transform: translateY(-50%);"></button>
        </div>
    @endif

    <div class="row g-4">
        {{-- BAGIAN 1: IDENTITAS SEKOLAH --}}
        <div class="col-md-5">
            <div class="card modern-card shadow-sm border-secondary h-100">
                <div class="card-header border-bottom border-secondary py-3 px-4">
                    <h5 class="mb-0 fw-bold"><i class="bi bi-card-heading me-2 text-primary"></i>Identitas Sekolah</h5>
                </div>
                <div class="card-body p-4">
                    <form action="{{ route('sekolah.update', $sekolah->id) }}" method="POST">
                        @csrf
                        @method('PUT')
                        
                        <div class="mb-3">
                            <label class="fw-bold small text-muted text-uppercase mb-2">Nama Sekolah</label>
                            <input type="text" name="name" class="form-control py-2" value="{{ $sekolah->name }}" required>
                        </div>
                        <div class="mb-3">
                            <label class="fw-bold small text-muted text-uppercase mb-2">Alamat</label>
                            <textarea name="address" class="form-control rounded-4 p-3" rows="3">{{ $sekolah->address }}</textarea>
                        </div>
                        <div class="mb-3">
                            <label class="fw-bold small text-muted text-uppercase mb-2">Kepala Sekolah</label>
                            <input type="text" name="principal_name" class="form-control py-2" value="{{ $sekolah->principal_name }}" required>
                        </div>
                        <div class="mb-4">
                            <label class="fw-bold small text-muted text-uppercase mb-2">NIP Kepsek</label>
                            <input type="text" name="principal_nip" class="form-control py-2" value="{{ $sekolah->principal_nip }}" required>
                        </div>
                        <button type="submit" class="btn btn-primary btn-pill w-100 py-2 fw-bold shadow-sm">
                            <i class="bi bi-save me-1"></i> Simpan Profil
                        </button>
                    </form>
                </div>
            </div>
        </div>

        {{-- BAGIAN 2: PENGATURAN JAM PELAJARAN --}}
        <div class="col-md-7">
            <div class="card modern-card shadow-sm border-secondary h-100">
                <div class="card-header border-bottom border-secondary py-3 px-4">
                    <h5 class="mb-0 fw-bold"><i class="bi bi-clock-history me-2 text-primary"></i>Pengaturan Jam Pelajaran</h5>
                </div>
                <div class="card-body p-4">
                    {{-- Form Tambah Jam --}}
                    <form action="{{ route('sekolah.jam.store') }}" method="POST" class="row g-2 align-items-end mb-4 border-bottom border-secondary pb-4">
                        @csrf
                        <div class="col-md-4">
                            <label class="small text-muted fw-bold text-uppercase mb-2">Nama (Cth: Jam 1)</label>
                            <input type="text" name="name" class="form-control rounded-pill" required placeholder="Jam Ke-...">
                        </div>
                        <div class="col-md-3">
                            <label class="small text-muted fw-bold text-uppercase mb-2">Mulai</label>
                            <input type="time" name="start_time" class="form-control rounded-pill" required>
                        </div>
                        <div class="col-md-3">
                            <label class="small text-muted fw-bold text-uppercase mb-2">Selesai</label>
                            <input type="time" name="end_time" class="form-control rounded-pill" required>
                        </div>
                        <div class="col-md-2">
                            <button type="submit" class="btn btn-success btn-pill w-100 shadow-sm fw-bold"><i class="bi bi-plus-lg"></i> Tambah</button>
                        </div>
                    </form>

                    {{-- Tabel Jam Pelajaran --}}
                    <div class="table-responsive border border-secondary rounded-4 overflow-hidden">
                        <table class="table table-striped align-middle mb-0 text-white">
                            <thead class="table-light">
                                <tr>
                                    <th class="ps-4 py-3">Nama Jam</th>
                                    <th class="py-3">Waktu Pelaksanaan</th>
                                    <th class="text-end pe-4 py-3">Aksi</th>
                                </tr>
                            </thead>
                            <tbody>
                                @forelse($lessonHours as $lh)
                                <tr>
                                    <td class="ps-4 border-secondary fw-bold text-light">{{ $lh->name }}</td>
                                    <td class="border-secondary">
                                        <span class="badge bg-primary bg-opacity-25 text-primary border border-primary rounded-pill px-3 py-2">
                                            <i class="bi bi-clock me-1"></i>
                                            {{ \Carbon\Carbon::parse($lh->start_time)->format('H:i') }} - 
                                            {{ \Carbon\Carbon::parse($lh->end_time)->format('H:i') }}
                                        </span>
                                    </td>
                                    <td class="text-end pe-4 border-secondary">
                                        <form action="{{ route('sekolah.jam.destroy', $lh->id) }}" method="POST" onsubmit="return confirm('Hapus jam pelajaran ini?')">
                                            @csrf @method('DELETE')
                                            <button class="btn btn-sm btn-outline-danger rounded-circle" style="width:32px; height:32px;" title="Hapus"><i class="bi bi-trash"></i></button>
                                        </form>
                                    </td>
                                </tr>
                                @empty
                                <tr><td colspan="3" class="text-center small text-muted py-4 border-0">Belum ada jadwal jam yang diatur.</td></tr>
                                @endforelse
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection