var productController = function () {
    this.initialize = function () {
        loadData();
        registerEvents();
    }

    function registerEvents() {
        //todo: binding events to controls
        $('#ddlsortby').on('change', function () {
            loadData(true);
        });
        $('#menu-product li a').click(function () {
            event.preventDefault();
            var categoryId = $(this).attr('href');
            //alert(txt);
            $("#categoryId").val(categoryId);
            var url = "/" + categoryId;
            window.location.href = categoryId;
            //loadData(true);
        });

        //$('#btnSearch').on('click', function () {
        //    loadData(true);
        //});
        //$('#txt-keyword').on('keypress', function (e) {
        //    if (e.which === 13) {
        //        loadData(true);
        //    }
        //});
    }
    
   
    function loadData(isPageChanged) {
        $('.someBlock').preloader();
        var sodong = 0;
        var template = $('#table-template').html();
        var render = "";
        $.ajax({
            type: 'GET',
            data: {
                keyword: $('#txt-keyword').val(),
                categoryId: $('#categoryId').val(),
                sortBy: $('#ddlsortby').val(),
                page: monecore.configs.pageIndex,
                pageSize: monecore.configs.pageSize
            },
            url: '/product/GetAllPaging',
            dataType: 'json',
            success: function (response) {
                //console.log(response);
                if (response.RowCount > 0) {
                    $.each(response.Results, function (i, item) {
                        sodong = sodong + 1;
                        render += Mustache.render(template, {
                            Stt: sodong,
                            Id: item.Id,
                            Name: item.Name,
                            SeoAlias: item.SeoAlias,
                            //Image: item.Image === null ? '<img src="/images/noimage.png" width=25' : '<img src="' + item.Image + '" width=25 />',
                            Image: item.Image,
                            CategoryName: item.CategoryName,
                            Price: monecore.formatNumber(item.Price, 0),
                            PromotionPrice: monecore.formatNumber(item.PromotionPrice, 0),
                            CreatedDate: monecore.dateTimeFormatJson(item.DateCreated),
                            Status: monecore.getStatus(item.Status)
                        });
                        $('#lblTotalRecords').text(response.RowCount);
                        if (render != '') {
                            $('#tbl-content').html(render);
                        }
                        wrapPaging(response.RowCount, function () {
                            loadData();
                        }, isPageChanged);
                    });
                    $('.someBlock').preloader('remove');
                }
                else {
                    $('#tbl-content').html('');
                    $('#paginationUL').empty();
                    //monecore.sweetalert("KHÔNG CÓ DỮ LIỆU !", "Vui lòng thử lại....", "error");                    
                    $('.someBlock').preloader('remove');
                    
                }
                //monecore.sweetalert('Không có dữ liệu', 'error');


            },
            error: function (status) {
                console.log(status);
                monecore.notify('Cannot loading data', 'error');
            }
        });
    }

    function wrapPaging(recordCount, callBack, changePageSize) {
        var totalsize = Math.ceil(recordCount / monecore.configs.pageSize);
        //Unbind pagination if it existed or click change pagesize
        if ($('#paginationUL a').length === 0 || changePageSize === true) {
            $('#paginationUL').empty();
            $('#paginationUL').removeData("twbs-pagination");
            $('#paginationUL').unbind("page");
        }
        //Bind Pagination Event
        $('#paginationUL').twbsPagination({
            totalPages: totalsize,
            visiblePages: 7,
            first: 'Đầu',
            prev: 'Trước',
            next: 'Tiếp',
            last: 'Cuối',
            onPageClick: function (event, p) {
                monecore.configs.pageIndex = p;
                setTimeout(callBack(), 200);
            }
        });
    }
}