{% put scripts %}

<script>
    getProducts(`{{limit}}`, 1);
    // console.log("qqq");
    function getProducts(limit, page) {
        $(document).ready(function () {
            $.ajax({
                url: `https://nurgul.com.tm/app/api/seller/products/{{user.seller_id}}?locale=tm&limit=`+limit + `&page=` + page,
                type: 'GET',
                dataType: 'json',
                beforeSend: function () {
                    $('#products_{{id}}').html("");
                    $('#pagination_numbers').html("");
                    $('#loader_{{id}}').show();
                },
                success: function (data, textStatus, xhr) {
                    $('#loader_{{id}}').hide();
                    $('#products_{{id}}').show();


                    console.log(data.data);

                    var products = data.data;
                    var prodFeatured = "";
                    var prodImg = "";

                    for (var product of products) {

                        if(product.featured){
                            prodFeatured = "HAWA";
                        }else{
                            prodFeatured = "ÝOK";
                        }

                        if(product.images.length > 0){
                            prodImg = product.images[0].original_image_url;
                        }else{
                            prodImg = `{{'assets/no_img.jpg'|theme}}`;
                        }

                        // console.log(product.hasOwnProperty('special_price'));
                        $('#products_{{id}}').append(`{% partial "seller/prod-card" featured="`+ prodFeatured +`" price="`+product.formatted_price+`" qty="`+product.qty+`" status="`+product.status+`" id="`+product.id+`" new="`+product.new+`" name="`+product.name+`" price="`+product.formatted_price+`" img="`+prodImg+`" %}`);
                    }

                    var pagination = data.meta;

                    $('#pagination_numbers').append(
                        (pagination.current_page != 1 ?
                                `<li class="page-item"><a class="page-link" href="javascript:;" onClick="getProducts(` + `'{{limit}}'` + `,` + pagination.links[1].label + `)"><</a></li>`
                                :
                                `<li class="page-item"><a class="page-link" href="javascript:;"><</a></li>`
                        )
                    );
                    // for (var paginate of pagination.links) {
                    for (var i = 0; i < pagination.links.length; i++) {
                        if (i != 0 && i != pagination.links.length-1) {
                            console.log(pagination.links[i])
                            $('#pagination_numbers').append(
                                `<li` + (pagination.links[i].active ? ` class="page-item active" ` : ` class="page-item"`) + `><a class="page-link" href="javascript:;" onClick="getProducts(` + `'{{limit}}'` + `,` + pagination.links[i].label + `)">` + pagination.links[i].label + `</a></li>`
                            );
                        }
                    }

                    $('#pagination_numbers').append(
                        (pagination.current_page != pagination.last_page ?
                                `<li class="page-item"><a class="page-link" href="javascript:;" onClick="getProducts(` + `'{{limit}}'` + `,` + (pagination.current_page + 1) + `)"> > </a></li>`
                                :
                                `<li class="page-item"><a class="page-link" href="javascript:;"> > </a></li>`
                        )
                    );

                },
                error: function (xhr, textStatus, errorThrown) {
                    console.log('Error in Operation');
                }
            });
        });
    }
</script>
{% endput %}
