/*--------------------------文档信息-------------------------------------------
///功能：      药品出入库统计
///创建人：  王群
///创建时间：2011年05月31日
///修改人：  --------------
///修改时间：--------------
///详细说明：--------------
-----------------------------------------------------------------------------*/
//页面全局变量的定义
var strTableP0312 = "Tp0312";      //表格ID
var objSelRowP0312 = {};              //选择行的数据
var intSelRowIDP0312 = 0;           //选择行的ID
var objWhereJsonP0312 = {};      //记录查询时的条件

/*↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓**事件方法 开始**↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓*/
//页面加载的方法
function pageLoad_P0312() {
    try {
        //选择日期范围
        loadDateExtent_P0312();
        //加载仓库信息
        loadCkData_P0312();
        //加载药品信息
        loadSpData_P0312();
        //加载数据
        searchDataClick_P0312();
    } catch (e) { }
}

//查询按钮的事件
function searchDataClick_P0312() {
    try {
        var strWhereJson = "";      //存放查询条件
        var objConfig = {};              //存放类配置
        var objResult = {};              //存放查询结果

        if (!$("#cmbCkP0312").combobox("getValue")) {    //当前选择的仓库不存在的时候，赋值为【全部仓库】!
            $("#cmbCkP0312").combobox("setValue", "");
        }
        if (!$("#cmbSpP0312").combobox("getValue")) {    // "当前选择的药品不存在时，赋值为【全部药品】"
            $("#cmbSpP0312").combobox("setValue", "");
        }
        //清空选择行信息
        intSelRowIDP0312 = 0;
        objSelRowP0312 = {};
        //查询条件的取得
        strWhereJson = "{\"sdate\":\"" + $("#dateStartTimeP0312").val() + "\",";
        strWhereJson += "\"edate\":\"" + $("#dateEndTimeP0312").val() + "\",";
        strWhereJson += "\"ckdm\":\"" + $("#cmbCkP0312").combobox("getValue") + "\",";
        strWhereJson += "\"spdm\":\"" + $("#cmbSpP0312").combobox("getValue") + "\"}";

        //记录查询条件
        objWhereJsonP0312 = ParseData(strWhereJson.replace("}",
        ",\"dname\":\"" + $("#cmbDateExtentP0312").combobox("getValue") + "\",\"ckmc\":\"" + $("#cmbCkP0312").combobox("getText") + "\",\"spmc\":\"" + $("#cmbSpP0312").combobox("getText") + "\"}"));
        objConfig = ParseData(window.external.DoOptions(strTableP0312, "GetTableConfig", "{}"));

        //格式化表格中的数据
        setData_P0312(objConfig.colModel);
        objResult = ParseData(window.external.DoOptions(strTableP0312, "GetList", strWhereJson));
        //创建表格
        createGrid_P0312(strTableP0312, objConfig.colNames, objConfig.colModel, objResult.Records);

        strWhereJson = null;
        objConfig = null;
        objResult = null;
    } catch (e) { }
}

//查看明细按钮事件
function viewDetail_P0312() {
    if (intSelRowIDP0312 > 0) {
        //改变标签的Name 
        $(window.parent.document).find("#span_312").text("药品出入库明细");

        //选择的日期
        $(window.parent.document).find("#idIframe-31201").attr("dname", objWhereJsonP0312.dname);
        $(window.parent.document).find("#idIframe-31201").attr("sdate", objWhereJsonP0312.sdate);
        $(window.parent.document).find("#idIframe-31201").attr("edate", objWhereJsonP0312.edate);

        //药品代码
        $(window.parent.document).find("#idIframe-31201").attr("spdm", objSelRowP0312.spdm ? objSelRowP0312.spdm : objWhereJsonP0312.spdm);
        $(window.parent.document).find("#idIframe-31201").attr("spmc", objSelRowP0312.spdm ? objSelRowP0312.spmc : objWhereJsonP0312.spmc);

        //仓库代码
        $(window.parent.document).find("#idIframe-31201").attr("ckdm", objSelRowP0312.ckdm ? objSelRowP0312.ckdm : objWhereJsonP0312.ckdm);
        $(window.parent.document).find("#idIframe-31201").attr("ckmc", objSelRowP0312.ckdm ? objSelRowP0312.ckmc : objWhereJsonP0312.ckmc);

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

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


//日期范围修改事件
function dateTimeChange_P0312() {
    if ($("#cmbDateExtentP0312").combobox("getText") != "自定义") {
        $("#cmbDateExtentP0312").combobox("setValue", "");
        $("#cmbDateExtentP0312").combobox("setText", "自定义");
    }
}

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

/*↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓**自定义方法 开始**↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓*/
//格式化表格中的数据
function setData_P0312(objP0312) {
    for (var i = 0; i < objP0312.length; i++) {
        if (objP0312[i].name == "kcsl" || objP0312[i].name == "rksl" || objP0312[i].name == "cksl" || objP0312[i].name == "sysl") {
            //格式化成整数格式
            objP0312[i].formatter = formatterP0312Count;
        }
    }
}


//选择日期范围
function loadDateExtent_P0312() {
    //设置默认日期为本月
    dateExtentCalculation("本月", "dateStartTimeP0312", "dateEndTimeP0312");

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

//加载仓库信息的方法
function loadCkData_P0312() {
    $("#cmbCkP0312").combobox({
        width: _iWidth120,
        listWidth: _iWidth120,
        valueField: "dm",
        textField: "mc",
        onblur: function () {
            if ($("#cmbCkP0312").combobox("getText") == "") {
                $("#cmbCkP0312").combobox("setValue", "");
            }
        },
        onfocus: function () {
            if ($("#cmbCkP0312").combobox("getText") == "全部仓库") {
                $("#cmbCkP0312").combobox("setText", "");
            }
        },
        enterDown: function () { }
    });

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

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

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

//合计行的计算
function totalCompute_P0312() {
    //设置合计行的数据
    var iKcsl = parseFloat($("#" + strTableP0312).getCol("kcsl", false, "sum")).toFixed(window.parent.strCountPlace);
    var iRksl = parseFloat($("#" + strTableP0312).getCol("rksl", false, "sum")).toFixed(window.parent.strCountPlace);
    var iCksl = parseFloat($("#" + strTableP0312).getCol("cksl", false, "sum")).toFixed(window.parent.strCountPlace);
    var iSysl = parseFloat($("#" + strTableP0312).getCol("sysl", false, "sum")).toFixed(window.parent.strCountPlace);
    var intIDlength = $("#" + strTableP0312).getDataIDs();
    if (intIDlength.length > 0) {
        $("#" + strTableP0312).footerData("set", { rn: "合计", kcsl: iKcsl, rksl: iRksl, cksl: iCksl, sysl: iSysl }, true);
    }
    else {
        $("#" + strTableP0312).footerData("set", { rn: "合计", kcsl: "&nbsp;", rksl: "&nbsp;", cksl: "&nbsp;", sysl: "&nbsp;" }, false);
    }
    //释放内存
    iKcsl = null;
    iRksl = null;
    iCksl = null;
    iSysl = null;
    intIDlength = null;
}

//数量的格式化
function formatterP0312Count(cellvalue, options, rowObject) {
    if (isNaN(parseFloat(cellvalue))) {
        cellvalue = 0;
    }
    return parseFloat(cellvalue).toFixed(window.parent.strCountPlace);
}

//当基本资料数据有修改时 刷新相关的combobox
function refreshMyCombobox(strModels) {
    if ($(window.parent.document).find('#idIframe-31201').length > 0) {
        $(window.parent.document.getElementById("idIframe-31201").contentWindow.document.getElementById("idRefreshCombobox")).val(strModels[0]);
        $(window.parent.document.getElementById("idIframe-31201").contentWindow.document.getElementById("idRefreshCombobox")).click();
    }
    for (var i = 0; i < strModels.length; i++) {
        var data = getComboboxData(strModels[i] + "2");

        if (strModels[i] == "TCkxx") {
            data.unshift({ dm: "", mc: "全部仓库", "selected": true });
            reLoadCombobox("cmbCkP0312", data);
        }
        if (strModels[i] == "Tspxx") {
            data.unshift({ dm: "", mc: "全部药品", "selected": true });
            reLoadCombobox("cmbSpP0312", data);
        }
    }
}

/*↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓**自定义方法 结束**↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓*/
/*-------------------------------------------------------------
功能：创建普通表格
*****①strTableID：表格的ID
*****②objColNames:表格需要显示的列名
*****③objColModel：表格的列配置
*****④objDataRecords：表格中需要加载的数据
-----------------------------------------------------------------*/
function createGrid_P0312(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) {//当前选择行数据取得
            intSelRowIDP0312 = rowId;
            objSelRowP0312 = $(this).getRowData(rowId);
        },
        ondblClickRow: function () {
            viewDetail_P0312();
        }
    }).parent(".ui-jqgrid-bdiv").siblings().css("width", "100%");

    //设置行号列的名称
    //$("table.ui-jqgrid-htable th:first", $("#gbox_" + strTableID)).text("序号");
    $("#" + strTableID).setLabel("rn", "序号");

    //设置表格的高度
    //调整表格的大小
    resizeGrid(strTableP0312, true, false);
    //有滚动条时改变表格宽度
    resizeGridScroll(strTableP0312);
    //合计数据的计算
    totalCompute_P0312();
}

