/*--------------------------文档信息-------------------------------------------
///
///功能：    采购返利管理-采购返利统计
///创建人：  王思波
///创建时间：2011年06月03日
///修改人：  --------------
///修改时间：--------------
///说明：    --------------
///
-----------------------------------------------------------------------------*/

//页面全局变量的定义
var strTableP0112 = "Tp0112";   //表格ID
var strSelRowIDP0112 = "";       //选择行的ID
var objSelRowP0112 = {};        //选择行的数据
var objWhereJsonP0112 = {};     //记录查询时的条件

/*↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓**事件方法 开始**↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓*/

//页面加载方法
function pageLoad_P0112() {
    try {
        //查询类型
        loadSearchType_P0112();
        //选择日期范围
        loadDateExtent_P0112();
        //供应商信息的加载
        loadProviderData_P0112();
        //药品信息的加载
        loadDrugData_P0112();
        //加载数据
        searchDataClick_P0112();
    } catch (e) { }
}

//查询按钮的方法
function searchDataClick_P0112() {
    try {
        var objConfig = {};     //列配置信息
        var objDataSet = {};    //数据源
        var strWhereJson = "";  //查询条件

        //供应商
        if ($("#cmbProviderP0112").combobox("getValue") == undefined) {
            $("#cmbProviderP0112").combobox("setValue", "");
        }
        //药品
        if ($("#cmbDrugP0112").combobox("getValue") == undefined) {
            $("#cmbDrugP0112").combobox("setValue", "");
        }

        //查询条件的取得
        strWhereJson = "{\"sdate\":\"" + $("#dateStartTimeP0112").val() + "\",";
        strWhereJson += "\"edate\":\"" + $("#dateEndTimeP0112").val() + "\",";
        strWhereJson += "\"gysdm\":\"" + $("#cmbProviderP0112").combobox("getValue") + "\",";
        strWhereJson += "\"spdm\":\"" + $("#cmbDrugP0112").combobox("getValue") + "\"}";
        //记录查询条件
        objWhereJsonP0112 = ParseData(strWhereJson.replace("}", ",\"dname\":\"" + $("#cmbDateExtentP0112").combobox("getText") + "\",\"gysmc\": \"" + $("#cmbProviderP0112").combobox("getText") + "\",\"spmc\": \"" + $("#cmbDrugP0112").combobox("getText") + "\"}"));

        //加载数据
        objConfig = ParseData(window.external.DoOptions(strTableP0112, "GetTableConfig", "{}"));
        objDataSet = ParseData(window.external.DoOptions(strTableP0112, "GetList", strWhereJson));
        //添加列格式化方法
        addColFormat_P0112(objConfig.colModel);
        //数据的加载
        createGrid_P0112(strTableP0112, objConfig.colNames, objConfig.colModel, objDataSet.Records);

        //合计设置
        if (objDataSet) {
            if (objDataSet.Records.length > 0) {
                $("#" + strTableP0112).footerData("set", { rn: "合计", fhje: $("#" + strTableP0112).getCol("fhje", false, "sum"), fxje: $("#" + strTableP0112).getCol("fxje", false, "sum") }, true);
            }
            else {
                $("#" + strTableP0112).footerData("set", { rn: "合计", fhje: "&nbsp;", fxje: "&nbsp;" }, false);
            }
            //清空选择行信息
            strSelRowIDP0112 = "";
            objSelRowP0112 = {};
        }

        //释放内存
        objConfig = null;
        objDataSet = null;
        strWhereJson = null;
    } catch (e) { }
}

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

//查看明细按钮方法
function viewDetail_P0112() {
    if (strSelRowIDP0112 != "") {
        //改变标签的Name 
        $(window.parent.document).find("#span_112").text("返利明细");
        //选择的日期
        $(window.parent.document).find("#idIframe-11202").attr("dname", objWhereJsonP0112.dname);
        $(window.parent.document).find("#idIframe-11202").attr("sdate", objWhereJsonP0112.sdate);
        $(window.parent.document).find("#idIframe-11202").attr("edate", objWhereJsonP0112.edate);
        //供应商代码
        $(window.parent.document).find("#idIframe-11202").attr("gysdm", objSelRowP0112.gysdm ? objSelRowP0112.gysdm : objWhereJsonP0112.gysdm);
        $(window.parent.document).find("#idIframe-11202").attr("gysmc", objSelRowP0112.gysdm ? objSelRowP0112.gysmc : objWhereJsonP0112.gysmc);
        //药品代码
        $(window.parent.document).find("#idIframe-11202").attr("spdm", objSelRowP0112.spdm ? objSelRowP0112.spdm : objWhereJsonP0112.spdm);
        $(window.parent.document).find("#idIframe-11202").attr("spmc", objSelRowP0112.spdm ? objSelRowP0112.spmc : objWhereJsonP0112.spmc);
        //改变标签Iframe的Src
        $(window.parent.document).find("#idIframe-112").css("display", "none");   //隐藏统计页面
        $(window.parent.document).find("#idIframe-11202").css("display", "");     //显示明页面
        $(window.parent.document).find("#idIframe-11202").attr("src", "01/p011202.htm");
        $(window.parent.document).find("#idIframe-11202").load(function () {
            //表格高度
            var iHeight = window.parent.document.documentElement.clientHeight - window.parent._iFrmHeight;
            $(this).height(iHeight);
        });
    }
    else {
        window.parent.cihoAlert(msg.other.selDataRow);
    }
}

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



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

//按供应商、按药品
function loadSearchType_P0112() {
    $("#cbmSearchTypeP0112").combobox({
        width: _iWidth80,
        listWidth: _iWidth80,
        panelHeight: 42,
        valueField: "dm",
        textField: "mc",
        editable: false,
        onSelect: function (option) {
            searchTypeChange_P0112(option.dm);
        },
        enterDown: function () {
            searchTypeChange_P0112($(this).combobox("getValue"));
        }
    });
    //加载数据
    $("#cbmSearchTypeP0112").combobox("loadData", [{ dm: "按供应商", mc: "按供应商", selected: true }, { dm: "按药品", mc: "按药品"}]);
}

//选择日期范围
function loadDateExtent_P0112() {
    //设置默认日期为本月
    dateExtentCalculation("本月", "dateStartTimeP0112", "dateEndTimeP0112");

    //添加选择事件
    $("#cmbDateExtentP0112").combobox({
        width: _iWidth100,
        listWidth: _iWidth100,
        panelHeight: 102,
        valueField: "dm",
        textField: "mc",
        editable: false,
        onSelect: function (option) {
            dateExtentCalculation(option.dm, "dateStartTimeP0112", "dateEndTimeP0112");
        },
        enterDown: function () {
            dateExtentCalculation($(this).combobox("getValue"), "dateStartTimeP0112", "dateEndTimeP0112");
        }
    });

    //加载数据
    $("#cmbDateExtentP0112").combobox("loadData", [{ dm: "本月", mc: "本月", selected: true }, { dm: "上一月", mc: "上一月" }, { dm: "本周", mc: "本周" }, { dm: "过去三个月", mc: "过去三个月" }, { dm: "本年", mc: "本年"}]);
}

//供应下拉框信息的加载
function loadProviderData_P0112() {
    $("#cmbProviderP0112").combobox({
        width: _iWidth150,
        listWidth: _iWidth150,
        valueField: "dm",
        textField: "mc",
        onblur: function () {
            if ($("#cmbProviderP0112").combobox("getText") == "") {
                $("#cmbProviderP0112").combobox("setValue", "");
            }
        },
        onfocus: function () {
            if ($("#cmbProviderP0112").combobox("getText") == "全部供应商") {
                $("#cmbProviderP0112").combobox("setValue", "-1");
                $("#cmbProviderP0112").combobox("setText", "");
            }
        }
    });

    //加载数据
    var objProvider = getComboboxData("Tgysxx2");
    objProvider.unshift({ "dm": "", "mc": "全部供应商", "selected": true });
    $("#cmbProviderP0112").combobox("loadData", objProvider);

    //释放内存
    objProvider = null;
}

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

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

    //释放内存
    objDrug = null;
}

//添加列格式化方法
function addColFormat_P0112(objColModel) {
    for (var intI = 0; intI < objColModel.length; intI++) {
        switch (objColModel[intI]["name"]) {
            case "fhje": //返现金额(按供应、按药品时使用)
            case "fxje": //返货金额(按供应、按药品时使用)
            case "fhjj": //返货均价(按药品时使用)
                objColModel[intI].formatter = function (cellvalue, options, rowObject) {
                    if (!cellvalue) {
                        cellvalue = "0";
                    }
                    return parseFloat(cellvalue).toFixed(window.parent.strMoneyPlace);
                }
                break;
            case "fhsl": //返货数量(按药品时使用)
                objColModel[intI].formatter = function (cellvalue, options, rowObject) {
                    if (!cellvalue) {
                        cellvalue = "0";
                    }
                    return parseFloat(cellvalue).toFixed(window.parent.strCountPlace);
                }
                break;
        }
    }
}

//统计类型的变更
function searchTypeChange_P0112(strTypeName) {
    if (strTypeName == "按供应商") {
        strTableP0112 = "Tp0112";
        $("#divTp0112").css("display", "");
        $("#divTp011201").css("display", "none");
    }
    else if (strTypeName == "按药品") {
        strTableP0112 = "Tp011201";
        $("#divTp0112").css("display", "none");
        $("#divTp011201").css("display", "");
    }

    //加载数据
    searchDataClick_P0112();
}

//创建表格
function createGrid_P0112(strTableID, objColNames, objColModel, objDataRecords) {
    //删除旧表格
    $("#" + strTableID).GridUnload();
    //生成表格
    $("#" + 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) {
            strSelRowIDP0112 = rowid;
            objSelRowP0112 = $(this).getRowData(rowid);
        },
        ondblClickRow: function () {
            viewDetail_P0112();
        }
    }).parent(".ui-jqgrid-bdiv").siblings().css("width", "100%");

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

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