"use strict";
(function ($) {
    $.view_list_services = function (element, options) {
        let defaults = {
            visiblePages: 5,
            total_record : 100,
            limit : 6,
            start : 0,
            row_state_template: "",
            data_input:[]
        };
        let plugin = this;
        plugin.settings = {}
        let $element = $(element);
        plugin.init = function () {
            plugin.settings = $.extend({}, defaults, options);
            let price=plugin.settings.data_input.price_search;
            $element.find('select[name="location"]').select2();
            $element.find('#slider-container-1').slider({
                range: true,
                min: 0,
                max: 1099,
                values: [0, 1099],
                create: function () {
                    $("#amount_1").val("$0 - $1099");
                },
                slide: function (event, ui) {
                    $("#amount_1").val("$" + ui.values[0] + " - $" + ui.values[1]);
                    var mi = ui.values[0];
                    var mx = ui.values[1];
                    plugin.filterSystem(mi, mx);
                },
                change: function(event, ui) {
                    var mi = ui.values[0];
                    var mx = ui.values[1];
                    let price_value=[];
                    price_value.push(mi);
                    price_value.push(mx);
                    $element.find(`input[name="price_search"]`).val(JSON.stringify(price_value));
                }
            });

            $element.find('.block-filter-price-more-option').slideUp();
            $element.find('.block-service-categories').slideUp();
            $element.find('p.more-option').click(function (e) {
                let $block_more_option = $(this).closest('.block-more-option');
                $block_more_option.find('.block-filter-price-more-option').slideToggle();
                $block_more_option.find('.block-service-categories').slideToggle();
            });
            $element.find('.title-item').click(function (e) {
                let $parent = $(this).parent();
                $parent.find('div:nth-child(2)').slideToggle();
                $(this).toggleClass('slide-up')
            });

            let total_record=plugin.settings.total_record;
            let limit=plugin.settings.limit;
            let start=plugin.settings.start;
            let $fist_show=true;
            if (total_record>limit && total_record % limit >0 ){
                $element.find('#pagination-service').twbsPagination({
                    totalPages: parseInt((total_record/limit) + 1),
                    visiblePages: 3,
                    startPage:(start/limit)+1,
                    onPageClick: function (service, page) {
                        if($fist_show===false) {
                            let $form = $(this).closest('form.list-services');
                            $element.find('input[name="start"]').val(page*limit-limit);
                            $form.submit();
                            console.log("Page", page);
                            $('#page-content').text('Page ' + page);
                        }
                        $fist_show=false;
                    }
                });
            } else if(total_record>limit && total_record % limit ==0){
                $element.find('#pagination-service').twbsPagination({
                    totalPages: parseInt(total_record/limit),
                    visiblePages: 3,
                    startPage:(start/limit)+1,
                    onPageClick: function (service, page) {
                        if($fist_show===false) {
                            let $form = $(this).closest('form.list-services');
                            $element.find('input[name="start"]').val(page*limit-limit);
                            $form.submit();
                            console.log("Page", page);
                            $('#page-content').text('Page ' + page);
                        }
                        $fist_show=false;
                    }
                });
            }
            else if(total_record<=limit && total_record>0) {
                $element.find('#pagination-service').twbsPagination({
                    totalPages: 1,
                    visiblePages: 1,
                    startPage:(start/limit)+1,
                    onPageClick: function (event, page) {
                        if($fist_show===false) {
                            let $form = $(this).closest('form.list-events');
                            $element.find('input[name="start"]').val(page*limit-limit);
                            $form.submit();
                            console.log("Page", page);
                            $('#page-content').text('Page ' + page);
                        }
                        $fist_show=false;
                    }
                });
            } else {
                $element.find('#pagination-service').hide();
            }

            let total_record1=plugin.settings.total_record;
            let limit1=plugin.settings.limit;
            let start1=plugin.settings.start;
            let $fist_show1=true;
            if (total_record1>limit1 && total_record1 % limit1 >0 ){
                $element.find('#pagination-service1').twbsPagination({
                    totalPages: parseInt((total_record1/limit1) + 1),
                    visiblePages: 3,
                    startPage:(start1/limit1)+1,
                    onPageClick: function (service, page) {
                        if($fist_show1===false) {
                            let $form = $(this).closest('form.list-services');
                            $element.find('input[name="start"]').val(page*limit1-limit1);
                            $form.submit();
                            console.log("Page", page);
                            $('#page-content').text('Page ' + page);
                        }
                        $fist_show1=false;
                    }
                });
            } else if(total_record1>limit1 && total_record1 % limit1 ==0){
                $element.find('#pagination-service1').twbsPagination({
                    totalPages: parseInt(total_record1/limit1),
                    visiblePages: 3,
                    startPage:(start1/limit1)+1,
                    onPageClick: function (service, page) {
                        if($fist_show1===false) {
                            let $form = $(this).closest('form.list-services');
                            $element.find('input[name="start"]').val(page*limit1-limit1);
                            $form.submit();
                            console.log("Page", page);
                            $('#page-content').text('Page ' + page);
                        }
                        $fist_show1=false;
                    }
                });
            }
            else if(total_record1<=limit1 && total_record1>0) {
                $element.find('#pagination-service1').twbsPagination({
                    totalPages: 1,
                    visiblePages: 1,
                    startPage:(start1/limit1)+1,
                    onPageClick: function (event, page) {
                        if($fist_show1===false) {
                            let $form = $(this).closest('form.list-events');
                            $element.find('input[name="start"]').val(page*limit1-limit1);
                            $form.submit();
                            console.log("Page", page);
                            $('#page-content').text('Page ' + page);
                        }
                        $fist_show1=false;
                    }
                });
            } else {
                $element.find('#pagination-service1').hide();
            }


        };
        plugin.filterSystem = function (minPrice, maxPrice) {
            $("#computers div.system").hide().filter(function () {
                var price = parseInt($(this).data("price"), 10);
                return price >= minPrice && price <= maxPrice;
            }).show();
        }
        plugin.example_function = function () {
        }
        plugin.init();
    }
    $.fn.view_list_services = function (options) {
        return this.each(function () {
            if (undefined == $(this).data('view_list_services')) {
                let plugin = new $.view_list_services(this, options);
                $(this).data('view_list_services', plugin);
            }
        });
    }
})(jQuery);
