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

//页面全局变量的定义
var strTableP0111 = "Tp0111";   //表格ID
var strSelRowIDP0111 = "";      //选择行的ID
var objSelRowP0111 = {};        //选择行的数据
var objWhereJsonP0111 = {};     //记录查询时的条件


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

//页面加载方法
function pageLoad_P0111() {
    try {
        //查询类型
        loadSearchType_P0111();
        //选择日期范围
        loadDateExtent_P0111();
        //加载供应商信息
        loadProviderData_P0111();
        //药品下拉框信息的加载
        loadDrugData_P0111();
        //加载数据
        searchDataClick_P0111();
    } catch (e) { }
}

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

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

        //查询条件的取得
        strWhereJson = "{\"sdate\":\"" + $("#dateStartTimeP0111").val() + "\",";
        strWhereJson += "\"edate\":\"" + $("#dateEndTimeP0111").val() + "\",";
        strWhereJson += "\"gysdm\":\"" + $("#cmbProviderP0111").combobox("getValue") + "\",";
        strWhereJson += "\"spdm\":\"" + $("#cmbDrugP0111").combobox("getValue") + "\",";
        strWhereJson += "\"istuihuo\":\"" + ($("#cheIsGoodsRejected").attr("checked") == true ? "1" : "0") + "\"}";
        //记录查询条件
        objWhereJsonP0111 = ParseData(strWhereJson.replace("}", ",\"dname\":\"" + $("#cmbDateExtentP0111").combobox("getText") + "\",\"gysmc\": \"" + $("#cmbProviderP0111").combobox("getText") + "\",\"spmc\": \"" + $("#cmbDrugP0111").combobox("getText") + "\"}"));

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

        if (objDataSet) {
            if (objDataSet.Records.length > 0) {
                $("#" + strTableP0111).footerData("set", { rn: "合计", je: $("#" + strTableP0111).getCol("je", false, "sum") }, true);
            }
            else {
                $("#" + strTableP0111).footerData("set", { rn: "合计", je: "&nbsp;" }, false);
            }
            //清空选择行信息
            strSelRowIDP0111 = "";
            objSelRowP0111 = {};
        }

        //释放内存
        objConfig = null;     //列配置信息
        objDataSet = null;    //数据源
        strWhereJson = null;  //查询条件
    } catch (e) { }
}

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

//查看明细按钮方法
function viewDetail_P0111() {
    if (strSelRowIDP0111 != "") {
        //改变标签的Name 
        $(window.parent.document).find("#span_111").text("采购明细");
        //选择的日期
        $(window.parent.document).find("#idIframe-11102").attr("dname", objWhereJsonP0111.dname);
        $(window.parent.document).find("#idIframe-11102").attr("sdate", objWhereJsonP0111.sdate);
        $(window.parent.document).find("#idIframe-11102").attr("edate", objWhereJsonP0111.edate);
        //是否统计退货
        $(window.parent.document).find("#idIframe-11102").attr("istuihuo", objWhereJsonP0111.istuihuo);
        //供应商代码
        $(window.parent.document).find("#idIframe-11102").attr("gysdm", objSelRowP0111.gysdm ? objSelRowP0111.gysdm : objWhereJsonP0111.gysdm);
        $(window.parent.document).find("#idIframe-11102").attr("gysmc", objSelRowP0111.gysdm ? objSelRowP0111.gysmc : objWhereJsonP0111.gysmc);
        //药品代码
        $(window.parent.document).find("#idIframe-11102").attr("spdm", objSelRowP0111.spdm ? objSelRowP0111.spdm : objWhereJsonP0111.spdm);
        $(window.parent.document).find("#idIframe-11102").attr("spmc", objSelRowP0111.spdm ? objSelRowP0111.spmc : objWhereJsonP0111.spmc);
        //改变标签Iframe的Src
        $(window.parent.document).find("#idIframe-111").css("display", "none"); //隐藏统计页面
        $(window.parent.document).find("#idIframe-11102").css("display", ""); //显示明页面
        $(window.parent.document).find("#idIframe-11102").attr("src", "01/p011102.htm");

        $(window.parent.document).find("#idIframe-11102").load(function () {
            var iHeight = window.parent.document.documentElement.clientHeight - window.parent._iFrmHeight;
            $(this).height(iHeight);
        });
    }
    else {
        window.parent.cihoAlert(msg.other.selDataRow);
    }
}

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



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

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

//选择日期范围
function loadDateExtent_P0111() {
    //设置默认日期为本月
    dateExtentCalculation("本月", "dateStartTimeP0111", "dateEndTimeP0111");

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

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

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

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

    //释放内存
    objProvider = null;
}

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

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

    //释放内存
    objDrug = null;
}

//添加列格式化方法
function addColFormat_P0111(objColModel) {
    for (var intI = 0; intI < objColModel.length; intI++) {
        switch (objColModel[intI]["name"]) {
            case "sl":
                objColModel[intI].formatter = function (cellvalue, options, rowObject) {
                    if (cellvalue != "") {
                        return parseFloat(cellvalue).toFixed(window.parent.strCountPlace);
                    }
                    else {
                        return "0";
                    }
                }
                break;
            case "szbl":
                objColModel[intI].formatter = function (cellvalue, options, rowObject) {
                    if (cellvalue != "") {
                        return (parseFloat(cellvalue) * 100).toFixed(window.parent.strPercentPlace) + "%";
                    }
                    else {
                        return "0%";
                    }
                }
                break;
            case "je":
            case "prices":
                objColModel[intI].formatter = function (cellvalue, options, rowObject) {
                    if (cellvalue != "") {
                        return parseFloat(cellvalue).toFixed(window.parent.strMoneyPlace);
                    }
                    else {
                        return "0";
                    }
                }
                break;
        }
    }
}

//统计类型的变更
function searchTypeChange_P0111(strTypeName) {
    //设置选择的查询类型
    if (strTypeName == "按供应商") {
        strTableP0111 = "Tp0111";
        $("#divTp0111").css("display", "");
        $("#divTp011101").css("display", "none");
    }
    else if (strTypeName == "按药品") {
        strTableP0111 = "Tp011101";
        $("#divTp0111").css("display", "none");
        $("#divTp011101").css("display", "");
    }

    //加载数据
    searchDataClick_P0111();
}

//创建表格
function createGrid_P0111(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) {
            strSelRowIDP0111 = rowid;
            objSelRowP0111 = $(this).getRowData(rowid);
        },
        ondblClickRow: function () {
            viewDetail_P0111();
        }
    }).parent(".ui-jqgrid-bdiv").siblings().css("width", "100%");

    //设置行号列的名称
    $("#" + strTableID).setLabel("rn", "序号");

    //调整表格的大小
    resizeGrid(strTableP0111, false, true);
    //resizeTbl(true, strTableID, false, true);

    //有滚动条时改变表格宽度
    resizeGridScroll(strTableP0111);
    //resizeGrid(strTableID);
}

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