{{ header }} {{ column_left }}

<div id="content">
    <div class="page-header">

    </div>

    <div class="container-fluid">
        {% if error_warning %}
            <div class="alert alert-danger"><i class="fa fa-exclamation-circle"></i> {{ error_warning }}
                <button type="button" class="close" data-dismiss="alert">&times;</button>
            </div>
        {% endif %}
        {% if success %}
            <div class="alert alert-success"><i class="fa fa-check-circle"></i> {{ success }}
                <button type="button" class="close" data-dismiss="alert">&times;</button>
            </div>
        {% endif %}
        <div class="row well"  style="border-top: 2px solid #27ad5e;">


            <div class="col-sm-1">
                <img class="img-responsive" src="view/image/entegrasyon/bilgi.png"/>
            </div>
            <div class="col-sm-11">
                <h3 style="color:#27ad5e;font-weight: bolder ">Bilgilendirme</h3>
                <p class="">Toplu ürün gönderebilmek için önce bir kategori seçmelisiniz. Seçtiğiniz kategori pazaryeri kategorisi ile eşleşmişse, <strong>Ürün Listele</strong> butonu aktif olacaktır. Sadece <strong>Trendyol</strong>'da hem kategori hemde marka eşleştirmesi zorunludur. Kategori seçildikten sonra <strong>Ürün Listele</strong> butonuna tıkladığınızda seçtiğiniz pazaryerine <strong>gönderilmemiş</strong> ürünler listelenir. Eğer pazaryeri için zorunlu kategori özellikleri varsa listelenen ürünün solundaki ayar butonuna tıklayarak ürün özel seçenek ayarlarını yapabilirsiniz. Seçenekleri her ürün için ayrı belirleyebildiğiniz gibi isterseniz sadece ilk sıradaki ürüne belirleyip,<strong>Seçilen ilk satırdaki ürün özelliklerini baz al</strong> kutucuğunu işartlerseniz tüm ürünler ilk ürünün seçenekleri ile gönderilir. </p>
            </div>
        </div>

        <div class="panel panel-default" style="border-top: 2px solid #27ad5e;">

            <div class="panel-body">
                <div class="">
                    <div class="row">


                        <div class="col-md">
                            <div class="">
                                <label class="control-label" for="input-name">Ürün Ara</label>
                                <input type="text" value="{{ filter_name }}" placeholder="Ürün Ara" class="form-control filter_name" name="filter_name">


                            </div>
                        </div>




                        <div class="col-md">
                            <div class="">
                                <label class="control-label" for="input-model">{{ 'Kategori' }}</label>
                                <select class="form-control selectpicker category" name="filter_category" data-live-search='true'
                                        data-title='Kategori'>
                                    {% for category in categories %}
                                        {% if filter_category == category.category_id %}
                                            <option value="{{ category.category_id }}"
                                                    selected="selected"> {{ category.name }} </option>
                                        {% else %}
                                            <option value="{{ category.category_id }}"> {{ category.name }} </option>
                                        {% endif %}

                                    {% endfor %}
                                </select>
                            </div>
                        </div>

                        <div class="col-md">
                            <div class="">
                                <label class="control-label" for="input-model">{{ 'Üretici' }}</label>
                                <input type="text" value="{{ filter_manufacturer_name}}" placeholder="Otomatik Tamamlama" class="form-control" name="manufacturer">
                                <input type="hidden" value="{{ filter_manufacturer }}" class="form-control" name="filter_manufacturer">

                            </div>
                        </div>

                        <div class="col-md">
                            <label class="control-label" for="input-model">Ürün Durumu</label>

                            <select name="filter_status" class="form-control" >


                                {% if filter_status == '*' %}
                                    <option selected value="*">Tümü</option>
                                    <option value="1">Açık</option>
                                    <option  value="0">Kapalı</option>
                                {% elseif filter_status == 1 %}
                                    <option value="*">Tümü</option>
                                    <option selected value="1">Açık</option>
                                    <option  value="0">Kapalı</option>
                                {% elseif filter_status == 0  %}

                                    <option value="*">Tümü</option>
                                    <option  value="1">Açık</option>
                                    <option selected value="0">Kapalı</option>
                                {% endif %}



                            </select>
                        </div>

                        <div class="col-md">
                            <label class="control-label" for="input-model">Stok</label>

                            <select name="filter_stock_prefix" class="form-control filter_stock_prefix" >
                                <option selected value="*">Tümü</option>

                                {% if filter_stock_prefix == '=' %}
                                    <option selected value="=">Şuna Eşit</option>
                                    <option  value=">">Şundan Büyük</option>
                                    <option value="<">Şundan Küçük</option>
                                {% elseif filter_stock_prefix =='>'  %}
                                    <option  value="=">Şuna Eşit</option>
                                    <option selected  value=">">Şundan Büyük</option>
                                    <option value="<">Şundan Küçük</option>
                                {% elseif filter_stock_prefix =='<'  %}

                                    <option  value="=">Şuna Eşit</option>
                                    <option   value=">">Şundan Büyük</option>
                                    <option selected value="<">Şundan Küçük</option>
                                {% else %}
                                    <option  value="=">Şuna Eşit</option>
                                    <option   value=">">Şundan Büyük</option>
                                    <option value="<">Şundan Küçük</option>
                                {% endif %}



                            </select>

                            <input placeholder="stok adedi" value="{{ filter_stock }}" class="form-control hidden filter_stock" type="number" name="filter_stock" />

                        </div>

                        <div class="col-sm" style="margin-top: 20px">
                            <button  type="button" {% if not category_name %} disabled {% endif %} id="button-filter" class="btn btn-primary"><i
                                        class="fa fa-search"></i> Ürünleri Listele</button>
                        </div>
                    </div>

                    <div class="row">
                        <div class="panel-body">
                            <table class="table category_info">
                                {% if category_name %}

                                    <tr><td>Eşleşmiş {{ market_name }} Kategorisi</td><td>{{  category_name  }}</td></tr><tr><td>Eşleşmiş {{ market_name }} Kategori Kodu</td><td>{{ category_id }}</td></tr>

                                {% endif %}
                            </table>

                        </div>
                    </div>

                    <div class="row">
                    </div>
                </div>
            </div>


        </div>



        <div class="container-fluid">
            <div class="checkbox">
                <label style="font-size: 2em">
                    <input name="firstrow" id="firstrow" type="checkbox">
                    <span class="cr"><i class="cr-icon fa fa-check"></i></span>
                    <span style="font-size: medium"> Seçilen ilk satırdaki ürün özelliklerini baz al</span>
                </label>
            </div>


        </div>
        <div class="alert alert-info hidden"> <strong>Özellikler ( Eğer Ürününüzün seçeneklerinde aşağıdaki özelliklerden varsa bu özellikleri boş bırakınız aksi halde gönderim yapmaz)</strong></div>
        <div id="progres_bar"></div>
        <div id="report"></div>
        <div class="panel panel-default" style="border-top: 2px solid #27ad5e;">
            <div class="panel-heading">
                <h2 class="panel-title"><i class="fa fa-list"></i> {{ text_list }}</h2>
                <button type="button" style="margin-top: -9px;margin-right: -14px" data-loading-text="<i class='fa fa-spinner fa-spin '></i> Gönderiliyor" class="btnsend btn btn-primary pull-right">
                    <i class="fa fa-send"  ></i> Seçilenleri Gönder
                </button>
            </div>
            <div class="table-responsive">
                <table class="table table-bordered table-hover">
                    <thead>
                    <tr>

                        <td style="width: 1px;" class="">
                            <div class="checkbox">
                                <label style="font-size: 1em">
                                    <input type="checkbox"
                                           onclick="$('input[name*=\'selected\']').prop('checked', this.checked);"/>                                    <span class="cr"><i class="cr-icon fa fa-check"></i></span>
                                </label></div>

                            <!-- <input type="checkbox" onclick="$('input[name*=\'selected\']').prop('checked', this.checked);" />-->
                        </td>

                        <td style="width: 70%">

                            {{ column_name }}</td>
                        <td style="width: 10%" class="text-right">Model</td>
                        <td style="width: 10%" class="text-right">Fiyat</td>
                        <td style="width: 10%" class="text-right">Adet</td>
                        <td style="width: 10%" class="text-right">Ayarlar</td>


                    </tr>
                    </thead>
                    <tbody>
                    {% if products %}
                        {% for product in products %}
                            <tr id="tr{{ product.product_id }}">
                                <td>
                                    <div class="checkbox">
                                        <label style="font-size: 1em">
                                            {% if  product.product_id in selected %}
                                                <input class="select" type="checkbox" name="selected[]"
                                                       value="{{ product.product_id}}" checked="checked"/>

                                            {% else %}
                                                <input class="select" type="checkbox" name="selected[]"
                                                       value="{{ product.product_id }}"/>
                                            {% endif %}



                                            <span class="cr"><i class="cr-icon fa fa-check"></i></span>
                                        </label></div>
                                </td>
                                <td id="name{{ product.product_id }}" class="text-left" width="">{{ product.name }}  </td>

                                <td class="text-right">{{ product.model }}</td>

                                <td class="text-right">{% if product.special %} <span style="text-decoration: line-through;">{{ product.price }}</span><br/>
                                        <div class="text-danger">{{ product.special }}</div>
                                    {% else %}
                                        {{ product.price }}
                                    {% endif %}</td>
                                <td class="text-right">{% if product.quantity <= 0 %} <span class="label label-warning">{{ product.quantity }}</span> {% elseif product.quantity <= 5 %} <span class="label label-danger">{{ product.quantity }}</span> {% else %} <span class="label label-success">{{ product.quantity }}</span> {% endif %}</td>


                                <td class="text-right">

                                    <button product_id="{{ product.product_id }}" code="{{ code }}"  marketname="{{ market_name }}" name="{{ product.name }}"  data-toggle="tooltip" title="{{ button_edit }}" class="btn btn-default btnSetting"><i class="fa fa-pencil"></i></button>


                                </td>
                            </tr>


                        {% endfor %}

                    {% endif %}
                    </tbody>
                </table>
            </div>

        </div>

    </div>
</div>
<script type="text/javascript"><!--
    $('#button-filter').on('click', function () {
        var url = 'index.php?route=entegrasyon/product/send_bulk&code={{code}}&{{ token_link }}';

        var filter_category = $('select[name=\'filter_category\']').val();

        if (filter_category != '*') {
            url += '&filter_category=' + encodeURIComponent(filter_category);
        }
        var filter_name = $('.filter_name').val();

        if (filter_name != '*') {
            url += '&filter_name=' + encodeURIComponent(filter_name);
        }

       

        var filter_manufacturer = $('input[name=\'filter_manufacturer\']').val();

        if (filter_manufacturer != '*') {
            url += '&filter_manufacturer=' + encodeURIComponent(filter_manufacturer);
        }

        var filter_status = $('select[name=\'filter_status\']').val();

        if (filter_status) {
            url += '&filter_status=' + encodeURIComponent(filter_status);
        }
        var filter_stock_prefix = $('select[name=\'filter_stock_prefix\']').val();

        if (filter_stock_prefix) {
            url += '&filter_stock_prefix=' + encodeURIComponent(filter_stock_prefix);
        }

        var filter_stock = $('input[name=\'filter_stock\']').val();

        if (filter_stock) {
            url += '&filter_stock=' + encodeURIComponent(filter_stock);
        }


        location = url;
    });
    //--></script>



<script>
    {% if filter_stock_prefix !='' %}
    {% if ilter_stock_prefix !='*' %}
    $(function (event){
        $('.filter_stock').removeClass('hidden');

        //$('.filter_marketplace_do label').fadeTo(100, 0.1).fadeTo(200, 1.0);
    })
    {% endif %}
    {% endif %}
</script>
<script>
    $('.filter_stock_prefix').change(function(){
        var selected=$(this).val();
        if(selected!='*'){

            $('.filter_stock').removeClass('hidden');
            $('.filter_stock').fadeTo(100, 0.1).fadeTo(200, 1.0);


        }else {

            $('.filter_stock').addClass('hidden');

        }

    });

</script>
<script>


    $('input[name=\'manufacturer\']').autocomplete({
        'source': function(request, response) {
            $.ajax({
                url: 'index.php?route=catalog/manufacturer/autocomplete&{{ token_link }}&filter_name=' + encodeURIComponent(request),
                dataType: 'json',
                success: function(json) {
                    json.unshift({
                        manufacturer_id: 0,
                        name: '{{ text_none }}'
                    });

                    response($.map(json, function(item) {
                        return {
                            label: item['name'],
                            value: item['manufacturer_id']
                        }
                    }));
                }
            });
        },
        'select': function(item) {
            $('input[name=\'manufacturer\']').val(item['label']);
            $('input[name=\'filter_manufacturer\']').val(item['value']);
        }
    });



</script>


<script>
    $(function () {
        $('.selectpicker').selectpicker();
    })

</script>


<script>

    $(document).on('click', '.btnsend', function () {
        var product_list = [];
        $('.btnsend').button('loading');
        $(".table .select:checked").each(function () {
            var product_id = $(this).val();
            product_list.push(product_id);
        });




        if (product_list.length != 0) {
            $('#report').empty();
            $('#progres_bar').html('<div id="prgBulk" class="progress active" style="margin-top:20px;"> <div class="progress-bar"  role="progressbar" aria-valuenow="45" aria-valuemin="0" aria-valuemax="100" style="width:0%"><span class="yuzde"></span></div></div>');

            sendProduct(product_list[0],product_list);


        }
        else {
            $('.btnsend').button('reset');
            Swal.fire('ürün seçmediniz');
        }

    });


    function sendProduct(item,pList) {

        var first_element=$("input[type='checkbox'][name='selected[]']:checked").first();
        var reference= first_element.val();


        var data={};
        data.list=pList;

        if ($("#firstrow:checked").val()) {
            data.reference= reference;
        }else {
            data.reference= 0;
        }

        $.post('index.php?route=entegrasyon/product/send_to_marketplace&code={{ code }}&product_id='+item+'&{{ token_link }}&category_id={{ filter_category }}', data, function (json) {


            var product_name=$('#name'+item).text();
            var yuzde = Math.round(parseInt(json['current']) * 100 / pList.length);

            $('#prgBulk  .progress-bar').css('width', yuzde + '%');
            $('.progress  .yuzde').html(yuzde + '%' + ' tamamlandı');
            if(json['status']) {
                $('#report').append('<div class="alert alert-success"><i class="fa fa-check-circle"></i> ' + product_name + json['message'] + '</div>');
            }else {

                $('#report').append('<div class="alert alert-warning"><i class="fa fa-exclamation-triangle"></i> ' + product_name + json['message'] + '</div>');

            }


            if (json['next']) {

                sendProduct(json['item'], json['list']);

            }  else {

                $('.btnsend').button('reset');
                $('#report').append('<div class="alert alert-info"><i class="fa fa-check-circle"></i>Gönderme İşlemi Tamamlandı.</div>');

            }



        }, 'JSON');

    }



</script>


<script>

    var category_id;
    $('.category').change(function () {
        category_id=$(this).val();
        if(category_id!='*') {

            var category_name = $(".category option:selected").text();
            $.post('index.php?route=entegrasyon/category/is_category_match&code={{ code }}&{{ token_link }}',{category_id:category_id}, function (json) {
                if (!json['status']) {
                    $('#button-filter').prop('disabled',true);
                    Swal.fire({
                        title: "Eşleşmemiş Kategori",
                        text: 'Seçtiğiniz Kategori {{ market_name }} kategorileri ile eşleşmemiş, Şimdi eşleştirmek istermisiniz ?',
                        type: "warning",
                        showCancelButton: true,
                        confirmButtonColor: "#8fbb6c",
                        cancelButtonColor: "#d9534f",
                        confirmButtonText: "Evet",
                        cancelButtonText: "Hayır",
                        closeOnConfirm: true,
                        closeOnCancel: true
                    }).then((result) => {
                        if (result.value) {

                            BootstrapDialog.show({
                                title: category_name + '{{ market_name }} Kategori Eşleştir',
                                message: function (dialog) {
                                    var $message = $('<div></div>');
                                    var pageToLoad = dialog.getData('pageToLoad');
                                    $message.load(pageToLoad);

                                    return $message;
                                },
                                data: {
                                    'pageToLoad': 'index.php?route=entegrasyon/category/setting&code={{ code }}&category_id='+category_id+'&{{ token_link }}'
                                }
                            });

                        }
                    })

                } else {
                    $('#button-filter').prop('disabled',false);
                    $('.category_info').empty();
                    $('.category_info').append('<tr><td>Eşleşmiş {{ market_name }} Kategorisi</td><td>' + json["category_name"] + '</td></tr><tr><td>Eşleşmiş {{ market_name }} Kategori Kodu</td><td>' + json["category_id"] + '</td></tr>');
                }
            }, 'JSON');
        }else {
            $('#button-filter').prop('disabled',false);

        }
    });

    $(document).on("hide.bs.modal", function () {
        // put your default event here


        $.post('index.php?route=entegrasyon/category/is_category_match&code={{ code }}&{{ token_link }}',{category_id:category_id}, function (json) {
            if (json['status']) {
                $('#button-filter').prop('disabled',false);
                $('.category_info').empty();
                $('.category_info').append('<tr><td>Eşleşmiş {{ market_name }} Kategorisi</td><td>' + json["category_name"] + '</td></tr><tr><td>Eşleşmiş {{ market_name }} Kategori Kodu</td><td>' + json["category_id"] + '</td></tr>');

            }
        }, 'JSON');



    });

</script>

<script>
    $('.btnSetting').on('click',function (event) {
        event.preventDefault();
        var $thisBtn=$(this);
        var name=$thisBtn.attr('name');
        var code=$thisBtn.attr('code');
        var marketname=$thisBtn.attr('marketname');
        var product_id=$thisBtn.attr('product_id');


        BootstrapDialog.show({
            title:name+'-'+marketname +'-'+'Ürün Ayarları',
            size: BootstrapDialog.SIZE_WIDE,

            message: function(dialog) {
                var $message = $('<div></div>');
                var pageToLoad = dialog.getData('pageToLoad');
                $message.load(pageToLoad);
                return $message;
            },
            data: {
                'pageToLoad':'index.php?route=entegrasyon/product/setting&code='+code+'&category_id={{ filter_category }}&product_id='+product_id+'&{{ token_link }}'
            }
        });


    })

</script>
<script>

    $(document).on('click','#btnComplate',function () {

        $.each(BootstrapDialog.dialogs, function(id, dialog){
            dialog.close();
        });
    })

</script>

{{ footer }}