@extends('layouts.admin.app')

@section('content')
    <div class="card">

        <h5 class="card-header">
            <a href="@if (request('chapter_id')) {{ url('admin/units?chapter_id=') }}{{ request('chapter_id') }}
                    @else{{ url('admin/units') }} @endif"
                class="btn rounded-pill btn-label-primary">
                <i class="ti me-2 ti-arrow-forward-up"></i> {{ __('trans.unit.title') }}
            </a>
            @yield('title')
        </h5>

        <div class="card-body">

            <form action="{{ route('units.store') }}" method="POST">
                @csrf
                <div class="row">


                    <div class="col-md-12 mb-3">
                        <label class="form-label">{{ __('trans.unit.name') }} <b class="text-danger">*</b></label>
                        <input type="text" class="form-control" required name="title" value="{{ old('title') }}"
                            placeholder="{{ __('trans.unit.name') }}" />
                    </div>


                    @if (!empty($existedChapter))
                        <div class="col-md-12 mb-3">
                            <label class="form-label">{{ __('trans.unit.category_id') }} <b
                                    class="text-danger">*</b></label>
                            <input type="hidden" name="category_id" value="{{ $existedChapter->category->id }}">
                            <input type="text" class="form-control" required
                                value="{{ $existedChapter->category->name }}"
                                placeholder="{{ __('trans.unit.category_id') }}" readonly />
                        </div>


                        <div class="col-md-12 mb-3">
                            <label class="form-label">{{ __('trans.unit.course_id') }} <b class="text-danger">*</b></label>
                            <input type="hidden" name="course_id" value="{{ $existedChapter->course->id }}">
                            <input type="text" class="form-control" required
                                value="{{ $existedChapter->course->title }}"
                                placeholder="{{ __('trans.unit.course_id') }}" readonly />
                        </div>


                        <div class="col-md-12 mb-3">
                            <label class="form-label">{{ __('trans.unit.chapter_id') }} <b
                                    class="text-danger">*</b></label>
                            <input type="hidden" name="chapter_id" value="{{ $existedChapter->id }}">
                            <input type="text" class="form-control" required value="{{ $existedChapter->title }}"
                                placeholder="{{ __('trans.unit.chapter_id') }}" readonly />
                        </div>
                    @elseif (!empty($categories))
                        <div class="col-md-12 mb-3">
                            <label class="form-label">{{ __('trans.unit.category_id') }} <b
                                    class="text-danger">*</b></label>
                            <select name="category_id" class="select2 form-select" id="category" required>
                                @foreach ($categories as $category)
                                    <option value="{{ $category->id }}" @selected(old('category_id') == $category->id)>
                                        {{ $category->name }}
                                    </option>
                                @endforeach
                            </select>
                        </div>


                        <div class="col-md-12 mb-3">
                            <label class="form-label">{{ __('trans.unit.course_id') }} <b class="text-danger">*</b></label>
                            <select class="select2 form-select" name="course_id" id="course" required></select>
                        </div>


                        <div class="col-md-12 mb-3">
                            <label class="form-label">{{ __('trans.unit.chapter_id') }} <b
                                    class="text-danger">*</b></label>
                            <select class="select2 form-select" name="chapter_id" id="chapter" required></select>
                        </div>
                    @endif


                    <div class="col-md-12 mt-3">
                        <button class="btn btn-primary btn-create w-100">{{ __('trans.save') }}</button>
                    </div>
                </div>
            </form>
        </div>
    </div>
@endsection

@section('css')
    <link rel="stylesheet" href="{{ asset('admin/vendor/libs/select2/select2.css') }}" />
@endsection

@section('js')
    <script src="{{ asset('admin/vendor/libs/select2/select2.js') }}"></script>
    <script>
        $('.select2').select2();

        $(window).on('load', function() {
            $('#category').trigger('input');
        });

        $('#category').on('input', function() {
            var MyVal = $(this).val();
            $.ajax({
                type: 'get',
                url: "{{ url('admin/ajax/courses') }}",
                data: {
                    category_id: MyVal,
                },
                beforeSend: function() {
                    $("#course").html('');
                },
            }).done(function(res) {
                $.each(res.data, function(key, val) {
                    $("#course").append(
                        "<option value='" + val.id + "'>" +
                        val.title + "</option>");
                });
                $('#course').trigger('input');
            });
        });

        $('#course').on('input', function() {
            var MyVal = $(this).val();
            $.ajax({
                type: 'get',
                url: "{{ url('admin/ajax/chapters') }}",
                data: {
                    course_id: MyVal,
                },
                beforeSend: function() {
                    $("#chapter").html('');
                },
            }).done(function(res) {
                $.each(res.data, function(key, val) {
                    $("#chapter").append(
                        "<option value='" + val.id + "'>" +
                        val.title + "</option>");
                });
            });
        });
    </script>
@endsection
