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

//页面加载方法
function pageLoad_P051501() {
    //选择日期范围
    loadDateExtent_P051501();
    //加载客户信息
    loadCustmoerData_P051501();
    //加载数据
    searchDataClick_P051501();
}

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

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

//树Grid
function createTreeGrid_P051501(strTableID, objColNames, objColModel, objJsonDatas, strColExpandKey, 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: "khdm",
        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 searchDataClick_P051501() {
    try {
        //combobox的key不存在，赋值空
        if ($("#cmbCustomerP051501").combobox("getValue") == undefined) {
            $("#cmbCustomerP051501").combobox("setValue", "");
        }
        //查询条件的取得
        var strWhereJson = "";
        //快捷日期是历史以来，传空值
        if ($("#cmbDateExtentP051501").combobox("getValue") == "历史以来") {
            strWhereJson = "{\"sdate\":\"0\",";
            strWhereJson += "\"edate\":\"0\",";
        }
        else {
            strWhereJson = "{\"sdate\":\"" + $("#dateStartTimeP051501").val() + "\",";
            strWhereJson += "\"edate\":\"" + $("#dateEndTimeP051501").val() + "\",";
        }
        strWhereJson += "\"khdm\":\"" + $("#cmbCustomerP051501").combobox("getValue") + "\"}";
        createTable_P051501(strWhereJson);
        strWhereJson = null;
    } catch (e) { }
}

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

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

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

//选择日期范围
function loadDateExtent_P051501() {
    //设置默认日期为本月
    dateExtentCalculation("本月", "dateStartTimeP051501", "dateEndTimeP051501");

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

//客户cmb信息加载
function loadCustmoerData_P051501() {
    $("#cmbCustomerP051501").combobox({
        valueField: "dm",
        textField: "mc",
        onblur: function () {
            if ($("#cmbCustomerP051501").combobox("getText") == "") {
                $("#cmbCustomerP051501").combobox("setValue", "");
            }
        },
        onfocus: function () {
            if ($("#cmbCustomerP051501").combobox("getText") == "全部客户") {
                $("#cmbCustomerP051501").combobox("setText", "");
            }
        },
        enterDown: function () {
            //searchDataClick_P051501();
        },
        afterChange: function (newValue, newText) {
            $(this).combobox("setText", newText.replace(/&nbsp;/g, ""));
        }
    });

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

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

//合计
function gridComplete_P051501() {
    //设置合计行的数据
    var objTable = $("#" + strTableP051501);
    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 (arrIds.length == 0) { //"&nbsp;"
        $("#" + strTableP051501).footerData("set", { khmc: "合计", fse: "&nbsp;" }, false);
    }
    else {
        $("#" + strTableP051501).footerData("set", { khmc: "合计", fse: strTotal }, true);
    }
    objTable = null;
    arrIds = null;
    strTotal = null;
}

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