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

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

        //把弹出窗口框架及底部按钮添加到父窗口创建dialog中
        creatDiv_ShowDlgHaveTbl(strDivP031201_bsd, "报损/赠送", false, false, strTableP031201_bsd, "850");

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

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

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

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

}

//创建表单
function tableFormP031201_bsd(strDivP031201_bsd, strDjbh) {
    var strHeadP031201_bsd = "";                    //页头默认为空
    var strFormTableP031201_bsd = "";           //表格默认为空
    var strDownP031201_bsd = "";                    //页尾默认为空
    var objP031201_bsd = {};                            //存放表格的列配置
    var strPageHeader = [];                               //页头的列配置
    var strpageFooter = [];                                //页尾的列配置

    //加载编号的语句
    $("#idMust", $("#" + strDivP031201_bsd)).append("NO:" + strDjbh);

    //获得jqGrid的列名和属性
    objP031201_bsd = ParseData(window.external.DoOptions(strTableP031201_bsd, 'GetTableConfig', '{}'));
    strPageHeader = objP031201_bsd.pageHeader;
    strpageFooter = objP031201_bsd.pageFooter;

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

    strFormTableP031201_bsd += "<table id='Tp030301'></table>";         //创建表格
    $("#" + strDivP031201_bsd + "_divform").append(strFormTableP031201_bsd);

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

    //释放内存
    strHeadP031201_bsd = null;
    strFormTableP031201_bsd = null;
    strDownP031201_bsd = null;
    objP031201_bsd = null;
    strPageHeader = null;
    strpageFooter = null;
}

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

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

        //创建表格
        createGrid_P031201_bsd(strTableP031201_bsd, objP031201_bsd.colNames, objP031201_bsd.colModel);

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

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

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

        resizeGridScroll(strTableP031201_bsd);           //调整列自适应宽度

        //给单据日期赋值
        $("#djdate", $("#" + strDivP031201_bsd + "_divform")).val(objResultP031201_bsd.djdate);
        //给仓库赋值
        $("#ckmc", $("#" + strDivP031201_bsd + "_divform")).val(objResultP031201_bsd.ckmc);
        //给经手人赋值
        $("#zymc", $("#" + strDivP031201_bsd + "_divform")).val(objResultP031201_bsd.zymc);
        //给移库类型赋值
        if (objResultP031201_bsd.djtypebh == 701) {
            $("#djtypebh", $("#" + strDivP031201_bsd + "_divform")).val("报损");
        }
        if (objResultP031201_bsd.djtypebh == 703) {
            $("#djtypebh", $("#" + strDivP031201_bsd + "_divform")).val("赠送");
        }
        //给制单人赋值
        $("#czymc", $("#" + strDivP031201_bsd + "_divform")).val(objResultP031201_bsd.czymc);
        //给备注赋值
        $("#a4", $("#" + strDivP031201_bsd + "_divform")).val(escape(objResultP031201_bsd.a4));

        //创建合计行
        $("#" + strTableP031201_bsd).footerData("set", { rn: "合计",
            sl: bssl.toFixed(window.parent.strCountPlace),
            je: bsje.toFixed(window.parent.strMoneyPlace)
        });

        //释放内存
        objP031201_bsd = null;
        myList = null;
        objResultP031201_bsd = 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_bsd(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);
}