@extends('layouts.app')

@section('title', 'Daya Tampung Murid Baru')

@section('content')
<div class="container-fluid">
    <!-- Page Header -->
    <div class="mb-4">
        <h2 class="mb-1">
            <i class="bi bi-clipboard-data text-primary"></i>
            Daya Tampung Murid Baru
        </h2>
        <p class="text-muted mb-0">Kelola kapasitas penerimaan siswa baru untuk tahun ajaran {{ $tahunAjaranBaru }}</p>
    </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

    <!-- Tab Navigation -->
    <div class="card border-0 shadow-sm">
        <div class="card-header bg-white">
            <ul class="nav nav-tabs card-header-tabs" id="dayaTampungTabs" role="tablist">
                <li class="nav-item" role="presentation">
                    <button class="nav-link active" id="rombel-saat-ini-tab" data-bs-toggle="tab" 
                            data-bs-target="#rombel-saat-ini" type="button" role="tab" 
                            aria-controls="rombel-saat-ini" aria-selected="true">
                        <i class="bi bi-grid-3x3-gap me-2"></i>
                        Rombel Saat Ini
                    </button>
                </li>
                <li class="nav-item" role="presentation">
                    <button class="nav-link" id="rencana-rombel-tab" data-bs-toggle="tab" 
                            data-bs-target="#rencana-rombel" type="button" role="tab" 
                            aria-controls="rencana-rombel" aria-selected="false">
                        <i class="bi bi-calendar-check me-2"></i>
                        Rencana Rombel Tahun Ajaran Selanjutnya
                    </button>
                </li>
                <li class="nav-item" role="presentation">
                    <button class="nav-link" id="daya-tampung-jenjang-tab" data-bs-toggle="tab" 
                            data-bs-target="#daya-tampung-jenjang" type="button" role="tab" 
                            aria-controls="daya-tampung-jenjang" aria-selected="false">
                        <i class="bi bi-bar-chart-line me-2"></i>
                        Daya Tampung Tiap Jenjang
                    </button>
                </li>
            </ul>
        </div>
        
        <div class="card-body">
            <div class="tab-content" id="dayaTampungTabsContent">
                <!-- Tab 1: Rombel Saat Ini -->
                <div class="tab-pane fade show active" id="rombel-saat-ini" role="tabpanel" 
                     aria-labelledby="rombel-saat-ini-tab">
                    <div class="row mb-3">
                        <div class="col-md-12">
                            <div class="alert alert-info border-0">
                                <i class="bi bi-info-circle me-2"></i>
                                <strong>Rombongan Belajar Saat Ini:</strong> Data rombel yang sedang aktif pada tahun ajaran {{ $tahunAjaranAktif }}
                            </div>
                        </div>
                    </div>
                    
                    <!-- Konten Rombel Saat Ini - Khusus SLB -->
                    <div class="row">
                        <div class="col-lg-8 mx-auto">
                            <div class="card border-0 shadow-sm">
                                <div class="card-header bg-danger text-white">
                                    <h5 class="mb-0">
                                        <i class="bi bi-heart me-2"></i>
                                        Data Rombel SLB (Sekolah Luar Biasa)
                                    </h5>
                                </div>
                                <div class="card-body">
                                    <form id="rombelSaatIniForm" method="POST" action="javascript:void(0);" onsubmit="submitFormTab1(this, event); return false;">
                                        @csrf
                                        <div class="table-responsive">
                                            <table class="table table-bordered align-middle">
                                                <thead class="table-light">
                                                    <tr>
                                                        <th width="40%">Jenjang</th>
                                                        <th width="30%" class="text-center">Jumlah Rombel</th>
                                                        <th width="30%" class="text-center">Jumlah Anggota Rombel</th>
                                                    </tr>
                                                </thead>
                                                <tbody>
                                                    <tr>
                                                        <td><strong>TKLB A</strong></td>
                                                        <td>
                                                            <input type="number" class="form-control form-control-sm text-center rombel-count" 
                                                                   name="tklb_a_rombel" min="0" value="{{ $rombelSaatIni->tklb_a_rombel ?? 0 }}" data-jenjang="tklb_a">
                                                        </td>
                                                        <td>
                                                            <input type="number" class="form-control form-control-sm text-center siswa-count" 
                                                                   name="tklb_a_siswa" min="0" value="{{ $rombelSaatIni->tklb_a_siswa ?? 0 }}">
                                                        </td>
                                                    </tr>
                                                    <tr>
                                                        <td><strong>TKLB B</strong></td>
                                                        <td>
                                                            <input type="number" class="form-control form-control-sm text-center rombel-count" 
                                                                   name="tklb_b_rombel" min="0" value="{{ $rombelSaatIni->tklb_b_rombel ?? 0 }}" data-jenjang="tklb_b">
                                                        </td>
                                                        <td>
                                                            <input type="number" class="form-control form-control-sm text-center siswa-count" 
                                                                   name="tklb_b_siswa" min="0" value="{{ $rombelSaatIni->tklb_b_siswa ?? 0 }}">
                                                        </td>
                                                    </tr>
                                                    @for($i = 1; $i <= 12; $i++)
                                                    <tr>
                                                        <td><strong>Kelas {{ $i }}</strong></td>
                                                        <td>
                                                            <input type="number" class="form-control form-control-sm text-center rombel-count" 
                                                                   name="kelas_{{ $i }}_rombel" min="0" value="{{ $rombelSaatIni->{'kelas_'.$i.'_rombel'} ?? 0 }}" data-jenjang="kelas_{{ $i }}">
                                                        </td>
                                                        <td>
                                                            <input type="number" class="form-control form-control-sm text-center siswa-count" 
                                                                   name="kelas_{{ $i }}_siswa" min="0" value="{{ $rombelSaatIni->{'kelas_'.$i.'_siswa'} ?? 0 }}">
                                                        </td>
                                                    </tr>
                                                    @endfor
                                                </tbody>
                                                <tfoot class="table-secondary">
                                                    <tr>
                                                        <td><strong>Jumlah Rombel</strong></td>
                                                        <td colspan="2" class="text-center">
                                                            <input type="text" class="form-control form-control-sm text-center fw-bold bg-light" 
                                                                   name="total_rombel" id="totalRombel" readonly value="{{ $rombelSaatIni->total_rombel ?? 0 }}">
                                                        </td>
                                                    </tr>
                                                    <tr>
                                                        <td><strong>Jumlah Siswa</strong></td>
                                                        <td colspan="2" class="text-center">
                                                            <input type="text" class="form-control form-control-sm text-center fw-bold bg-light" 
                                                                   name="total_siswa" id="totalSiswa" readonly value="{{ $rombelSaatIni->total_siswa ?? 0 }}">
                                                        </td>
                                                    </tr>
                                                </tfoot>
                                            </table>
                                        </div>
                                        
                                        <div class="d-flex justify-content-end gap-2 mt-3">
                                            <button type="reset" class="btn btn-secondary">
                                                <i class="bi bi-x-circle me-1"></i> Reset
                                            </button>
                                            <button type="submit" class="btn btn-primary">
                                                <i class="bi bi-save me-1"></i> Simpan Data
                                            </button>
                                        </div>
                                    </form>
                                </div>
                            </div>
                            
                            <div class="alert alert-info mt-3 border-0">
                                <i class="bi bi-lightbulb me-2"></i>
                                <strong>Petunjuk:</strong> 
                                <ul class="mb-0 mt-2 small">
                                    <li>TKLB A = Taman Kanak-Kanak Luar Biasa kelompok A</li>
                                    <li>TKLB B = Taman Kanak-Kanak Luar Biasa kelompok B</li>
                                    <li>Kelas 1-12 = Tingkat pendidikan dari SDLB hingga SMALB untuk siswa berkebutuhan khusus</li>
                                    <li>Total akan otomatis dihitung saat Anda mengisi data</li>
                                </ul>
                            </div>
                        </div>
                    </div>
                </div>

                <!-- Tab 2: Rencana Rombel Tahun Ajaran Selanjutnya -->
                <div class="tab-pane fade" id="rencana-rombel" role="tabpanel" 
                     aria-labelledby="rencana-rombel-tab">
                    <div class="row mb-3">
                        <div class="col-md-12">
                            <div class="alert alert-warning border-0">
                                <i class="bi bi-calendar-check me-2"></i>
                                <strong>Rencana Rombel:</strong> Perencanaan rombongan belajar untuk tahun ajaran yang akan datang
                            </div>
                        </div>
                    </div>
                    
                    <!-- Konten Rencana Rombel - Khusus SLB -->
                    <div class="row">
                        <div class="col-lg-10 mx-auto">
                            <div class="card border-0 shadow-sm">
                                <div class="card-header bg-warning text-dark">
                                    <h5 class="mb-0">
                                        <i class="bi bi-calendar-plus me-2"></i>
                                        Rencana Rombel Tahun Ajaran Baru - SLB
                                    </h5>
                                </div>
                                <div class="card-body">
                                    <form id="rencanaRombelForm" onsubmit="handleRencanaRombelSubmit(event); return false;">
                                        @csrf
                                        <div class="table-responsive">
                                            <table class="table table-bordered align-middle">
                                                <thead class="table-light">
                                                    <tr>
                                                        <th width="25%" class="text-center">
                                                            <i class="bi bi-flower1 me-1"></i>
                                                            Rombel<br>Kelas TK TA Baru
                                                        </th>
                                                        <th width="25%" class="text-center">
                                                            <i class="bi bi-123 me-1"></i>
                                                            Rombel<br>Kelas 1 TA Baru
                                                        </th>
                                                        <th width="25%" class="text-center">
                                                            <i class="bi bi-book me-1"></i>
                                                            Rombel<br>Kelas 7 TA Baru
                                                        </th>
                                                        <th width="25%" class="text-center">
                                                            <i class="bi bi-mortarboard me-1"></i>
                                                            Rombel<br>Kelas 10 TA Baru
                                                        </th>
                                                    </tr>
                                                </thead>
                                                <tbody>
                                                    <tr>
                                                        <td class="text-center">
                                                            <input type="number" class="form-control form-control-lg text-center fw-bold rencana-rombel" 
                                                                   name="rombel_tk" min="0" value="{{ $dayaTampung->rencana_rombel_tk ?? 0 }}" placeholder="0">
                                                        </td>
                                                        <td class="text-center">
                                                            <input type="number" class="form-control form-control-lg text-center fw-bold rencana-rombel" 
                                                                   name="rombel_kelas_1" min="0" value="{{ $dayaTampung->rencana_rombel_kelas_1 ?? 0 }}" placeholder="0">
                                                        </td>
                                                        <td class="text-center">
                                                            <input type="number" class="form-control form-control-lg text-center fw-bold rencana-rombel" 
                                                                   name="rombel_kelas_7" min="0" value="{{ $dayaTampung->rencana_rombel_kelas_7 ?? 0 }}" placeholder="0">
                                                        </td>
                                                        <td class="text-center">
                                                            <input type="number" class="form-control form-control-lg text-center fw-bold rencana-rombel" 
                                                                   name="rombel_kelas_10" min="0" value="{{ $dayaTampung->rencana_rombel_kelas_10 ?? 0 }}" placeholder="0">
                                                        </td>
                                                    </tr>
                                                </tbody>
                                            </table>
                                        </div>
                                        
                                        <div class="d-flex justify-content-end gap-2 mt-3">
                                            <button type="reset" class="btn btn-secondary">
                                                <i class="bi bi-x-circle me-1"></i> Reset
                                            </button>
                                            <button type="submit" class="btn btn-warning">
                                                <i class="bi bi-save me-1"></i> Simpan Rencana
                                            </button>
                                        </div>
                                    </form>
                                </div>
                            </div>
                            
                            <div class="alert alert-info mt-3 border-0">
                                <i class="bi bi-lightbulb me-2"></i>
                                <strong>Keterangan:</strong> 
                                <ul class="mb-0 mt-2 small">
                                    <li><strong>Kelas TK TA Baru:</strong> Rencana jumlah rombel untuk tingkat Taman Kanak-kanak (TKLB A & B)</li>
                                    <li><strong>Kelas 1 TA Baru:</strong> Rencana jumlah rombel untuk kelas 1 SDLB (siswa baru tingkat dasar)</li>
                                    <li><strong>Kelas 7 TA Baru:</strong> Rencana jumlah rombel untuk kelas 7 SMPLB (siswa baru tingkat menengah pertama)</li>
                                    <li><strong>Kelas 10 TA Baru:</strong> Rencana jumlah rombel untuk kelas 10 SMALB (siswa baru tingkat menengah atas)</li>
                                    <li class="mt-2 text-primary"><i class="bi bi-info-circle me-1"></i>Data ini digunakan untuk perencanaan penerimaan siswa baru tahun ajaran mendatang</li>
                                </ul>
                            </div>
                        </div>
                    </div>
                </div>

                <!-- Tab 3: Daya Tampung Tiap Jenjang -->
                <div class="tab-pane fade" id="daya-tampung-jenjang" role="tabpanel" 
                     aria-labelledby="daya-tampung-jenjang-tab">
                    
                    @if($isSlb)
                    <!-- Khusus SLB - Tabel Gabungan Semua Jenis -->
                    <div class="row">
                        <div class="col-lg-11 mx-auto">
                            <div class="card border-0 shadow-sm">
                                <div class="card-header bg-warning text-dark">
                                    <h5 class="mb-0">
                                        <i class="bi bi-heart me-2"></i>
                                        Daya Tampung SLB (Sekolah Luar Biasa)
                                    </h5>
                                </div>
                                <div class="card-body">
                                    <form id="dayaTampungSlbForm" onsubmit="handleDayaTampungSlbSubmit(event); return false;">
                                        @csrf
                                        <div class="table-responsive">
                                            <table class="table table-bordered align-middle">
                                                <thead class="table-warning">
                                                    <tr>
                                                        <th class="text-center bg-warning text-dark align-middle" width="20%">
                                                            <i class="bi bi-people me-1"></i>
                                                            Jumlah Siswa<br>Per Rombel
                                                        </th>
                                                        <th class="text-center" width="20%">
                                                            Daya Tampung<br>Kelas TK TA Baru
                                                        </th>
                                                        <th class="text-center" width="20%">
                                                            Daya Tampung<br>Kelas 1 TA Baru
                                                        </th>
                                                        <th class="text-center" width="20%">
                                                            Daya Tampung<br>Kelas 7 TA Baru
                                                        </th>
                                                        <th class="text-center" width="20%">
                                                            Daya Tampung<br>Kelas 10 TA Baru
                                                        </th>
                                                    </tr>
                                                </thead>
                                                <tbody>
                                                    <tr>
                                                        <td class="text-center bg-warning bg-opacity-10">
                                                            <input type="number" class="form-control form-control-sm text-center fw-bold siswa-per-rombel" 
                                                                   name="siswa_per_rombel" min="0" value="{{ $dayaTampung->siswa_per_rombel ?? '' }}" placeholder="Isi jumlah siswa" data-jenis="slb">
                                                        </td>
                                                        <td class="text-center">
                                                            <input type="number" class="form-control form-control-sm text-center daya-tk" 
                                                                   name="daya_tampung_tk" min="0" value="{{ $dayaTampung->daya_tampung_tk ?? '' }}" placeholder="Isi daya tampung">
                                                        </td>
                                                        <td class="text-center">
                                                            <input type="number" class="form-control form-control-sm text-center daya-1" 
                                                                   name="daya_tampung_1" min="0" value="{{ $dayaTampung->daya_tampung_kelas_1 ?? '' }}" placeholder="Isi daya tampung">
                                                        </td>
                                                        <td class="text-center">
                                                            <input type="number" class="form-control form-control-sm text-center daya-7" 
                                                                   name="daya_tampung_7" min="0" value="{{ $dayaTampung->daya_tampung_kelas_7 ?? '' }}" placeholder="Isi daya tampung">
                                                        </td>
                                                        <td class="text-center">
                                                            <input type="number" class="form-control form-control-sm text-center daya-10" 
                                                                   name="daya_tampung_10" min="0" value="{{ $dayaTampung->daya_tampung_kelas_10 ?? '' }}" placeholder="Isi daya tampung">
                                                        </td>
                                                    </tr>
                                                </tbody>
                                            </table>
                                        </div>
                                        
                                        <div class="alert alert-info border-0 small mt-3">
                                            <i class="bi bi-info-circle me-2"></i>
                                            <strong>Keterangan:</strong> 
                                            <ul class="mb-0 mt-2">
                                                <li>Isi semua kolom sesuai kebutuhan sekolah</li>
                                                <li>Pastikan data yang diisi sudah sesuai dengan rencana penerimaan siswa baru</li>
                                            </ul>
                                        </div>
                                        
                                        <div class="d-flex justify-content-end gap-2 mt-3">
                                            <button type="reset" class="btn btn-secondary">
                                                <i class="bi bi-x-circle me-1"></i> Reset
                                            </button>
                                            <button type="submit" class="btn btn-warning">
                                                <i class="bi bi-save me-1"></i> Simpan Data Daya Tampung
                                            </button>
                                        </div>
                                    </form>
                                </div>
                            </div>
                        </div>
                    </div>
                    @else
                    <!-- Sub-tabs untuk Jenjang (SD, SMP, SMA/SMK, SLB) -->
                    <ul class="nav nav-pills mb-4" id="jenjangTabs" role="tablist">
                        <li class="nav-item" role="presentation">
                            <button class="nav-link active" id="jenjang-sd-tab" data-bs-toggle="tab" 
                                    data-bs-target="#jenjang-sd" type="button" role="tab" 
                                    aria-controls="jenjang-sd" aria-selected="true">
                                <i class="bi bi-book me-1"></i> SD
                            </button>
                        </li>
                        <li class="nav-item" role="presentation">
                            <button class="nav-link" id="jenjang-smp-tab" data-bs-toggle="tab" 
                                    data-bs-target="#jenjang-smp" type="button" role="tab" 
                                    aria-controls="jenjang-smp" aria-selected="false">
                                <i class="bi bi-book me-1"></i> SMP
                            </button>
                        </li>
                        <li class="nav-item" role="presentation">
                            <button class="nav-link" id="jenjang-sma-tab" data-bs-toggle="tab" 
                                    data-bs-target="#jenjang-sma" type="button" role="tab" 
                                    aria-controls="jenjang-sma" aria-selected="false">
                                <i class="bi bi-mortarboard me-1"></i> SMA/SMK
                            </button>
                        </li>
                        <li class="nav-item" role="presentation">
                            <button class="nav-link" id="jenjang-slb-tab" data-bs-toggle="tab" 
                                    data-bs-target="#jenjang-slb" type="button" role="tab" 
                                    aria-controls="jenjang-slb" aria-selected="false">
                                <i class="bi bi-heart me-1"></i> SLB
                            </button>
                        </li>
                    </ul>
                    
                    <div class="tab-content" id="jenjangTabsContent">
                        <!-- SD -->
                        <div class="tab-pane fade show active" id="jenjang-sd" role="tabpanel" 
                             aria-labelledby="jenjang-sd-tab">
                            <div class="card bg-light border-0">
                                <div class="card-body text-center py-5">
                                    <i class="bi bi-book text-primary" style="font-size: 3rem;"></i>
                                    <h5 class="mt-3">Jenjang SD</h5>
                                    <p class="text-muted">Data daya tampung untuk Sekolah Dasar akan ditampilkan di sini</p>
                                </div>
                            </div>
                        </div>
                        
                        <!-- SMP -->
                        <div class="tab-pane fade" id="jenjang-smp" role="tabpanel" 
                             aria-labelledby="jenjang-smp-tab">
                            <div class="card bg-light border-0">
                                <div class="card-body text-center py-5">
                                    <i class="bi bi-book text-info" style="font-size: 3rem;"></i>
                                    <h5 class="mt-3">Jenjang SMP</h5>
                                    <p class="text-muted">Data daya tampung untuk Sekolah Menengah Pertama akan ditampilkan di sini</p>
                                </div>
                            </div>
                        </div>
                        
                        <!-- SMA/SMK -->
                        <div class="tab-pane fade" id="jenjang-sma" role="tabpanel" 
                             aria-labelledby="jenjang-sma-tab">
                            <div class="card bg-light border-0">
                                <div class="card-body text-center py-5">
                                    <i class="bi bi-mortarboard text-success" style="font-size: 3rem;"></i>
                                    <h5 class="mt-3">Jenjang SMA/SMK</h5>
                                    <p class="text-muted">Data daya tampung untuk Sekolah Menengah Atas/Kejuruan akan ditampilkan di sini</p>
                                </div>
                            </div>
                        </div>
                        
                        <!-- SLB (untuk non-SLB school) -->
                        <div class="tab-pane fade" id="jenjang-slb" role="tabpanel" 
                             aria-labelledby="jenjang-slb-tab">
                            <div class="card bg-light border-0">
                                <div class="card-body text-center py-5">
                                    <i class="bi bi-heart text-danger" style="font-size: 3rem;"></i>
                                    <h5 class="mt-3">Jenjang SLB</h5>
                                    <p class="text-muted">Data daya tampung untuk Sekolah Luar Biasa akan ditampilkan di sini</p>
                                </div>
                            </div>
                        </div>
                    </div>
                    @endif
                </div>
            </div>
        </div>
    </div>
</div>

<!-- Script timestamp: {{ now() }} -->
<script type="text/javascript">
document.addEventListener('DOMContentLoaded', function() {
    'use strict';
    console.log('=== SCRIPT LOADED AT {{ now() }} ===');
    
    // Auto-calculate totals for Tab 1 (Rombel Saat Ini)
    function calculateTotals() {
        let totalRombel = 0;
        let totalSiswa = 0;
        
        document.querySelectorAll('.rombel-count').forEach(function(input) {
            totalRombel += parseInt(input.value) || 0;
        });
        
        document.querySelectorAll('.siswa-count').forEach(function(input) {
            totalSiswa += parseInt(input.value) || 0;
        });
        
        document.getElementById('totalRombel').value = totalRombel;
        document.getElementById('totalSiswa').value = totalSiswa;
    }
    
    // Attach event listeners to all rombel and siswa inputs
    document.querySelectorAll('.rombel-count, .siswa-count').forEach(function(input) {
        input.addEventListener('input', calculateTotals);
    });
    
    // Function to submit form with AJAX
    function submitFormAjax(form, url, successMessage) {
        var formData = new FormData(form);
        var data = {};
        
        formData.forEach(function(value, key) {
            data[key] = value || 0;
        });
        
        data.tahun_ajaran = '{{ $tahunAjaranBaru }}';
        
        console.log('Submitting to:', url);
        console.log('Data:', data);
        
        var csrfToken = document.querySelector('meta[name="csrf-token"]');
        if (!csrfToken) {
            alert('CSRF token tidak ditemukan!');
            return;
        }
        
        fetch(url, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
                'X-CSRF-TOKEN': csrfToken.content,
                'Accept': 'application/json',
                'X-Requested-With': 'XMLHttpRequest'
            },
            body: JSON.stringify(data)
        })
        .then(function(response) {
            console.log('Response status:', response.status);
            
            if (!response.ok) {
                return response.text().then(function(text) {
                    console.error('Error response:', text);
                    throw new Error('HTTP ' + response.status);
                });
            }
            
            return response.json();
        })
        .then(function(result) {
            console.log('Success result:', result);
            if (result.success) {
                alert(successMessage || 'Data berhasil disimpan!');
                location.reload();
            } else {
                alert('Error: ' + (result.message || 'Terjadi kesalahan'));
            }
        })
        .catch(function(error) {
            console.error('Fetch error:', error);
            alert('Terjadi kesalahan saat menyimpan data: ' + error.message);
        });
    }
    
    // Tab 1: Submit Rombel Saat Ini
    window.submitFormTab1 = function(form, event) {
        event.preventDefault();
        event.stopPropagation();
        console.log('submitFormTab1 called');
        
        // Update tahun ajaran for Tab 1 to use active academic year
        var formData = new FormData(form);
        var data = {};
        
        formData.forEach(function(value, key) {
            data[key] = value || 0;
        });
        
        data.tahun_ajaran = '{{ $tahunAjaranAktif }}';
        
        console.log('Submitting Rombel Saat Ini');
        console.log('Data:', data);
        
        var csrfToken = document.querySelector('meta[name="csrf-token"]');
        if (!csrfToken) {
            alert('CSRF token tidak ditemukan!');
            return false;
        }
        
        fetch('/admin-sekolah/murid-baru/daya-tampung/rombel-saat-ini', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
                'X-CSRF-TOKEN': csrfToken.content,
                'Accept': 'application/json',
                'X-Requested-With': 'XMLHttpRequest'
            },
            body: JSON.stringify(data)
        })
        .then(function(response) {
            console.log('Response status:', response.status);
            
            if (!response.ok) {
                return response.text().then(function(text) {
                    console.error('Error response:', text);
                    throw new Error('HTTP ' + response.status);
                });
            }
            
            return response.json();
        })
        .then(function(result) {
            console.log('Success result:', result);
            if (result.success) {
                alert('Data Rombel Saat Ini berhasil disimpan!');
                location.reload();
            } else {
                alert('Error: ' + (result.message || 'Terjadi kesalahan'));
            }
        })
        .catch(function(error) {
            console.error('Fetch error:', error);
            alert('Terjadi kesalahan: ' + error.message);
        });
        
        return false;
    };
    
    // Tab 2: Handle Rencana Rombel Submit
    window.handleRencanaRombelSubmit = function(event) {
        event.preventDefault();
        event.stopPropagation();
        console.log('handleRencanaRombelSubmit called');
        
        var form = document.getElementById('rencanaRombelForm');
        submitFormAjax(form, '/admin-sekolah/murid-baru/daya-tampung/rencana-rombel', 'Rencana Rombel berhasil disimpan!');
        
        return false;
    };
    
    // Tab 3: Handle Daya Tampung SLB Submit
    window.handleDayaTampungSlbSubmit = function(event) {
        event.preventDefault();
        event.stopPropagation();
        console.log('handleDayaTampungSlbSubmit called');
        
        var form = document.getElementById('dayaTampungSlbForm');
        submitFormAjax(form, '/admin-sekolah/murid-baru/daya-tampung/slb', 'Data Daya Tampung berhasil disimpan!');
        
        return false;
    };
    
    console.log('All functions defined successfully');
});
</script>

@endsection
