/*-------------------------------文档信息-----------------------------------
///功能：药品分布
///创建人: 翟新乐
///创建时间：----------------------
///修改人：------------------------
///修改时间：-----------------------
///修改原因：-----------------------
///详细说明：-----------------------
---------------------------------------------------------------------*/
//页面全局变量的定义
var strTableP051502 = "Tp051502";   //表格ID
/*↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓**事件方法 开始**↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓*/

//页面加载方法
function pageLoad_P051502() {
    //选择日期范围
    loadDateExtent_P051502();
    //加载药品下拉信息
    loadDrugData_P021502();
    //加载数据
    searchDataClick_P051502();
}

//加载grid
function createTable_P051502(strjson) {
    try {
        $("#" + strTableP051502).GridUnload(strTableP051502);

        var objConfig = ParseData(window.external.DoOptions(strTableP051502, "GetTableConfig", "{}"));
        //添加列格式化方法
        for (var i = 0; i < objConfig.colModel.length; i++) {
            objConfig.colModel[i].sortable = true;
            if (objConfig.colModel[i].name == "fse") {
                objConfig.colModel[i].formatter = formatterP051502Money;
            }
        }
        //创建表格
        var objResult = ParseData(window.external.DoOptions(strTableP051502, "GetList", strjson));
        createGrid(strTableP051502, objConfig.colNames, objConfig.colModel, objResult.Records, null, gridComplete_P051502, true, null);
        //表格事件的添加
        //addGridEvent_P051502(strTableP051502);
        resizeTbl(true, strTableP051502, true, false);
        resizeGrid(strTableP051502);
        objConfig = {};
        objResult = {};
    } catch (e) { }
}

//查询按钮的方法
function searchDataClick_P051502() {
    try {
        //combobox的key不存在，赋值空
        if ($("#cmbDrugP051502").combobox("getValue") == undefined) {
            $("#cmbDrugP051502").combobox("setValue", "");
        }
        //查询条件的取得
        var strWhereJson = "";
        //快捷日期是历史以来，传空值
        if ($("#cmbDateExtentP051502").combobox("getValue") == "历史以来") {
            strWhereJson = "{\"sdate\":\"0\",";
            strWhereJson += "\"edate\":\"0\",";
        }
        else {
            strWhereJson = "{\"sdate\":\"" + $("#dateStartTimeP051502").val() + "\",";
            strWhereJson += "\"edate\":\"" + $("#dateEndTimeP051502").val() + "\",";
        }
        strWhereJson += "\"spdm\":\"" + $("#cmbDrugP051502").combobox("getValue") + "\"}";
        createTable_P051502(strWhereJson);
        strWhereJson = null;
    } catch (e) { }
}

//返回按钮的方法
function pageBack_P051502() {
    //改变标签的Name
    $(window.parent.document).find("#span_515").text("费用统计表");
    //改变标签Iframe的Src
    $(window.parent.document).find("#idIframe-515").css("display", ""); //显示统计页面
    $(window.parent.document).find("#idIframe-51503").css("display", "none"); //隐藏明细页面
    $(window.parent.document).find("#idIframe-51503").attr("src", "");
}

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

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

//选择日期范围
function loadDateExtent_P051502() {
    //设置默认日期为本月
    dateExtentCalculation("本月", "dateStartTimeP051502", "dateEndTimeP051502");

    //添加选择事件
    $("#cmbDateExtentP051502").combobox({
        valueField: "dm",
        textField: "mc",
        editable: false,
        panelHeight: 150,
        onChange: function (newValue,oldValue) {
            if (newValue == "历史以来") {          //历史以来，灰化日期控件框
                $("#dateStartTimeP051502").attr("disabled", true);
                $("#dateEndTimeP051502").attr("disabled", true);
            }
            else {                                  //其它的不灰化
                $("#dateStartTimeP051502").attr("disabled", false);
                $("#dateEndTimeP051502").attr("disabled", false);
            }
            //dateExtentCalculation(newValue, "dateStartTimeP051502", "dateEndTimeP051502");
        },
        onSelect: function (option) {
            dateExtentCalculation(option.dm, "dateStartTimeP051502", "dateEndTimeP051502");
        },
        enterDown: function () {
            dateExtentCalculation($(this).combobox("getValue"), "dateStartTimeP051502", "dateEndTimeP051502");
            //searchDataClick_P051502();
        }
    });
    //加载数据
    $("#cmbDateExtentP051502").combobox("loadData", [{ dm: "本月", mc: "本月", selected: true },
        { dm: "上一月", mc: "上一月" },
        { dm: "过去三个月", mc: "过去三个月" },
        { dm: "本年", mc: "本年" },
        { dm: "上一年", mc: "上一年" },
        { dm: "前年", mc: "前年" }, 
        { dm: "历史以来", mc: "历史以来"}]);

    var strDateItem = $(window.parent.document).find("#idIframe-51503").attr("dname");
    if (strDateItem != "") {
        $("#cmbDateExtentP051502").combobox("setValue", strDateItem);
        dateExtentCalculation(strDateItem, "dateStartTimeP051502", "dateEndTimeP051502");
    }
    else {
        var strStartTime = $(window.parent.document).find("#idIframe-51503").attr("sdate");
        var strEndTime = $(window.parent.document).find("#idIframe-51503").attr("edate");
        $("#cmbDateExtentP051502").combobox("setText", "自定义");
        $("#dateStartTimeP051502").val(strStartTime);
        $("#dateEndTimeP051502").val(strEndTime);
    }
    strDateItem = null;
    strStartTime = null;
    strEndTime = null;
}

//药品下拉框信息的加载
function loadDrugData_P021502() {
    //添加变更事件
    $("#cmbDrugP051502").combobox({
        valueField: "dm",
        textField: "mc",
        onblur: function () {
            if ($("#cmbDrugP051502").combobox("getText") == "") {
                $("#cmbDrugP051502").combobox("setValue", "");
            }
        },
        onfocus: function () {
            if ($("#cmbDrugP051502").combobox("getText") == "全部药品") {
                $("#cmbDrugP051502").combobox("setText", "");
            }
        },
        enterDown: function () {
            //searchDataClick_P051502();
        }
    });

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

//表格事件的添加
//function addGridEvent_P051502(strTableID) {
//    $("#" + strTableID).setGridParam({
//        onSelectRow: function (rowId) {//当前选择行数据取得
//            intSelRowIDP051502 = rowId;
//            objSelRowP051502 = $(this).getRowData(rowId);
//        }
//    });
//}

//合计
function gridComplete_P051502() {
    //设置合计行的数据
    var objTable = $("#" + strTableP051502);
    var arrIds = objTable.getDataIDs();
    var strTotal = 0;
    for (var i = 0; i < arrIds.length; i++) {
        strTotal += parseFloat(objTable.getCell(arrIds[i], "fse"));
    }
    if (arrIds.length == 0) { //"&nbsp;"
        $("#" + strTableP051502).footerData("set", { rn: "合计", fse: "&nbsp;" }, false);
    }
    else {
        $("#" + strTableP051502).footerData("set", { rn: "合计", fse: strTotal }, true);
    }
    objTable = null;
    arrIds = null;
    strTotal = null;
}

//je格式化money
function formatterP051502Money(cellvalue, options, rowObject) {
    if (!isNaN(parseFloat(cellvalue))) {
        return parseFloat(cellvalue).toFixed(window.parent.strMoneyPlace);
    }
    else {
        return parseFloat(0).toFixed(window.parent.strMoneyPlace);
    }
}
///*↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑**自定义方法 结束**↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑*/