@extends('dashboard.app')

@section('title', 'Tambah Biaya Kuliah')

@section('content')
@push('style')
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/Dropify/0.2.2/css/dropify.min.css"
        integrity="sha512-EZSUkJWTjzDlspOoPSpUFR0o0Xy7jdzW//6qhUkoZ9c4StFkVsp9fbbd0O06p9ELS3H486m4wmrCELjza4JEog=="
        crossorigin="anonymous" referrerpolicy="no-referrer" />
@endpush

<div class="page-content">
    <nav class="page-breadcrumb">
        <ol class="breadcrumb">
            <li class="breadcrumb-item"><a href="{{ route('dashboard') }}">Dashboard</a></li>
            <li class="breadcrumb-item"><a href="{{ route('biaya.index') }}">Biaya Kuliah</a></li>
            <li class="breadcrumb-item active" aria-current="page">Tambah Biaya</li>
        </ol>
    </nav>

    <div class="row">
        <div class="stretch-card">
            <div class="card">
                <div class="card-body">
                    <h4 class="card-title">Tambah Biaya Kuliah</h4>

                    @if ($errors->any())
                        <div id="error-alert" class="alert alert-danger">
                            <strong>Terjadi Kesalahan!</strong>
                            <ul>
                                @foreach ($errors->all() as $error)
                                    <li>{{ $error }}</li>
                                @endforeach
                            </ul>
                        </div>
                    @endif

                    <form action="{{ route('biaya.store') }}" method="POST" enctype="multipart/form-data">
                        @csrf
                        
                        <!-- Kategori -->
                        <div class="form-group mb-4">
                            <label for="kategori" class="form-label">Kategori</label>
                            <select class="form-control @error('kategori') is-invalid @enderror" id="kategori" name="kategori">
                                <option value="">-- Pilih Kategori --</option>
                                <option value="Sarjana Terapan" {{ old('kategori') == 'Sarjana Terapan' ? 'selected' : '' }}>Sarjana Terapan</option>
                                <option value="Diploma" {{ old('kategori') == 'Diploma' ? 'selected' : '' }}>Diploma</option>
                            </select>
                            @error('kategori')
                                <div class="invalid-feedback">{{ $message }}</div>
                            @enderror
                        </div>

                        <!-- Jurusan -->
                        <div class="form-group mb-4">
                            <label for="jurusan" class="form-label">Jurusan</label>
                            <input type="text" class="form-control @error('jurusan') is-invalid @enderror" id="jurusan" name="jurusan" value="{{ old('jurusan') }}">
                            @error('jurusan')
                                <div class="invalid-feedback">{{ $message }}</div>
                            @enderror
                        </div>

                        <!-- Biaya Awal -->
                        <div class="form-group mb-4">
                            <label for="biaya_awal" class="form-label">Biaya Awal</label>
                            <input type="number" class="form-control @error('biaya_awal') is-invalid @enderror" id="biaya_awal" name="biaya_awal" value="{{ old('biaya_awal') }}">
                            @error('biaya_awal')
                                <div class="invalid-feedback">{{ $message }}</div>
                            @enderror
                        </div>

                        <!-- Biaya SPP -->
                        <div class="form-group mb-4">
                            <label for="biaya_spp" class="form-label">Biaya SPP</label>
                            <input type="number" class="form-control @error('biaya_spp') is-invalid @enderror" id="biaya_spp" name="biaya_spp" value="{{ old('biaya_spp') }}">
                            @error('biaya_spp')
                                <div class="invalid-feedback">{{ $message }}</div>
                            @enderror
                        </div>

                        <!-- Biaya Semester (Otomatis x6 dari SPP tapi bisa diubah) -->
                        <div class="form-group mb-4">
                            <label for="biaya_semester" class="form-label">Biaya Semester (1 Semester)</label>
                            <div class="input-group">
                                <span class="input-group-text">Rp</span>
                                <input type="number" class="form-control @error('biaya_semester') is-invalid @enderror" id="biaya_semester" name="biaya_semester" value="{{ old('biaya_semester') }}">
                            </div>
                            <small class="form-text text-muted">Otomatis terisi SPP × 6, tapi bisa diubah manual jika berbeda.</small>
                            @error('biaya_semester')
                                <div class="invalid-feedback">{{ $message }}</div>
                            @enderror
                        </div>

                        <!-- Deskripsi -->
                        <div class="form-group mb-3">
                            <label class="form-label">Deskripsi</label>
                            <textarea class="form-control" name="deskripsi" rows="20" id="editor" required>{{ old('deskripsi') }}</textarea>
                        </div>

                        <button type="submit" class="btn btn-primary mt-3">Simpan</button>
                        <a href="{{ route('biaya.index') }}" class="btn btn-secondary mt-3">Kembali</a>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection

@push('script')
    <script src="https://cdn.ckeditor.com/4.21.0/standard/ckeditor.js"></script>
    <script>
        CKEDITOR.replace('editor', {
            enterMode: CKEDITOR.ENTER_P,
            shiftEnterMode: CKEDITOR.ENTER_BR
        });
    </script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/Dropify/0.2.2/js/dropify.min.js"
        integrity="sha512-8QFTrG0oeOiyWo/VM9Y8kgxdlCryqhIxVeRpWSezdRRAvarxVtwLnGroJgnVW9/XBRduxO/z1GblzPrMQoeuew=="
        crossorigin="anonymous" referrerpolicy="no-referrer"></script>
    <script>
        $('.dropify').dropify();
    </script>
    <script>
        // Auto-calculate biaya_semester from biaya_spp * 6, but allow manual override
        document.addEventListener("DOMContentLoaded", function() {
            const biayaSpp = document.getElementById('biaya_spp');
            const biayaSemester = document.getElementById('biaya_semester');
            let isManualChange = false;

            // Function to calculate semester cost
            function calculateSemester() {
                if (!isManualChange && biayaSpp.value) {
                    const sppValue = parseFloat(biayaSpp.value);
                    if (!isNaN(sppValue)) {
                        biayaSemester.value = sppValue * 6;
                    }
                }
            }

            // Auto-calculate when SPP changes
            biayaSpp.addEventListener('input', function() {
                // Reset manual flag when SPP changes
                isManualChange = false;
                calculateSemester();
            });

            // Allow manual override
            biayaSemester.addEventListener('focus', function() {
                isManualChange = true;
            });

            biayaSemester.addEventListener('input', function() {
                isManualChange = true;
            });

            // If SPP has value on page load, calculate it
            if (biayaSpp.value) {
                calculateSemester();
            }

            // Hide error alert after 10 seconds
            const errorAlert = document.getElementById("error-alert");
            if (errorAlert) {
                setTimeout(() => {
                    errorAlert.style.display = "none";
                }, 10000);
            }
        });
    </script>
@endpush