/*--------------------------文档信息-------------------------------------------
///功能：      销售回款统计
///创建人：  王群
///创建时间：2011年05月31日
///修改人：  --------------
///修改时间：--------------
///详细说明：--------------
-----------------------------------------------------------------------------*/

//页面全局变量的定义
var strTableP0213 = "Tp0213";    //表格ID
var objSelRowP0213 = {};           //选择行的数据
var intSelRowIDP0213 = 0;         //选择行的ID
var objWhereJsonP0213 = {};     //记录查询时的条件
var sortfieldP0213 = "";             //记录一下默认排序字段
var orderbyP0213 = "";             //记录一下默认的排序方式
var strRecordWhereJson = "";   //记录一下查询条件

/*↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓**事件方法 开始**↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓*/
function pageLoad_P0213() {
    try {
        //查询类型
        loadSearchType_P0213();
        //选择日期范围
        loadDateExtent_P0213();
        //加载供应商信息
        loadProviderData_P0213();
        //药品下拉框信息的加载
        loadDrugData_P0213();
        //销售人员的下拉框信息的加载
        loadXsyData_P0213();
        //加载数据
        searchDataClick_P0213();
    } catch (e) { }
}

//查询按钮的方法
function searchDataClick_P0213(flag) {
    try {
        var strWhereJson = "";          //用于存放查询条件的变量
        var objConfig = {};               //用于接收从后台传的列配置
        var objResult = {};               //用于存放查询的结果

        if (!$("#cmbProviderP0213").combobox("getValue")) {      //当前选择的客户不存在，就给赋值为【全部客户】           
            $("#cmbProviderP0213").combobox("setValue", "");
        }

        if (!$("#cmbDrugP0213").combobox("getValue")) {     // "当前选择的药品不存在，就给赋值为【全部药品】"
            $("#cmbDrugP0213").combobox("setValue", "");
        }
        if (!$("#cmbXsyP0213").combobox("getValue")) {       // "当前选择的药品不存在，就给赋值为【全部销售员】"
            $("#cmbXsyP0213").combobox("setValue", "");
        }
        //清空选择行信息
        intSelRowIDP0213 = 0;
        objSelRowP0213 = {};
        //查询条件的取得
        if (flag == "1") {
        }
        else {
            strWhereJson = "{\"sdate\":\"" + $("#dateStartTimeP0213").val() + "\",";
            strWhereJson += "\"edate\":\"" + $("#dateEndTimeP0213").val() + "\",";
            strWhereJson += "\"khdm\":\"" + $("#cmbProviderP0213").combobox("getValue") + "\",";
            strWhereJson += "\"spdm\":\"" + $("#cmbDrugP0213").combobox("getValue") + "\",";
            strWhereJson += "\"zydm\":\"" + $("#cmbXsyP0213").combobox("getValue") + "\"}";
            strRecordWhereJson = strWhereJson;                              //记录一下查询条件
            //记录查询条件
            objWhereJsonP0213 = ParseData(strWhereJson.replace("}",
        ",\"dname\":\"" + $("#cmbDateExtentP0213").combobox("getValue") + "\",\"dname1\":\"" + $("#cmbDateExtentP0213").combobox("getText") + "\",\"khmc\":\"" + $("#cmbProviderP0213").combobox("getText") + "\",\"spmc\":\"" + $("#cmbDrugP0213").combobox("getText") + "\",\"zymc\":\"" + $("#cmbXsyP0213").combobox("getText") + "\"}"));
        }

        objConfig = ParseData(window.external.DoOptions(strTableP0213, "GetTableConfig", "{}"));
        //格式化表格中的数据
        setData_P0213(objConfig.colModel);

        if ($("#cbmSearchTypeP0213").combobox("getValue") == "按客户") {
            if (flag != "1") {
                sortfieldP0213 = "";
                orderbyP0213 = "";
            }
            var strCollateWhereJson = strRecordWhereJson.replace("}", ",\"sortfield\":\"" + sortfieldP0213 + "\",\"orderby\":\"" + orderbyP0213 + "\"}");
            //创建表格
            objResult = ParseData(window.external.DoOptions(strTableP0213, "GetList", strCollateWhereJson));
            if (flag == "1" || flag == "2") {
                loadTreeData(strTableP0213, objResult.Records);
            }
            else {
                createTreeGrid_P0213(strTableP0213, objConfig.colNames, objConfig.colModel, objResult.Records, "khmc");
                //调整表格的大小
                resizeGrid(strTableP0213, true, false);
                loadTreeData(strTableP0213, objResult.Records);
            }
        }
        else {
            //创建表格
            objResult = ParseData(window.external.DoOptions(strTableP0213, "GetList", strWhereJson));
            createGrid_P0213(strTableP0213, objConfig.colNames, objConfig.colModel, objResult.Records);
        }
        //有滚动条时改变表格宽度
        resizeGridScroll(strTableP0213);
        //释放内存
        strCollateWhereJson = null;
        strWhereJson = null;
        objConfig = null;
        objResult = null;
    } catch (e) { }
}

//查看明细按钮事件
function viewDetail_P0213() {
    if (intSelRowIDP0213 > 0) {
        //改变标签的Name 
        $(window.parent.document).find("#span_213").text("回款明细");
        //选择的日期
        $(window.parent.document).find("#idIframe-21303").attr("dname", objWhereJsonP0213.dname);
        $(window.parent.document).find("#idIframe-21303").attr("sdate", objWhereJsonP0213.sdate);
        $(window.parent.document).find("#idIframe-21303").attr("edate", objWhereJsonP0213.edate);

        //客户
        $(window.parent.document).find("#idIframe-21303").attr("khdm", objSelRowP0213.khdm ? objSelRowP0213.khdm : objWhereJsonP0213.khdm);
        $(window.parent.document).find("#idIframe-21303").attr("khmc", objSelRowP0213.khdm ? objSelRowP0213.khmc : objWhereJsonP0213.khmc);

        //药品
        $(window.parent.document).find("#idIframe-21303").attr("spdm", objSelRowP0213.spdm ? objSelRowP0213.spdm : objWhereJsonP0213.spdm);
        $(window.parent.document).find("#idIframe-21303").attr("spmc", objSelRowP0213.spdm ? objSelRowP0213.spmc : objWhereJsonP0213.spmc);

        //销售人员代码
        $(window.parent.document).find("#idIframe-21303").attr("xsydm", objSelRowP0213.xsydm ? objSelRowP0213.xsydm : objWhereJsonP0213.zydm);
        $(window.parent.document).find("#idIframe-21303").attr("xsymc", objSelRowP0213.xsydm ? objSelRowP0213.zymc : objWhereJsonP0213.zymc);

        //改变标签Iframe的Src
        $(window.parent.document).find("#idIframe-213").css("display", "none");     //隐藏统计页面
        $(window.parent.document).find("#idIframe-21303").css("display", "");          //显示明页面
        $(window.parent.document).find("#idIframe-21303").attr("src", "02/p021303.htm");

        //调整iframe的高度
        $(window.parent.document).find("#idIframe-21303").load(function () {
            var iHeight = window.parent.document.documentElement.clientHeight - window.parent._iFrmHeight;
            $(this).height(iHeight);
        });
    }
    else {
        window.parent.cihoAlert(msg.other.selDataRow);     //"请选择数据！"
    }
}

/*↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓**事件方法 结束**↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓*/

/*↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓**自定义方法 开始**↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓*/

//格式化表中的数据
function setData_P0213(objP0213) {
    for (var i = 0; i < objP0213.length; i++) {
        if (objP0213[i].name == "hkje" || objP0213[i].name == "je") {   //给【回款金额】格式化
            objP0213[i].formatter = formatterP0213Money;
        }

        if (objP0213[i].name == "szbl") {   //给【所占比例】格式化
            objP0213[i].formatter = formatterP0213Percent;
        }
        if (objP0213[i].name == "xh") {     //前台隐藏序号列
            objP0213[i].hidden = true;
        }
    }
}

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

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

//选择日期范围
function loadDateExtent_P0213() {
    //设置默认日期为本月
    dateExtentCalculation("本月", "dateStartTimeP0213", "dateEndTimeP0213");

    //添加选择事件
    $("#cmbDateExtentP0213").combobox({
        width: _iWidth100,
        listWidth: _iWidth100,
        valueField: "dm",
        textField: "mc",
        panelHeight: 102,
        editable: false,
        onSelect: function (option) {
            dateExtentCalculation(option.dm, "dateStartTimeP0213", "dateEndTimeP0213");
        },
        enterDown: function () {
            dateExtentCalculation($("#cmbDateExtentP0213").combobox("getValue"), "dateStartTimeP0213", "dateEndTimeP0213");
        }
    });
    //加载数据
    $("#cmbDateExtentP0213").combobox("loadData",
     [{ dm: "本月", mc: "本月", selected: true },
      { dm: "上一月", mc: "上一月" },
      { dm: "本周", mc: "本周" },
      { dm: "过去三个月", mc: "过去三个月" },
      { dm: "本年", mc: "本年"}]);
}

//客户下拉框信息的加载
function loadProviderData_P0213() {
    $("#cmbProviderP0213").combobox({
        width: _iWidth120,
        listWidth: _iWidth120,
        valueField: "dm",
        textField: "mc",
        onblur: function () {
            if ($("#cmbProviderP0213").combobox("getText") == "") {
                $("#cmbProviderP0213").combobox("setValue", "");
            }
        },
        onfocus: function () {
            if ($("#cmbProviderP0213").combobox("getText") == "全部客户") {
                $("#cmbProviderP0213").combobox("setText", "");
            }
        },
        afterChange: function (newValue, newText) {
            $(this).combobox("setText", newText.replace(/&nbsp;/g, ""));
        }
    });
    //加载数据
    var objProvider = getComboboxData("Tkhxx2");
    if (objProvider == "{}") {
        objProvider = [];
    }
    objProvider.unshift({ "dm": "", "mc": "全部客户", "selected": true });
    $("#cmbProviderP0213").combobox("loadData", objProvider);
    //释放内存
    objProvider = null;
}

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

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

//销售员下拉框信息的加载
function loadXsyData_P0213() {
    $("#cmbXsyP0213").combobox({
        width: _iWidth120,
        listWidth: _iWidth120,
        valueField: "dm",
        textField: "mc",
        onblur: function () {
            if ($("#cmbXsyP0213").combobox("getText") == "") {
                $("#cmbXsyP0213").combobox("setValue", "");
            }
        },
        onfocus: function () {
            if ($("#cmbXsyP0213").combobox("getText") == "全部销售员") {
                $("#cmbXsyP0213").combobox("setText", "");
            }
        }
    });

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

//普通表格合计行的计算
function totalCompute_P0213() {
    //设置合计行的数据
    var intMoney = parseFloat($("#" + strTableP0213).getCol("je", false, "sum")).toFixed(window.parent.strMoneyPlace);
    var intMoney1 = parseFloat($("#" + strTableP0213).getCol("hkje", false, "sum")).toFixed(window.parent.strMoneyPlace);
    var intarrLength = $("#" + strTableP0213).getDataIDs();
    if ($("#cbmSearchTypeP0213").combobox("getValue") == "按药品") {   //按药品的情况
        if (intarrLength.length > 0) {                         //表格中有记录时         
            $("#" + strTableP0213).footerData("set", { rn: "合计", hkje: intMoney1 }, true);
        }
        else {                                                             //表格中没有记录时
            $("#" + strTableP0213).footerData("set", { rn: "合计", hkje: "&nbsp;" }, false);
        }
    }
    else {
        if (intarrLength.length > 0) {                          //表格中有记录时
            $("#" + strTableP0213).footerData("set", { rn: "合计", je: intMoney }, true);
        }
        else {                                                              //表格中没有记录时
            $("#" + strTableP0213).footerData("set", { rn: "合计", je: "&nbsp;" }, false);
        }
    }
    //释放内存
    intMoney = null;
    strOccupyPercent = null;
    intarrLength = null;
}

//树的合计
function totalCompute_P0213p() {
    //设置合计行的数据
    $("#" + strTableP0213).setGridParam({
        gridComplete: function () {
            var objTable = $("#" + strTableP0213);   //定义一个表格的对象
            var arrIds = objTable.getDataIDs();          //用于取得表格中所有数据的ID
            var intMoney = 0;                                    //初始默认回款金额0
            if (arrIds.length > 0) {                              //表格没有数据的情况下，合计行格式化成空
                for (var i = 0; i < arrIds.length; i++) {
                    if (objTable.getCell(arrIds[i], "parent") == "0") {   //只统计子节点的数据
                        intMoney += parseFloat(objTable.getCell(arrIds[i], "hkje"));
                    }
                }
                $("#" + strTableP0213).footerData("set", { khmc: "合计", hkje: intMoney }, true);
            }
            else {                                                       //表格没有数据的情况下，合计行数据正常格式化
                $("#" + strTableP0213).footerData("set", { khmc: "合计", hkje: "&nbsp;" }, false);
            }
        }
    });
}

//查询类型的选择事件的方法
function selectSearchType_P0213(strValue) {
    //设置选择的查询类型
    if (strValue == "按客户") {        //按客户的页面
        strTableP0213 = "Tp0213";
        $("#divTp0213").css("display", "");
        $("#divTp021301").css("display", "none");
        $("#divTp021302").css("display", "none");
    }
    else if (strValue == "按药品") {    //显示按药品的页面
        strTableP0213 = "Tp021301";
        $("#divTp0213").css("display", "none");
        $("#divTp021301").css("display", "");
        $("#divTp021302").css("display", "none");
    }
    else {                                            //显示按职员的页面
        strTableP0213 = "Tp021302";
        $("#divTp0213").css("display", "none");
        $("#divTp021301").css("display", "none");
        $("#divTp021302").css("display", "");
    }

    /*--------设置默认值--------*/
    objSelRowP0213 = {};
    intSelRowIDP0213 = 0;
    //加载数据
    searchDataClick_P0213();
}

//money格式化
function formatterP0213Money(cellvalue, options, rowObject) {
    if (isNaN(parseFloat(cellvalue))) {
        cellvalue = 0;
    }
    return parseFloat(cellvalue).toFixed(window.parent.strMoneyPlace);
}

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

//当基本资料数据有修改时 刷新相关的combobox
function refreshMyCombobox(strModels) {
    //刷新明细页面中combobox前的准备工作
    if ($(window.parent.document).find('#idIframe-21303').length > 0) {
        $(window.parent.document.getElementById("idIframe-21303").contentWindow.document.getElementById("idRefreshCombobox")).val(strModels[0]);
        $(window.parent.document.getElementById("idIframe-21303").contentWindow.document.getElementById("idRefreshCombobox")).click();
    }
    for (var i = 0; i < strModels.length; i++) {
        var data = getComboboxData(strModels[i] + "2");     //从后台取得数据集
        if (strModels[i] == "Tkhxx") {
            data.unshift({ dm: "", mc: "全部客户", "selected": true });
            reLoadCombobox("cmbProviderP0213", data);
        }
        if (strModels[i] == "Tspxx") {
            data.unshift({ dm: "", mc: "全部药品", "selected": true });
            reLoadCombobox("cmbDrugP0213", data);
        }
        if (strModels[i] == "TZyxx") {
            data.unshift({ dm: "", mc: "全部销售员", "selected": true });
            data.push({ dm: "-1", mc: "其他销售员" });
            reLoadCombobox("cmbXsyP0213", data);
        }
    }
}

/*↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓**自定义方法 结束**↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓*/

/*-------------------------------------------------------------
功能：创建普通表格
*****①strTableID：表格的ID
*****②objColNames:表格需要显示的列名
*****③objColModel：表格的列配置
*****④objDataRecords：表格中需要加载的数据
-----------------------------------------------------------------*/
function createGrid_P0213(strTableID, objColNames, objColModel, objDataRecords) {
    $("#" + strTableID).GridUnload();       //删除旧表格
    if (objDataRecords == "{}") {               //当数据集为空时，格式化数据集的空值
        objDataRecords = [];
    }
    //生成表格
    $("#" + strTableID).jqGrid({
        datatype: "local",
        sortname: "id",
        colNames: objColNames,
        colModel: objColModel,
        data: objDataRecords,
        rownumbers: true,
        autowidth: true,
        footerrow: true,
        gridview: true,
        rownumWidth: 30,
        cellsubmit: "clientArray",
        rowNum: objDataRecords.length,
        onSelectRow: function (rowId) {//当前选择行数据取得
            intSelRowIDP0213 = rowId;
            objSelRowP0213 = $(this).getRowData(rowId);
        },
        ondblClickRow: function () {
            viewDetail_P0213();
        }
    }).parent(".ui-jqgrid-bdiv").siblings().css("width", "100%");

    //设置行号列的名称
    $("#" + strTableID).setLabel("rn", "序号");
    //调整表格的大小
    resizeGrid(strTableP0213, true, false);
    //有滚动条时改变表格宽度
    resizeGridScroll(strTableP0213);
    //合计数据的计算
    totalCompute_P0213();
}

/*------------------------------------------------------------------------
***** ①strGridID:表格ID
*****②objColNames:表格列名
*****③objColModel:表格列属性
*****④objJsonDatas:表格数据源，Json格式
*****⑤strColExpandKey:伸缩列的字段名称
*****⑥funcGridComplete:表格的gridComplete事件方法，此参数类型是方法对象
*****⑦funcSelectRow:表格的onSelectRow事件方法，此参数类型是方法对象
-----------------------------------------------------------------------------*/
function createTreeGrid_P0213(strTableID, objColNames, objColModel, objJsonDatas, strColExpandKey) {

    $("#" + strTableID).GridUnload();           //删除旧表格
    if (objJsonDatas == "{}") {                      //当数据集为空时，格式化数据集的空值
        objJsonDatas = [];
    }
    //创建表格
    $("#" + strTableID).jqGrid({
        datatype: "local",
        colNames: objColNames,
        colModel: objColModel,
        data: objJsonDatas[0],
        treeGrid: true,
        treedatatype: "local",
        treeGridModel: 'adjacency',
        ExpandColumn: strColExpandKey,
        ExpandColClick: false,
        editable: true,
        rownumbers: false,
        rowNum: 999999,
        sortname: "khdm",
        localReader: { id: "bh" },
        ExpandColClick: true,
        autowidth: true,
        footerrow: true,
        onSelectRow: function (rowId) {
            intSelRowIDP0213 = rowId;
            objSelRowP0213 = $(this).getRowData(rowId);
        },
        ondblClickRow: function () {
            viewDetail_P0213();
        },
        onSortCol: function (index, colindex, sortorder) {
            sortfieldP0213 = index;                         //排序字段
            orderbyP0213 = sortorder;                   //排序顺序
            searchDataClick_P0213("1");                     //加载数据
            return "stop";
        }
    }).parent(".ui-jqgrid-bdiv").siblings().css("width", "100%");
    //调整表格的大小
    resizeGrid(strTableP0213, true, false);
    //有滚动条时改变表格宽度
    resizeGridScroll(strTableP0213);
    //设计合计行
    totalCompute_P0213p();
}