@extends('admin.index')
@section('content')

<form action="{{ route('admin.exams.create') }}" method="post" enctype="multipart/form-data">
    @csrf
    <div class="card">
        <div class="border-bottom card-header">
            <h4 class="card-title">{{ trans('general.create_exam') }}</h4>
        </div>
        <div class="card-body">
            <div class="row pt-2">
                <div class=" col-md-6 col-12">
                    <div class="row">
                        <div class="col-12">
                            <div class="mb-1">
                                <label class="form-label" for="title">{{ trans('general.title') }}</label>
                                <input required type="text" name="title" class="form-control" id="title" value="{{old('title')? old('title') : ''}}">
                            </div>
                        </div>
                          <div class="col-12">
                            <div class="mb-1">
                                <label class="form-label" for="quiz_mark">{{ trans('general.quiz_mark') }}</label>
                                <input required type="int" name="quiz_mark" class="form-control" id="quiz_mark" value="{{old('quiz_mark')? old('quiz_mark') : ''}}">
                            </div>
                        </div>
                        <div class=" col-12">
                            <div class="mb-1">
                                <label class="form-label" for="divisionSelect">{{ trans('general.division') }}</label>
                                <select required name="division_id" onchange="getDivisionLevelMaterials(this.value)" class="form-select" id="divisionSelect">
                                    <option value="">--</option>
                                    @foreach($divisions as $division)
                                    <option value="{{ $division->id }}" {{ old('division_id')? ($division->id == old('division_id')? 'selected':'') : ''}}>

                                        {{ $division->stage }} |
                                        {{ $division->level }} |
                                        {{ $division->title }} |

                                    </option>
                                    @endforeach
                                </select>
                            </div>
                        </div>



                    </div>
                </div>
                <div class=" col-md-6 col-12">
                    <div class="mb-1">
                        <label class="form-label" for="description">{{ trans('general.description') }}</label>
                        <textarea  name="description" class="form-control" id="description" rows="8">{{old('description')? old('description') : ''}}</textarea>
                    </div>
                </div>
                
                <div class=" col-md-6 col-12">
                    <div class="mb-1">
                        <label class="form-label" for="description">{{trans('general.quiz_type')}}</label>
                        <select required name="quiz_type" class="form-select" id="quiz_type">
                            @for($i = 0; $i < 5; $i++)
                                <option value="{{trans('general.quiz_type'.$i)}}">{{ trans('general.quiz_type'.$i) }}</option>
                            @endfor
                         </select>
                    </div>
                </div>

                <div class=" col-md-6 col-12">
                    <div class="mb-1">
                        <label class="form-label" for="studyMaterialSelect">{{ trans('general.studyMaterial') }}</label>
                        <select required name="study_material_id" class="form-select" id="studyMaterialSelect">
                            <option value="">--</option>
                            
                        </select>
                    </div>
                </div>
                <div class=" col-md-6  col-12">
                    <div class="mb-1">
                        <label class="form-label" for="date">{{ trans('general.date') }}</label>
                        <input type="text" name="date" id="date" class="form-control flatpickr-basic" value="{{old('date')? old('date') : ''}}" placeholder="YYYY-MM-DD" />
                    </div>
                </div>
            </div>
        </div>

        <div class="card-footer">
            <button type="submit" class="btn btn-primary waves-effect waves-float waves-light">{{ trans('general.save') }}</button>
            <button type="reset" class="btn btn-outline-secondary waves-effect">{{ trans('general.reset') }}</button>
        </div>
    </div>

</form>

@push('scripts')

<script src="{{ url('design') }}/app-assets/vendors/js/pickers/flatpickr/flatpickr.min.js"></script>
<script>
    (function(window, document, $) {
        'use strict';
        $('.flatpickr-basic').flatpickr();
    })(window, document, jQuery);



    function getDivisionLevelMaterials(division_id) {
        var jqxhr = $.ajax({
                url: "{{route('admin.division.level.materials')}}",
                method: "GET",
                timeout: 0,
                data: {
                    division_id: division_id
                }
            })
            .done(function(response) {
                setMaterialsOptions(response.data);
                console.log(response);
            })
            .fail(function(response) {
                console.log(response);
                setMaterialsOptions([]);
                toastr.error(response.responseJSON.msg, '{{ trans("general.error") }}');
            });
    }

    function setMaterialsOptions(data) {
        var studyMaterialSelect = $('#studyMaterialSelect');
        studyMaterialSelect.html('');
        studyMaterialSelect.append($('<option>', {
            value: '',
            text: '--'
        }));
        $.each(data, function(index, val) {
            studyMaterialSelect.append($('<option>', {
                value: val.id,
                text: val.title + ' | ' + val.level
            }));
        });
    }
</script>

@endpush

@push('header')
<link rel="stylesheet" type="text/css" href="{{ url('design') }}/app-assets/vendors/css/pickers/flatpickr/flatpickr.min.css">


@endpush
@endsection