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

    function registerEvents() {
        //todo: binding events to controls
        $('#ddlShowPage').on('change', function () {
            monecore.configs.pageSize = $(this).val();
            monecore.configs.pageIndex = 1;
            loadData(true);
        });
        $('#ddlCategoryMain').on('change', function () {
            loadCategoriesSub();
        });
        $('#ddlCategorySub').on('change', function () {
            monecore.configs.pageSize = $(this).val();
            monecore.configs.pageIndex = 1;
            loadData(true);
        });
        //$('#ddlCategorySearch').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: '/product/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: '/product/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');
            }
        });
    }
    
}