/*-------------------------------文档信息-----------------------------------
///功能：应付查询
///创建人: yangf
///创建时间：------------------------
///修改人：翟新乐
///修改时间：-----------------------
///修改原因：-----------------------
///详细说明：-----------------------
---------------------------------------------------------------------*/

//定义页面全局变量
var strTableP0114 = "Tp0114";  //表格的id
var objSelRowP0114 = {};  //选择行的数据
var intSelRowIDP0114 = 0;  //选择行的id
var objYcgcModel;           //要展开的列
var objWherejson = {};      //记录查询时的条件

//加载按默认统计的grid
function pageLoad_P0114() {
    //日期下拉框加载
    dateExtent_P0114();
    //加载供应商
    loadGysData_P0114();
    //设置为false
    $("#ckbGc").attr("checked", false);
    //查询数据
    searchData_P0114();
}

//加载表格数据
function createTable_P0114(strjson) {
    try {
        $("#" + strTableP0114).GridUnload(strTableP0114);
        var objConfig = ParseData(window.external.DoOptions(strTableP0114, "GetTableConfig", "{}"));
        //添加列格式化方法
        objYcgcModel = objConfig.colModel;
        setModel();
        for (var i = 0; i < objConfig.colModel.length; i++) {
            if (objConfig.colModel[i].name == "qcyf" || objConfig.colModel[i].name == "yh" || objConfig.colModel[i].name == "cg" || objConfig.colModel[i].name == "fk"
            || objConfig.colModel[i].name == "th" || objConfig.colModel[i].name == "yfye") {
                objConfig.colModel[i].formatter = formatterP0114Money;
            }
        }
        //创建表格
        var objResult = ParseData(window.external.DoOptions(strTableP0114, "GetList", strjson));
        createGrid(strTableP0114, objConfig.colNames, objConfig.colModel, objResult.Records, null, gridComplete_P0114, true, showDetail);
        //表格事件的添加
        addGridEvent_P0114(strTableP0114);
        resizeGrid(strTableP0114, true, false);
        resizeGridScroll(strTableP0114);
        objConfig = {};
        objResult = {};
    } catch (e) { }
}

//查询按钮的方法
function searchData_P0114() {
    try {
        if ($("#cmbGysP0114").combobox("getValue") == undefined) {       //combobox的key不存在，赋值空
            $("#cmbGysP0114").combobox("setValue", "");
        }
        //查询条件的取得
        var strWhereJson = "";
        strWhereJson = "{\"sdate\":\"" + $("#dateStartTimeP0114").val() + "\",";
        strWhereJson += "\"edate\":\"" + $("#dateEndTimeP0114").val() + "\",";
        strWhereJson += "\"gysdm\":\"" + $("#cmbGysP0114").combobox("getValue") + "\"}";
        //记录查询条件
        objWherejson = ParseData(strWhereJson.replace("}", ",\"dname\":\"" + $("#cmbDateExtentP0114").combobox("getText") + "\",\"gysmc\":\"" + $("#cmbGysP0114").combobox("getText") + "\"}"));
        createTable_P0114(strWhereJson);
        //      
        strWhereJson = null;
        intSelRowIDP0114 = "";
        objSelRowP0114 = {};
    } catch (e) { }
}

//展开列
function setModel() {
    var isHidden = !($("#ckbGc").attr("checked"));
    for (var i = 0; i < objYcgcModel.length; i++) {   //修改model
        if (objYcgcModel[i]["index"] == "cg") {
            objYcgcModel[i]["hidden"] = isHidden;
        }
        else if (objYcgcModel[i]["index"] == "qcyf") {
            objYcgcModel[i]["hidden"] = isHidden;
        }
        else if (objYcgcModel[i]["index"] == "yh") {
            objYcgcModel[i]["hidden"] = isHidden;
        }
        else if (objYcgcModel[i]["index"] == "fk") {
            objYcgcModel[i]["hidden"] = isHidden;
        }
        else if (objYcgcModel[i]["index"] == "th") {
            objYcgcModel[i]["hidden"] = isHidden;
        }
    }
    isHidden = null;
}

//时间范围事件
function dateTimeChange_P0114() {
    if ($("#cmbDateExtentP0114").combobox("getText") != "自定义") {
        $("#cmbDateExtentP0114").combobox("setValue", "");
        $("#cmbDateExtentP0114").combobox("setText", "自定义");
    }
}

//选择日期范围
function dateExtent_P0114() {
    //设置默认日期为本月
    dateExtentCalculation("本月", "dateStartTimeP0114", "dateEndTimeP0114");

    //添加选择事件
    $("#cmbDateExtentP0114").combobox({
        valueField: "dm",
        textField: "mc",
        editable: false,
        panelHeight: 102,
        onSelect: function (option) {
            dateExtentCalculation(option.dm, "dateStartTimeP0114", "dateEndTimeP0114");
        },
        enterDown: function () {
            dateExtentCalculation($(this).combobox("getValue"), "dateStartTimeP0114", "dateEndTimeP0114");
            //searchData_P0114();
        }
    });
    //加载数据
    $("#cmbDateExtentP0114").combobox("loadData", [{ dm: "本月", mc: "本月", selected: true },
        { dm: "上一月", mc: "上一月" }, 
        { dm: "本周", mc: "本周" },
        { dm: "过去三个月", mc: "过去三个月" },
        { dm: "本年", mc: "本年"}]);
}

//供应下拉框信息的加载
function loadGysData_P0114() {
    $("#cmbGysP0114").combobox({
        valueField: "dm",
        textField: "mc",
        onblur: function () {
            if ($("#cmbGysP0114").combobox("getText") == "") {
                $("#cmbGysP0114").combobox("setValue", "");
            }
        },
        onfocus: function () {
            if ($("#cmbGysP0114").combobox("getText") == "全部供应商") {
                $("#cmbGysP0114").combobox("setText", "");
            }
        },
        enterDown: function () {
            //searchData_P0114();
        }
    });

    //加载数据
    var objProvider = getComboboxData('Tgysxx2');
    if (objProvider == "{}") {
        objProvider = [];
    }
    objProvider.unshift({ "dm": "", "mc": "全部供应商", "selected": true });
    $("#cmbGysP0114").combobox("loadData", objProvider);
    objProvider = {};
}

//合计
function gridComplete_P0114() {
    var sumCgje = $("#" + strTableP0114).getCol("cg", false, "sum");
    var sumYfje = $("#" + strTableP0114).getCol("fk", false, "sum");
    var sumYfye = $("#" + strTableP0114).getCol("yfye", false, "sum");
    var arrIDs = $("#" + strTableP0114).getDataIDs();
    if (arrIDs.length == 0) {
        $("#" + strTableP0114).footerData("set", { rn: "合计",cg:"&nbsp;", fk: "&nbsp;",yfye:"&nbsp;"}, false);
    }
    else {
        $("#" + strTableP0114).footerData("set", { rn: "合计",
            cg: sumCgje.toFixed(window.parent.strMoneyPlace),
            fk: sumYfje.toFixed(window.parent.strMoneyPlace),
            yfye: sumYfye.toFixed(window.parent.strMoneyPlace)
        }, true);
    }
    arrIDs = null;
    sumCgje = null;
    sumYfje = null;
    sumYfye = null;
}

//dj,je格式化money
function formatterP0114Money(cellvalue, options, rowObject) {
    if (isNaN(parseFloat(cellvalue))) {
        cellvalue = "&nbsp;";
    }
    return parseFloat(cellvalue).toFixed(window.parent.strMoneyPlace);
}

//添加事件
function addGridEvent_P0114(strTableID) {
    $("#" + strTableID).setGridParam({
        onSelectRow: function (rowId) {
            intSelRowIDP0114 = rowId;
            objSelRowP0114 = $(this).getRowData(rowId);
        }
    })
}

//改变tab标签的名称，切换iframe中的内容。
function showDetail() {
    try {
        if (intSelRowIDP0114 > 0) {
            $(window.parent.document).find("#span_114").text("往来明细账");

            //选择的日期
            $(window.parent.document).find("#idIframe-11401").attr("dname", objWherejson.dname);
            $(window.parent.document).find("#idIframe-11401").attr("sdate", objWherejson.sdate);
            $(window.parent.document).find("#idIframe-11401").attr("edate", objWherejson.edate);
            //供应商代码
            $(window.parent.document).find("#idIframe-11401").attr("gysdm", objSelRowP0114.gysdm ? objSelRowP0114.gysdm : objWherejson.gysdm);
            $(window.parent.document).find("#idIframe-11401").attr("gysmc", objSelRowP0114.gysdm ? objSelRowP0114.gysmc : objWherejson.gysmc);
            //改变标签Iframe的Src；
            $(window.parent.document).find("#idIframe-114").css("display", "none");
            $(window.parent.document).find("#idIframe-11401").css("display", "block");
            $(window.parent.document).find("#idIframe-11401").attr("src", "01/p011401.htm");

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

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