@extends('admin.layouts.container')

@section('content')
    <div class="side-title">
        <h3>{{ $head ?? ''}}</h3>
    </div>

    <div class="border-item">
        <form class="form-row" method="post" onsubmit="saveForm(this)" action="{{ $action }}" enctype="multipart/form-data">
            @if($coupon ?? ''  && $coupon->id)
                @method('PUT')
            @endif
            @csrf

            <div class="form-group col-lg-6">
                <label>{{ meta('code', getCurrentLocale()) ?? __('coupons.code')}}</label>
                <input type="text"  class="form-control  {{ $errors->has('code') ? 'is-invalid' : '' }}" name="code" placeholder="{{ meta('code', getCurrentLocale()) ?? __('coupons.code') }}" value='{{ old("code", $coupon->code ?? "") }}'/>
            </div>

            <div class="form-group col-lg-6">
                <label>{{ __('coupons.discount_status') }} </label>
                <select id="discount_status" class="form-control {{ $errors->has('discount_status') ? 'is-invalid' : '' }}" name="discount_status">
                    @foreach($discount_statuses as $row)
                        <option {{isset($coupon) ? ($coupon->discount_status == $row ? 'selected' : '') : ''}} value="{{$row}}">{{__('coupons.discount_statuses.'.$row)}}</option>
                    @endforeach
                </select>
            </div>

            <div class="form-group col-lg-6" >
                <label>{{ __('coupons.discount') }} </label>
                <input type="number" class="form-control {{ $errors->has('discount') ? 'is-invalid' : '' }}" name="discount" placeholder="{{ __('coupons.discount') }}" value='{{ old("discount", $coupon->discount ?? "") }}'/>
            </div>


            <div class="form-group col-lg-6" >
                <label>{{ __('coupons.user_count') }} </label>
                <input type="number" class="form-control {{ $errors->has('user_count') ? 'is-invalid' : '' }}" name="user_count" placeholder="{{ __('coupons.user_count') }}" value='{{ old("user_count", $coupon->user_count ?? "") }}'/>
            </div>

            <div class="form-group col-lg-6" >
                <label>{{ __('coupons.count') }} </label>
                <input type="number" class="form-control {{ $errors->has('count') ? 'is-invalid' : '' }}" name="count" placeholder="{{ __('coupons.count') }}" value='{{ old("count", $coupon->count ?? "") }}'/>
            </div>

            <div class="form-group col-lg-6" >
                <label>{{ __('coupons.start_date') }} </label>
                <input type="date" class="form-control {{ $errors->has('start_date') ? 'is-invalid' : '' }}" name="start_date" placeholder="{{ __('coupons.start_date') }}" value='{{ old("start_date", $coupon->start_date ?? "") }}' />
            </div>

            <div class="form-group col-lg-6" >
                <label>{{ __('coupons.end_date') }} </label>
                <input type="date" class="form-control {{ $errors->has('end_date') ? 'is-invalid' : '' }}" name="end_date" placeholder="{{ __('coupons.end_date') }}" value='{{ old("end_date", $coupon->end_date ?? "") }}' />
            </div>


            <div class="text-center mt-5 col-12">
                <button class="butn"><span>{{ __('dashboard.save') }}</span></button>
            </div>
        </form>
    </div>

@endsection

@section('inner_js')
    <!-- Editor JS orders -->
    <script src="{{ asset('/assets/admin/js/lib/tinymce.min.js') }}"></script>
    <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
    <script>

        $('input[name=\'search_product\']').autocomplete({

            'source': function (request, response) {
                let target = request['term'];
                $.ajax({
                    url: url_search_product,
                    data: 'title='+target,
                    dataType: 'json',
                    type: 'get',

                    success: function (json) {
                        response($.map(json, function (item) {
                            return {
                                label:`${item['title']}` ,
                                value: item['title'],
                                id: item['id']
                            };
                        }));
                    }
                });
            }, 'select': function (e, selected) {
                var item = selected['item'];
                $('#branche-product' + item['id']).remove();
                $('#input-product').removeAttr('value');
                $('#branche-product').append('<div id="branche-product' + item['id'] + '" class="branches"><i class="fa fa-minus-circle"></i> ' + item['label'] + '<input type="hidden" name="product_ids[]" value="' + item['id'] + '" /></div>');
            }
        });
        $('#branche-product').delegate('.fa-minus-circle', 'click', function () {
            $(this).parent().remove();
        });

        function statusSelect()
        {
            let data = $('#status').val()
            if(data == '0')
            {
                $('#product_status').show();
            }
            else
            {
                $('#product_status').hide();
            }
        }
        $('#status').trigger('change');
    </script>
    <script>
        let route_prefix = "{{ url(config('lfm.prefix')) }}",
            url = "{{url('/')}}/",
            url_search_product = "{{route('admin.search_products')}}",
            currentLanguage = "{{getCurrentLocale()}}",
            imagePath = "{{asset('')}}";
    </script>
    <script src="{{ asset('assets/admin/js/editor.js') }}"></script>
    <script src="{{ asset('assets/admin/js/lfm.js') }}"></script>
    <script src="{{ asset('assets/admin/js/articles.js') }}"></script>
@endsection



