﻿NhanSu_Class = function() { };
////////////////////////////////////
NhanSu_Class.prototype = {

    TopTemplate: function (element, pageIndex) {
        var classPath = "Main.BL.NhanSuBL";
        var methodName = "NhanSuTopTemplate";

        $.ajax({
            url: "Handler.ashx",
            dataType: "json",
            data: "ClassPath=" + classPath + "&MethodName=" + methodName + "&PageSize=10&PageIndex=" + pageIndex,
            type: "POST",
            timeout: GlobalVariable.AjaxTimeOut,
            beforeSend: function () {
                Other.showNotify("Đang tải dữ liệu...");
            },
            success: function (result) {
                if (result.Objects != null) {
                    element.setTemplateURL("./Template/NhanSu/TopTemplate.htm");
                    //Add Params to JTemplate
                    element.setParam("numberPage", result.NumberPage);
                    element.setParam("rowCount", result.RowCount);
                    element.setParam("pageIndex", pageIndex);
                    //                    
                    element.processTemplate(result);
                }
            },
            error: function (xhr, message, ex) {
                alert(GlobalVariable.MsgErrorLoadData);
            },
            complete: function () {
                Other.hideNotify();
            }
        });
    },

    TopTemplateBySubItemId: function (element, pageIndex, subItemId) {
        var classPath = "Main.BL.NhanSuBL";
        var methodName = "NhanSuTopTemplateBySubItemId";

        $.ajax({
            url: "Handler.ashx",
            dataType: "json",
            data: "ClassPath=" + classPath + "&MethodName=" + methodName + "&SubItemId=" + subItemId + "&PageSize=10&PageIndex=" + pageIndex,
            type: "POST",
            timeout: GlobalVariable.AjaxTimeOut,
            beforeSend: function () {
                Other.showNotify("Đang tải dữ liệu...");
            },
            success: function (result) {
                if (result.Objects != null) {
                    element.setTemplateURL("./Template/NhanSu/TopTemplateBySubItemId.htm");
                    //Add Params to JTemplate
                    element.setParam("numberPage", result.NumberPage);
                    element.setParam("rowCount", result.RowCount);
                    element.setParam("pageIndex", pageIndex);
                    //                    
                    element.processTemplate(result);
                }
            },
            error: function (xhr, message, ex) {
                alert(GlobalVariable.MsgErrorLoadData);
            },
            complete: function () {
                Other.hideNotify();
            }
        });
    },

    SearchTemplate: function (pageSize, pageIndex) {
        $("html, body").animate({ scrollTop: 0 }, 0);
        $("#Sort-Zone").css("display", "inline");
        //Set Query String for Ajax
        Ajax.SetQueryString(GlobalVariable.MainItem, GlobalVariable.SubItem, GlobalVariable.SearchProvince, GlobalVariable.SearchDistrict, GlobalVariable.SearchCost, encodeURIComponent(GlobalVariable.SearchKeyword), GlobalVariable.SearchTime, pageIndex, "0");

        classPath = "Main.BL.NhanSuBL";
        methodName = "NhanSuSearchTemplate";
        postValue = Gui.GetGlobalVariable() + "&TopLevel=" + GlobalVariable.SearchTopLevel + "&SortColumn=" + GlobalVariable.SortColumn + "&SortDirection=" + GlobalVariable.SortDirection + "&PageSize=" + pageSize + "&PageIndex=" + pageIndex;

        $.ajax({
            url: "Handler.ashx",
            dataType: "json",
            data: "ClassPath=" + classPath + "&MethodName=" + methodName + postValue,
            type: "POST",
            timeout: GlobalVariable.AjaxTimeOut,
            beforeSend: function () {
                Other.showNotify("Đang tải dữ liệu...");
            },
            success: function (result) {
                if (result.NumberPage > 0) {
                    $("#Result-Zone").html("Đã tìm thấy <span style='font-size:12pt'> " + result.RowCount + " </span> kết quả phù hợp với nhu cầu của bạn");
                    $("#Content-Zone").setTemplateURL("./Template/NhanSu/SearchTemplate.htm");
                    //Add Params to JTemplate    
                    $("#Content-Zone").setParam("numberPage", result.NumberPage);
                    $("#Content-Zone").setParam("rowCount", result.RowCount);
                    $("#Content-Zone").setParam("pageIndex", pageIndex);
                    //
                    $("#Content-Zone").processTemplate(result);
                }
                else {
                    if (pageIndex - 1 > 0)
                        Gui.SearchTemplate(pageIndex - 1);
                    else {
                        $("#Result-Zone").html("<b>Không tìm thấy kết quả theo yêu cầu của bạn</b>");
                        $("#Content-Zone").html(GlobalVariable.MsgNoRecord);
                    }
                }
            },
            error: function (xhr, message, ex) {
                alert(GlobalVariable.MsgErrorLoadData);
            },
            complete: function () {
                Other.hideNotify();
            }
        });
    },

    ViewByMaSo: function (maSo) {
        $("html, body").animate({ scrollTop: 0 }, 0);
        $("#Sort-Zone").css("display", "none");
        //Set Query String for Ajax
        Ajax.SetQueryString(GlobalVariable.MainItem, GlobalVariable.SubItem, GlobalVariable.SearchProvince, GlobalVariable.SearchDistrict, GlobalVariable.SearchCost, encodeURIComponent(GlobalVariable.SearchKeyword), GlobalVariable.SearchTime, "0", maSo);

        var classPath = "Main.BL.NhanSuBL";
        var methodName = "NhanSuSelectByMaSo";
        $.ajax({
            url: "Handler.ashx",
            dataType: "json",
            data: "ClassPath=" + classPath + "&MethodName=" + methodName + "&MaSo=" + maSo,
            type: "POST",
            timeout: GlobalVariable.AjaxTimeOut,
            beforeSend: function () {
                Other.showNotify("Đang tải dữ liệu...");
            },
            success: function (result) {
                $("#Content-Zone").setTemplateURL('./Template/NhanSu/ViewByMaSo.htm');
                $("#Content-Zone").processTemplate(result);
            },
            error: function (xhr, message, ex) {
                alert(GlobalVariable.MsgErrorLoadData);
            },
            complete: function () {
                Other.hideNotify();
                Other.TabInit();
                $("#gallery a").lightBox();
            }
        });
    }


};

var NhanSu = new NhanSu_Class();


