@extends('layouts.app')

@section('content')
    <section class="page-header row">
        <h2> {{ $pageTitle }} </h2>
        <ol class="breadcrumb">
            <li><a href="{{ url('') }}"> Dashboard </a></li>
            <li><a href="{{ url('/manage/'.$pageModule) }}"> {{ $pageTitle }} </a></li>
            <li class="active"> Form</li>
        </ol>
    </section>
    <div class="page-content row">
        <div class="page-content-wrapper no-margin">

            {!! Form::open(array('url'=>'/manage/featuredstores?return='.$return, 'class'=>'form-horizontal validated','files' => true )) !!}
            <input type="hidden" name="status_type" value="{{ $pageTitle }}"/>

            <div class="sbox-title clearfix">
                <div class="sbox-tools pull-right">
                    <a href="{{ url('/manage/'.$pageModule.'?return='.$return) }}" class="tips btn btn-sm "
                       title="{{ __('core.btn_back') }}"><i class="fa  fa-times"></i></a>
                </div>
                <div class="sbox-tools1 pull-left1">
                    <button name="apply" class="btn btn-sm btn-primary  " title="{{ __('core.btn_back') }}"><i
                                class="fa  fa-check"></i> {{ __('core.sb_apply') }} </button>
                    <button name="save" class="btn btn-sm btn-success" title="{{ __('core.btn_back') }}"><i
                                class="fa  fa-paste"></i> {{ __('core.sb_save') }} </button>

                </div>
            </div>
            <div class="sbox1">
                <div class="sbox-content clearfix row">
                    <div class="col-xs-12">
                        <ul class="parsley-error-list">
                            @foreach($errors->all() as $error)
                                <li>{{ $error }}</li>
                            @endforeach
                        </ul>

                        <?php $curTime = time();?>

                        <div class="sbox1">
                            <div class="col-md-6">
                                <fieldset class="sbox" style="border-top:0; border-radius: 5px;">
                                    <legend>  Featured Store of the week</legend>
                                    <?php if (!isset($row['id'])) $row['id'] = null;   ?>
                                    <input type='hidden' name='id' id='id' value='{{$row['id']}}'/>
                                    <div class="form-group  depen-  wrap-region_code  ">
                                        <label for="ipt" class=" control-label col-md-3 text-left "> Region *</label>

                                        <div class="col-md-9">
                                            <?php if (!isset($row['region_code'])) $row['region_code'] = null;   ?>

                                            <?php $row['region_code'] = trim($row['region_code'], '[]'); ?>

                                            <select name='region_code[]' id='region_code' class='select2' multiple
                                                    data-placeholder='Select Region' placeholder='Select Region'
                                                    required></select>
                                        </div>
                                    </div>
                                    <div class="form-group depen- wrap-store_id  ">
                                        <label for="Store" class=" control-label col-md-3 text-left"> Store <span
                                                    class="asterix"> * </span></label>


                                        <div class="col-md-9">
                                            <?php if (!isset($row['store_id'])) $row['store_id'] = null;   ?>

                                            <?php $row['store_id'] = trim($row['store_id'], '[]'); ?>

                                            <select name='store_id' rows='5' id='store_id' class='select2 '
                                                    data-placeholder='Select Store' placeholder='Select Store'
                                                    required></select>
                                        </div>
                                        <div class="col-md-2">

                                        </div>
                                    </div>

                                    <div class="form-group depen- wrap-description  " >
                                        <label for="Description" class=" control-label col-md-3 text-left"> Description <span class="asterix"> * </span></label>


                                        <div class="col-md-9">
                                            <?php if( !isset($row['description']))  $row['description'] = null;   ?>
                                            <?php $val = json_decode($row['description']); $is_first = true; ?>
                                            <ul class='editor-nav nav nav-tabs'>

                                                @foreach(explode(',', config('website.cnf_langs')) as $ilang )<li class='{{$is_first ? "active" : null}} '><a  href='#description_{{$ilang}}_{{$curTime}}' data-toggle='tab'>{{$ilang}}</a></li>
                                                <?php  $is_first = false;?>
                                                @endforeach </ul>

                                            <div class="tab-content ">
                                                <?php $is_first = true; ?>
                                                @foreach(explode(',', config('website.cnf_langs')) as $ilang )
                                                    <div class='tab-pane tab_{{$ilang}}  {{$is_first ? "active" : null}}' id='description_{{$ilang}}_{{$curTime}}'>
                                                        <input  type='text' name="description[{{$ilang}}]" id='description' value='{{   isset($val->{$ilang}) ? $val->{$ilang} : ''   }}'
                                                                required  placeholder='Description - {{$ilang}}'     class='form-control input-sm ' />
                                                    </div>
                                                    <?php  $is_first = false;?>
                                                @endforeach
                                            </div>

                                        </div>
                                        <div class="col-md-2">

                                        </div>
                                    </div>
                                    <div class="form-group depen- wrap-status  ">
                                        <label for="Status" class=" control-label col-md-3 text-left"> Status <span
                                                    class="asterix"> * </span></label>


                                        <div class="col-md-9">
                                            <?php if (!isset($row['status'])) $row['status'] = null;   ?>
                                            <div class='switch-wrap'><label class='switch'>
                                                    <input type='hidden' name='status' value='0'/>
                                                    <input type='checkbox' name='status' value='1'
                                                           @if($row['status']) checked @endif
                                                    /> <span class="slider round"></span></label></div>
                                        </div>
                                        <div class="col-md-2">

                                        </div>
                                    </div>
                                </fieldset>
                            </div>
                            <div class="col-md-6">
                                <fieldset class="sbox" style="border-top:0; border-radius: 5px;">
                                    <legend> Photos </legend>
                                    <?php if (!isset($row['id'])) $row['id'] = null;   ?>
                                    <input type='hidden' name='id' id='id' value='{{$row['id']}}'/>


                                    <div class="form-group depen- wrap-first_image_en  ">
                                        <label for="First Image En" class=" control-label col-md-3 text-left"> First Image
                                            En </label>
                                        <div class="col-md-9">
                                            <input type='hidden' name='first_image_en' id='first_image_en'
                                                   value='{{(isset($row['first_image_en']))?$row['first_image_en']:null }}'
                                                   class='first_image_en form-control input-sm ' required/>


                                            <button class='btn btn-primary mr-5' data-type='mediaUpload'
                                                    data-target='.first_image_en' type='button'>Choose file
                                            </button>
                                            <button class='btn btn-danger unUploadImage' data-type='mediaUnUpload'
                                                    data-target='first_image_en' type='button'>Remove File
                                            </button>

                                            <div class='first_image_en_preview'></div>
                                            <div class='first_image_enPreview'>
                                                <div class='img-preview'>{!! SiteHelpers::showUploadedFile( (isset($row['first_image_en']))?$row['first_image_en']:null,'/uploads/images/featured_stores',80,'img-thumbnail') !!}</div>
                                            </div>

                                        </div>
                                        <div class="col-md-2">

                                        </div>
                                    </div>
                                    <div class="form-group depen- wrap-second_image_en  ">
                                        <label for="Second Image En" class=" control-label col-md-3 text-left"> Second Image En </label>


                                        <div class="col-md-9">
                                            <input type='hidden' name='second_image_en' id='second_image_en'
                                                   value='{{ (isset($row['second_image_en']))?$row['second_image_en']:null }}'
                                                   class='second_image_en form-control input-sm ' />


                                            <button class='btn btn-primary mr-5' data-type='mediaUpload'
                                                    data-target='.second_image_en' type='button'>Choose file
                                            </button>
                                            <button class='btn btn-danger unUploadImage' data-type='mediaUnUpload'
                                                    data-target='second_image_en' type='button'>Remove File
                                            </button>

                                            <div class='second_image_en_preview'></div>
                                            <div class='second_image_enPreview'>
                                                <div class='img-preview'>{!! SiteHelpers::showUploadedFile((isset($row['second_image_en']))?$row['second_image_en']:null ,'/uploads/images/featured_stores',80,'img-thumbnail') !!}</div>
                                            </div>

                                        </div>
                                        <div class="col-md-2">

                                        </div>
                                    </div>
                                    <div class="form-group depen- wrap-third_image_en  ">
                                        <label for="Third Image En" class=" control-label col-md-3 text-left"> Third Image En </label>


                                        <div class="col-md-9">
                                            <input type='hidden' name='third_image_en' id='third_image_en'
                                                   value='{{ (isset($row['third_image_en']))?$row['third_image_en']:null }}'
                                                   class='third_image_en    form-control input-sm ' required/>


                                            <button class='btn btn-primary mr-5' data-type='mediaUpload'
                                                    data-target='.third_image_en' type='button'>Choose file
                                            </button>
                                            <button class='btn btn-danger unUploadImage' data-type='mediaUnUpload'
                                                    data-target='third_image_en' type='button'>Remove File
                                            </button>

                                            <div class='third_image_en_preview'></div>
                                            <div class='third_image_enPreview'>
                                                <div class='img-preview'>{!! SiteHelpers::showUploadedFile((isset($row['third_image_en']))?$row['third_image_en']:null ,'/uploads/images/featured_stores',80,'img-thumbnail') !!}</div>
                                            </div>

                                        </div>
                                        <div class="col-md-2">

                                        </div>
                                    </div>
                                    <div class="form-group depen- wrap-forth_image_en  ">
                                        <label for="Forth Image En" class=" control-label col-md-3 text-left"> Forth Image
                                            En </label>


                                        <div class="col-md-9">
                                            <input type='hidden' name='forth_image_en' id='forth_image_en'
                                                   value='{{ (isset($row['forth_image_en']))?$row['forth_image_en']:null }}'
                                                   class='forth_image_en    form-control input-sm ' required/>


                                            <button class='btn btn-primary mr-5' data-type='mediaUpload'
                                                    data-target='.forth_image_en' type='button'>Choose file
                                            </button>
                                            <button class='btn btn-danger unUploadImage' data-type='mediaUnUpload'
                                                    data-target='forth_image_en' type='button'>Remove File
                                            </button>

                                            <div class='forth_image_en_preview'></div>
                                            <div class='forth_image_enPreview'>
                                                <div class='img-preview'>{!! SiteHelpers::showUploadedFile((isset($row['forth_image_en']))?$row['forth_image_en']:null ,'/uploads/images/featured_stores',80,'img-thumbnail') !!}</div>
                                            </div>

                                        </div>
                                        <div class="col-md-2">

                                        </div>
                                    </div>

                                    <div class="form-group depen- wrap-first_image_ar  ">
                                        <label for="First Image Ar" class=" control-label col-md-3 text-left"> First Image
                                            Ar </label>


                                        <div class="col-md-9">
                                            <input type='hidden' name='first_image_ar' id='first_image_ar'
                                                   value='{{ (isset($row['first_image_ar']))?$row['first_image_ar']:null }}'
                                                   class='first_image_ar form-control input-sm ' required/>


                                            <button class='btn btn-primary mr-5' data-type='mediaUpload'
                                                    data-target='.first_image_ar' type='button'>Choose file
                                            </button>
                                            <button class='btn btn-danger unUploadImage' data-type='mediaUnUpload'
                                                    data-target='first_image_ar' type='button'>Remove File
                                            </button>


                                            <div class='first_image_ar_preview'></div>
                                            <div class='first_image_arPreview'>
                                                <div class='img-preview'>{!! SiteHelpers::showUploadedFile((isset($row['first_image_ar']))?$row['first_image_ar']:null ,'/uploads/images/featured_stores',80,'img-thumbnail') !!}</div>
                                            </div>

                                        </div>
                                        <div class="col-md-2">

                                        </div>
                                    </div>
                                    <div class="form-group depen- wrap-second_image_ar  ">
                                        <label for="second Image Ar" class=" control-label col-md-3 text-left"> second Image
                                            Ar </label>


                                        <div class="col-md-9">
                                            <input type='hidden' name='second_image_ar' id='second_image_ar'
                                                   value='{{ (isset($row['second_image_ar']))?$row['second_image_ar']:null }}'
                                                   class='second_image_ar    form-control input-sm ' required/>


                                            <button class='btn btn-primary mr-5' data-type='mediaUpload'
                                                    data-target='.second_image_ar' type='button'>Choose file
                                            </button>
                                            <button class='btn btn-danger unUploadImage' data-type='mediaUnUpload'
                                                    data-target='second_image_ar' type='button'>Remove File
                                            </button>

                                            <div class='second_image_ar_preview'></div>
                                            <div class='second_image_arPreview'>
                                                <div class='img-preview'>{!! SiteHelpers::showUploadedFile( (isset($row['second_image_ar']))?$row['second_image_ar']:null,'/uploads/images/featured_stores',80,'img-thumbnail') !!}</div>
                                            </div>
                                        </div>

                                    </div>
                                    <div class="form-group depen- wrap-third_image_ar  ">
                                        <label for="Third Image Ar" class=" control-label col-md-3 text-left">Third_ Image
                                            Ar </label>


                                        <div class="col-md-9">
                                            <input type='hidden' name='third_image_ar' id='third_image_ar'
                                                   value='{{ (isset($row['third_image_ar']))?$row['third_image_ar']:null }}'
                                                   class='third_image_ar    form-control input-sm ' required/>


                                            <button class='btn btn-primary mr-5' data-type='mediaUpload'
                                                    data-target='.third_image_ar' type='button'>Choose file
                                            </button>
                                            <button class='btn btn-danger unUploadImage' data-type='mediaUnUpload'
                                                    data-target='third_image_ar' type='button'>Remove File
                                            </button>

                                            <div class='third_image_ar_preview'></div>
                                            <div class='third_image_arPreview'>
                                                <div class='img-preview'>{!! SiteHelpers::showUploadedFile( (isset($row['third_image_ar']))?$row['third_image_ar']:null,'/uploads/images/featured_stores',80,'img-thumbnail') !!}</div>
                                            </div>
                                        </div>

                                    </div>
                                    <div class="form-group depen- wrap-forth_image_ar  ">
                                        <label for="Forth Image Ar" class=" control-label col-md-3 text-left"> Forth Image Ar </label>


                                        <div class="col-md-9">
                                            <input type='hidden' name='forth_image_ar' id='forth_image_ar'
                                                   value='{{ (isset($row['forth_image_ar']))?$row['forth_image_ar']:null }}'
                                                   class='forth_image_ar    form-control input-sm ' required/>


                                            <button class='btn btn-primary mr-5' data-type='mediaUpload'
                                                    data-target='.forth_image_ar' type='button'>Choose file
                                            </button>
                                            <button class='btn btn-danger unUploadImage' data-type='mediaUnUpload'
                                                    data-target='forth_image_ar' type='button'>Remove File
                                            </button>

                                            <div class='forth_image_ar_preview'></div>
                                            <div class='forth_image_arPreview'>
                                                <div class='img-preview'>{!! SiteHelpers::showUploadedFile( (isset($row['forth_image_ar']))?$row['forth_image_ar']:null,'/uploads/images/featured_stores',80,'img-thumbnail') !!}</div>
                                            </div>
                                        </div>

                                    </div>

                                </fieldset>
                            </div>



                        </div>


                    </div>
                </div>
            </div>
            @include('/manage/'.$pageModule.'/subform')


            <div class="sbox-title clearfix">
                <div class="sbox-tools pull-right">
                    <a href="{{ url('/manage/'.$pageModule.'?return='.$return) }}" class="tips btn btn-sm "
                       title="{{ __('core.btn_back') }}"><i class="fa  fa-times"></i></a>
                </div>
                <div class="sbox-tools pull-left">
                    <button name="apply" class=" btn btn-sm btn-primary  " title="{{ __('core.btn_back') }}"><i
                                class="fa  fa-check"></i> {{ __('core.sb_apply') }} </button>
                    <button name="save" class=" btn btn-sm btn-success" title="{{ __('core.btn_back') }}"><i
                                class="fa  fa-paste"></i> {{ __('core.sb_save') }} </button>
                </div>
            </div>


            <input type="hidden" name="action_task" value="save"/>
            {!! Form::close() !!}
        </div>
    </div>



    <script type="text/javascript">
        $(document).ready(function () {


            $("#region_code").select2({
                ajax: {
                    url: "{{url('/manage/stores/ajaxselect?filter=regions:code:name')}}",
                    data: function (params) {
                        return {
                            search: params.term,
                            page: params.page || 1
                        };
                    },
                    dataType: 'json',
                    processResults: function (data) {
                        data.page = data.page || 1;
                        return {
                            results: data.items.map(function (item) {
                                return {
                                    id: item.code,
                                    text: selctToJson(item, 'name')
                                };
                            }),
                            pagination: {
                                more: data.pagination
                            }
                        }
                    },
                    cache: true,
                    delay: 250
                },
                // minimumInputLength: 2,
                multiple: 1,
                width: '100%'
            });


            @if( strlen($row["region_code"]) > 0  )
            // Fetch the preselected item, and add to the control
            var region_codeSelect = $('#region_code');
            $.ajax({
                type: 'GET',
                url: "{!! url('/manage/coupons/ajaxselect?filter=regions:code:name&values='.urlencode($row["region_code"])) !!}",
            })
                .then(function (data) {
                    // create the option and append to Select2
                    data.items.map(function (item) {
                        var option = new Option(selctToJson(item, 'name'), item.code, true, true);
                        region_codeSelect.append(option).trigger('change');

                    });
                    // manually trigger the `select2:select` event
                    region_codeSelect.trigger({
                        type: 'select2:select',
                        params: {
                            data: data
                        }
                    });
                });

            region_codeSelect.select2_sortable();
            @endif

            $("#store_id").select2({
                ajax: {
                    url: "{{url('/manage/featuredstores/ajaxselect?filter=stores:id:name')}}",
                    data: function (params) {
                        return {
                            search: params.term,
                            page: params.page || 1
                        };
                    },
                    dataType: 'json',
                    processResults: function (data) {
                        data.page = data.page || 1;
                        return {
                            results: data.items.map(function (item) {
                                return {
                                    id: item.id,
                                    text: selctToJson(item, 'name')
                                };
                            }),
                            pagination: {
                                more: data.pagination
                            }
                        }
                    },
                    cache: true,
                    delay: 250
                },
                // minimumInputLength: 2,
                multiple: 0,
                width: '100%'
            });


            @if( strlen($row["store_id"]) > 0  )
            // Fetch the preselected item, and add to the control
            var store_idSelect = $('#store_id');
            $.ajax({
                type: 'GET',
                url: "{!! url('/manage/featuredstores/ajaxselect?filter=stores:id:name&values='.urlencode($row["store_id"])) !!}",
            })
                .then(function (data) {
                    // create the option and append to Select2
                    data.items.map(function (item) {
                        var option = new Option(selctToJson(item, 'name'), item.id, true, true);
                        store_idSelect.append(option).trigger('change');

                    });


                    // manually trigger the `select2:select` event
                    store_idSelect.trigger({
                        type: 'select2:select',
                        params: {
                            data: data
                        }
                    });
                });

            store_idSelect.select2_sortable();
            @endif



            $('.removeMultiFiles').on('click', function () {
                var removeUrl = '{{ url("featuredstores/removefiles?file=")}}' + $(this).attr('url');
                $(this).parent().remove();
                $.get(removeUrl, function (response) {
                });
                $(this).parent('div').empty();
                return false;
            });

        });
    </script>
@stop