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

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

        <h5 class="card-header">
            <a href="{{ url('admin/subscriptions') }}" class="btn rounded-pill btn-label-primary">
                <i class="ti me-2 ti-arrow-forward-up"></i> {{ __('trans.subscription.title') }}
            </a>
            @yield('title')
        </h5>

        <div class="card-body">

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

                <div class="row">

                    <div class="col-md-6 mb-3">
                        <label class="form-label">{{ __('trans.subscription.owner_user') }} <b
                                class="text-danger">*</b></label>
                        <select name="user_id[]" class="select2 form-select" required multiple>
                            @foreach ($users as $user)
                                <option value="{{ $user->id }}">
                                    {{ $user->name }}
                                </option>
                            @endforeach
                        </select>
                    </div>

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

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

                    <div class="col-md-6 mb-3">
                        <label class="form-label">{{ __('trans.subscription.expires_at') }} <b
                                class="text-danger">*</b></label>
                        <input type="text" name="expires_at" class="form-control" placeholder="YYYY-MM-DD"
                            value="{{ old('expires_at') }}" id="flatpickr-date" />
                    </div>

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

                    <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/flatpickr/flatpickr.css') }}" />
@endsection

@section('js')

    <script src="{{ asset('admin/vendor/libs/flatpickr/flatpickr.js') }}"></script>
    <script src="{{ asset('admin/vendor/js/forms-pickers.js') }}"></script>

    <script>
        $(window).on('load', function() {
            $('#course').trigger('input');
        });
        $('#course').on('input', function() {
            var MyVal = $(this).val();
            $.ajax({
                type: 'get',
                url: "{{ url('admin/ajax/single-course') }}",
                data: {
                    course_id: MyVal,
                },
            }).done(function(res) {
                $('#price').val(res.data.price);
            });
        });
    </script>
@endsection
