var newsController = function () {
    this.initialize = function () {
        //loadCategoriesMain();
        loadData();
        registerEvents();
    }

    function registerEvents() {
        //todo: binding events to controls
        //$('#ddlCategoryMain').on('change', function () {
        //    loadCategoriesSub();
        //});
        //$('#ddlCategorySub').on('change', function () {
        //    monecore.configs.pageSize = $(this).val();
        //    monecore.configs.pageIndex = 1;
        //    loadData(true);
        //});
        $('#btnSearch').on('click', function () {
            loadData(true);
        });
        $('#txt-keyword').on('keypress', function (e) {
            if (e.which === 13) {
                loadData(true);
            }
        });
    }
    function loadCategoriesMain() {
        $.ajax({
            type: 'GET',
            url: '/news/GetAllCategoriesMain',
            dataType: 'json',
            success: function (response) {
                var render = "<option value=''>--Select category--</option>";
                $.each(response, function (i, item) {
                    //render += "<option value='-1'>Hiển thị tất cả</option>";
                    render += "<option value='" + item.Id + "'>" + item.Name + "</option>";
                });
                $('#ddlCategoryMain').html(render);
            },
            error: function (status) {
                console.log(status);
                monecore.notify('Cannot loading product category data', 'error');
            }
        });
    }
    function loadCategoriesSub() {
        $.ajax({
            type: 'GET',
            data: {
                categoryId: $('#ddlCategoryMain').val()
            },
            url: '/news/GetAllCategoriesSub',
            dataType: 'json',
            success: function (response) {
                var render = "<option value=''>--Select category--</option>";
                $.each(response, function (i, item) {
                    //render += "<option value='-1'>Hiển thị tất cả</option>";
                    render += "<option value='" + item.Id + "'>" + item.Name + "</option>";
                });
                $('#ddlCategorySub').html(render);
            },
            error: function (status) {
                console.log(status);
                monecore.notify('Cannot loading product category data', 'error');
            }
        });
    }
    function loadData(isPageChanged) {
        $('.someBlock').preloader();
        var sodong = 0;
        var template = $('#table-template').html();
        var render = "";
        $.ajax({
            type: 'GET',
            data: {
                categoryId: $('#ddlCategoryMain').val(),
                keyword: $('#txtKeyword').val(),
                page: monecore.configs.pageIndex,
                pageSize: monecore.configs.pageSize
            },
            url: '/news/GetAllPaging',
            dataType: 'json',
            success: function (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,
                                    Description: item.Description,
                                    CategoryName: item.NewsCategory.Name,
                                    //Price: monecore.formatNumber(item.Price, 0),
                                    DateModified: monecore.dateTimeFormatJson(item.DateModified),
                                    DateCreated: 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');
                }
            },
            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);
            }
        });
    }
}