{% put scripts %}

<script>
    getBrandProducts('{{query}}', '{{limit}}', '{{brandId}}', '1');
    $('#pagination').hide();
    function getBrandProducts(query, limit, brandId, page) {
        $(document).ready(function () {
            $.ajax({
                url: `https://nurgul.com.tm/app/api/vendor/products/`+ brandId + `?` + query + `{{activeLocale}}&limit=` + limit + `&page=` + page,
                type: 'GET',
                dataType: 'json',
                beforeSend: function () {
                    $('#products_{{id}}').html("");
                    $('#products_list_{{id}}').html("");
                    $('#pagination_numbers').html("");
                    $('#detail_screen').hide();
                    $('#products_{{id}}').hide();
                    $('#loader_{{id}}').show();
                },
                success: function (data, textStatus, xhr) {
                    $('#detail_screen').show();
                    $('#loader_{{id}}').hide();
                    $('#products_{{id}}').show();
                    $('#show_title').html(`<span>{{'all'|_}}` + data.meta.total + `, {{'per.page'|_}} ` + data.meta.to + `</span>`);

                    if (data.meta.total > 8) {
                        $('#pagination').show();
                    }
                    // console.log(data.data);

                    var products = data.data;
                    for (var product of products) {
                        $('#products_{{id}}').append(productCard(product));
                        $('#products_list_{{id}}').append(productList(product));
                    }

                    var pagination = data.meta;


                    $('#pagination_numbers').append(
                        (pagination.current_page != 1 ?
                        `<li><a href="javascript:;" onClick="getBrandProducts(` + `'{{query}}'` + `,` + `'{{limit}}'` + `,` + `'{{brandId}}'` + `,` + pagination.links[1].label + `)"><i class="icon-arrow-left"></i></a></li>`
                        :
                        `<li><a href="javascript:;"><i class="icon-arrow-left"></i></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) {
                        $('#pagination_numbers').append(
                            `<li` + (pagination.links[i].active ? ` class="active" ` : ``) + `><a href="javascript:;" onClick="getBrandProducts(` + `'{{query}}'` + `,` + `'{{limit}}'` + `,` + `'{{brandId}}'` + `,` + pagination.links[i].label + `)">` + pagination.links[i].label + `</a></li>`
                        );
                        }
                    }

                    $('#pagination_numbers').append(
                        (pagination.current_page != pagination.last_page ?
                        `<li><a href="javascript:;" onClick="getBrandProducts(` + `'{{query}}'` + `,` + `'{{limit}}'` + `,` + `'{{brandId}}'` + `,` + (pagination.current_page + 1) + `)"><i class="icon-arrow-right"></i></a></li>`
                        :
                        `<li><a href="javascript:;"><i class="icon-arrow-right"></i></a></li>`
                        )
                    );

                    // $('#pagination_numbers').append(
                    //     `<li` + (pagination.current_page == (pagination.from - pagination.per_page) ? ` class="active" ` : ``) + `><a href="javascript:;" onClick="getBrandProducts(` + `'{{query}}'` + `,` + `'{{limit}}'` + `,` + `'{{brandId}}'` + `, '2')">` + (pagination.from - pagination.per_page) + `</a></li>`
                    //     // <li><a href="javascript:;" id="pagination_prev"><i class="icon-arrow-right"></i></a></li>
                    // );


                },
                error: function (xhr, textStatus, errorThrown) {
                    console.log('Error in Operation');
                }
            });
        });
    }

    function getCat(query, catId) {
        $(document).ready(function () {
            $.ajax({
                url: `https://nurgul.com.tm/app/api/categories/` + catId + `?` + query,
                type: 'GET',
                dataType: 'json',
                beforeSend: function () {
                    $('#products_{{id}}').hide();
                    $('#loader_{{id}}').show();
                },
                success: function (data, textStatus, xhr) {
                    $('#loader_{{id}}').hide(500);
                    $('#products_{{id}}').show(500);

                    $('#category_name').html(data.data.name);
                    $('#category_name_main').html(data.data.name);

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

{%partial 'scripts/setModal' %}
{%partial 'scripts/prodCard' %}
