@extends('layouts.app')

@section('title','Tambah Jadwal')

@section('content')

<div class="container-fluid">

    <div class="d-flex justify-content-between align-items-center mb-4">

        <div>

            <h3 class="font-weight-bold mb-1">
                Tambah Jadwal Mengajar
            </h3>

            <p class="text-muted mb-0">
                Tambahkan jadwal mengajar guru.
            </p>

        </div>

        <a href="{{ route('jadwal.index') }}"
           class="btn btn-secondary btn-modern">

            <i class="fas fa-arrow-left mr-1"></i>

            Kembali

        </a>

    </div>

    <form action="{{ route('jadwal.store') }}" method="POST">

        @csrf

        <div class="card card-modern">

            <div class="card-header bg-white border-0">

                <h5 class="mb-0">

                    <i class="fas fa-calendar-plus text-primary mr-2"></i>

                    Form Jadwal Mengajar

                </h5>

            </div>

            <div class="card-body">

                <div class="row">

                    {{-- Guru --}}
                    <div class="col-md-6 mb-3">

                        <label>Guru</label>

                        <select
                            name="guru_id"
                            class="form-control"
                            required>

                            <option value="">
                                Pilih Guru
                            </option>

                            @foreach($guru as $g)

                            <option value="{{ $g->id }}">

                                {{ $g->nama }}

                            </option>

                            @endforeach

                        </select>

                    </div>

                    {{-- Mata Pelajaran --}}
                    <div class="col-md-6 mb-3">

                        <label>Mata Pelajaran</label>

                        <select
                            name="mata_pelajaran_id"
                            class="form-control"
                            required>

                            <option value="">
                                Pilih Mata Pelajaran
                            </option>

                            @foreach($mapel as $m)

                            <option value="{{ $m->id }}">

                                {{ $m->nama_mapel }}

                            </option>

                            @endforeach

                        </select>

                    </div>

                    {{-- Kelas --}}
                    <div class="col-md-6 mb-3">

                        <label>Kelas</label>

                        <select
                            name="kelas_id"
                            class="form-control"
                            required>

                            <option value="">
                                Pilih Kelas
                            </option>

                            @foreach($kelas as $k)

                            <option value="{{ $k->id }}">

                                {{ $k->nama_kelas }}

                            </option>

                            @endforeach

                        </select>

                    </div>

                    {{-- Hari --}}
                    <div class="col-md-6 mb-3">

                        <label>Hari</label>

                        <select
                            name="hari"
                            class="form-control"
                            required>

                            <option value="">Pilih Hari</option>
                            <option>Senin</option>
                            <option>Selasa</option>
                            <option>Rabu</option>
                            <option>Kamis</option>
                            <option>Jumat</option>
                            <option>Sabtu</option>

                        </select>

                    </div>

                    {{-- Jam --}}
                    <div class="col-md-4 mb-3">

                        <label>Jam Mulai</label>

                        <input
                            type="time"
                            id="jam_mulai"
                            name="jam_mulai"
                            class="form-control"
                            required>

                    </div>

                    <div class="col-md-4 mb-3">

                        <label>Jam Selesai</label>

                        <input
                            type="time"
                            id="jam_selesai"
                            name="jam_selesai"
                            class="form-control"
                            required>

                    </div>

                    <div class="col-md-4 mb-3">

                        <label>Total Jam</label>

                        <div class="input-group">

                            <input
                                type="number"
                                step="0.1"
                                id="jam_mengajar"
                                name="jam_mengajar"
                                class="form-control"
                                readonly>

                            <div class="input-group-append">

                                <span class="input-group-text">

                                    Jam

                                </span>

                            </div>

                        </div>

                    </div>

                </div>

            </div>

            <div class="card-footer bg-white">

                <button class="btn btn-primary btn-modern">

                    <i class="fas fa-save mr-1"></i>

                    Simpan Jadwal

                </button>

                <a href="{{ route('jadwal.index') }}"
                   class="btn btn-light btn-modern">

                    Batal

                </a>

            </div>

        </div>

    </form>

</div>

<script>

const mulai=document.getElementById('jam_mulai');
const selesai=document.getElementById('jam_selesai');

mulai.addEventListener('change',hitungJam);
selesai.addEventListener('change',hitungJam);

function hitungJam(){

    if(!mulai.value || !selesai.value){

        document.getElementById('jam_mengajar').value='';

        return;

    }

    let start=new Date('2000-01-01 '+mulai.value);
    let end=new Date('2000-01-01 '+selesai.value);

    let total=(end-start)/(1000*60*60);

    if(total<0){

        total=0;

    }

    document.getElementById('jam_mengajar').value=total.toFixed(1);

}

</script>

@endsection