/*************************
*功能：应收维护
*创建人：张振华
*时间：2010-08-17
*备注：
*************************/

//往弹出层里添加内容
function tableGzqcForm(strId) {

    //table对象
    var objTable = $("#" + strTableNameP060701);

    //列配置
    var objNames = objTable.jqGrid('getGridParam', "colNames");
    var objIndexs = objTable.jqGrid('getGridParam', "colModel");

    //部门，职员
    var objBm = getComboboxData('Tbmxx');
    var objZy = getComboboxData('TZyxx');
    var objZyRecords = objZy;
    objBm.unshift({ "dm": "-1", "mc": "全部部门", selected: true });
    objZyRecords.push({ "dm": "-1", "mc": "<font color='red'>添加职员</font>" });

    //其他变量
    var sign = "";
    var inputtype = "";
    var strForm = "";
    var strAppendTmp = "";
    var i = 0;

    //生成动态html
    strForm += "<ul>";
    for (i = 1; i < objNames.length; i++) {
        if (objIndexs[i]["name"] == "jldm" || objIndexs[i]["name"] == "bmmc"
        || objIndexs[i]["name"] == "zymc" || objIndexs[i]["name"] == "cz" || objIndexs[i]["name"] == "id") {
            continue;
        }
        //判断是否必填，加样式
        if (objIndexs[i]["editrules"] != null) {
            if (objIndexs[i]["editrules"].required == true) {
                sign = "<font color='red'>*</font>";
            }
            else {
                sign = "";
            }
        }
        if (objIndexs[i]["edittype"] == "text") {
            strAppendTmp += "<li>";
            strAppendTmp += sign;
            strAppendTmp += objNames[i] + "：";
            strAppendTmp += "<input type='text'  value=''  name='" + objIndexs[i]["name"] + "'";
            strAppendTmp += " id='" + objIndexs[i]["name"] + "' maxlength='50'"; //限制输入长度
            strAppendTmp += " onkeypress='return checkValidate(event)' onpaste='return validatePaste()'/>"; //限制输入，粘贴特殊字符
            strAppendTmp += "</li>";
        }
        if (objIndexs[i]["edittype"] == "select") {
            strAppendTmp += "<li>";
            strAppendTmp += sign + objNames[i] + "：";
            strAppendTmp += "<select name='" + objIndexs[i]["name"] + "' id='" + objIndexs[i]["name"] + "'";
            strAppendTmp += " class='easyui-combobox'>";
            strAppendTmp += "</select>";
            strAppendTmp += "</li>";
        }
    }
    strForm += strAppendTmp;
    strForm += "</ul>";

    //添加到层里
    $("#" + strDialogP060701 + "_divform").append(strForm);

    //职员代码
    $("#zydm", "#" + strDialogP060701).combobox({
        textField: "mc",
        valueField: "dm",
        width: 190,
        editable: true,
        onChange: function (newValue, oldValue) {
            $("#" + strDialogP060701 + "_divform").data("change", true);
            if (newValue != undefined) {
                var strBmdm = "";
                for (i = 0; i < objZy.length; i++) {
                    if (objZy[i].dm == newValue) {
                        strBmdm = objZy[i].bmdm;
                    }
                }
                //根据职员，找到部门，更新部门选中项
                if (strBmdm != "") {
                    $("#bmdm", "#" + strDialogP060701).combobox("setValue", strBmdm);
                }
                else {
                    $("#bmdm", "#" + strDialogP060701).combobox("setValue", "-1");
                }
            }
        },
        afterChange: function (newValue, newText) {
            if (newValue == "-1") {
                ciho.IniData({ id: "divAddType", title: "职员", table: "Tflxx" }, { jquery: $("#zydm", "#" + strDialogP060701), model: "TZyxx" });
                ciho.showTypeDialog();
            }
        }
    });
    if (objZyRecords != "{}") {
        $("#zydm", "#" + strDialogP060701).combobox("loadData", objZyRecords);
    }

    //部门代码
    $("#bmdm", "#" + strDialogP060701).combobox({
        textField: "mc",
        valueField: "dm",
        width: 190,
        editable: true,
        onSelect: function (record) {
            $("#" + strDialogP060701 + "_divform").data("change", true);

            //根据代码重新筛选职员
            objZyRecords = [];
            if (record.dm == "-1") {
                objZyRecords = getComboboxData('TZyxx'); ;
            }
            else {
                for (i = 0; i < objZy.length; i++) {
                    if (record.dm == objZy[i].bmdm) {
                        objZyRecords.push({ "dm": objZy[i].dm, "mc": objZy[i].mc });
                    }
                }
            }

            // 重新筛选出来数据，加上添加职员
            objZyRecords.push({ "dm": "-1", "mc": "<font color='red'>添加职员</font>" });
            $("#zydm", "#" + strDialogP060701).combobox("loadData", objZyRecords);
        }
    });
    if (objBm != "{}") {
        $("#bmdm", "#" + strDialogP060701).combobox("loadData", objBm);
    }

    //绑定控件
    $("#a1", "#" + strDialogP060701).autoNumeric({ mDec: window.parent.strMoneyPlace, aSep: "", aNeg: '-' }).focus(function () { this.select(); }).css("ime-mode", "disabled");

    //修改时，加载value
    if (strSaveSign == '2') {
        for (var i = 0; i < objIndexs.length; i++) {
            var objTr = objTable.getRowData(strId);
            objRecordsP060704 = objTr;                  //存放一下修改的数据集
            if (objIndexs[i].name == "bmdm" && objTr.bmdm != undefined) {
                $("#bmdm", "#" + strDialogP060701).combobox("setValue", objTr.bmdm);
                $("#bmdm", "#" + strDialogP060701).combobox("setText", objTr.bmmc);
            }
            else if (objIndexs[i].name == "zydm" && objTr.zydm != undefined) {
                $("#zydm", "#" + strDialogP060701).combobox("setValue", objTr.zydm);
                $("#zydm", "#" + strDialogP060701).combobox("setText", objTr.zymc);
            }
            else if (objIndexs[i].name == "a1") {
                $("#a1", "#" + strDialogP060701).val(parseFloat(objTr.a1).toFixed(window.parent.strMoneyPlace));
            }
            else {
                $('#' + objIndexs[i].name, "#" + strDialogP060701 + "_divform").val(objTr[objIndexs[i].name]);
            }
        }
    }

    //form change设定
    $(":input", "#" + strDialogP060701 + "_divform").change(function () {
        $("#" + strDialogP060701 + "_divform").data("change", true);
    });
    $("#" + strDialogP060701 + "_divform").data("change", false);

    //设置层序
    $(".layoutpanel").css("z-index", "50000009");

    setTimeout(function () { $("#a1", "#" + strDialogP060701).focus(); }, 100);
    //释放内存
    objTable = null;
    objNames = null;
    objIndexs = null;
}

//验证
function formDataValidateGzqc() {

    //职员代码
    var strZydm = $("#zydm", "#" + strDialogP060701).combobox("getValue");
    var strA1 = $("#a1", "#" + strDialogP060701).val();

    if (!strZydm) {
        //修改时，验证数据是否是停用的数据
        if (strSaveSign == "2" && $("#zydm", $("#" + strDialogP060701 + "_divform")).combobox('getText') == objRecordsP060704.zymc) {
            $("#zydm", $("#" + strDialogP060701 + "_divform")).combobox("setValue", objRecordsP060704.zydm);
            $("#zydm", $("#" + strDialogP060701 + "_divform")).combobox("setText", objRecordsP060704.zymc);
        }
        else {
            window.parent.cihoAlert(msg.sz.zy.empOrNotExit, null, function () { $("#zydm", "#" + strDialogP060701).combobox("focus"); });
            return false;
        }
    }
    if (!strA1) {
        window.parent.cihoAlert(msg.other.selQcNoEmpty, null, function () { $("#a1", "#" + strDialogP060701).focus(); });
        return false;
    }
    return true;
}

//保存
function SaveGzqc(saveType) {

    //验证
    if (false == formDataValidateGzqc()) {
        return;
    }

    var objRecord;
    var objResult;


    //组保存字符串
    var strData = "{";
    strData += "\"zydm\":\"" + $("#zydm", "#" + strDialogP060701).combobox("getValue") + "\"";
    strData += ",\"a1\":\"" + $("#a1", "#" + strDialogP060701).val() + "\"";
    strData += ",\"kmdm\":\"" + strKmdmP0607 + "\"";

    if (strSaveSign == "2") {
        var objRow = $("#" + strTableNameP060701).getRowData(strSelRowIdP060701);
        strData += ",\"jldm\":\"" + objRow.jldm + "\"";
    }
    strData += "}";

    //添加到前台的数据和保存的数据
    objRecord = ParseData(strData);
    objResult = saveData(strTableNameP060701, strData, strSaveSign);

    //保存失败后
    if (objResult.ReturnCode != "0") {
        window.parent.cihoAlert(objResult.MSG);
        return;
    }

    //table对象
    var objTable = $("#" + strTableNameP060701);

    if (strSaveSign == "1") {
        //新增
        objTable.addRowData(objResult.id, objRecord);
        objTable.setCell(objResult.id, "jldm", objResult.jldm);

    }
    else {
        //保存
        objTable.setRowData(objResult.id, objRecord);
        strSaveSign = "1";
    }

    //添加操作
    addCzHtmlP060701();

    //表格中部门，职员设置名称
    if ($("#bmdm", "#" + strDialogP060701).combobox('getValue') != "-1") {
        objTable.jqGrid("setCell", objResult.id, "bmmc", $("#bmdm", "#" + strDialogP060701).combobox('getText'));
    }
    objTable.jqGrid("setCell", objResult.id, "zymc", $("#zydm", "#" + strDialogP060701).combobox('getText'));

    //设置层无变化
    $("#" + strDialogP060701 + "_divform").data("change", false);

    if (saveType == "2") {

        //保存新增
        $(":input", "#" + strDialogP060701 + "_divform").val("");
        $("#zydm", "#" + strDialogP060701).combobox('setValue', "");
        $("#bmdm", "#" + strDialogP060701).combobox('setValue', "");
        setTimeout(function () { $("#a1", "#" + strDialogP060701).focus(); }, 100);
    }
    else {

        //保存退出
        ChangeValue();
        cihoDlgClose(strDialogP060701);
    }

    //调整表格
    resizeGridScroll(strTableNameP060701);

    //释放内存
    strRecord = null;
    objTable = null;
}

//删除
function deleteGzqcRow(rowId) {

    //取得要删除数据
    var objCurrent = $("#" + strTableNameP060701).getRowData(rowId);

    window.parent.cihoAlert(msg.del.delConfirm, msgBox.dlgBtn.YesNo, function (result) {
        if (result == msgBox.dlgRst.Yes) {

            //获取删除的行
            var objTr = $("#" + strTableNameP060701).getRowData(rowId);

            //删除字符串
            var strJsonData = "{\"kmdm\":\"" + strKmdmP0607 + "\",\"jldm\":\"" + objTr.jldm + "\"}";

            //后台删除
            var objResult = ParseData(window.external.DoOptions(strTableNameP060701, 'Delete', strJsonData));

            //保存失败时弹出提示信息
            if (objResult == null || objResult.ReturnCode == undefined) {
                window.parent.cihoAlert(msg.del.delFail);
                return;
            }
            if (objResult.ReturnCode != 0) {
                window.parent.cihoAlert(objResult.MSG);
                return;
            }


            //前台删除
            $("#" + strTableNameP060701).jqGrid("delRowData", rowId);

            //重新计算合计
            GetTotal();

            //调整 表格
            resizeGridScroll(strTableNameP060701);

            //清除浮动层的内容
            $(".operate").remove();

        }
    });
}