/*-------------------------------文档信息-----------------------------------
///功能：销售统计
///创建人: 翟新乐
///创建时间：------------------------
///修改人：------------------------
///修改时间：-----------------------
///修改原因：-----------------------
///详细说明：-----------------------
---------------------------------------------------------------------*/
//页面全局变量的定义
var strTableP0211 = "Tp0211";   //表格ID
var objSelRowP0211 = {};        //选择行的数据
var intSelRowIDP0211 = 0;       //选择行的ID
var objWhereJsonP0211 = {};     //记录查询时的条件
var sortfieldP0211 = "";    //排序字段
var orderbyP0211 = "";      //排序顺序
var strRecordJson = "";
/*↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓**事件方法 开始**↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓*/

//页面加载方法
function pageLoad_P0211() {
    //查询类型
    loadSearchType_P0211();
    //选择日期范围
    loadDateExtent_P0211();
    //加载客户信息
    loadProviderData_P0211();
    //药品下拉框信息的加载
    loadDrugData_P0211();
    //职员信息
    loadZyData_P0211();

    //加载数据
    searchDataClick_P0211();
    //createTable_P0211();
//    resizeGrid(strTableP0211, true, false);
//    resizeGridScroll(strTableP0211);
}

function searchDataClick_P0211(strFlag) {
    try {
        var objConfig = {};
        var objResult = {};
        //combobox的key不存在，赋值空
        if ($("#cmbCustomerP0211").combobox("getValue") == undefined) {
            $("#cmbCustomerP0211").combobox("setValue", "");
        }
        if ($("#cmbDrugP0211").combobox("getValue") == undefined) {
            $("#cmbDrugP0211").combobox("setValue", "");
        }
        if ($("#cmbZyP0211").combobox("getValue") == undefined) {
            $("#cmbZyP0211").combobox("setValue", "");
        }
        //清空选择行信息
        intSelRowIDP0211 = ""
        objSelRowP0211 = {};
        //查询条件的取得
        var strWhereJson = "";
        if (strFlag != "1") {
            strWhereJson = "{\"sdate\":\"" + $("#dateStartTimeP0211").val() + "\",";
            strWhereJson += "\"edate\":\"" + $("#dateEndTimeP0211").val() + "\",";
            strWhereJson += "\"khdm\":\"" + $("#cmbCustomerP0211").combobox("getValue") + "\",";
            strWhereJson += "\"spdm\":\"" + $("#cmbDrugP0211").combobox("getValue") + "\",";
            strWhereJson += "\"zydm\":\"" + $("#cmbZyP0211").combobox("getValue") + "\",";
            strWhereJson += "\"bhth\":\"" + ($("#cheIsGoodsRejected").attr("checked") == true ? "true" : "false") + "\"}";
            strRecordJson = strWhereJson;
            //记录查询条件
            objWhereJsonP0211 = ParseData(strWhereJson.replace("}", ",\"dname\":\"" + $("#cmbDateExtentP0211").combobox("getText") + "\",\"khmc\":\"" + $("#cmbCustomerP0211").combobox("getText") + "\"}"));
        }
        objConfig = ParseData(window.external.DoOptions(strTableP0211, "GetTableConfig", "{}"));
        setColMedolFormatter(objConfig.colModel);
        if (strTableP0211 == "Tp0211") {
            var strTreeJson = strRecordJson.replace("}", ",\"sortfield\":\"" + sortfieldP0211 + "\",\"orderby\":\"" + orderbyP0211 + "\"}");
            objResult = ParseData(window.external.DoOptions(strTableP0211, "GetList", strTreeJson));
            if (strFlag=="0"||strFlag=="1") {
                loadTreeData(strTableP0211, objResult.Records);
            }
            else {
                $("#" + strTableP0211).GridUnload();
                createTreeGrid_P0211(strTableP0211, objConfig.colNames, objConfig.colModel, [], "khmc", viewDetail_P0211);
                resizeGrid(strTableP0211, true, false);
                //表格事件的添加
                addGridEvent_P0211(strTableP0211);
                loadTreeData(strTableP0211, objResult.Records);
            }
            sortfieldP0211 = "";
            orderbyP0211 = "";
        } else {
            objResult = ParseData(window.external.DoOptions(strTableP0211, "GetList", strWhereJson));
            $("#" + strTableP0211).GridUnload();
            createGrid(strTableP0211, objConfig.colNames, objConfig.colModel, objResult.Records, null, gridComplete_P0211, true, viewDetail_P0211);
            //表格事件的添加
            addGridEvent_P0211(strTableP0211);
            resizeGrid(strTableP0211, true, false);
        }
        resizeGridScroll(strTableP0211);
        objConfig = {};
        objResult = {};
        strWhereJson = "";
       
    } catch (e) {
        proceError(strTableP0211, "");
    }
}

function setColMedolFormatter(objColModel) {
    for (var i = 0; i < objColModel.length; i++) {

        if (objColModel[i].name == "sl") {
            objColModel[i].formatter = formatterP0211Count;
        }
        if (objColModel[i].name == "je" || objColModel[i].name == "dj" || objColModel[i].name == "cbje" || objColModel[i].name == "ml"
                || objColModel[i].name == "jj" || objColModel[i].name == "cbjj") {
            objColModel[i].formatter = formatterP0211Money;
        }
        if (objColModel[i].name == "szbl" || objColModel[i].name == "mll") {
            objColModel[i].formatter = formatterP0211Percent;
        }
        if (objColModel[i].name == "xh") {
            objColModel[i].hidden = true;
        }
    }
}

//树grid
function createTreeGrid_P0211(strTableID, objColNames, objColModel, objJsonDatas, strColExpandKey, funcDbClick) {
    //当数据集为空时，格式化数据集的空值
    if (objJsonDatas == "{}") {
        objJsonDatas = [];
    }
    //创建表格
    $("#" + strTableID).jqGrid({
        datatype: "local",
        colNames: objColNames,
        colModel: objColModel,
        data: objJsonDatas[0],
        treeGrid: true,
        treedatatype: "local",
        treeGridModel: 'adjacency',
        ExpandColumn: strColExpandKey,
        ExpandColClick: true,
        editable: true,
        footerrow: true,
        rownumbers: false,
        rowNum: 999999,
        sortname: "khdm",
        localReader: { id: "bh" },
        autowidth: true,
        //height: "100%",
        ondblClickRow: function (selRowId) {
            if (funcDbClick) {
                funcDbClick.call(this, selRowId);
            }
        }
    }); //.parent(".ui-jqgrid-bdiv").siblings().css("width", "100%");
}

//日期范围修改方法
function dateTimeChange_P0211() {
    if ($("#cmbDateExtentP0211").combobox("getText") != "自定义") {
        $("#cmbDateExtentP0211").combobox("setValue", "");
        $("#cmbDateExtentP0211").combobox("setText", "自定义");
    }
}

//查看明细按钮方法
function viewDetail_P0211() {
    try {
        if (intSelRowIDP0211 > 0) {
            //改变标签的Name 
            $(window.parent.document).find("#span_211").text("销售明细");
            //选择的日期
            $(window.parent.document).find("#idIframe-21103").attr("dname", objWhereJsonP0211.dname);
            $(window.parent.document).find("#idIframe-21103").attr("sdate", objWhereJsonP0211.sdate);
            $(window.parent.document).find("#idIframe-21103").attr("edate", objWhereJsonP0211.edate);
            //是否统计退货
            $(window.parent.document).find("#idIframe-21103").attr("bhth", ($("#cheIsGoodsRejected").attr("checked") == true ? "true" : "false"));
            //客户代码
            $(window.parent.document).find("#idIframe-21103").attr("khdm", objSelRowP0211.khdm ? objSelRowP0211.khdm : objWhereJsonP0211.khdm);
            $(window.parent.document).find("#idIframe-21103").attr("khmc", objSelRowP0211.khdm ? objSelRowP0211.khmc : objWhereJsonP0211.khmc);
            //药品代码
            $(window.parent.document).find("#idIframe-21103").attr("spdm", objSelRowP0211.spdm ? objSelRowP0211.spdm : objWhereJsonP0211.spdm);
            $(window.parent.document).find("#idIframe-21103").attr("zydm", objSelRowP0211.zydm ? objSelRowP0211.zydm : objWhereJsonP0211.zydm);
            //改变标签Iframe的Src
            $(window.parent.document).find("#idIframe-211").css("display", "none"); //隐藏统计页面
            $(window.parent.document).find("#idIframe-21103").css("display", ""); //显示明页面
            $(window.parent.document).find("#idIframe-21103").attr("src", "02/p021103.htm");
            $(window.parent.document).find("#idIframe-21103").load(function () {
                var iHeight = window.parent.document.documentElement.clientHeight - window.parent._iFrmHeight;
                $(this).height(iHeight);
            });
        }
        else {
            window.parent.cihoAlert(msg.other.selDataRow);
        }
    } catch (e) {
        proceError(strTableP0211, "");
    }
}

/*↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑**事件方法 结束**↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑*/
/*↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓**自定义方法 开始**↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓*/

//查询类型
function loadSearchType_P0211() {
    $("#cbmSearchTypeP0211").combobox({
        width: _iWidth80,
        listWidth: _iWidth80,
        valueField: "dm",
        textField: "mc",
        editable: false,
        panelHeight: 62,
        onSelect: function (option) {
            selectSearchType_P0211(option.dm);
        },
        enterDown: function () {
            selectSearchType_P0211($(this).combobox("getValue"));
        }
    });
    //加载数据
    $("#cbmSearchTypeP0211").combobox("loadData", [{ dm: "按客户", mc: "按客户", selected: true }, { dm: "按药品", mc: "按药品" }, { dm: "按销售员", mc: "按销售员"}]);
}

//选择日期范围
function loadDateExtent_P0211() {
    //设置默认日期为本月
    dateExtentCalculation("本月", "dateStartTimeP0211", "dateEndTimeP0211");

    //添加选择事件
    $("#cmbDateExtentP0211").combobox({
        width: _iWidth80,
        listWidth: _iWidth80,
        valueField: "dm",
        textField: "mc",
        editable: false,
        panelHeight: 122,
        onSelect: function (option) {
            dateExtentCalculation(option.dm, "dateStartTimeP0211", "dateEndTimeP0211");
        },
        enterDown: function () {            
            dateExtentCalculation($(this).combobox("getValue"), "dateStartTimeP0211", "dateEndTimeP0211");
            //searchDataClick_P0211();
        }
    });
    //加载数据
    $("#cmbDateExtentP0211").combobox("loadData", [{ dm: "今天", mc: "今天" },
        { dm: "昨天", mc: "昨天" },
        { dm: "本月", mc: "本月", selected: true },
        { dm: "上一月", mc: "上一月" },
        { dm: "本年", mc: "本年" }, 
        { dm: "上一年", mc: "上一年"}]);
}

//供应下拉框信息的加载
function loadProviderData_P0211() {
    $("#cmbCustomerP0211").combobox({
        width: _iWidth100,
        listWidth: _iWidth100,
        valueField: "dm",
        textField: "mc",
        onblur: function () {
            if ($("#cmbCustomerP0211").combobox("getText") == "") {
                $("#cmbCustomerP0211").combobox("setValue", "");
            }
        },
        onfocus: function () {
            if ($("#cmbCustomerP0211").combobox("getText") == "全部客户") {
                $("#cmbCustomerP0211").combobox("setText", "");
            }
        },
        enterDown: function () {
            //searchDataClick_P0211();
        },
        afterChange: function (newValue, newText) {
            $(this).combobox("setText", newText.replace(/&nbsp;/g, ""));
        }
    });

    //加载数据
    var objProvider = getComboboxData('Tkhxx2');
    if (objProvider == "{}") {
        objProvider = [];
    }
    objProvider.unshift({ "dm": "", "mc": "全部客户", "selected": true });
    $("#cmbCustomerP0211").combobox("loadData", objProvider);
    objProvider = {};
}

//药品下拉框信息的加载
function loadDrugData_P0211() {
    $("#cmbDrugP0211").combobox({
        width: _iWidth100,
        listWidth: _iWidth100,
        valueField: "dm",
        textField: "mc",
        onblur: function () {
            if ($("#cmbDrugP0211").combobox("getText") == "") {
                $("#cmbDrugP0211").combobox("setValue", "");
            }
        },
        onfocus: function () {
            if ($("#cmbDrugP0211").combobox("getText") == "全部药品") {
                $("#cmbDrugP0211").combobox("setText", "");
            }
        },
        enterDown: function () {
            //searchDataClick_P0211(); 
        }
    });

    //加载数据
    var objDrug = getComboboxData('Tspxx2');
    if (objDrug == "{}") {
        objDrug = [];
    }
    objDrug.unshift({ "dm": "", "mc": "全部药品", "selected": true });
    $("#cmbDrugP0211").combobox("loadData", objDrug);
    objDrug = {};
}

//职员信息加载
function loadZyData_P0211() {
    $("#cmbZyP0211").combobox({
        width: _iWidth100,
        listWidth: _iWidth100,
        valueField: "dm",
        textField: "mc",
        onblur: function () {
            if ($("#cmbZyP0211").combobox("getText") == "") {
                $("#cmbZyP0211").combobox("setValue", "");
            }
        },
        onfocus: function () {
            if ($("#cmbZyP0211").combobox("getText") == "全部销售员") {
                $("#cmbZyP0211").combobox("setText", "");
            }
        },
        enterDown: function () {
            //searchDataClick_P0211(); 
        }
    });

    //加载数据
    var objZy = getComboboxData('TZyxx2');
    if (objZy == "{}") {
        objZy = [];
    }
    objZy.unshift({ "dm": "", "mc": "全部销售员", "selected": true });
    objZy.push({ "dm": "-1", "mc": "其他销售员" });
    $("#cmbZyP0211").combobox("loadData", objZy);
    objZy = {};
}

//表格事件的添加
function addGridEvent_P0211(strTableID) {
    $("#" + strTableID).setGridParam({
        footerrow: true,
        onSelectRow: function (rowId) {//当前选择行数据取得
            intSelRowIDP0211 = rowId;
            objSelRowP0211 = $(this).getRowData(rowId);
            if (strTableID == "Tp0211") {
                resizeGridScroll(strTableID);
            }
        },
        gridComplete: function () {
            gridComplete_P0211();
        },
        onSortCol: function (index, colindex, sortorder) {
            if (strTableID == "Tp0211") {
                sortfieldP0211 = index;
                orderbyP0211 = sortorder;
                searchDataClick_P0211("1");
                return "stop";
            }
        }
    });
}

//合计
function gridComplete_P0211() {
    //设置合计行的数据
    var intGoodsMoney = parseFloat($("#" + strTableP0211).getCol("sl", false, "sum")).toFixed(window.parent.strCountPlace);
    var intMoney = parseFloat($("#" + strTableP0211).getCol("je", false, "sum")).toFixed(window.parent.strMoneyPlace);
    var intCbje = parseFloat($("#" + strTableP0211).getCol("cbje", false, "sum")).toFixed(window.parent.strMoneyPlace);
    var intMl = parseFloat($("#" + strTableP0211).getCol("ml", false, "sum")).toFixed(window.parent.strMoneyPlace);
    var strOccupyPercent = $("#" + strTableP0211).getCol("szbl", false, "sum").toString()+"%";
    if (strOccupyPercent != "0%") {
        strOccupyPercent = 1;
    }
    if (strTableP0211 == "Tp0211") {
        var objTable = $("#" + strTableP0211);
        var arrIds = objTable.getDataIDs();
        var strJehj = 0;
        var strCbje = 0;
        var strMl = 0;
        var intSl = 0;
        for (var i = 0; i < arrIds.length; i++) {
            if (objTable.getCell(arrIds[i], "isLeaf") == "true") {
                intSl += parseFloat(objTable.getCell(arrIds[i], "sl"));
                strJehj += parseFloat(objTable.getCell(arrIds[i], "je"));
                strCbje += parseFloat(objTable.getCell(arrIds[i], "cbje"));
                strMl += parseFloat(objTable.getCell(arrIds[i], "ml"));
            }
        }
        if (arrIds.length == 0) {
            $("#" + strTableP0211).footerData("set", { khmc: "合计", sl:"&nbsp;",je: "&nbsp;", cbje: "&nbsp;", ml: "&nbsp;" }, false);
        }
        else {
            $("#" + strTableP0211).footerData("set", { khmc: "合计", sl:intSl,je: strJehj, cbje: strCbje, ml: strMl }, true);
        }
    }
    else {
        var arrIDLens = $("#" + strTableP0211).getDataIDs();
        if (arrIDLens.length == 0) {    //"&nbsp;"
            $("#" + strTableP0211).footerData("set", { rn: "合计", sl: "&nbsp;", je: "&nbsp;", cbje: "&nbsp;", ml: "&nbsp;" }, false);
        }
        else {
            $("#" + strTableP0211).footerData("set", { rn: "合计", sl: intGoodsMoney, je: intMoney, cbje: intCbje, ml: intMl }, true);
        }
    }
    intGoodsMoney = null;
    intMoney = null;
    strOccupyPercent = null;
    strJehj = null;
    strCbje = null;
    strMl = null;
    objTable = null;
    arrIds = null;
    intCbje = null;
    intMl = null;
    arrIDLens = null;
    intSl = null;
}

//sl格式化Count
function formatterP0211Count(cellvalue, options, rowObject) {
    if (isNaN(parseFloat(cellvalue))) {
        cellvalue = "&nbsp;";
    }
    return parseFloat(cellvalue).toFixed(window.parent.strCountPlace);
}

//dj,je格式化money
function formatterP0211Money(cellvalue, options, rowObject) {
    if (isNaN(parseFloat(cellvalue))) {
        cellvalue = "&nbsp;";
    }
    return parseFloat(cellvalue).toFixed(window.parent.strMoneyPlace);
}

//szbl格式化
function formatterP0211Percent(cellvalue, options, rowObject) {
    if (isNaN(parseFloat(cellvalue))) {
        cellvalue = "0";
    }
    return (parseFloat(cellvalue) * 100).toFixed(window.parent.strPercentPlace) + "%";
}

//查询类型的选择事件的方法
function selectSearchType_P0211(strValue) {
    try {
        //设置选择的查询类型
        if (strValue == "按客户") {
            strTableP0211 = "Tp0211";
            $("#divTp0211").css("display", "");
            $("#divTp021101").css("display", "none");
            $("#divTp021102").css("display", "none");
        }
        else if (strValue == "按药品") {
            strTableP0211 = "Tp021101";
            $("#divTp0211").css("display", "none");
            $("#divTp021101").css("display", "");
            $("#divTp021102").css("display", "none");
        }
        else if (strValue == "按销售员") {
            strTableP0211 = "Tp021102";
            $("#divTp0211").css("display", "none");
            $("#divTp021101").css("display", "none");
            $("#divTp021102").css("display", "");
        }

        /*--------设置默认值--------*/
        objSelRowP0211 = {};
        intSelRowIDP0211 = "";
        //加载数据
        searchDataClick_P0211();
        resizeGrid(strTableP0211, true, false);
        resizeGridScroll(strTableP0211);
        //createTable_P0211();
    } catch (e) {
        proceError(strTableP0211, "");
    }
}

//导出数据的方法
function toExcelMethod() {
    makeExportData(strTableP0211, "销售统计");
}
///*↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑**自定义方法 结束**↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑*/