/*--------------------------文档信息-------------------------------------------
///功能：      报溢单的查看单据
///创建人：  王群
///创建时间：2011年05月31日
///修改人：  --------------
///修改时间：--------------
///详细说明：--------------
-----------------------------------------------------------------------------*/
//先定义一下全局变量
strTableP031201_byd = "Tp030302";

//查看单据
function showDialogP031201_byd(strDivP031201_byd, strDjbh) {
    try {
        $(".operate").remove();                            //清除浮动按钮层
        $("#" + strDivP031201_byd).empty();     //清空弹出窗口内容

        //把弹出窗口框架及底部按钮添加到父窗口创建dialog中
        creatDiv_ShowDlgHaveTbl(strDivP031201_byd, "报溢单", false, false, strTableP031201_byd);

        //添加表单元素到窗口中
        tableFormP031201_byd(strDivP031201_byd, strDjbh);

        //调用所查数据同时加载到所对应的控件
        myListP031201_byd(strDivP031201_byd, strDjbh);

        //重新定位dialog的位置
        //$("#" + strDivP031201_byd).dialog("option", "position", "center");

        //释放内存
        strDivP031201_byd = null;
        strDjbh = null;
    } catch (e) { }
}

//创建表单
function tableFormP031201_byd(strDivP031201_byd, strDjbh) {
    var strHeadP031201_byd = "";             //页头默认为空
    var strFormTableP031201_byd = "";    //表格默认为空
    var strDownP031201_byd = "";            //页尾默认为空
    var objP031201_byd = {};                    //存放表格的列配置
    var strPageHeader = [];                       //页头的配置
    var strpageFooter = [];                        //页尾的配置

    //加载编号的语句
    $("#idMust", $("#" + strDivP031201_byd)).append("NO:" + strDjbh);
    //获得jqGrid的列名和属性
    objP031201_byd = ParseData(window.external.DoOptions(strTableP031201_byd, 'GetTableConfig', '{}'));

    strPageHeader = objP031201_byd.pageHeader;
    strpageFooter = objP031201_byd.pageFooter;

    strHeadP031201_byd += "<ul>";
    for (var i = 0; i < strPageHeader.length; i++) {            //创建页头
        strHeadP031201_byd += "<li>";
        strHeadP031201_byd += strPageHeader[i]["xsmc"] + "：";
        strHeadP031201_byd += "<input type='text'  value=''  name='" + strPageHeader[i]["name"] + "' disabled";
        strHeadP031201_byd += " id='" + strPageHeader[i]["name"] + "' maxlength='50'/>"; //限制输入长度
        strHeadP031201_byd += "</li>";
    }
    strHeadP031201_byd += "</ul>";
    $("#" + strDivP031201_byd + "_divform").append(strHeadP031201_byd);

    strFormTableP031201_byd += "<table id='Tp030302'></table>";         //创建表格
    $("#" + strDivP031201_byd + "_divform").append(strFormTableP031201_byd);

    strDownP031201_byd += "<ul>";
    for (var i = 0; i < strpageFooter.length; i++) {            //创建页尾
        if (strpageFooter[i]["name"] == "a4") {
            strDownP031201_byd += "<li class='bz'>";
            strDownP031201_byd += strpageFooter[i]["xsmc"] + "：";
            strDownP031201_byd += "<input type='text'  value=''  name='" + strpageFooter[i]["name"] + "' disabled";
            strDownP031201_byd += " id='" + strpageFooter[i]["name"] + "' maxlength='50'/>"; //限制输入长度
            strDownP031201_byd += "</li>";
        } else {
            strDownP031201_byd += "<li>";
            strDownP031201_byd += strpageFooter[i]["xsmc"] + "：";
            strDownP031201_byd += "<input type='text'  value=''  name='" + strpageFooter[i]["name"] + "' disabled";
            strDownP031201_byd += " id='" + strpageFooter[i]["name"] + "' maxlength='50'/>"; //限制输入长度
            strDownP031201_byd += "</li>";
        }
    }
    strDownP031201_byd += "</ul>";
    $("#" + strDivP031201_byd + "_divform").append(strDownP031201_byd);

    //释放内存
    strHeadP031201_byd = null;
    strFormTableP031201_byd = null;
    strDownP031201_byd = null;
    objP031201_byd = null;
    strPageHeader = null;
    strpageFooter = null;
}

//从后台加载所查单据数据
function myListP031201_byd(strDivP031201_byd, strDjbh) {
    try {
        var objP031201_byd = {};                //存放表格的列配置
        var objResultP031201_byd = {};      //存放返回信息
        var myList = [];                                //存放返回的数据集
        var count = 5;                                  //表格默认显示5条数据

        //获得jqGrid的列名和属性
        objP031201_byd = ParseData(window.external.DoOptions(strTableP031201_byd, 'GetTableConfig', '{}'));
        objResultP031201_byd = ParseData(window.external.DoOptions(strTableP031201_byd, 'GetList', '{"djbh":"' + strDjbh + '"}'));

        //创建表格
        createGrid_P031201_byd(strTableP031201_byd, objP031201_byd.colNames, objP031201_byd.colModel);

        //格式化表格中的数据
        $("#" + strTableP031201_byd).setColProp("sl", { formatter: formatCountPlace });
        $("#" + strTableP031201_byd).setColProp("dj", { formatter: formatMoneyPlace });
        $("#" + strTableP031201_byd).setColProp("je", { formatter: formatMoneyPlace });

        myList = objResultP031201_byd.Records;            //把返回的数据赋给【myList】
        count = myList.length > 5 ? myList.length : 5;      //表格默认最少显示5条数据

        var bysl = 0, byje = 0;
        for (var i = 0; i < count; i++) {
            if (myList[i]) {
                if (myList[i].sl) {                                              //报溢数量存在时
                    bysl += parseFloat(myList[i].sl);
                }
                if (myList[i].je) {                                             //报溢金额存在时
                    byje += parseFloat(myList[i].je);
                }
                $("#" + strTableP031201_byd).addRowData(i + 1, myList[i]);
            }
            else {
                $("#" + strTableP031201_byd).addRowData(i + 1, {});
            }
        }

        //调整表格列宽度自适应
        resizeGridScroll(strTableP031201_byd);

        //给单据日期赋值
        $("#djdate", $("#" + strDivP031201_byd + "_divform")).val(objResultP031201_byd.djdate);
        //给仓库赋值
        $("#ckmc", $("#" + strDivP031201_byd + "_divform")).val(objResultP031201_byd.ckmc);
        //给经手人赋值
        $("#zymc", $("#" + strDivP031201_byd + "_divform")).val(objResultP031201_byd.zymc);
        //给制单人赋值
        $("#czymc", $("#" + strDivP031201_byd + "_divform")).val(objResultP031201_byd.czymc);
        //给备注赋值
        $("#a4", $("#" + strDivP031201_byd + "_divform")).val(escape(objResultP031201_byd.a4));

        //合计
        $("#" + strTableP031201_byd).footerData("set", { rn: "合计",
            sl: bysl.toFixed(window.parent.strCountPlace),
            je: byje.toFixed(window.parent.strMoneyPlace)
        });

        //释放内存
        objP031201_byd = null;
        myList = null;
        objResultP031201_byd = null;
    } catch (e) { }
}

//格式化数量方法
function formatCountPlace(cellValue) {
    if (!isNaN(parseFloat(cellValue))) {
        return parseFloat(cellValue).toFixed(window.parent.strCountPlace);
    }
    else {
        return "&nbsp;";
    }
}

//格式化金额的方法
function formatMoneyPlace(cellValue) {
    if (!isNaN(parseFloat(cellValue))) {
        return parseFloat(cellValue).toFixed(window.parent.strMoneyPlace);
    }
    else {
        return "&nbsp;";
    }
}

/*-------------------------------------------------------------
功能：创建普通表格
*****①strTableID：表格的ID
*****②objColNames:表格需要显示的列名
*****③objColModel：表格的列配置
-----------------------------------------------------------------*/
function createGrid_P031201_byd(strTableID, objColNames, objColModel) {
    $("#" + strTableID).jqGrid({
        datatype: "local",
        colNames: objColNames,
        colModel: objColModel,
        rownumbers: true,
        rownumWidth: 30,
        footerrow: true,
        autowidth: true
    });
    //给列头加上序号
    $("#" + strTableID).setLabel("rn", "序号");
    //设置表格高度
    $("#" + strTableID).setGridHeight(141);
}