<div class="container-fluid">
    {% if message %}
        <div class="alert alert-danger alert-dismissible"><i class="fa fa-exclamation-circle"></i>{{ marketplace }}
            Mağazanız için {{ marketplace_info.name }} {{ message }}
            <button type="button" class="close" data-dismiss="alert">&times;</button>
        </div>
    {% endif %}
</div>
<div class="row">

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

            <table class="table well table-bordered">

                <thead>
                <th>{{ marketplace }}</th>
                <th>Eşitlenen</th>
                <th>Eşitlenmeyen</th>

                </thead>
                <tbody>
                <div class="progress active" style="margin-top:20px;">
                    <div class="progress-bar progress-bar-primary" role="progressbar" aria-valuenow="45" aria-valuemin="0"
                         aria-valuemax="100" style="width:0%"><span class="yuzde"></span></div>
                </div>


                <tr>
                    <td><span class="btn btn-primary btn-block btnTotal">{{ total }}</span></td>
                    <td><span class="btn btn-success btn-block btnMatched">{{ matched }}</span></td>
                    <td><span class="btn btn-danger btn-block btnUnMatched">{{ unmatched }}</span></td>


                </tr>

                </tbody>

            </table>

            <div id="report" style="overflow-y:scroll; overflow-x:hidden; height:100px;">



            </div>


        </div>

    </div>


</div>
<div class="row">
<div class="btn-group pull-right " style="margin-right: 10px">
    <button data-loading-text="Eşitleniyor..." class="btn  btn-lg  btn-primary btn-start">Eşitle</button>
    <button data-loading-text="Siliniyor..." class="btn  hidden  btn-lg  btn-warning btn-del">Sıfırla</button>
    <button  class="btn   btn-lg  btn-danger ">Kapat</button>

</div>
</div>

<script>

    var updated_products = 0;
    var totalpage;
    var current = 1;
    var lastnext = '';
    var activeButton=null;

    $(document).on('click','.btn-del', function () {
        $('.btn-del').button('loading');

        $.post("index.php?route=entegrasyon/sync/delete_market_products&code={{ code }}&{{ token_link }}", {}, function (donenVeri) {

            if (donenVeri['status']) {
                $('.btn-del').button('reset');
                $('.btnTotalDownloaded').text(0);
                Swal.fire('Başarılı', 'Tüm Ürünler Silindi', 'success');
            }


        }, 'json');


    });

    $('.btn-start').on('click', function () {
        $('#report').empty();
        activeButton=$(this);
        activeButton.button('loading');

        var total=parseInt({{ total }});
        var downloaded=parseInt({{ downloaded }});


        console.log("total"+parseInt({{ total }})+'indirilen'+{{ downloaded }});
       // if(parseInt({{ total }}) > parseInt({{ downloaded }})){

           console.log("indiriliyroum")

            startDownload('index.php?route=entegrasyon/sync/get_products_progress&totalDownloaded=' + parseInt($('.btnTotalDownloaded').text()) + '&downloaded=0&matched=0&unmatched=0&updated=0&code={{ code }}&{{ token_link }}')

      /*  }else {

            console.log("Eşitliuprım");

             startToSync('index.php?route=entegrasyon/sync/sync_products&total=' + parseInt($('.btnTotal').text()) + '&matched=0&unmatched=0&code={{ code }}&{{ token_link }}')


        }*/



    });


    function startDownload(url) {

        lastnext = url;



        $.ajax({
            url: url,
            type: 'post',
            dataType: 'json',
            success: function (json) {
                var yuzde = Math.round((json['current_page']  * 100) / json['total_page']);

                $('#updated_product').html(json['total']);
                $(' .progress-bar').css('width', yuzde + '%');
                $('.yuzde').html(yuzde + '%' + ' tamamlandı');


                if (!json['status']) {
                    activeButton.button('reset');

                    startToSync('index.php?route=entegrasyon/sync/sync_products&total=' + parseInt($('.btnTotal').text()) + '&matched=0&unmatched=0&code={{ code }}&{{ token_link }}')
                    console.log('sync');


                } else {

                    console.log('download');

                    startDownload(json['next']);

                }

                $('.btnDownloaded').text(parseInt(json['downloaded']));

                $('.btnTotalDownloaded').text(parseInt(json['totaldownloaded']));


            },
            error: function (request, status, error) {
                //alert(request.responseText);
               // startDownload(lastnext);
            }
        });
    }

</script>

<script>

    function startToSync(url) {
        lastnext = url;

        $.ajax({
            url: url,
            type: 'post',
            dataType: 'json',
            success: function (json) {
                var yuzde = Math.round(json['current_page'] * 100 / json['total_page']);

                $('#updated_product').html(json['total']);
                $('.prScnc  .progress-bar').css('width', yuzde + '%');
                $('.prScnc  .yuzde').html(yuzde + '%' + ' tamamlandı');


                if (!json['status']) {
                    activeButton.button('reset');


                } else {

                    startToSync(json['next']);

                }

                $('.btnMatched').text(parseInt(json['matched']));
                $('.btnUnMatched').text(parseInt(json['unmatched']));



            },
            error: function (request, status, error) {
                //alert(request.responseText);
                //startToSync(lastnext);
            }
        });

    }



</script>

