@extends('layouts.admin.app')
@section('title', __('trans.lecture.title'))

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

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

        <div class="card-body">

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


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


                    <div class="col-md-12 mb-3">
                        <label class="form-label">{{ __('trans.lecture.description') }}</label>
                        <textarea name="description" class="form-control" rows="5" placeholder="{{ __('trans.lecture.description') }}">{{ old('description') }}</textarea>
                    </div>


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

                    <div>
                        <iframe width="560" height="315" id="video-player" src="" title="YouTube video player"
                            frameborder="0"
                            allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share"
                            referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>

                    </div>

                    <div class="col-md-12 mb-3">
                        <label class="form-label">{{ __('trans.lecture.file') }}</label>
                        <div class="input-group">

                            <a id="lfm" data-input="thumbnail" data-preview="holder"
                                class="UploadFile btn btn-primary">
                                <i class="fa fa-paperclip me-1"></i> {{ __('trans.choose') }}
                            </a>
                            <input id="thumbnail" class="form-control" type="text" name="file"
                                value="{{ old('file') }}" placeholder="{{ __('trans.no_file_chosen') }}" readonly />
                            <button type="button" class="btn btn-danger" id="clear-file">
                                <i class="fa fa-times"></i>
                            </button>
                        </div>
                    </div>

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


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


                        <div class="col-md-6 mb-3">
                            <label class="form-label">{{ __('trans.lecture.chapter_id') }} <b
                                    class="text-danger">*</b></label>
                            <input type="hidden" name="chapter_id" value="{{ $existedUnit->chapter->id }}">
                            <input type="text" class="form-control" required value="{{ $existedUnit->chapter->title }}"
                                placeholder="{{ __('trans.lecture.chapter_id') }}" readonly />
                        </div>


                        <div class="col-md-6 mb-3">
                            <label class="form-label">{{ __('trans.lecture.unit_id') }} <b
                                    class="text-danger">*</b></label>
                            <input type="hidden" name="unit_id" value="{{ $existedUnit->id }}">
                            <input type="text" class="form-control" required value="{{ $existedUnit->title }}"
                                placeholder="{{ __('trans.lecture.unit_id') }}" readonly />
                        </div>
                    @elseif (!empty($categories))
                        <div class="col-md-6 mb-3">
                            <label class="form-label">{{ __('trans.lecture.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-6 mb-3">
                            <label class="form-label">{{ __('trans.lecture.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-6 mb-3">
                            <label class="form-label">{{ __('trans.lecture.chapter_id') }} <b
                                    class="text-danger">*</b></label>
                            <select class="select2 form-select" name="chapter_id" id="chapter" required></select>
                        </div>

                        <div class="col-md-6 mb-3">
                            <label class="form-label">{{ __('trans.lecture.unit_id') }} <b
                                    class="text-danger">*</b></label>
                            <select class="select2 form-select" name="unit_id" id="unit" 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();

        function displayVideo() {
            let newVideoId = $('#video-id').val();

            let newSrc = "https://www.youtube.com/embed/" + newVideoId;

            $('#video-player').attr('src', newSrc);
        }

        $('#clear-file').on('click', function() {
            $('#thumbnail').val('');
        });

        $(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>");
                });
                $('#chapter').trigger('input');
            });
        });

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