@extends('admin.layouts.container')

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

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

            @if(!($cobon ?? ''  && $cobon->id))
                @csrf
            @endif
            <div class="form-group col-lg-6">
                <label>{{ meta('code', getCurrentLocale()) ?? __('cobons.code')}}</label>
                <input type="text"  class="form-control  {{ $errors->has('code') ? 'is-invalid' : '' }}" name="code" placeholder="{{ meta('code', getCurrentLocale()) ?? __('cobons.code') }}" value='{{ old("code", $cobon->code ?? "") }}'/>
            </div>
            <div class="form-group col-lg-6">
                <label>{{ meta('count', getCurrentLocale()) ?? __('cobons.count')}}</label>
                <input type="number" min="0" class="form-control  {{ $errors->has('count') ? 'is-invalid' : '' }}" name="count" placeholder="{{ meta('count', getCurrentLocale()) ?? __('cobons.count') }}" value='{{ old("count", $cobon->count ?? "") }}'/>
            </div>

            <div class="form-group col-lg-6">
                <label>{{ meta('cobon_status', getCurrentLocale()) ?? __('cobons.cobon_status')}}</label>
                <select id="cobon_status" onchange="typeSelect()" class="form-control {{ $errors->has('cobon_status') ? 'is-invalid' : '' }}" name="cobon_status">
                    <option @if($cobon ?? ''  && $cobon->id) @if($cobon->cobon_status == 'cobon') selected @endif @endif value="cobon">{{__('cobons.cobon')}}</option>
                    <option @if($cobon ?? ''  && $cobon->id) @if($cobon->cobon_status == 'event') selected @endif @endif value="event">{{__('cobons.event')}}</option>
                </select>
            </div>
            
            <div class="form-group col-lg-6 product_status">
                <label>{{ meta('status', getCurrentLocale()) ?? __('cobons.product_status')}}</label>
                <select id="status" onchange="statusSelect()" class="form-control {{ $errors->has('status') ? 'is-invalid' : '' }}" name="status">
                    <option @if($cobon ?? ''  && $cobon->id) @if($cobon->status == 'all') selected @endif @endif value="all">{{__('cobons.all')}}</option>
                    <option @if($cobon ?? ''  && $cobon->id) @if($cobon->status == 'specific') selected @endif @endif value="specific">{{__('cobons.specific')}}</option>
                </select>
            </div>
            <div class="form-group col-lg-6">
                <label>{{ meta('discount_type', getCurrentLocale()) ?? __('cobons.discount_type')}}</label>
                <select class="form-control {{ $errors->has('discount_type') ? 'is-invalid' : '' }}" name="discount_type">
                    <option @if($cobon ?? ''  && $cobon->id) @if($cobon->discount_type == '1') selected @endif @endif value="1">{{__('cobons.value')}}</option>
                    <option @if($cobon ?? ''  && $cobon->id) @if($cobon->discount_type == '2') selected @endif @endif  value="2">{{__('cobons.percentage')}}</option>
                </select>
            </div>
            <div class="form-group col-lg-6">
                <label>{{ meta('discount_amount', getCurrentLocale()) ?? __('cobons.discount_amount')}}</label>
                <input type="number"  class="form-control  {{ $errors->has('discount_amount') ? 'is-invalid' : '' }}" name="discount_amount" placeholder="{{ meta('discount_amount', getCurrentLocale()) ?? __('cobons.discount_amount') }}" value='{{ old("discount_amount", $cobon->discount_amount ?? "") }}'/>
            </div>

            @foreach($languages as $language)
                <div class="form-group col-lg-6 event" >
                    <label>{{ meta('name', $language->local) ?? __('cobons.name') }} {{ count($languages) > 1 ? "({$language->name})" : '' }} </label>
                    <input type="text" class="form-control {{ $errors->has('name_'.$language->local) ? 'is-invalid' : '' }}" name="name_{{ $language->local }}" placeholder="{{ meta('name', $language->local) ?? __('cobons.name') }}" value='{{ old("name_{$language->local}", $cobon[$language->local]->name ?? "") }}'/>
                </div>
            @endforeach

            <div class="form-group col-lg-6">
                <label>{{ meta('start_date', getCurrentLocale()) ?? __('cobons.start_date')}}</label>
                <input type="date"  class="form-control  {{ $errors->has('start_date') ? 'is-invalid' : '' }}" name="start_date" placeholder="{{ meta('start_date', getCurrentLocale()) ?? __('cobons.start_date') }}" value='{{ old("start_date", $cobon->start_date ?? "") }}'/>
            </div>
            <div class="form-group col-lg-6">
                <label>{{ meta('end_date', getCurrentLocale()) ?? __('cobons.end_date')}}</label>
                <input type="date"  class="form-control  {{ $errors->has('end_date') ? 'is-invalid' : '' }}" name="end_date" placeholder="{{ meta('end_date', getCurrentLocale()) ?? __('cobons.end_date') }}" value='{{ old("end_date", $cobon->end_date ?? "") }}'/>
            </div>
                <div class="col-md-6 mb-4 product_status" id="products">
                    <label for="validationServer">{{__('cobons.product')}}</label>
                    <input type="text" name="search_product" value="" placeholder="{{__('cobons.product')}}" id="input-product" class="form-control usersBox" />
                    <div id="branche-product" class="well well-sm usersBox form-control" style="height: 150px; overflow: auto;">
                        @if ($cobon ?? '' && $cobon->id)
                            @foreach($products as $row)
                                <div id="branche-product{{$row->id}}" class="branches"><i class="fa fa-minus-circle"></i> {{$row->name}}<input type="hidden" name="product_ids[]" value="{{$row->id}}"></div>
                            @endforeach
                        @endif
                    </div>
                </div>
                <div class="col-md-6 mb-4 event" id="users">
                    <label for="validationServer">{{__('cobons.user')}}</label>
                    <input type="text" name="search_user" value="" placeholder="{{__('cobons.user')}}" id="input-user" class="form-control usersBox" />
                    <div id="branche-user" class="well well-sm usersBox form-control" style="height: 150px; overflow: auto;">
                        @if ($cobon ?? '' && $cobon->id)
                            @foreach($users as $row)
                                <div id="branche-user{{$row->user_id}}" class="branches"><i class="fa fa-minus-circle"></i> {{$row->name}}<input type="hidden" name="user_ids[]" value="{{$row->id}}"></div>
                            @endforeach
                        @endif
                    </div>
                </div>
            @if(!($cobon ?? ''  && $cobon->id))
                <div class="text-center mt-5 col-12">
                    <button class="butn"><span>{{ __('dashboard.save') }}</span></button>
                </div>
            @endif
        </form>
    </div>

@endsection

@section('inner_js')
    <!-- Editor JS files -->
    {{--    <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 src="//cdn.tinymce.com/4/tinymce.min.js"></script>

    <script>
        let url_search_user = "{{url('')}}/admin/cobons/user"
        let url_search_product = "{{url('')}}/admin/cobons/product"
    </script>
    <script>
        function statusSelect()
        {
            let data = $('#status').val()
            if(data == 'all')
            {
                $('#products').css('display','none');
            }
            else
            {
                let event = $('#cobon_status').val()
                if(event != 'event')
                {
                    $('#products').css('display','block');
                }
            }
        }
        function typeSelect()
        {
            let data = $('#cobon_status').val()
            if(data == 'event')
            {
                $('.event').css('display','block');
                $('.product_status').css('display','none');
                statusSelect()
            }
            else
            {
                $('.event').css('display','none');
                $('.product_status').css('display','block');
                statusSelect()
            }
        }
        $('#cobon_status').trigger('change');
    </script>
    <script>

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

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

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

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

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

                    success: function (json) {
                        response($.map(json, function (item) {
                            return {
                                label:`${item['name']}` ,
                                value: item['name'],
                                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();
        });
    </script>

    <script>
        let route_prefix = "{{ url(config('lfm.prefix')) }}",
            url = "{{url('/')}}/",
            url_type = {!! json_encode(url(getCurrentLocale().'/admin/type/')) !!},
            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>
    <script src="{{ asset('assets/admin/js/cobons.js') }}"></script>
@endsection



