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

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

        <h5 class="card-header">

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

        <div class="card-body">

            <form action="{{ route('banners.store') }}" method="POST" enctype="multipart/form-data">
                @csrf
                <div class="row">

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


                    <div class="col-md-6 mb-3">
                        <label class="form-label">{{ __('trans.banner.type') }} <b class="text-danger">*</b></label>
                        <select name="type" class="form-select" required>
                            <option value="empty" @selected(old('type') == 'empty')>Empty</option>
                            <option value="course" @selected(old('type') == 'course')>Course</option>
                            <option value="lecture" @selected(old('type') == 'lecture')>Lecture</option>
                        </select>
                    </div>


                    <div class="col-md-6 mb-3">
                        <label class="form-label">{{ __('trans.banner.course') }}</label>
                        <select name="course_id" class="select2 form-select" id="course">
                            @foreach ($courses as $course)
                                <option value="{{ $course->id }}">{{ $course->title }}
                                </option>
                            @endforeach
                        </select>
                    </div>


                    <div class="col-md-6 mb-3">
                        <label class="form-label">{{ __('trans.banner.lecture') }}</label>
                        <select name="lecture_id" class="select2 form-select" id="lecture"> </select>
                    </div>


                    <div class="col-md-6 mb-3">
                        <label class="form-label">{{ __('trans.banner.order') }} <b class="text-danger">*</b></label>
                        <input type="text" class="form-control" required name="order"
                            value="{{ old('order', $lastBannerIndex) }}" placeholder="{{ __('trans.banner.order') }}"
                            oninput="this.value = this.value.replace(/[^0-9]/g, '')" />
                    </div>

                    <div class="col-md-12 mb-3">
                        <div class="input-group">
                            <label>{{ __('trans.banner.img') }} <b class="text-danger">*</b></label>

                            <div class="input-group">
                                <span class="input-group-btn">
                                    <a id="lfm" data-input="thumbnail" data-preview="holder"
                                        class="UploadFile btn btn-primary">
                                        <i class="fa fa-picture-o"></i> {{ __('trans.choose') }}
                                    </a>
                                </span>
                                <input id="thumbnail" class="form-control" type="text" name="img"
                                    value="{{ old('img', '/admin/img/doctorito-logo.png') }}" readonly />
                            </div>
                        </div>

                        <div id="holder" style="margin-top:15px;max-height:100px;">
                            @if (!empty(old('img', '/admin/img/doctorito-logo.png')))
                                <img src="{{ old('img', '/admin/img/doctorito-logo.png') }}" style="height: 5rem;">
                            @endif
                        </div>
                    </div>


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


@section('js')
    <script>
        $(window).on('load', function() {
            $('#course').trigger('input');
        })

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

@endsection
