////箱号批号所查看的单据
//先定义一下全局变量
var strTableP050904 = "Tp050904";
var strDivP050904 = "divP050904";
var objP050904;
var iRowP050904;    //当前选中单元格的row索引
var iColP050904;    //当前选中单元格的col索引
/*-----------------------------------------------------------------------------
//参数：
//strDivP050904:弹出所用的层
-----------------------------------------------------------------------------*/
function showDialogP050904() {
    //清除浮动按钮层
    $(".operate").remove();
    //清空弹出窗口内容
    $("#" + strDivP050904).empty();
    //添加
    //把弹出窗口框架及底部按钮添加到父窗口创建dialog中
    creatDiv_ShowDlgHaveTbl(strDivP050904, "期初录入", false, true, strTableP050904);  //创建弹出窗口
    //隐藏保存新增的按钮
    $("#divP050904 .btn02").hide();
    //获得jqGrid的列名和属性
    objP050904 = ParseData(window.external.DoOptions(strTableP050904, 'GetTableConfig', '{}'));
    var strFormTableP050904 = "<table id='Tp050904'></table>";
    $("#" + strDivP050904 + "_divform").append(strFormTableP050904);
    //调用所查数据同时加载到所对应的控件
    myListP050904();
    //重新定位dialog的位置
    $("#" + strDivP050904).dialog("option", "position", "center");
}
//创建jqGrid
function createGrid_P050904(strTableID, objColNames, objColModel) {
    $("#" + strTableID).jqGrid({
        datatype: "local",
        sortname: "id",
        colNames: objColNames,
        colModel: objColModel,
        data: [],
        rowNum: 9999999,
        rownumbers: true,
        autowidth: true,
        rownumWidth: 30,
        height: "100%",
        cellEdit: true,
        cellsubmit: "clientArray",
        gridview: true
    }).parent(".ui-jqgrid-bdiv").siblings();
    //增加表头‘序号’列
    $("table.ui-jqgrid-htable th:first", $("#gbox_" + strTableID)).text("序号");
}
//从后台加载数据
function myListP050904() {
    var objResultP050904 = ParseData(window.external.DoOptions(strTableP050904, 'GetList', '{}'));
    createGrid_P050904(strTableP050904, objP050904.colNames, objP050904.colModel);
    $("#" + strTableP050904).setGridParam({ afterEditCell: afterEditCell_P050904 });
    $("#" + strTableP050904).setColProp("je", { formatter: formatMoneyPlace });
    var myList = objResultP050904.Records;
    var count = myList.length > 5 ? myList.length : 5;
    for (var i = 0; i < count; i++) {
        if (myList[i]) {
            //添加后台数据行
            $("#" + strTableP050904).addRowData(i + 1, myList[i]);      //添加后台数据行
            //灰化掉不可以改的东西
            $("#" + strTableP050904).setCell((i + 1), "kmmc", "", { color: "#CCCCCC" });
            $("#" + strTableP050904).setCell((i + 1), "ytdxmc", "", { color: "#CCCCCC" });
            $("#" + strTableP050904).setCell((i + 1), "jsrmc", "", { color: "#CCCCCC" });
        }
        else {
            //添加空行
            $("#" + strTableP050904).addRowData(i + 1, {});
        }
    }

}
//..............................方法..................................
function afterEditCell_P050904(rowId, cellname, value, iRow, iCol) {
    var objTr = $(this).getRowData(rowId);
    if (objTr.gzguid == "") {
        $(this).restoreCell(iRow, iCol);
        return;
    }
    iRowP050904 = iRow;
    iColP050904 = iCol;
    var item = $("#" + rowId + " :input[name='" + cellname + "']", this);
    if (cellname == "je") {
        item.change(function () { dataChangeState(true) }); //流动金额
        item.autoNumeric({ mDec: window.parent.strMoneyPlace, aSep: "" }).focus(function () { this.select(); }).css("ime-mode", "disabled");
    }
    //释放内存
    item = null;
}
function saveDataP050904() {
    //数据有效性验证,组织要保存的数据
    var strRecordP050904 = checkDataValidateP050904();
    if (!strRecordP050904) {
        //录入数据不完整;
        return;
    }
    var objRetP050904 = ParseData(window.external.DoOptions(strTableP050904, 'Add', strRecordP050904));
    if (!objRetP050904) {
        //DB操作失败!
        window.parent.cihoAlert(msg.other.dbOprateErr);
        return;
    }
    if (objRetP050904.ReturnCode == "0") {
        //关闭当前窗口
        $("#" + strDivP050904).dialog("close");
    }
    //操作失败，弹出提示消息
    else {
        window.parent.cihoAlert(msg.save.saveFail);
    }
    //内存释放区域
    strRecordP050904 = null;
    objRetP050904 = null;
}
//验证表单
function checkDataValidateP050904() {
    //取消表格中的数据的编辑状态，保存数据
    $("#" + strTableP050904).saveCell(iRowP050904, iColP050904);
    var arrIds = $("#" + strTableP050904).getDataIDs();
    var strRecord = "{'Records':[";
    for (var i = 0; i < arrIds.length; i++) {
        var objTr = $("#" + strTableP050904).getRowData(arrIds[i]);
        if (objTr.gzguid == "") {
            continue;
        }
        strRecord += "{";
        strRecord += "\"gzguid\":\"" + objTr.gzguid + "\",";
        strRecord += "\"je\":\"" + objTr.je + "\"";
        strRecord += "},";
    }
    if (strRecord.substring(strRecord.length - 1, strRecord.length) != "[") {
        strRecord = strRecord.substr(0, strRecord.length - 1);
    }
    strRecord += "]}"
     //alert(strRecord);
    return strRecord;
}
//格式化金额的方法
function formatMoneyPlace(cellValue) {
    if (!isNaN(parseFloat(cellValue))) {
        return parseFloat(cellValue).toFixed(window.parent.strMoneyPlace);
    }
    else {
        return "&nbsp;";
    }
}
function dataChangeState(bState) {
    //修改Data change状态
    $("#" + strDivP050904 + "_divform").data("change", bState);
}