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

//页面加载方法
function pageLoad_P051503() {
    //选择日期范围
    loadDateExtent_P051503();
    //加载职员信息
    loadZyData_P051503();
    //加载数据
    searchDataClick_P051503();
}

//加载grid
function createTable_P051503(strjson) {
    try {
        $("#" + strTableP051503).GridUnload(strTableP051503);
        var objConfig = ParseData(window.external.DoOptions(strTableP051503, "GetTableConfig", "{}"));
        //添加列格式化方法
        for (var i = 0; i < objConfig.colModel.length; i++) {
            if (objConfig.colModel[i].name == "fse") {
                objConfig.colModel[i].formatter = formatterP051503Money;
            }
            if (objConfig.colModel[i].name == "xh") {
                objConfig.colModel[i].hidden = true;
            }
        }
        //创建表格
        var objResult = ParseData(window.external.DoOptions(strTableP051503, "GetList", strjson));
        createTreeGrid_P051503(strTableP051503, objConfig.colNames, objConfig.colModel, objResult.Records, "mc");
        //表格事件的添加
        addGridEvent_P051503(strTableP051503);
        loadTreeData(strTableP051503, objResult.Records);
        resizeTbl(true, strTableP051503, true, false);
        resizeGrid(strTableP051503);
        objConfig = {};
        objResult = {};
    } catch (e) { }
}

//树Grid
function createTreeGrid_P051503(strTableID, objColNames, objColModel, objJsonDatas, strColExpandKey) {
    //当数据集为空时，格式化数据集的空值
    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: "zydm",
        localReader: { id: "bh" },
        autowidth: true,
        height: "100%"
    }).parent(".ui-jqgrid-bdiv").siblings().css("width", "100%");
}

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

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

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

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

//选择日期范围
function loadDateExtent_P051503() {
    //设置默认日期为本月
    dateExtentCalculation("本月", "dateStartTimeP051503", "dateEndTimeP051503");

    //添加选择事件
    $("#cmbDateExtentP051503").combobox({
        valueField: "dm",
        textField: "mc",
        editable: false,
        panelHeight: 150,
        onChange: function (newValue,oldValue) {
            if (newValue == "历史以来") {              //历史以来，灰化日期控件文本框
                $("#dateStartTimeP051503").attr("disabled", true);
                $("#dateEndTimeP051503").attr("disabled", true);
            }
            else {                                      //其它的不灰化
                $("#dateStartTimeP051503").attr("disabled", false);
                $("#dateEndTimeP051503").attr("disabled", false);
            }
            //dateExtentCalculation(newValue, "dateStartTimeP051503", "dateEndTimeP051503");
        },
        onSelect: function (option) {
            dateExtentCalculation(option.dm, "dateStartTimeP051503", "dateEndTimeP051503");
        },
        enterDown: function () {
            dateExtentCalculation($(this).combobox("getValue"), "dateStartTimeP051503", "dateEndTimeP051503");
            //searchDataClick_P051503();
        }
    });
    //加载数据
    $("#cmbDateExtentP051503").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-51501").attr("dname");
    if (strDateItem != "") {
        $("#cmbDateExtentP051503").combobox("setValue", strDateItem);
        dateExtentCalculation(strDateItem, "dateStartTimeP051503", "dateEndTimeP051503");
    }
    else {
        var strStartTime = $(window.parent.document).find("#idIframe-51501").attr("sdate");
        var strEndTime = $(window.parent.document).find("#idIframe-51501").attr("edate");
        $("#cmbDateExtentP051503").combobox("setText", "自定义");
        $("#dateStartTimeP051503").val(strStartTime);
        $("#dateEndTimeP051503").val(strEndTime);
    }
    strDateItem = null;
    strStartTime = null;
    strEndTime = null;
}

//职员信息加载
function loadZyData_P051503() {
    $("#cmbZyP051503").combobox({
        valueField: "dm",
        textField: "mc",
        onblur: function () {
            if ($("#cmbZyP051503").combobox("getText") == "") {
                $("#cmbZyP051503").combobox("setValue", "");
            }
        },
        onfocus: function () {
            if ($("#cmbZyP051503").combobox("getText") == "全部职员") {
                $("#cmbZyP051503").combobox("setText", "");
            }
        },
        enterDown: function () {
            //searchDataClick_P051503(); 
        }
    });

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

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

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

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