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

var strTableP011401 = "Tp011401";  //表格的id
var objSelRowP011401 = {};  //选择行的数据
var intSelRowIDP011401 = "";  //选择行的id;
var strDialogId = "divP011401";     //单据层的ID
var strPageP011401 = "divPageP011401";
var objPageParamP011401 = { sortfield: "", orderby: "", where: "", flag: 0 };  //分页控件使用

//加载页面时的方法
function pageLoad_P011401() {
    //选择日期的范围
    dateExtent_P011401();
    //加载供应商信息
    loadProviderData_P011401();
    var objConfig = ParseData(window.external.DoOptions(strTableP011401, "GetTableConfig", "{}"));
    //添加列格式化方法
    for (var i = 0; i < objConfig.colModel.length; i++) {
        if (objConfig.colModel[i].name == "cgje" || objConfig.colModel[i].name == "yhje" || objConfig.colModel[i].name == "fkje"
            || objConfig.colModel[i].name == "thje") {
            objConfig.colModel[i].formatter = formatterP011401Money;
        }
        if (objConfig.colModel[i].name == "ye") {
            objConfig.colModel[i].formatter = formatterYsyeMoney;
        }
        if (objConfig.colModel[i].name == "xh") {
            objConfig.colModel[i].hidden = true;
        }
    }
    //创建表格
    createGrid(strTableP011401, objConfig.colNames, objConfig.colModel, [], null, null, true);
    //表格事件的添加
    addEventPro();
    resizeGrid(strTableP011401, true, true);
    //创建分页
    createPage(strPageP011401, strTableP011401);
    //查询数据
    searchDataClick_P011401();
    //resizeGridScroll(strTableP011401);
    objConfig = {};
}

// 查询按钮的事件
function searchDataClick_P011401() {
    try {
        objPageParamP011401.sortfield = ""; //排序字段
        objPageParamP011401.orderby = "";     //排序顺序
        objPageParamP011401.flag = 1;             //表示点击查询按钮
        loadGridDataP011401();                     //加载数据
        objPageParamP011401.flag = 0;              //使用完后，设置为零
        //resizeGrid(strTableP011401);
    } catch (e) { }
}

//加载数据
function loadGridDataP011401() {
    var objResult = [];
    objPageParamP011401.where = getSelJson_P011401();
    objResult = loadGridData_Page(objPageParamP011401);
    //后台返回的合计
    if (objResult) {
        if (objResult.Records.length > 0) {
            $("#" + strTableP011401).footerData("set", { rn: "合计",
                cgje: objResult.cghj.toFixed(window.parent.strMoneyPlace),
                yhje: objResult.yhhj.toFixed(window.parent.strMoneyPlace),
                fkje: objResult.fkhj.toFixed(window.parent.strMoneyPlace),
                thje: objResult.thhj.toFixed(window.parent.strMoneyPlace)
            }, false);
        }
        else {
            $("#" + strTableP011401).footerData("set", { rn: "合计", cgje: "&nbsp;", yhje: "&nbsp;", fkje: "&nbsp;", thje: "&nbsp;" }, false);
        }
    }
    objResult = {};
    intSelRowIDP011401 = "";
    objSelRowP011401 = {};
}

//取得查询条件
function getSelJson_P011401() {
    if (!$("#cmbProviderP011401").combobox("getValue")) {        //combobox的key不存在，赋值空
        $("#cmbProviderP011401").combobox("setValue", "");
    }
    var strWhereJson = "";
    strWhereJson += "\"sdate\":\"" + $("#dateStartTimeP011401").val() + "\",";
    strWhereJson += "\"edate\":\"" + $("#dateEndTimeP011401").val() + "\",";
    strWhereJson += "\"gysdm\":\"" + $("#cmbProviderP011401").combobox("getValue") + "\"";
    return strWhereJson;
}

//添加事件
function addEventPro() {
    $("#" + strTableP011401).setGridParam({
        onSelectRow: function (rowId) {
            intSelRowIDP011401 = rowId;
            objSelRowP011401 = $(this).getRowData(rowId);
        },
        ondblClickRow: function (rowId) {   //双击事件
            showDjInfo();
        }
    });
}

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

//选择日期范围
function dateExtent_P011401() {
    $("#cmbDateExtentP011401").combobox({
        valueField: "dm",
        textField: "mc",
        editable: false,
        panelHeight: 102,
        onSelect: function (option) {
            dateExtentCalculation(option.dm, "dateStartTimeP011401", "dateEndTimeP011401");
        },
        enterDown: function () {
            dateExtentCalculation($(this).combobox("getValue"), "dateStartTimeP011401", "dateEndTimeP011401");
            //searchDataClick_P011401();
        }
    });
    //加载数据
    $("#cmbDateExtentP011401").combobox("loadData", [{ dm: "本月", mc: "本月", selected: true },
        { dm: "上一月", mc: "上一月" },
        { dm: "本周", mc: "本周" },
        { dm: "过去三个月", mc: "过去三个月" },
        { dm: "本年", mc: "本年"}]);

    $("#cmbDateExtentP011401").combobox("setValue", $(window.parent.document).find("#idIframe-11401").attr("dname"));
    $("#cmbDateExtentP011401").combobox("setText", $(window.parent.document).find("#idIframe-11401").attr("dname"));
    $("#dateStartTimeP011401").val($(window.parent.document).find("#idIframe-11401").attr("sdate"));
    $("#dateEndTimeP011401").val($(window.parent.document).find("#idIframe-11401").attr("edate"));

}

//窗口关闭按钮调用方法
function closeMyDlg(strDialog, strType, strTable) {
    //$("#" + strDialog).dialog("close");
    cihoDlgClose(strDialog);
}

//查看单据
function showDjInfo() {
    try {
        if (intSelRowIDP011401 > 0) {
            if (objSelRowP011401.djbh) {
                if (objSelRowP011401.djname == "付款") {
                    showDialog_P010103(objSelRowP011401.djbh, strDialogId);
                }
                else if (objSelRowP011401.djname == "采购退货") {
                    showDialogP011302_fhd(strDialogId, objSelRowP011401.djbh);
                }
                else if (objSelRowP011401.djname == "采购") {
                    cgd(objSelRowP011401.djbh, strDialogId);
                }
            }
        }
        else {
            window.parent.cihoAlert(msg.other.selDataRow);
        }
    } catch (e) { }
}

//加载供应商数据
function loadProviderData_P011401() {
    $("#cmbProviderP011401").combobox({
        valueField: "dm",
        textField: "mc",
        onblur: function () {
            if ($("#cmbProviderP011401").combobox("getText") == "") {
                $("#cmbProviderP011401").combobox("setValue", "");
            }
        },
        onfocus: function () {
            if ($("#cmbProviderP011401").combobox("getText") == "全部供应商") {
                $("#cmbProviderP011401").combobox("setText", "");
            }
        },
        enterDown: function () {
            //searchDataClick_P011401();
        }
    });

    //加载数据
    var objProvider = getComboboxData('Tgysxx2') ;
    if (objProvider == "{}") {
        objProvider= [];
    }
    objProvider.unshift({ "dm": "", "mc": "全部供应商", "selected": true });
    $("#cmbProviderP011401").combobox("loadData", objProvider);
    $("#cmbProviderP011401").combobox("setValue", $(window.parent.document).find("#idIframe-11401").attr("gysdm"));
    $("#cmbProviderP011401").combobox("setText", $(window.parent.document).find("#idIframe-11401").attr("gysmc"));
    objProvider = {};
}

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

}

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

//改变tab标签的名称，切换iframe中的内容。
function showDetailP011401() {
    //改变标签的Name 
    $(window.parent.document).find("#span_114").text("应付查询");
    //改变标签Iframe的Src；
    $(window.parent.document).find("#idIframe-11401").css("display", "none");
    $(window.parent.document).find("#idIframe-11401").attr("src", "");
    $(window.parent.document).find("#idIframe-114").css("display", "");
}