/*-------------------------------文档信息-----------------------------------
///功能：费用统计表
///创建人: 翟新乐
///创建时间：----------------------
///修改人：------------------------
///修改时间：-----------------------
///修改原因：-----------------------
///详细说明：-----------------------
---------------------------------------------------------------------*/
//页面全局变量的定义
var strTableP0515 = "Tp0515";   //表格ID
var objSelRowP0515 = {};
var intSelRowIDP0515 = "";
var objWhereJsonP0515 = {};     //记录查询时的条件
/*↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓**事件方法 开始**↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓*/

//页面加载方法
function pageLoad_P0515() {
    loadSearchType_P0515();
    //选择日期范围
    loadDateExtent_P0515();
    //科目信息
    loadKmData_P0515();
    //加载客户信息
    loadProviderData_P0515();
    //职员信息
    loadZyData_P0515();
    //药品信息
    loadYpData_P0515();
    //$("#ypbox").attr("style", "display:none");
    //加载数据
    searchDataClick_P0515();
}

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

//        var objConfig = ParseData(window.external.DoOptions(strTableP0515, "GetTableConfig", "{}"));
//        //添加列格式化方法
//        for (var i = 0; i < objConfig.colModel.length; i++) {
//            //objConfig.colModel[i].sortable = true;
//            if (objConfig.colModel[i].name == "qcs" || objConfig.colModel[i].name == "fse") {
//                objConfig.colModel[i].formatter = formatterP0515Money;
//            }
//            if (objConfig.colModel[i].name == "xh") {
//                objConfig.colModel[i].hidden = true;
//            }
//        }
//        //创建表格
//        var objResult = ParseData(window.external.DoOptions(strTableP0515, "GetList", strjson));
//        if (strTableP0515 == "Tp0515" || strTableP0515 == "Tp051502") {
//            createGrid(strTableP0515, objConfig.colNames, objConfig.colModel, objResult.Records, null, gridComplete_P0515, true, showDetailP0515);
//            //表格事件的添加
//            addGridEvent_P0515(strTableP0515);
//        }
//        else {
//            if (strTableP0515 == "Tp051501") {
//                createTreeGrid_P0515(strTableP0515, objConfig.colNames, objConfig.colModel, [], "khmc", "khdm", showDetailP0515);
//            }
//            else if (strTableP0515 == "Tp051503") {
//                createTreeGrid_P0515(strTableP0515, objConfig.colNames, objConfig.colModel, [], "mc", "zydm", showDetailP0515);
//            }
//            //表格事件的添加
//            addGridEvent_P051501(strTableP0515);
//            loadTreeData(strTableP0515, objResult.Records);
//        }
//        resizeGrid(strTableP0515, true, false);
//        resizeGridScroll(strTableP0515);
//        objConfig = {};
//        objResult = {};
//    } catch (e) { }
//}

//查询按钮的方法
function searchDataClick_P0515(strFlag) {
    try {
        //combobox的key不存在，赋值空
        if (!$("#cmbKmP0515").cihoComboTree("getValue")) {
            $("#cmbKmP0515").cihoComboTree("setValue", "");
        }
        if (!$("#cmbCustomerP0515").combobox("getValue")) {
            $("#cmbCustomerP0515").combobox("setValue", "");
        }
        if (!$("#cmbZyP0515").combobox("getValue")) {
            $("#cmbZyP0515").combobox("setValue", "");
        }
        if (!$("#cmbYpP0515").combobox("getValue")) {
            $("#cmbYpP0515").combobox("setValue", "");
        }
        objSelRowP0515 = {};
        intSelRowIDP0515 = "";
        //查询条件的取得
        var strWhereJson = "";
        //快捷日期是历史以来，传空值
        if ($("#cmbDateExtentP0515").combobox("getValue") == "历史以来") {
            strWhereJson = "{\"sdate\":\"0\",";
            strWhereJson += "\"edate\":\"0\",";
        }
        else {
            strWhereJson = "{\"sdate\":\"" + $("#dateStartTimeP0515").val() + "\",";
            strWhereJson += "\"edate\":\"" + $("#dateEndTimeP0515").val() + "\",";
        }
        strWhereJson += "\"kmdm\":\"" + $("#cmbKmP0515").cihoComboTree("getValue") + "\",";
        strWhereJson += "\"khdm\":\"" + $("#cmbCustomerP0515").combobox("getValue") + "\",";
        strWhereJson += "\"spdm\":\"" + $("#cmbYpP0515").combobox("getValue") + "\",";
        strWhereJson += "\"zydm\":\"" + $("#cmbZyP0515").combobox("getValue") + "\"}";
        //记录查询条件
        objWhereJsonP0515 = ParseData(strWhereJson.replace("}", ",\"dname\":\"" + $("#cmbDateExtentP0515").combobox("getValue") + "\",\"kmmc\":\"" + $("#cmbKmP0515").cihoComboTree("getText") + "\"}"));
        //createTable_P0515(strWhereJson);
        var objConfig = ParseData(window.external.DoOptions(strTableP0515, "GetTableConfig", "{}"));
        //添加列格式化方法
        for (var i = 0; i < objConfig.colModel.length; i++) {
            //objConfig.colModel[i].sortable = true;
            if (objConfig.colModel[i].name == "qcs" || objConfig.colModel[i].name == "fse") {
                objConfig.colModel[i].formatter = formatterP0515Money;
            }
            if (objConfig.colModel[i].name == "xh") {
                objConfig.colModel[i].hidden = true;
            }
        }
        //创建表格
        var objResult = ParseData(window.external.DoOptions(strTableP0515, "GetList", strWhereJson));
        if (strTableP0515 == "Tp0515" || strTableP0515 == "Tp051502") {
            $("#" + strTableP0515).GridUnload();
            createGrid(strTableP0515, objConfig.colNames, objConfig.colModel, objResult.Records, null, gridComplete_P0515, true, showDetailP0515);
            //表格事件的添加
            addGridEvent_P0515(strTableP0515);
            resizeGrid(strTableP0515, true, false);
        }
        else {
            if (strTableP0515 == "Tp051501") {
                if (strFlag == "0") {
                    loadTreeData(strTableP0515, objResult.Records);
                }
                else {
                    $("#" + strTableP0515).GridUnload();
                    createTreeGrid_P0515(strTableP0515, objConfig.colNames, objConfig.colModel, [], "khmc", "khdm", showDetailP0515);

                    resizeGrid(strTableP0515, true, false);
                    //表格事件的添加
                    addGridEvent_P051501(strTableP0515);
                    loadTreeData(strTableP0515, objResult.Records);
                }
            }
            else if (strTableP0515 == "Tp051503") {
                if (strFlag == "0") {
                    loadTreeData(strTableP0515, objResult.Records);
                }
                else {
                    $("#" + strTableP0515).GridUnload();
                    createTreeGrid_P0515(strTableP0515, objConfig.colNames, objConfig.colModel, [], "mc", "zydm", showDetailP0515);

                    resizeGrid(strTableP0515, true, false);
                    //表格事件的添加
                    addGridEvent_P051501(strTableP0515);
                    loadTreeData(strTableP0515, objResult.Records);
                }
            }
        }
        resizeGridScroll(strTableP0515);
        objConfig = {};
        objResult = {};
        strWhereJson = "";
    } catch (e) { }
}

//树Grid
function createTreeGrid_P0515(strTableID, objColNames, objColModel, objJsonDatas, strColExpandKey,strColSotrName, funcDbClick) {
    //当数据集为空时，格式化数据集的空值
    if (objJsonDatas == "{}") {
        objJsonDatas = [];
    }
    //创建表格
    $("#" + strTableID).jqGrid({
        datatype: "local",
        colNames: objColNames,
        colModel: objColModel,
        data: objJsonDatas[0],
        treeGrid: true,
        treedatatype: "local",
        treeGridModel: 'adjacency',
        ExpandColumn: strColExpandKey,
        ExpandColClick: true,
        editable: true,
        footerrow: true,
        rownumbers: false,
        rowNum: 999999,
        sortname: strColSotrName,
        localReader: { id: "bh" },
        autowidth: true,
        //height: "100%",
        ondblClickRow: function (selRowId) {
            if (funcDbClick) {
                funcDbClick.call(this, selRowId);
            }
        }
    }); //.parent(".ui-jqgrid-bdiv").siblings().css("width", "100%");
}

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

//查询类型
function loadSearchType_P0515() {
    $("#cbmSearchTypeP0515").combobox({
        valueField: "dm",
        textField: "mc",
        editable: false,
        panelHeight: 90,
        onSelect: function (option) {
            selectSearchType_P0515(option.dm);
        },
        enterDown: function () {
            selectSearchType_P0515($(this).combobox("getValue"));
        }
    });
    //加载数据
    $("#cbmSearchTypeP0515").combobox("loadData", [{ dm: "按科目", mc: "按科目", selected: true },
        { dm: "按客户", mc: "按客户" },
        { dm: "按药品", mc: "按药品" },
        {dm:"按职员",mc:"按职员"}]);
}

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

//选择日期范围
function loadDateExtent_P0515() {
    //设置默认日期为本月
    dateExtentCalculation("本月", "dateStartTimeP0515", "dateEndTimeP0515");

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

function loadKmData_P0515() {
    var objKmdata = [];
    objKmdata = ParseData(window.external.DoOptions("Tp051504", "DoOthers", "{}")).Records;
    objKmdata.unshift({ "id": "", "text": "全部费用科目", "selected": true });
    $("#cmbKmP0515").cihoComboTree("loadData", objKmdata);
    objKmdata = {};
}

function showDetailP0515() {
    try {
        if (intSelRowIDP0515 > 0) {
            $(window.parent.document).find("#span_515").text("费用明细");

            //选择的日期
            $(window.parent.document).find("#idIframe-51504").attr("dname", objWhereJsonP0515.dname);
            $(window.parent.document).find("#idIframe-51504").attr("sdate", objWhereJsonP0515.sdate);
            $(window.parent.document).find("#idIframe-51504").attr("edate", objWhereJsonP0515.edate);
            //供应商代码
            $(window.parent.document).find("#idIframe-51504").attr("kmdm", objSelRowP0515.kmdm ? objSelRowP0515.kmdm : objWhereJsonP0515.kmdm);
            $(window.parent.document).find("#idIframe-51504").attr("kmmc", objSelRowP0515.kmmc ? objSelRowP0515.kmmc : objWhereJsonP0515.kmmc);
            $(window.parent.document).find("#idIframe-51504").attr("khdm", objSelRowP0515.khdm ? objSelRowP0515.khdm : objWhereJsonP0515.khdm);
            $(window.parent.document).find("#idIframe-51504").attr("spdm", objSelRowP0515.spdm ? objSelRowP0515.spdm : objWhereJsonP0515.spdm);
            $(window.parent.document).find("#idIframe-51504").attr("zydm", objSelRowP0515.zydm ? objSelRowP0515.zydm : objWhereJsonP0515.zydm);
            //改变标签Iframe的Src；
            $(window.parent.document).find("#idIframe-515").css("display", "none");
            $(window.parent.document).find("#idIframe-51504").css("display", "block");
            $(window.parent.document).find("#idIframe-51504").attr("src", "05/p051504.htm");

            $(window.parent.document).find("#idIframe-51504").load(function () {
                var iHeight = window.parent.document.documentElement.clientHeight - window.parent._iFrmHeight;
                $(this).height(iHeight);
            });

        }
        else {
            window.parent.cihoAlert(msg.other.selDataRow);
        }
    } catch (e) { }
}

//客户下拉框信息的加载
function loadProviderData_P0515() {
    $("#cmbCustomerP0515").combobox({
        width: _iWidth80,
        listWidth: _iWidth80,
        valueField: "dm",
        textField: "mc",
        onblur: function () {
            if ($("#cmbCustomerP0515").combobox("getText") == "") {
                $("#cmbCustomerP0515").combobox("setValue", "");
            }
        },
        onfocus: function () {
            if ($("#cmbCustomerP0515").combobox("getText") == "全部客户") {
                $("#cmbCustomerP0515").combobox("setText", "");
            }
        },
        enterDown: function () {
            //searchDataClick_P0515();
        },
        afterChange: function (newValue, newText) {
            $(this).combobox("setText", newText.replace(/&nbsp;/g, ""));
        }
    });

    //加载数据
    var objProvider = getComboboxData('Tkhxx2');
    if (objProvider == "{}") {
        objProvider = [];
    }
    objProvider.unshift({ "dm": "", "mc": "全部客户", "selected": true });
    $("#cmbCustomerP0515").combobox("loadData", objProvider);
    objProvider = {};
}

//职员下拉信息加载
function loadZyData_P0515() {
    $("#cmbZyP0515").combobox({
        width: _iWidth80,
        listWidth: _iWidth80,
        valueField: "dm",
        textField: "mc",
        onblur: function () {
            if ($("#cmbZyP0515").combobox("getText") == "") {
                $("#cmbZyP0515").combobox("setValue", "");
            }
        },
        onfocus: function () {
            if ($("#cmbZyP0515").combobox("getText") == "全部职员") {
                $("#cmbZyP0515").combobox("setText", "");
            }
        },
        enterDown: function () {
            //searchDataClick_P0515();
         }
    });

    //加载数据
    var objDrug = getComboboxData('TZyxx2');
    if (objDrug == "{}") {
        objDrug = [];
    }
    objDrug.unshift({ "dm": "", "mc": "全部职员", "selected": true });
    objDrug.push({ "dm": "-1", "mc": "其他职员" });
    $("#cmbZyP0515").combobox("loadData", objDrug);
    objDrug = {};
}

//药品加载数据方法
function loadYpData_P0515() {
    $("#cmbYpP0515").combobox({
        width: _iWidth80,
        listWidth: _iWidth80,
        valueField: "dm",
        textField: "mc",
        onblur: function () {
            if ($("#cmbYpP0515").combobox("getText") == "") {
                $("#cmbYpP0515").combobox("setValue", "");
            }
        },
        onfocus: function () {
            if ($("#cmbYpP0515").combobox("getText") == "全部药品") {
                $("#cmbYpP0515").combobox("setText", "");
            }
        },
        enterDown: function () {
            //searchDataClick_P0515();
        }
    });

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

//查询类型的选择事件的方法
function selectSearchType_P0515(strValue) {
    try {
        //设置选择的查询类型
        if (strValue == "按科目") {
            strTableP0515 = "Tp0515";
            $("#divTp0515").css("display", "");
            $("#divTp051501").css("display", "none");
            $("#divTp051502").css("display", "none");
            $("#divTp051503").css("display", "none");
        }
        else if (strValue == "按客户") {
            strTableP0515 = "Tp051501";
            $("#divTp0515").css("display", "none");
            $("#divTp051501").css("display", "");
            $("#divTp051502").css("display", "none");
            $("#divTp051503").css("display", "none");
        }
        else if (strValue=="按药品") {
            strTableP0515 = "Tp051502";
            $("#divTp0515").css("display", "none");
            $("#divTp051501").css("display", "none");
            $("#divTp051502").css("display", "");
            $("#divTp051503").css("display", "none");
        }
        else if (strValue=="按职员") {
            strTableP0515 = "Tp051503";
            $("#divTp0515").css("display", "none");
            $("#divTp051501").css("display", "none");
            $("#divTp051502").css("display", "none");
            $("#divTp051503").css("display", "");
        }

        /*--------设置默认值--------*/
        objSelRowP0515 = {};
        intSelRowIDP0515 = "";
        //加载数据
        searchDataClick_P0515();
        resizeGrid(strTableP0515, true, false);
        resizeGridScroll(strTableP0515);
    } catch (e) { }
}

//普通grid事件的添加
function addGridEvent_P0515(strTableID) {
    $("#" + strTableID).setGridParam({
        onSelectRow: function (rowId) {//当前选择行数据取得
            intSelRowIDP0515 = rowId;
            objSelRowP0515 = $(this).getRowData(rowId);
        }
    });
}

//treeGrid事件的添加
function addGridEvent_P051501(strTableID) {
    $("#" + strTableID).setGridParam({
        onSelectRow: function (rowId) {//当前选择行数据取得
            intSelRowIDP0515 = rowId;
            objSelRowP0515 = $(this).getRowData(rowId);
            resizeGridScroll(strTableID);
        },
        gridComplete: function () {
            gridComplete_P051501();
        }
    });
}

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

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

//格式化money
function formatterP0515Money(cellvalue, options, rowObject) {
    if (!isNaN(parseFloat(cellvalue))) {
        return parseFloat(cellvalue).toFixed(window.parent.strMoneyPlace);
    }
    else {
        return "&nbsp;";
    }
}

//导出
function toExcelMethod() {
    if (strTableP0515 == "Tp0515") {
        makeExportData(strTableP0515, "费用统计");
    } else if (strTableP0515 == "Tp051501") {
        makeExportData(strTableP0515, "费用统计－按客户分布");
    } else if (strTableP0515 == "Tp051502") {
        makeExportData(strTableP0515, "费用统计－按药品分布");
    } else if (strTableP0515 == "Tp051503") {
        makeExportData(strTableP0515, "费用统计－按职员分布");
    }
}
///*↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑**自定义方法 结束**↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑*/