/*--------------------------文档信息-------------------------------------------
///功能：      预提规则设置——期初录入单
///创建人：  王群
///创建时间：2011年05月31日
///修改人：  --------------
///修改时间：--------------
///详细说明：--------------
-----------------------------------------------------------------------------*/
//先定义一下全局变量
var strTableP050604 = "Tp050604";
var strDivP050604 = "divP050604";
var objP050604 = {};
var iRowP050604 = 0;    //当前选中单元格的row索引
var iColP050604 = 0;    //当前选中单元格的col索引

/*-----------------------------------------------------------------------------
//参数：
//strDivP050604:弹出所用的层
-----------------------------------------------------------------------------*/
function showDialogP050604() {
    try {
        $(".operate").remove();                         //清除浮动按钮层
        $("#" + strDivP050604).empty();         //清空弹出窗口内容

        //把弹出窗口框架及底部按钮添加到父窗口创建dialog中
        creatDiv_ShowDlgHaveTbl(strDivP050604, "期初录入", false, true, strTableP050604);

        //隐藏保存新增的按钮
        $("#divP050604 .btn02").hide();

        //获得jqGrid的列名和属性
        objP050604 = ParseData(window.external.DoOptions(strTableP050604, 'GetTableConfig', '{}'));
        var strFormTableP050604 = "<table id='Tp050604'></table>";
        $("#" + strDivP050604 + "_divform").append(strFormTableP050604);

        //调用所查数据同时加载到所对应的控件
        myListP050604();
    } catch (e) { }
}

//从后台加载数据的方法
function myListP050604() {
    var objResultP050604 = ParseData(window.external.DoOptions(strTableP050604, 'GetList', '{}'));      //存放从后台请求的数据
    var objmyList = [];        //存放后台返回的数据信息
    var icount = 5;            //存放返回的数据的条目数

    //创建表格 
    createGrid_P050604(strTableP050604, objP050604.colNames, objP050604.colModel);
    $("#" + strTableP050604).setGridParam({ afterEditCell: afterEditCell_P050604 });
    $("#" + strTableP050604).setColProp("je", { formatter: formatMoneyPlace });

    objmyList = objResultP050604.Records;       //得到当前的数据集
    icount = objmyList.length > 5 ? objmyList.length : 5;       //得到当前条目数
    for (var i = 0; i < icount; i++) {
        if (objmyList[i]) {
            //添加后台数据行
            $("#" + strTableP050604).addRowData(i + 1, objmyList[i]);      //添加后台数据行
            //灰化掉不可以改的东西
            $("#" + strTableP050604).setCell((i + 1), "kmmc", "", { color: "#CCCCCC" });
            $("#" + strTableP050604).setCell((i + 1), "ytdxmc", "", { color: "#CCCCCC" });
            $("#" + strTableP050604).setCell((i + 1), "jsrmc", "", { color: "#CCCCCC" });
        }
        else {
            //添加空行
            $("#" + strTableP050604).addRowData(i + 1, {});
        }
    }
    resizeGridScroll(strTableP050604);       //调整表格自适应的宽度
    //释放内存
    objResultP050604 = null;
    objmyList = null;
    icount = null;
}

//..............................单元格的保存方法..................................
function afterEditCell_P050604(rowId, cellname, value, iRow, iCol) {
    var objTr = $(this).getRowData(rowId);      //存放当前选中行的数据对象
    if (objTr.gzguid == "") {
        $(this).restoreCell(iRow, iCol);
        return;
    }
    iRowP050604 = iRow;
    iColP050604 = iCol;
    var item = $("#" + rowId + " :input[name='" + cellname + "']", this);
    if (cellname == "je") {
        item.change(function () { dataChangeState(true) }); //流动金额
        item.autoNumeric({ mDec: window.parent.strMoneyPlace, aSep: "", aNeg: "-" }).focus(function () { this.select(); }).css("ime-mode", "disabled");

    }
    //释放内存
    item = null;
}

//保存方法
function saveDataP050604() {
    try {
        var strRecordP050604 = checkDataValidateP050604();   //数据有效性验证,组织要保存的数据
        var objRetP050604 = {};      // 存放从后台返回来的信息
        if (strRecordP050604 == "{'Records':[]}") {
            window.parent.cihoAlert(msg.other.dataNoComplete);                //录入数据不完整;
            return;
        }
        //保存方法的执行
        objRetP050604 = ParseData(window.external.DoOptions(strTableP050604, 'Add', strRecordP050604));
        if (!objRetP050604) {      //DB操作失败!
            window.parent.cihoAlert(msg.other.dbOprateErr);
            return;
        }
        if (objRetP050604.ReturnCode == "0") {  //关闭当前窗口
            changeComboZt("TKjkm");
            cihoDlgClose(strDivP050604);               //关闭弹出层
            //$("#" + strDivP050604).dialog("close");
        }
        //操作失败，弹出提示消息
        else {
            window.parent.cihoAlert(msg.save.saveFail);
        }
        //内存释放区域
        strRecordP050604 = null;
        objRetP050604 = null;
    } catch (e) { }
}

//验证表单方法
function checkDataValidateP050604() {
    $("#" + strTableP050604).saveCell(iRowP050604, iColP050604);   //取消表格中的数据的编辑状态，保存数据
    var arrIds = $("#" + strTableP050604).getDataIDs();    //存放表格的ID
    var strRecord = "{'Records':[";         //存放向后台传递的json数据
    for (var i = 0; i < arrIds.length; i++) {
        var objTr = $("#" + strTableP050604).getRowData(arrIds[i]);     //存放表格中数据对象
        if (objTr.gzguid == "") {
            continue;
        }
        strRecord += "{";
        strRecord += "\"gzguid\":\"" + objTr.gzguid + "\",";
        strRecord += "\"kmdm\":\"" + objTr.kmdm + "\",";
        strRecord += "\"je\":\"" + objTr.je + "\"";
        strRecord += "},";
    }
    if (strRecord.substring(strRecord.length - 1, strRecord.length) != "[") {
        strRecord = strRecord.substr(0, strRecord.length - 1);
    }
    strRecord += "]}"

    return strRecord;
    //释放内存
    arrIds = null;
    strRecord = null;
    objTr = null;
}

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

//变动数据的标志位方法
function dataChangeState(bState) {
    //修改Data change状态
    $("#" + strDivP050604 + "_divform").data("change", bState);
}

/*-------------------------------------------------------------
功能：创建普通表格
*****①strTableID：表格的ID
*****②objColNames:表格需要显示的列名
*****③objColModel：表格的列配置
-----------------------------------------------------------------*/
function createGrid_P050604(strTableID, objColNames, objColModel) {
    $("#" + strTableID).jqGrid({
        datatype: "local",
        sortname: "id",
        colNames: objColNames,
        colModel: objColModel,
        rowNum: 9999999,
        rownumbers: true,
        autowidth: true,
        rownumWidth: 30,
        cellEdit: true,
        cellsubmit: "clientArray",
        gridview: true
    });
    //设置表格高度
    $("#" + strTableID).setGridHeight(157);
    //增加表头‘序号’列
    $("#" + strTableID).setLabel("rn", "序号");
    //调整表格横向滚动条
    resizeGridScroll(strTableID);
}