/*--------------------------文档信息-------------------------------------------
///功能：      点击资金转账——弹出资金转账单
///创建人：  王群
///创建时间：2011年05月31日
///修改人：  --------------
///修改时间：--------------
///详细说明：--------------
-----------------------------------------------------------------------------*/
var strTableP050201 = "Tp050201";          //定义表格的ID
var strDlgIdP050201 = "divP0502";           //定义弹出层的Div
var bSvaeSignP050201 = 1;                       //定义保存的标志位
var objRetRecord = {};                               //修改时保存要修改的记录
var objconfig = {};                                     //列配置
var iRowP050201 = 0;                               //当前选中单元格的row索引
var iColP050201 = 0;                                //当前选中单元格的col索引
var iRowIdP050201 = 0;                           //当前选中单元格的rowid

//在jqGrid中自定义下拉框
//创建select表单元素
function my_inputP050201(value, options) {
    var strHtml = "<input id=\"" + options.id + "\" class=\"ciho-Input\"  name=\"" + options.name + "\">";
    return $(strHtml);
}

//保存选中的值
function my_valueP050201(ele) {
    return ele.cihoComboTree('getText');
}

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

//给表格中select控件转化成自定义的形式
function userdefined_P050201() {
    //设置属性custom:绑定combobox：流出账户
    $("#" + strTableP050201).setColProp("lczhmc", { edittype: "custom" });
    $("#" + strTableP050201).setColProp("lczhmc", { editoptions: { custom_element: my_inputP050201, custom_value: my_valueP050201} });

    //流入账户
    $("#" + strTableP050201).setColProp("lrzhmc", { edittype: "custom" });
    $("#" + strTableP050201).setColProp("lrzhmc", { editoptions: { custom_element: my_inputP050201, custom_value: my_valueP050201} });

    //编辑状态，绑定事件
    $("#" + strTableP050201).setGridParam({ afterEditCell: afterEditCellP050201 });
    //格式化表中的数据
    $("#" + strTableP050201).setColProp("a1", { formatter: formatterP050201Money });
    //给转账日期绑定日期控件
    inputAndBindData("djdate", false, "#" + strDlgIdP050201, function () { $("#" + strDlgIdP050201 + "_divform").data("change", true); });
}

/*-----------------------------------------------------------------------------
//功能：点击添加按钮，弹出对话框
//参数：
//strType:1：添加
//2：修改
//strTableP050201 表名
-----------------------------------------------------------------------------*/
function showDialog_P050201(strType) {
    try {
        //清空弹出窗口内容
        $("#" + strDlgIdP050201).empty();
        //添加
        if (strType == "1") {
            //值为1，插入数据；值为2，更新数据
            bSvaeSignP050201 = "1";
        }
        //修改
        else if (strType == "2") {
            bSvaeSignP050201 = "2";
        }
        //创建弹出窗口dialog
        creatDiv_ShowDlgHaveTbl(strDlgIdP050201, "资金转账", false, true, strTableP050201, "500");

        //获得jqGrid的列名和属性
        objconfig = ParseData(window.external.DoOptions(strTableP050201, 'GetTableConfig', '{}'));
        if (!objconfig) {
            //列配置取得失败
            window.parent.cihoAlert(msg.cg.LineFail);
            return;
        }
        //添加表单元素到窗口中
        tableFormP050201(strType);

        //创建表格
        createGridP050201(strTableP050201, objconfig.colNames, objconfig.colModel);

        //设定表格的高度
        $("#" + strTableP050201).setGridHeight(50);
        resizeGridScroll(strTableP050201);

        //给表格中select控件转化成自定义的形式
        userdefined_P050201();
        //给摘要设上焦点
        $("#a2", "#" + strDlgIdP050201).focus();
        //添加
        if ("1" == strType) {
            var nowData = getNowDate();                                         //返现日期，默认当前日期
            $("#djdate", $("#" + strDlgIdP050201 + "_divform")).val(nowData);
            $("#" + strTableP050201).addRowData(1, {});                 //加载空记录
            nowData = null;
        }
        //修改
        else if ("2" == strType) {
            setDjValueP050201();                            //设定单据的原始值
            $("#djdate").attr("disabled", true);        //修改的时候日期设为灰化且不可编辑状态
            iRowIdP050201 = $("#" + strTableP050201).jqGrid("getDataIDs")[0];
        }

        //设定data change 为false
        dataChangeState(false);
    } catch (e) { }
}

//创建表单
function tableFormP050201() {
    //获取列配置
    var strPageHeader = objconfig.pageHeader;
    var strpageFooter = objconfig.pageFooter;
    //创建页头
    var pageHead = "<ul>";
    for (var i = 0; i < strPageHeader.length; i++) {
        pageHead += "<li>";
        pageHead += strPageHeader[i]["xsmc"] + "：";
        pageHead += "<input id='" + strPageHeader[i]["index"] + "' name='" + strPageHeader[i]["name"] + "' type='text'/> "; //限制输入，粘贴特殊字符
        pageHead += "</li>";
    }
    pageHead += "</ul>";
    //创建表格
    $("#" + strDlgIdP050201 + "_divform").append(pageHead);
    var tableP050201 = "<table id='Tp050201'><table>";
    $("#" + strDlgIdP050201 + "_divform").append(tableP050201);
    //创建页尾
    var pageFoot = "<ul>";
    for (var i = 0; i < strpageFooter.length; i++) {
        if (strpageFooter[i]["name"] == "czymc") {
            continue;
        }
        if (strpageFooter[i]["name"] == "a2") {
            pageFoot += "<li class='bz'>";
            pageFoot += strpageFooter[i]["xsmc"] + "：";
            pageFoot += "<input id='" + strpageFooter[i]["index"] + "' name='" + strpageFooter[i]["name"] + "' type='text' ";
            pageFoot += " onkeypress='return checkValidate(event)' onpaste='return validatePaste()'/>"; //限制输入，粘贴特殊字符
            pageFoot += "</li>";
        }
        else {
            pageFoot += "<li class='inp_70'>";
            pageFoot += strpageFooter[i]["xsmc"] + "：";
            pageFoot += "<input id='" + strpageFooter[i]["index"] + "' name='" + strpageFooter[i]["name"] + "' type='text' />";
            pageFoot += "</li>";
        }
    }
    pageFoot += "</ul>";
    $("#" + strDlgIdP050201 + "_divform").append(pageFoot);

    //设定制单人
    $("#czydm", "#" + strDlgIdP050201 + "_divform").attr("disabled", true);
    $("#czydm", "#" + strDlgIdP050201 + "_divform").val(window.parent.objLoginInfo.name);

    //设定状态
    $(":input[name]", $("#" + strDlgIdP050201 + "_divform")).change(function () {
        dataChangeState(true);
    });
}

//修改时，设定单据的原值
function setDjValueP050201() {
    var strDjbh = $("#" + strTableP0502).getCell(strSelRowIDP0502, "djbh");
    //选中行ID；
    objRetRecord = ParseData(window.external.DoOptions(strTableP050201, 'GetList', "{'djbh':'" + strDjbh + "'}"));
    if (!objRetRecord) {
        //"DB操作失败！"
        window.parent.cihoAlert(msg.other.dbOprateErr);
        return;
    }
    else {
        if (objRetRecord.Records == "{}") {
            //数据为空"
            window.parent.cihoAlert(msg.other.dataEmp);
            return;
        }
        else {
            //加载数据
            objRetRecord.Records[0]["outkmdm"] = objRetRecord.Records[0]["lczhdm"];
            objRetRecord.Records[0]["inkmdm"] = objRetRecord.Records[0]["lrzhdm"];
            $("#" + strTableP050201).setColProp("a1", { formatter: formatMoneyPlace });
            //填充表格
            loadTreeData(strTableP050201, objRetRecord.Records);
        }
        resizeGridScroll(strTableP050201);
    }
    //加载资金转账单编号
    $("#idMust", $("#" + strDlgIdP050201)).append("NO:" + objRetRecord.Records[0].djbh);
    //转账日期
    $("#djdate", $("#" + strDlgIdP050201 + "_divform")).val(objRetRecord.Records[0].djdate);
    $("#czydm", $("#" + strDlgIdP050201 + "_divform")).val(objRetRecord.Records[0]["czymc"]);
    $("#a2", $("#" + strDlgIdP050201 + "_divform")).val(escape(objRetRecord.Records[0].a2));
    //释放内存
    strDjbh = null;
}

/*--------------------------------------------------------
///    功能：用于创建一个表格
///    strTableID:表格ID
///    objColNames:表格的列名
///    objColModel:表格的列配置
-----------------------------------------------------------*/
function createGridP050201(strTableID, objColNames, objColModel) {
    $("#" + strTableID).jqGrid({
        datatype: "local",
        colNames: objColNames,
        colModel: objColModel,
        data: [],
        rownumbers: true,
        rowNum: 9999,
        sortname: "id",
        autowidth: true,
        width: 600,
        cellEdit: true,
        cellsubmit: "clientArray",
        gridview: true
    });
    //增加表头‘序号’列
    // $("table.ui-jqgrid-htable th:first", $("#gbox_" + strTableID)).text("序号");
    $("#" + strTableID).setLabel("rn", "序号");
}

//编辑状态，设置单元格属性
function afterEditCellP050201(rowId, cellname, value, iRow, iCol) {
    iRowIdP050201 = rowId;
    iRowP050201 = iRow;
    iColP050201 = iCol;
    var item = $("#" + rowId + " :input[name='" + cellname + "']", this);
    switch (cellname) {
        case "lczhmc":
            item.cihoComboTree({
                width: 120,
                showBox: false,
                mode: "child",
                showMode: "all"
            });
            var objLcResult = ParseData(window.external.DoOptions(strTableP050201, "GetComboBoxData", '{}'));
            //加载数据
            item.cihoComboTree('loadData', objLcResult.Records);
            if (trim(item.cihoComboTree("getText")) != "" && trim(item.cihoComboTree("getText")) == $(this).getCell(rowId, "outkmdm")) {
                item.cihoComboTree('setText', objRetRecord.Records[0]["lczhmc"]);
            }
            //流出账户
            item.cihoComboTree({
                onSelect: function (Record) {
                    if (Record.dm != undefined) {
                        $("#" + strTableP050201).setCell(rowId, "outkmdm", Record.dm);
                    }
                    else {
                        //流出账户为空或者不存在;
                        $("#" + strTableP050201).setCell(rowId, "outkmdm", "&nbsp;");
                    }
                    //修改Data change状态
                    dataChangeState(true);
                }
            });
            //设定选中值
            item.cihoComboTree("setValue", $(this).getCell(rowId, "outkmdm"));
            item.cihoComboTree('setText', value);
            item.cihoComboTree("focus");
            break;
        case "lrzhmc":
            item.cihoComboTree({
                width: 120,
                showBox: false,
                mode: "child",
                showMode: "all"
            });
            var objLrResult = ParseData(window.external.DoOptions(strTableP050201, "GetComboBoxData", '{}'));
            //加载数据
            item.cihoComboTree('loadData', objLrResult.Records);
            if (trim(item.cihoComboTree("getText")) != "" && trim(item.cihoComboTree("getText")) == $(this).getCell(rowId, "inkmdm")) {
                item.cihoComboTree('setText', objRetRecord.Records[0]["lrzhmc"]);
            }
            //流出账户
            item.cihoComboTree({
                onSelect: function (Record) {
                    if (Record.dm != undefined) {
                        $("#" + strTableP050201).setCell(rowId, "inkmdm", Record.dm);
                    }
                    else {
                        //流出账户为空或者不存在;
                        $("#" + strTableP050201).setCell(rowId, "inkmdm", "&nbsp;");
                    }
                    //修改Data change状态
                    dataChangeState(true);
                }
            });
            //设定选中值
            item.cihoComboTree("setValue", $(this).getCell(rowId, "inkmdm"));
            item.cihoComboTree('setText', value);
            item.cihoComboTree("focus");
            break;
        case "a1":
            item.change(function () { dataChangeState(true) }); //流动金额
            item.autoNumeric({ mDec: window.parent.strMoneyPlace, aSep: "" }).focus(function () { this.select(); }).css("ime-mode", "disabled");
            break;
    }
}

//资金转账单的保存方法
function saveDataP050201(strType) {
    //数据有效性验证,组织要保存的数据
    var strRecord_P050201 = checkDataValidateP050201();
    if (!strRecord_P050201) {    //录入数据不完整;
        return;
    }

    if (bSvaeSignP050201 == "1") {    //添加
        var objRet_P050201 = ParseData(window.external.DoOptions(strTableP050201, 'Add', strRecord_P050201));
    }
    else {     //修改
        var objRet_P050201 = ParseData(window.external.DoOptions(strTableP050201, 'Edit', strRecord_P050201));
    }

    if (!objRet_P050201) {
        window.parent.cihoAlert(msg.other.dbOprateErr);      //"DB操作失败!"
        return;
    }
    //操作成功
    if ("0" == objRet_P050201.ReturnCode) {
        $("#idMust", $("#" + strDlgIdP050201)).empty();       //清空编号

        //重新加载管理画面数据
        searchDataClick_P0502(0);

        //保存新增
        if (strType == "2") {

            //保存标志为设置为添加
            bSvaeSignP050201 = "1";

            //清空当前窗口数据
            $(":input[name]", $("#" + strDlgIdP050201 + "_divform")).val("");

            //清除单据编号
            $("#" + strDlgIdP050201 + "_pop_box_top").empty();

            //转账日期
            $("#djdate", $("#" + strDlgIdP050201 + "_divform")).val(getNowDate());
            $("#djdate", $("#" + strDlgIdP050201 + "_divform")).attr("disabled", false);

            //制单人
            $("#czydm", $("#" + strDlgIdP050201 + "_divform")).val(window.parent.objLoginInfo.name);
            $("#a2", "#" + strDlgIdP050201).focus();
            //清空表格中的元素
            $("#" + strTableP050201).jqGrid("clearGridData");

            //加载空记录
            $("#" + strTableP050201).addRowData(1, {});

            //表单元素置为未修改
            dataChangeState(false);
        }
        //保存退出
        else {
            //$("#" + strDlgIdP050201).dialog("close");   
            cihoDlgClose(strDlgIdP050201);                  //关闭当前窗口
        }
    }
    //操作失败，弹出提示消息
    else {
        window.parent.cihoAlert(msg.save.saveFail);
    }
    //内存释放区域
    strRecord_P050201 = null;
    objRet_P050201 = null;
}

//验证数据有效性方法
function checkDataValidateP050201() {
    //取消表格中的数据的编辑状态，保存数据
    $("#" + strTableP050201).saveCell(iRowP050201, iColP050201);

    //组织要保存的数据
    var arrList = "";
    var strZhdate = trim($("#djdate", $("#" + strDlgIdP050201 + "_divform")).val());
    //转账日期不存在时
    if (!strZhdate) {
        window.parent.cihoAlert(msg.cw.zhDataEmp, null, function () { $("#djdate", $("#" + strDlgIdP050201 + "_divform")).focus(); });
        return;
    }
    //日期不在有效期范围内时
    if ((strZhdate.replace(/-/g, "/")) < (window.parent._startDate.replace(/-/g, "/"))) {
        window.parent.cihoAlert(msg.other.djrqExtent, null, function () { $("#djdate", $("#" + strDlgIdP050201 + "_divform")).focus(); });
        return;
    }
    arrList += "\"djdate\":\"" + strZhdate + "\",";
    //制单人
    if (bSvaeSignP050201 == "1") {
        arrList += "\"czydm\":\"" + window.parent.objLoginInfo.code + "\",";
    }
    else {
        arrList += "\"czydm\":\"" + objRetRecord.Records[0]["czydm"] + "\",";
    }

    //备注
    arrList += "\"a2\":\"" + trim($("#a2", $("#" + strDlgIdP050201 + "_divform")).val()) + "\"";

    //获得表格的数据
    var strLdje = $("#" + strTableP050201).jqGrid("getCell", iRowIdP050201, "a1");
    //流动金额不存在时
    if (!strLdje) {
        window.parent.cihoAlert(msg.cw.ldMoneyEmp);
        return;
    }
    //获得当前的流入账户的代码
    var strLrzh = $("#" + strTableP050201).jqGrid("getCell", iRowIdP050201, "inkmdm");

    //流入账户不存在时
    if (!strLrzh) {
        window.parent.cihoAlert(msg.cw.lrZhEmp);
        return;
    }
    //获得当前的流出账户的代码
    var strLczh = $("#" + strTableP050201).jqGrid("getCell", iRowIdP050201, "outkmdm");
    //流出账户不存在时
    if (!strLczh) {
        window.parent.cihoAlert(msg.cw.lcZhEmp);
        return;
    }
    //流入账户和流出账户重名，请重新选择！
    if (strLrzh == strLczh) {
        window.parent.cihoAlert(msg.cw.sameZh);
        return;
    }
    var strRecord = "{\"Records\":";
    strRecord += "[{";
    if (bSvaeSignP050201 == "2") {
        strRecord += "\"a1\":\"" + strLdje + "\"";
        strRecord += ",\"outkmdm\":\"" + strLczh + "\"";
        strRecord += ",\"inkmdm\":\"" + strLrzh + "\"";
        strRecord += "," + arrList + "}]";
        strRecord += ",\"djbh\":\"";
        strRecord += objRetRecord.Records[0]["djbh"] + "\"";
        strRecord += "}";
    }
    else if (bSvaeSignP050201 == "1") {
        strRecord += "\"mxguid\":\"\",";
        strRecord += "\"a1\":\"" + strLdje + "\"";
        strRecord += ",\"outkmdm\":\"" + strLczh + "\"";
        strRecord += ",\"inkmdm\":\"" + strLrzh + "\"";
        strRecord += ",\"djbh\":\"\"";
        strRecord += ",\"djtypebh\":\"\"";
        strRecord += "," + arrList + "}]";
        strRecord += "}";
    }
    return strRecord;
    //释放内存
    arrList = null;
    strZhdate = null;
    strLczh = null;
    strLrzh = null;
    strRecord = null;
    strLdje = null;
}

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

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