@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/storecashback?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>
                    <?php if ($frontendSlug != '') {
                        $fsconfig = explode('@', $frontendSlug);
                        if (($row[$fsconfig[1]]))
                            echo '<a  class="btn btn-sm btn-outline-primary" target="_blank"  title="FrontEnd" href="' . (config('website.cnf_appurl') . '/' . config('website.cnf_lang') . str_replace('//', '/', ('/' . $fsconfig[0] . '/' . $row[$fsconfig[1]]))) . '"><i class="fa  fa-desktop"></i> FrontEnd</a>';
                    } ?>
                </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> Store Cashback</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-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-title  ">
                                        <label for="Cashback Structure Title" class=" control-label col-md-3 text-left">
                                            Cashback Structure Title <span class="asterix"> * </span></label>


                                        <div class="col-md-9">
                                            <?php if (!isset($row['title'])) $row['title'] = null;   ?>
                                            <?php $val = json_decode($row['title']); $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='#title_{{$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='title_{{$ilang}}_{{$curTime}}'>
                                                        <input type='text' name="title[{{$ilang}}]" id='title'
                                                               value='{{   isset($val->{$ilang}) ? $val->{$ilang} : ''   }}'
                                                               required
                                                               placeholder='Cashback Structure Title - {{$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-image  ">
                                        <label for="Image" class=" control-label col-md-3 text-left"> Image </label>
                                        <a class="adtip" data-toggle="tooltip" data-placement="bottom"
                                           title="Optional, required to show as cashback categories"><i
                                                    class="fa fa-info-circle"></i></a>

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


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


                                            <div class='image_preview'></div>
                                            <div class='imagePreview'>
                                                <div class='img-preview'>
                                                    {!! SiteHelpers::showUploadedFile($row['image'],'/uploads/images/store/') !!}

                                                </div>
                                            </div>

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

                                        </div>
                                    </div>
                                    <div class="form-group depen- wrap-thumbnail  ">
                                        <label for="thumbnail" class=" control-label col-md-3 text-left"> Thumbnail </label>
                                        <a class="adtip" data-toggle="tooltip" data-placement="bottom"
                                           title="Optional, required to show as cashback categories"><i
                                                    class="fa fa-info-circle"></i></a>

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


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


                                            <div class='thumbnail_preview'></div>
                                            <div class='imagePreview'>
                                                <div class='img-preview'>
                                                    {!! SiteHelpers::showUploadedFile($row['thumbnail'],'/uploads/images/store/') !!}

                                                </div>
                                            </div>

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

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


                                        <div class="col-md-9">
                                            <?php if (strlen($row['rate_type']) < 1) $row['rate_type'] = 'percent';      ?>

                                            <?php $rate_type = explode(',', $row['rate_type']);
                                            $rate_type_opt = array('fixed' => 'Fixed', 'percent' => 'Percent',); ?>
                                            <select name='rate_type' rows='5' required placeholder='Select Rate Type'
                                                    data-placeholder='Select Rate Type' class='regSel form-control'>
                                                <?php
                                                foreach ($rate_type_opt as $key => $val) {
                                                    echo "<option  value ='$key' " . ($row['rate_type'] == $key ? " selected='selected' " : '') . ">$val</option>";
                                                }
                                                ?></select>
                                        </div>
                                        <div class="col-md-2">

                                        </div>
                                    </div>
                                    <div class="form-group depen- wrap-rate_amount_network  ">
                                        <label for="Network Rate" class=" control-label col-md-3 text-left"> Network
                                            Rate </label>


                                        <div class="col-md-9">
                                            <?php if (!isset($row['rate_amount_network'])) $row['rate_amount_network'] = null;   ?>
                                            <input type='text' name='rate_amount_network' id='rate_amount_network'
                                                   value='{{ $row['rate_amount_network'] }}'
                                                   data-parsley-maxlength="10" class='form-control is_decimal'
                                                   onkeypress='return isNumberKey(event,this)'/>
                                        </div>
                                        <div class="col-md-2">

                                        </div>
                                    </div>
                                    <div class="form-group depen- wrap-link  ">
                                        <label for="Link" class=" control-label col-md-3 text-left"> Link </label>
                                        <a class="adtip" data-toggle="tooltip" data-placement="bottom"
                                           title="Optional, required to show as cashback categories"><i
                                                    class="fa fa-info-circle"></i></a>

                                        <div class="col-md-9">
                                            <?php if (!isset($row['link'])) $row['link'] = null;   ?>
                                            <?php $val = is_array($row['link']) ? (object)$row['link'] : json_decode($row['link']); $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='#link_{{$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='link_{{$ilang}}_{{$curTime}}'>
                                                        <input type='text' name="link[{{$ilang}}]" id='link'
                                                               value='{{   isset($val->{$ilang}) ? $val->{$ilang} : ''   }}'
                                                               required placeholder='Link - {{$ilang}}'
                                                               class='form-control input-sm ' style="direction: ltr;"/>
                                                    </div>
                                                    <?php  $is_first = false;?>
                                                @endforeach
                                            </div>

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

                                        </div>
                                    </div>
                                    <div class="form-group depen- wrap-app_deep_link  ">
                                        <label for="app_deep_link" class=" control-label col-md-3 text-left"> App DeepLink </label>

                                        <div class="col-md-9">
                                            <?php if (!isset($row['app_deep_link'])) $row['app_deep_link'] = null;   ?>
                                            <input type='text' name='app_deep_link' id='app_deep_link' value='{{ $row['app_deep_link'] }}'
                                                   class='form-control input-sm '/>
                                        </div>
                                        <div class="col-md-2">

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


                                        <div class="col-md-9">
                                            <?php if (strlen($row['enabled']) < 1) $row['enabled'] = '1';      ?>
                                            <div class='switch-wrap'><label class='switch'>
                                                    <input type='hidden' name='enabled' value='0'/>
                                                    <input type='checkbox' name='enabled' value='1'
                                                           @if($row['enabled']) 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> Additional Information</legend>

                                    <div class="form-group depen- wrap-description  ">
                                        <label for="Description" class=" control-label col-md-3 text-left">
                                            Description </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}}'>
				
				<textarea name="description[{{$ilang}}]" rows='5' id='description' class='form-control input-sm '
                >{{   isset($val->{$ilang}) ? $val->{$ilang} : ''   }}</textarea>


                                                    </div>
                                                    <?php  $is_first = false;?>
                                                @endforeach
                                            </div>

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

                                        </div>
                                    </div>
                                    <div class="form-group depen- wrap-exclusion  ">
                                        <label for="Exclusion" class=" control-label col-md-3 text-left">
                                            Exclusion </label>


                                        <div class="col-md-9">
                                            <?php if (!isset($row['exclusion'])) $row['exclusion'] = null;   ?>
                                            <?php $val = json_decode($row['exclusion']); $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='#exclusion_{{$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='exclusion_{{$ilang}}_{{$curTime}}'>
				
				<textarea name="exclusion[{{$ilang}}]" rows='5' id='exclusion' class='form-control input-sm '
                >{{   isset($val->{$ilang}) ? $val->{$ilang} : ''   }}</textarea>


                                                    </div>
                                                    <?php  $is_first = false;?>
                                                @endforeach
                                            </div>

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

                                        </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 () {


            $("#store_id").select2({
                ajax: {
                    url: "{{url('/manage/storecashback/ajaxselect?filter=stores:id:slug')}}",
                    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, 'slug')
                                };
                            }),
                            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/storecashback/ajaxselect?filter=stores:id:slug&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, 'slug'), 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("storecashback/removefiles?file=")}}' + $(this).attr('url');
                $(this).parent().remove();
                $.get(removeUrl, function (response) {
                });
                $(this).parent('div').empty();
                return false;
            });

        });
    </script>
@stop