/*-------------------------------文档信息-----------------------------------
///功能：预提费用统计
///创建人: 翟新乐
///创建时间：----------------------
///修改人：------------------------
///修改时间：-----------------------
///修改原因：-----------------------
///详细说明：-----------------------
---------------------------------------------------------------------*/
//页面全局变量的定义
var strTableP0511 = "Tp0511";   //表格ID
var objSelRowP0511 = {};        //选择行的数据
var intSelRowIDP0511 = 0;       //选择行的ID
var objWhereJsonP0511 = {};     //记录查询时的条件
var objYtgcModel;               //展开的列
/*↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓**事件方法 开始**↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓*/

//页面加载方法
function pageLoad_P0511() {
    //查询类型
    loadSearchType_P0511();
    //选择日期范围
    loadDateExtent_P0511();
    //加载科目信息
    loadFykmData_P0511();
    //经手人下拉框信息的加载
    loadJsrData_P0511();
    //预提对象信息
    loadYtdxData_P0511();
    $("#ckbGc").attr("checked", false); //设置展开为false
    //加载数据
    searchDataClick_P0511();
}

//加载grid
//function createTable_P0511(strjson) {
//    try {
//        $("#" + strTableP0511).GridUnload(strTableP0511);
//        var objConfig = ParseData(window.external.DoOptions(strTableP0511, "GetTableConfig", "{}"));
//        objYtgcModel = objConfig.colModel;
//        setModel();
//        //添加列格式化方法
//        for (var i = 0; i < objConfig.colModel.length; i++) {

//            if (objConfig.colModel[i].name == "qcje" || objConfig.colModel[i].name == "ytje" || objConfig.colModel[i].name == "zfje" || objConfig.colModel[i].name == "syje") {
//                objConfig.colModel[i].formatter = formatterP0511Money;
//            }
//        }
//        //创建表格
//        var objResult = ParseData(window.external.DoOptions(strTableP0511, "GetList", strjson));
//        if (strTableP0511 == "Tp0511") {
//            createTreeGrid_P0511(strTableP0511, objConfig.colNames, objConfig.colModel, [], "mc", "kmdm", viewDetail_P0511);
//        }
//        else {
//            createTreeGrid_P0511(strTableP0511, objConfig.colNames, objConfig.colModel, [], "mc", "jsrdm", viewDetail_P0511);
//        }

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

//树grid
function createTreeGrid_P0511(strTableID, objColNames, objColModel, objJsonDatas, strColExpandKey,strSotrname, 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: strSotrname,
        localReader: { id: "xh" },
        autowidth: true,
        //height: "100%",
        ondblClickRow: function (selRowId) {
            if (funcDbClick) {
                funcDbClick.call(this, selRowId);
            }
        }
    }); //.parent(".ui-jqgrid-bdiv").siblings().css("width", "100%");
}

//查询按钮的方法
function searchDataClick_P0511(strFlag) {
    try {
        //combobox的key不存在，赋值空
        if ($("#cmbFykmP0511").combobox("getValue") == undefined) {
            $("#cmbFykmP0511").combobox("setValue", "");
        }
        if ($("#cmbFyjsrP0511").combobox("getValue") == undefined) {
            $("#cmbFyjsrP0511").combobox("setValue", "");
        }
        if ($("#cmbYtdxP0511").combobox("getValue") == undefined) {
            $("#cmbYtdxP0511").combobox("setValue", "");
        }
        //查询条件的取得
        var strWhereJson = "";
        //快捷日期是历史以来，传空值
        if ($("#cmbDateExtentP0511").combobox("getValue") == "历史以来") {
            strWhereJson = "{\"sdate\":\"0\",";
            strWhereJson += "\"edate\":\"0\",";
        }
        else {
            strWhereJson = "{\"sdate\":\"" + $("#dateStartTimeP0511").val() + "\",";
            strWhereJson += "\"edate\":\"" + $("#dateEndTimeP0511").val() + "\",";
        }
        strWhereJson += "\"kmdm\":\"" + $("#cmbFykmP0511").combobox("getValue") + "\",";
        strWhereJson += "\"jsrdm\":\"" + $("#cmbFyjsrP0511").combobox("getValue") + "\",";
        strWhereJson += "\"ytdx\":\"" + $("#cmbYtdxP0511").combobox("getValue") + "\"}";
        //记录查询条件
        objWhereJsonP0511 = ParseData(strWhereJson.replace("}", ",\"dname\":\"" + $("#cmbDateExtentP0511").combobox("getValue") + "\",\"xsgc\":\"" + ($("#ckbGc").attr("checked") == true ? "true" : "false") + "\"}"));
        //createTable_P0511(strWhereJson);
        var objConfig = ParseData(window.external.DoOptions(strTableP0511, "GetTableConfig", "{}"));
        //添加列格式化方法
        for (var i = 0; i < objConfig.colModel.length; i++) {
            if (objConfig.colModel[i].name == "qcje" || objConfig.colModel[i].name == "ytje" || objConfig.colModel[i].name == "zfje" || objConfig.colModel[i].name == "syje") {
                objConfig.colModel[i].formatter = formatterP0511Money;
            }
        }
        var objResult = ParseData(window.external.DoOptions(strTableP0511, "GetList", strWhereJson));
        if (strTableP0511 == "Tp0511") {
            if (strFlag == "0") {
                setModel();
                loadTreeData(strTableP0511, objResult.Records);
            }
            else {
                $("#" + strTableP0511).GridUnload();
                createTreeGrid_P0511(strTableP0511, objConfig.colNames, objConfig.colModel, [], "mc", "kmdm", viewDetail_P0511);
                resizeGrid(strTableP0511, true, false);
                setModel();
                //表格事件的添加
                addGridEvent_P0511(strTableP0511);
                loadTreeData(strTableP0511, objResult.Records);
            }
        }
        else {
            if (strFlag == "0") {
                setModel();
                loadTreeData(strTableP0511, objResult.Records);
            }
            else {
                $("#" + strTableP0511).GridUnload();
                createTreeGrid_P0511(strTableP0511, objConfig.colNames, objConfig.colModel, [], "mc", "jsrdm", viewDetail_P0511);
                resizeGrid(strTableP0511, true, false);

                setModel();
                //表格事件的添加
                addGridEvent_P0511(strTableP0511);
                loadTreeData(strTableP0511, objResult.Records);
            }
        }

        resizeGridScroll(strTableP0511);
        //清空选择行信息
        intSelRowIDP0511 = "";
        objSelRowP0511 = {};
        strWhereJson = null;
    } catch (e) { }
}

//显示隐藏列
//function setModel() {
//    var isHidden = !($("#ckbGc").attr("checked"));
//    for (var i = 0; i < objYtgcModel.length; i++) {   //修改model
//        if (objYtgcModel[i]["index"] == "qcje") {
//            objYtgcModel[i]["hidden"] = isHidden;
//        }
//        else if (objYtgcModel[i]["index"] == "ytje") {
//            objYtgcModel[i]["hidden"] = isHidden;
//        }
//        else if (objYtgcModel[i]["index"] == "zfje") {
//            objYtgcModel[i]["hidden"] = isHidden;
//        }
//    }
//    isHidden = null;
//}
function setModel() {
    try {
        var isHidden = $("#ckbGc").attr("checked");
        if (isHidden == true) {
            $("#" + strTableP0511).jqGrid("showCol", "qcje");
            $("#" + strTableP0511).jqGrid("showCol", "ytje");
            $("#" + strTableP0511).jqGrid("showCol", "zfje");
        }
        else {
            $("#" + strTableP0511).jqGrid("hideCol", "qcje");
            $("#" + strTableP0511).jqGrid("hideCol", "ytje");
            $("#" + strTableP0511).jqGrid("hideCol", "zfje");
        }
        isHidden = null;
    } catch (e) { }
}

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

//查看明细按钮方法
function viewDetail_P0511() {
    try {
        if (intSelRowIDP0511 > 0) {
            //改变标签的Name 
            $(window.parent.document).find("#span_511").text("往来明细");
            //选择的日期
            $(window.parent.document).find("#idIframe-51102").attr("dname", objWhereJsonP0511.dname);
            $(window.parent.document).find("#idIframe-51102").attr("sdate", objWhereJsonP0511.sdate);
            $(window.parent.document).find("#idIframe-51102").attr("edate", objWhereJsonP0511.edate);
            //科目代码
            $(window.parent.document).find("#idIframe-51102").attr("kmdm", objSelRowP0511.kmdm ? objSelRowP0511.kmdm : objWhereJsonP0511.kmdm);
            //经手人，预提对象代码
            $(window.parent.document).find("#idIframe-51102").attr("jsrdm", objSelRowP0511.jsrdm ? objSelRowP0511.jsrdm : objWhereJsonP0511.jsrdm);
            $(window.parent.document).find("#idIframe-51102").attr("ytdx", objSelRowP0511.ytdx ? objSelRowP0511.ytdx : objWhereJsonP0511.ytdx);
            //改变标签Iframe的Src
            $(window.parent.document).find("#idIframe-511").css("display", "none"); //隐藏统计页面
            $(window.parent.document).find("#idIframe-51102").css("display", ""); //显示明页面
            $(window.parent.document).find("#idIframe-51102").attr("src", "05/p051102.htm");
            $(window.parent.document).find("#idIframe-51102").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 loadSearchType_P0511() {
    $("#cbmSearchTypeP0511").combobox({
        width: _iWidth100,
        listWidth: _iWidth100,
        valueField: "dm",
        textField: "mc",
        editable: false,
        panelHeight: 42,
        onSelect: function (option) {
            selectSearchType_P0511(option.dm);
        },
        enterDown: function () {
            selectSearchType_P0511($(this).combobox("getValue"));
        }
    });
    //加载数据
    $("#cbmSearchTypeP0511").combobox("loadData", [{ dm: "按费用科目", mc: "按费用科目", selected: true }, { dm: "按费用经手人", mc: "按费用经手人"}]);
}

//选择日期范围
function loadDateExtent_P0511() {
    //设置默认日期为本月
    dateExtentCalculation("本月", "dateStartTimeP0511", "dateEndTimeP0511");

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

//费用科目下拉框信息的加载
function loadFykmData_P0511() {
    $("#cmbFykmP0511").combobox({
        width: _iWidth100,
        listWidth: _iWidth100,
        valueField: "kmdm",
        textField: "kmmc",
        onblur: function () {
            if ($("#cmbFykmP0511").combobox("getText") == "") {
                $("#cmbFykmP0511").combobox("setValue", "");
            }
        },
        onfocus: function () {
            if ($("#cmbFykmP0511").combobox("getText") == "全部费用科目") {
                $("#cmbFykmP0511").combobox("setText", "");
            }
        },
        enterDown: function () {
            //searchDataClick_P0511();
        }
    });
    //加载数据
    //var objProvider = ParseData(window.external.DoOptions(strTableP0511, "GetTreeList", "{'flag':'1'}")).Records;
    var objProvider = ParseData(window.external.DoOptions("Tp0505", "GetComboBoxData", "{'fb':'2'}")).Records;
    if (objProvider == "{}") {
        objProvider = [];
    }
    objProvider.unshift({ "kmdm": "", "kmmc": "全部费用科目", "selected": true });
    $("#cmbFykmP0511").combobox("loadData", objProvider);
    objProvider = {};
}

//费用经手人下拉框信息的加载
function loadJsrData_P0511() {
    $("#cmbFyjsrP0511").combobox({
        width: _iWidth100,
        listWidth: _iWidth100,
        valueField: "jsrdm",
        textField: "jsrmc",
        onblur: function () {
            if ($("#cmbFyjsrP0511").combobox("getText") == "") {
                $("#cmbFyjsrP0511").combobox("setValue", "");
            }
        },
        onfocus: function () {
            if ($("#cmbFyjsrP0511").combobox("getText") == "全部费用经手人") {
                $("#cmbFyjsrP0511").combobox("setText", "");
            }
        },
        enterDown: function () {
            //searchDataClick_P0511(); 
        }
    });
    //加载数据
    //var objDrug = ParseData(window.external.DoOptions("Tp0507", "GetComboBoxData", '{"flag":"3"}')).Records;
    var objDrug = ParseData(window.external.DoOptions("Tp0505", "GetComboBoxData", "{'fb':'1'}")).Records;
    if (objDrug == "{}") {
        objDrug = [];
    }
    objDrug.unshift({ "jsrdm": "", "jsrmc": "全部费用经手人", "selected": true });
    $("#cmbFyjsrP0511").combobox("loadData", objDrug);
    objDrug = {};
}

//预提对象信息加载
function loadYtdxData_P0511() {
    $("#cmbYtdxP0511").combobox({
        width: _iWidth100,
        listWidth: _iWidth100,
        valueField: "ytdx",
        textField: "ytdxmc",
        onblur: function () {
            if ($("#cmbYtdxP0511").combobox("getText") == "") {
                $("#cmbYtdxP0511").combobox("setValue", "");
            }
        },
        onfocus: function () {
            if ($("#cmbYtdxP0511").combobox("getText") == "全部预提对象") {
                $("#cmbYtdxP0511").combobox("setText", "");
            }
        },
        enterDown: function () {
            //searchDataClick_P0511(); 
        }
    });
    //加载数据
    var objDrug = window.external.DoOptions("Tp0505", "GetComboBoxData", "{'fb':'3'}");
    //var objDrug = window.external.DoOptions(strTableP0511, "GetTreeList", "{'flag':'2'}");
    if (objDrug) {
        var objYtdx = ParseData(objDrug);
        //objYtdx.Records.unshift({ "ytdx": "", "ytdxmc": "预提对象", "selected": true });
        objYtdx.Records.unshift({ "ytdx": "", "ytdxmc": "全部预提对象", "selected": true });
        $("#cmbYtdxP0511").combobox("loadData", objYtdx.Records);
    }
    objDrug = {};
    objYtdx = {};    
}

//表格事件的添加
function addGridEvent_P0511(strTableID) {
    $("#" + strTableID).setGridParam({
        footerrow: true,
        onSelectRow: function (rowId) {//当前选择行数据取得
            intSelRowIDP0511 = rowId;
            objSelRowP0511 = $(this).getRowData(rowId);
            resizeGridScroll(strTableID);
        },
        gridComplete: function () {
            gridComplete_P0511();
        }
    });
}

//合计
function gridComplete_P0511() {
    //设置合计行的数据
    //隐藏合计行中不显示的列
    if ($("#ckbGc").attr("checked") == false) {
        $("table.ui-jqgrid-ftable td:eq(2)", $("#gbox_" + strTableP0511)).hide();
        $("table.ui-jqgrid-ftable td:eq(3)", $("#gbox_" + strTableP0511)).hide();
        $("table.ui-jqgrid-ftable td:eq(4)", $("#gbox_" + strTableP0511)).hide();
        $("table.ui-jqgrid-ftable td:eq(1)", $("#gbox_" + strTableP0511)).attr("colspan", "2");
    }
    else {
        $("table.ui-jqgrid-ftable td:eq(2)", $("#gbox_" + strTableP0511)).show();
        $("table.ui-jqgrid-ftable td:eq(3)", $("#gbox_" + strTableP0511)).show();
        $("table.ui-jqgrid-ftable td:eq(4)", $("#gbox_" + strTableP0511)).show();
        $("table.ui-jqgrid-ftable td:eq(5)", $("#gbox_" + strTableP0511)).show();
        $("table.ui-jqgrid-ftable td:eq(1)", $("#gbox_" + strTableP0511)).show();
    }
    var objTable = $("#" + strTableP0511);
    var arrIds = objTable.getDataIDs();
    var strJehj = 0;
    var strCbje = 0;
    var strYfye = 0;
    var strMl = 0;
    for (var i = 0; i < arrIds.length; i++) {
        if (objTable.getCell(arrIds[i], "isLeaf") == "true") {
            strJehj += parseFloat(objTable.getCell(arrIds[i], "qcje"));
            strCbje += parseFloat(objTable.getCell(arrIds[i], "ytje"));
            strMl += parseFloat(objTable.getCell(arrIds[i], "zfje"));
            strYfye += parseFloat(objTable.getCell(arrIds[i], "syje"));
        }
    }
    if (arrIds.length == 0) {
        $("#" + strTableP0511).footerData("set", { mc: "合计", qcje: "&nbsp;", ytje: "&nbsp;", zfje: "&nbsp;", syje: "&nbsp;" }, false);
    }
    else {
        $("#" + strTableP0511).footerData("set", { mc: "合计", qcje: strJehj, ytje: strCbje, zfje: strMl, syje: strYfye }, true);
    }
    strJehj =null;
    strCbje = null;
    strMl = null;
    objTable = null;
    arrIds = null;
    strYfye = null;
}

//格式化money
function formatterP0511Money(cellvalue, options, rowObject) {
    if (isNaN(parseFloat(cellvalue))) {
        cellvalue = "0";
    }
    return parseFloat(cellvalue).toFixed(window.parent.strMoneyPlace);
}

//查询类型的选择事件的方法
function selectSearchType_P0511(strValue) {
    try {
        //设置选择的查询类型
        if (strValue == "按费用科目") {
            strTableP0511 = "Tp0511";
            $("#divTp0511").css("display", "");
            $("#divTp051101").css("display", "none");
        }
        else if (strValue == "按费用经手人") {
            strTableP0511 = "Tp051101";
            $("#divTp0511").css("display", "none");
            $("#divTp051101").css("display", "");
        }

        /*--------设置默认值--------*/
        objSelRowP0511 = {};
        intSelRowIDP0511 = "";
        //加载数据
        searchDataClick_P0511();
        resizeGrid(strTableP0511, true, false);
        resizeGridScroll(strTableP0511);
    } catch (e) { }
}

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