{!! Form::open(array('url'=>'coupons/savepublic', 'class'=>'form-vertical','files' => true , 'parsley-validate'=>'','novalidate'=>' ')) !!}

@if(Session::has('messagetext'))

    {!! Session::get('messagetext') !!}

@endif
<ul class="parsley-error-list">
    @foreach($errors->all() as $error)
        <li>{{ $error }}</li>
    @endforeach
</ul>


<div class="sbox1">
    <div class="col-md-6">
        <fieldset class="sbox" style="border-top:0; border-radius: 5px;">
            <legend> Coupons</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="ipt" class=" control-label "> Store <span class="asterix"> * </span> </label>


                <?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="form-group  depen-  wrap-title  ">
                <label for="ipt" class=" control-label "> Coupon Title <span class="asterix"> * </span> </label>


                <?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='Coupon Title - {{$ilang}}' class='form-control input-sm '/>
                        </div>
                        <?php  $is_first = false;?>
                    @endforeach
                </div>

            </div>
            <div class="form-group  depen-  wrap-description  ">
                <label for="ipt" class=" control-label "> Coupon Description </label>


                <?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} : ''   }}'
                                   placeholder='Coupon Description - {{$ilang}}' class='form-control input-sm '/>
                        </div>
                        <?php  $is_first = false;?>
                    @endforeach
                </div>

            </div>
            <div class="form-group  depen-  wrap-type  ">
                <label for="ipt" class=" control-label "> Coupon Type <span class="asterix"> * </span> </label>
                <a class="adtip" data-toggle="tooltip" data-placement="bottom"
                   title="Select Coupon if there is Coupon Code"><i class="fa fa-info-circle"></i></a>

                <?php if (strlen($row['type']) < 1) $row['type'] = 'offer';      ?>
                <?php $type = (isset($row['type'])) ? $row['type'] : null; ?>

                <input type='radio' name='type' value='offer' required @if( $type == 'offer') checked="checked"
                       @endif class='minimal-red'> Offer

                <input type='radio' name='type' value='coupon' required @if( $type == 'coupon') checked="checked"
                       @endif class='minimal-red'> Coupon
            </div>
            <div class="form-group  depen-  wrap-link  ">
                <label for="ipt" class=" control-label "> Link <span class="asterix"> * </span> </label>
                <a class="adtip" data-toggle="tooltip" data-placement="bottom"
                   title="Enter plain link without any deeplink or extra paramter/subID."><i
                            class="fa fa-info-circle"></i></a>

                <?php if (!isset($row['link'])) $row['link'] = null;   ?>
                <input type='text' name='link' id='link' value='{{ $row['link'] }}'
                       required class='form-control input-sm '/>
            </div>
            <div class="form-group  depen-  wrap-expiry_date  ">
                <label for="ipt" class=" control-label "> Expiry Date <span class="asterix"> * </span> </label>
                <a class="adtip" data-toggle="tooltip" data-placement="bottom"
                   title="Coupons past expiry date will not be shown in front-end."><i
                            class="fa fa-info-circle"></i></a>

                <?php if (!isset($row['expiry_date'])) $row['expiry_date'] = null;   ?>

                <div class="input-group m-b" style="width:150px !important;">


                    <input type='text' name='expiry_date' id='expiry_date' value='{{ $row['expiry_date'] }}'
                           required class='form-control input-sm date '/>

                    <span class="input-group-addon calwrap"><i class="fa fa-calendar"></i></span>
                </div>
            </div> <?php if (strlen($row['clicks']) < 1) $row['clicks'] = '0';      ?>
            <input type='hidden' name='clicks' id='clicks' value='{{$row['clicks']}}'/>
            <?php if (!isset($row['created_mode'])) $row['created_mode'] = null;   ?>
            <input type='hidden' name='created_mode' id='created_mode' value='{{$row['created_mode']}}'/>

            <div class="form-group  depen-  wrap-network_id  ">
                <label for="ipt" class=" control-label "> Network </label>
                <a class="adtip" data-toggle="tooltip" data-placement="bottom"
                   title="Optional, used when creating coupons using Network coupon API."><i
                            class="fa fa-info-circle"></i></a>

                <?php if (!isset($row['network_id'])) $row['network_id'] = null;   ?>

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

                <select name='network_id' rows='5' id='network_id' class='select2 ' data-placeholder='Select Network'
                        placeholder='Select Network'></select>
            </div> <?php if (!isset($row['network_coupon_id'])) $row['network_coupon_id'] = null;   ?>
            <input type='hidden' name='network_coupon_id' id='network_coupon_id' value='{{$row['network_coupon_id']}}'/>
        </fieldset>
    </div>

    <div class="col-md-6">
        <fieldset class="sbox" style="border-top:0; border-radius: 5px;">
            <legend> Additional Details</legend>

            <div class="form-group  depen-  wrap-code  ">
                <label for="ipt" class=" control-label "> Code </label>
                <a class="adtip" data-toggle="tooltip" data-placement="bottom"
                   title="Optional, enter only if there's coupon code."><i class="fa fa-info-circle"></i></a>

                <?php if (!isset($row['code'])) $row['code'] = null;   ?>
                <input type='text' name='code' id='code' value='{{ $row['code'] }}'
                       class='form-control input-sm '/>
            </div>
            <div class="form-group  depen-  wrap-promo_text  ">
                <label for="ipt" class=" control-label "> Promo Text </label>
                <a class="adtip" data-toggle="tooltip" data-placement="bottom"
                   title="Applicable only for the Featured Offer, else leave it empty."><i
                            class="fa fa-info-circle"></i></a>

                <?php if (!isset($row['promo_text'])) $row['promo_text'] = null;   ?>
                <?php $val = json_decode($row['promo_text']); $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='#promo_text_{{$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='promo_text_{{$ilang}}_{{$curTime}}'>
                            <input type='text' name="promo_text[{{$ilang}}]" id='promo_text'
                                   value='{{   isset($val->{$ilang}) ? $val->{$ilang} : ''   }}'
                                   placeholder='Promo Text - {{$ilang}}' class='form-control input-sm '/>
                        </div>
                        <?php  $is_first = false;?>
                    @endforeach
                </div>

            </div>
            <div class="form-group  depen-  wrap-is_featured  ">
                <label for="ipt" class=" control-label "> Is Featured <span class="asterix"> * </span> </label>
                <a class="adtip" data-toggle="tooltip" data-placement="bottom"
                   title="Switch this ON if you wish to show this on Homepage, Make sure to enter Promo Text"><i
                            class="fa fa-info-circle"></i></a>

                <?php if (strlen($row['is_featured']) < 1) $row['is_featured'] = '0';      ?>
                <div class='switch-wrap'><label class='switch'>
                        <input type='hidden' name='is_featured' value='0'/>
                        <input type='checkbox' name='is_featured' value='1'
                               @if($row['is_featured']) checked @endif
                        /> <span class="slider round"></span></label></div>
            </div>


            <div class="form-group  depen-  wrap-is_recommended  ">
                <label for="ipt" class=" control-label "> Is Recommended <span class="asterix"> * </span> </label>
                <a class="adtip" data-toggle="tooltip" data-placement="bottom"
                   title="Switch this ON if you wish to show this on Homepage, Make sure to enter Promo Text"><i
                            class="fa fa-info-circle"></i></a>

                <?php if (strlen($row['is_recommended']) < 1) $row['is_recommended'] = '0';      ?>
                <div class='switch-wrap'><label class='switch'>
                        <input type='hidden' name='is_recommended' value='0'/>
                        <input type='checkbox' name='is_recommended' value='1'
                               @if($row['is_recommended']) checked @endif
                        /> <span class="slider round"></span></label></div>
            </div>


            <div class="form-group  depen-  wrap-is_exclusive  ">
                <label for="ipt" class=" control-label "> Is Exclusive <span class="asterix"> * </span> </label>
                <a class="adtip" data-toggle="tooltip" data-placement="bottom"
                   title="Switch this ON if you wish to show this on Homepage, Make sure to enter Promo Text"><i
                            class="fa fa-info-circle"></i></a>

                <?php if (strlen($row['is_exclusive']) < 1) $row['is_exclusive'] = '0';      ?>
                <div class='switch-wrap'><label class='switch'>
                        <input type='hidden' name='is_exclusive' value='0'/>
                        <input type='checkbox' name='is_exclusive' value='1'
                               @if($row['is_exclusive']) checked @endif
                        /> <span class="slider round"></span></label></div>
            </div>


            <div class="form-group  depen-  wrap-cats  ">
                <label for="ipt" class=" control-label "> Coupon Categories </label>
                <a class="adtip" data-toggle="tooltip" data-placement="bottom"
                   title="Select applicable coupon categories for this coupons, optional."><i
                            class="fa fa-info-circle"></i></a>

                <?php if (!isset($row['cats'])) $row['cats'] = null;   ?>

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

                <select name='cats[]' multiple rows='5' id='cats' class='select2 '
                        data-placeholder='Select Coupon Categories' placeholder='Select Coupon Categories'></select>
            </div>
            <div class="form-group  depen-  wrap-brands  ">
                <label for="ipt" class=" control-label "> Brands </label>
                <a class="adtip" data-toggle="tooltip" data-placement="bottom"
                   title="Select applicable brands for this coupons, optional."><i class="fa fa-info-circle"></i></a>

                <?php if (!isset($row['brands'])) $row['brands'] = null;   ?>

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

                <select name='brands[]' multiple rows='5' id='brands' class='select2 ' data-placeholder='Select Brands'
                        placeholder='Select Brands'></select>
            </div>
            <div class="form-group  depen-  wrap-tags  ">
                <label for="ipt" class=" control-label "> Tags </label>
                <a class="adtip" data-toggle="tooltip" data-placement="bottom"
                   title="Select applicable tags for this coupons, optional."><i class="fa fa-info-circle"></i></a>

                <?php if (!isset($row['tags'])) $row['tags'] = null;   ?>

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

                <select name='tags[]' multiple rows='5' id='tags' class='select2 ' data-placeholder='Select Tags'
                        placeholder='Select Tags'></select>
            </div>
            <div class="form-group  depen-  wrap-status  ">
                <label for="ipt" class=" control-label "> Status <span class="asterix"> * </span> </label>
                <a class="adtip" data-toggle="tooltip" data-placement="bottom"
                   title="Only Published coupons will be shown up in front-end."><i class="fa fa-info-circle"></i></a>

                <?php if (strlen($row['status']) < 1) $row['status'] = 'publish';      ?>
                <?php $status = (isset($row['status'])) ? $row['status'] : null; ?>

                <input type='radio' name='status' value='publish' required @if( $status == 'publish') checked="checked"
                       @endif class='minimal-red'> Publish

                <input type='radio' name='status' value='draft' required @if( $status == 'draft') checked="checked"
                       @endif class='minimal-red'> Draft

                <input type='radio' name='status' value='trash' required @if( $status == 'trash') checked="checked"
                       @endif class='minimal-red'> Trash
            </div>
        </fieldset>
    </div>


</div>


<div style="clear:both"></div>


<div class="form-group">
    <label class="col-sm-4 text-right">&nbsp;</label>
    <div class="col-sm-8">
        <button type="submit" name="apply" class="btn btn-info btn-sm"><i
                    class="fa  fa-check-circle"></i> {{ Lang::get('core.sb_apply') }}</button>
        <button type="submit" name="submit" class="btn btn-primary btn-sm"><i
                    class="fa  fa-save "></i> {{ Lang::get('core.sb_save') }}</button>
    </div>

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

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


        $("#store_id").select2({
            ajax: {
                url: "{{url('/manage/coupons/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/coupons/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


        $("#network_id").jCombo("{!! url('/manage/coupons/comboselect?filter=networks:id:name&values='.$row["network_id"]) !!}",
            {selected_value: '{{ $row["network_id"] }}'});

        $("#cats").select2({
            ajax: {
                url: "{{url('/manage/coupons/ajaxselect?filter=categories_coupon: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: 1,
            width: '100%'
        });


        @if( strlen($row["cats"]) > 0  )
        // Fetch the preselected item, and add to the control
        var catsSelect = $('#cats');
        $.ajax({
            type: 'GET',
            url: "{!! url('/manage/coupons/ajaxselect?filter=categories_coupon:id:name&values='.urlencode($row["cats"])) !!}",
        }).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);
                catsSelect.append(option).trigger('change');

            });


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

        catsSelect.select2_sortable();
        @endif


        $("#brands").select2({
            ajax: {
                url: "{{url('/manage/coupons/ajaxselect?filter=brands: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: 1,
            width: '100%'
        });


        @if( strlen($row["brands"]) > 0  )
        // Fetch the preselected item, and add to the control
        var brandsSelect = $('#brands');
        $.ajax({
            type: 'GET',
            url: "{!! url('/manage/coupons/ajaxselect?filter=brands:id:name&values='.urlencode($row["brands"])) !!}",
        }).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);
                brandsSelect.append(option).trigger('change');

            });


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

        brandsSelect.select2_sortable();
        @endif


        $("#tags").select2({
            ajax: {
                url: "{{url('/manage/coupons/ajaxselect?filter=tags: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: 1,
            width: '100%'
        });


        @if( strlen($row["tags"]) > 0  )
        // Fetch the preselected item, and add to the control
        var tagsSelect = $('#tags');
        $.ajax({
            type: 'GET',
            url: "{!! url('/manage/coupons/ajaxselect?filter=tags:id:name&values='.urlencode($row["tags"])) !!}",
        }).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);
                tagsSelect.append(option).trigger('change');

            });


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

        tagsSelect.select2_sortable();
        @endif



        $('.removeCurrentFiles').on('click', function () {
            var removeUrl = $(this).attr('href');
            $.get(removeUrl, function (response) {
            });
            $(this).parent('div').empty();
            return false;
        });

    });
</script>
