//打开预提支付
function openYtzf(djbh) {
    try {

        if (djbh) { alterState = 2; } else { alterState = 1; };                                     //如果存在单据编号，则说明是修改，否则为新增
        creatDiv_ShowDlgHaveTbl(strDivP050701, "预提费用支付", false, true, strTableP050701, 700);  //创建弹出窗口
        createElements(strTableP050701, strDivP050701);                                             //创建jqgrid表单元素
        initGridSetP050701(strTableP050701, strDivP050701);                                         //为grid绑定事件
        initInputsProp(strDivP050701, strTableP050701);                                             //为表单元素设置属性

        var mylist = [];
        if (alterState == 1) {                  //新增页头页脚默认值
            $("#djdate", "#" + strDivP050701).val(getNowDate());
            $("#czydm", "#" + strDivP050701).attr("disabled", true).val(window.parent.objLoginInfo.name);
        } else if (alterState == 2) {           //修改，页头页脚初始值.

            //去后台请求修改信息
            alterdata = ParseData(window.external.DoOptions(strTableP050701, "GetList", '{ "djbh": "' + djbh + '" }'));
            if (alterdata.ReturnCode == 0) {
                $("#idMust", "#" + strDivP050701).append('NO:<span id="djbh">' + djbh + '</span>');
                $("#djdate", "#" + strDivP050701).attr("disabled", true).val(alterdata.djdate);

                //为科目赋初始值
                $("#kmdm", "#" + strDivP050701).combobox("setValue", alterdata.kmdm);
                if ($("#kmdm", "#" + strDivP050701).combobox("getText") == alterdata.kmdm) {
                    $("#kmdm", "#" + strDivP050701).combobox("setText", alterdata.kmmc);
                }
                $("#kmdm", "#" + strDivP050701).combobox("disable");

                //为职员赋初始值
                $("#zydm", "#" + strDivP050701).combobox("setValue", alterdata.zydm);
                if ($("#zydm", "#" + strDivP050701).combobox("getText") == alterdata.zydm) {
                    $("#zydm", "#" + strDivP050701).combobox("setText", alterdata.zymc);
                }
                $("#zydm", "#" + strDivP050701).combobox("disable");
                if (alterdata.fykmdm) {

                    //为费用科目赋初始值
                    $("#fykmdm", "#" + strDivP050701).combobox("setValue", alterdata.fykmdm);
                    if ($("#fykmdm", "#" + strDivP050701).combobox("getText") == alterdata.fykmdm) {
                        $("#fykmdm", "#" + strDivP050701).combobox("setText", alterdata.fykmdmmc);
                    }
                } else {
                    $("#fykmdm", "#" + strDivP050701).combobox("setText", "");
                }
                $("#fykmdm", "#" + strDivP050701).combobox("disable");

                //为制单人、备注赋初始值
                $("#czydm", "#" + strDivP050701).attr("disabled", true).val(alterdata.czymc);
                $("#bz", "#" + strDivP050701).val(escape(alterdata.bz));
                if (alterdata.Records instanceof Array) { mylist = alterdata.Records };
            } else {
                window.parent.cihoAlert(alterdata.MSG, msgBox.dlgBtn.Ok);
            }

        }
        //加载jqgrid数据
        loadGridData(strTableP050701, mylist);
        $("#bz", "#" + strDivP050701).focus();                                    //给摘要设定焦点
        $("#" + strDivP050701).data("change", false);
        $("#" + strDivP050701).dialog("option", "position", "center");
        $(".layoutpanel").css("z-index", "50000009");
        setTimeout(' $("#bz", "#" + strDivP050701).focus();', 100);

    } catch (e) { }
}

//初始化grid表单元素
function initInputsProp(dlgId, tbId) {
    try {
        //去后台请求数据
        function getDataList() {
            var $dlgId = $("#" + dlgId);
            $dlgId.data("change", true);
            var zydm = $dlgId.find("#zydm").combobox("getValue");
            var fykmdm = $dlgId.find("#fykmdm").combobox("getValue");
            var djdate = $dlgId.find("#djdate").val();
            if (djdate && zydm) {
                var s = ParseData(window.external.DoOptions(strTableP050701, "DoOthers", '{"djdate":"' + djdate + '","zydm":"' + zydm + '","fykmdm":"' + fykmdm + '"}'));
                if (s.ReturnCode == 0 && s.Records instanceof Array) {
                    loadGridData(tbId, s.Records);
                }
            }
        }

        //设置单据时间、费用科目、费用经手人、备注的宽度
        $("#djdate,#kmdm,#zydm", "#" + dlgId).parent().addClass("inp_70");
        $("#bz", "#" + dlgId).parent().addClass("bz");
        inputAndBindData("djdate", false, "#" + dlgId, getDataList);

        //初始化账户combobox并加载数据
        $("#kmdm", "#" + dlgId).combobox({
            valueField: "dm",
            textField: "mc",
            onSelect: function (option) {
                if (option.dm == "-1") {
                    ciho.IniData({ id: "divAddType", title: "付款账户", table: "Tflxx" }, { model: "Tkjkm", jquery: $(this) });
                    $(this).combobox("setValue", "");
                }
            },
            afterChange: function (newValue, newText) {
                if (newValue == "-1") {
                    ciho.IniData({ id: "divAddType", title: "付款账户", table: "Tflxx" }, { model: "Tkjkm", jquery: $(this) });
                    ciho.showTypeDialog();
                    $(this).combobox("setValue", "");
                }
            }
        });
        var kmlist = getComboboxData("TKjkm");
        kmlist.push({ "mc": "<font color='red'>添加账户</font>", "dm": "-1" });
        $("#kmdm", "#" + dlgId).combobox("loadData", kmlist);
        $("#kmdm", "#" + dlgId).combobox({ panelHeight: getComHeight(kmlist) });
        $("#kmdm", "#" + dlgId).combobox({ onChange: function () { $("#" + dlgId).data("change", true); } });


        //初始化费用科目combobox并加载数据
        $("#fykmdm", "#" + dlgId).combobox({ valueField: "kmdm", textField: "mc", editable: false });
        var fykmlist = ParseData(window.external.DoOptions(strTableP0507, "GetComboBoxData", '{"flag":"1"}'));
        if (fykmlist.ReturnCode == 0 && fykmlist.Records instanceof Array) {
            fykmlist.Records.unshift({ "kmdm": "", "mc": "全部费用科目" });
            $("#fykmdm", "#" + dlgId).combobox("loadData", fykmlist.Records);
        }
        $("#fykmdm", "#" + dlgId).combobox({ onSelect: function () { getDataList(); $("#" + dlgId).data("change", true); } })

        //初始化费用经手人combobox并加载数据
        $("#zydm", "#" + dlgId).combobox({ valueField: "zydm", textField: "mc" });
        var zylist = ParseData(window.external.DoOptions(strTableP0507, "GetComboBoxData", '{"flag":"3"}'));
        if (zylist.ReturnCode == 0 && zylist.Records instanceof Array) {
            $("#zydm", "#" + dlgId).combobox("loadData", zylist.Records);
        }
        $("#zydm", "#" + dlgId).combobox({ onSelect: function () { $("#fykmdm").combobox("enable"); $("#fykmdm").combobox("setValue", ""); getDataList(); $("#" + dlgId).data("change", true); } });

        //为单据时间、制单人、备注注册事件
        $("#djdate,#czydm,#bz", "#" + dlgId).change(function () { $("#" + dlgId).data("change", true); });
        $("#djdate", "#" + dlgId).change(getDataList);
        $("#fykmdm", "#" + dlgId).combobox("setValue", "");
    } catch (e) { }
}

//为grid绑定事件
function initGridSetP050701(tbId, dlgId) {
    $("#" + tbId).setGridParam({
        "afterSaveCell": afterSaveCell,
        "afterEditCell": function (rowId, cellname, value, iRow, iCol) {
            try {
                cellX = iRow;
                cellY = iCol;
                var item = $("#" + rowId + " :input[name='" + cellname + "']", this);
                switch (cellname) {
                    case "bczfje":
                        var gzguid = $(this).getCell(rowId, "gzguid");
                        if (!gzguid) { $(this).restoreCell(iRow, iCol); return; }
                        item.val(value);
                        item.autoNumeric({ mDec: window.parent.strMoneyPlace, aSep: "" }).focus(function () { this.select(); }).css("ime-mode", "disabled");
                        item.change(function () {
                            changeZt(tbId, rowId);
                        });
                        break;
                }
            } catch (e) { }
        }
    });
    addColFormater(tbId);
    bindDelEvent(tbId);
    $("#" + tbId).setGridHeight(141);
}

//保存预提支付
function saveMyData(dlgId, afterSave, tbId) {
    try {
        //添加分类
        if (tbId == ciho.layal.table) {
            ciho.saveType(); return;
        }

        $("#" + tbId).saveCell(cellX, cellY);
        if (!authMyBills(tbId, dlgId)) { return false; } //验证单据信息

        //组织grid里面的数据
        var recordCount = 0;
        var ids = $("#" + tbId).getDataIDs();
        for (var i = 0; i < ids.length; i++) {
            var item = $("#" + tbId).getRowData(ids[i]);
            if (alterState == 1) {              //新增
                if (item.gzguid && item.bczfje * 1 > 0) {
                    recordCount++;
                    var record = '{"gzguid":"' + item.gzguid + '","fykmdm":"' + item.fykm + '","bczfje":"' + item.bczfje + '"}';
                    records.push(record);
                }
            } else if (alterState == 2) {            //修改
                if (item.mxguid) {
                    if (item.bczfje * 1 > 0) {
                        recordCount++;
                        var record = '{"mxguid":"' + item.mxguid + '","fykmdm":"' + item.fykm + '","bczfje":"' + item.bczfje + '"}';
                        records.push(record);
                    } else {
                        var record = '{"mxguid":"' + item.mxguid + '","flag":"3"}';
                        records.push(record);
                    }
                }
            }
        }

        if (recordCount == 0) {//"预提费用支付单不能为空！"
            window.parent.cihoAlert(msg.cw.ytfyzcdNotEmp, msgBox.dlgBtn.Ok);
            return false;
        }

        //组织页头和页脚
        var inputs = [];

        //日期
        inputs.push('"djdate":"' + $("#djdate", "#" + dlgId).val() + '"');

        //账户
        var kmdm = $("#kmdm", "#" + dlgId).combobox("getValue");
        var kmmc = $("#kmdm", "#" + dlgId).combobox("getText");
        if (kmdm) {
            inputs.push('"kmdm":"' + kmdm + '"');
        } else if (kmmc == alterdata.kmmc) {
            inputs.push('"kmdm":"' + alterdata.kmdm + '"');
        }

        //费用经手人
        var zydm = $("#zydm", "#" + dlgId).combobox("getValue");
        var zymc = $("#zydm", "#" + dlgId).combobox("getText");
        if (zydm) {
            inputs.push('"zydm":"' + zydm + '"');
        } else if (zymc == alterdata.zymc) {
            inputs.push('"zydm":"' + alterdata.zydm + '"');
        }

        //费用科目
        var fykmdm = $("#fykmdm", "#" + dlgId).combobox("getValue");
        var fykmmc = $("#fykmdm", "#" + dlgId).combobox("getText");
        if (fykmdm) {
            inputs.push('"fykmdm":"' + fykmdm + '"');
        } else if (fykmmc == alterdata.fykmmc) {
            inputs.push('"fykmdm":"' + alterdata.fykm + '"');
        } else {
            inputs.push('"fykmdm":""');
        }

        //制单人
        if (alterState == 1) {                                                                      //新增
            inputs.push('"czydm":"' + window.parent.objLoginInfo.code + '"');
        } else if (alterState == 2) {
            inputs.push('"czydm":"' + alterdata.czydm + '"');                        //修改
            inputs.push('"djbh":"' + $("#djbh", "#" + dlgId).text() + '"');      //修改
        }

        //备注
        inputs.push('"bz":"' + trim($("#bz", "#" + dlgId).val()) + '"');

        var jsonStr = '{' + inputs + ',"Records":[' + records + ']}';

        records.length = 0;
        var s = null;
        if (alterState == 1) {
            s = ParseData(window.external.DoOptions(tbId, "Add", jsonStr));
        } else {
            s = ParseData(window.external.DoOptions(tbId, "Edit", jsonStr));
        }

        if (s.ReturnCode == 0) {//添加成功！
            if (afterSave == 2) {       //保存新增
                resetGrid(tbId, dlgId);
                $("#bz", "#" + strDivP050701).focus();                                    //给摘要设定焦点
            } else {                            //保存退出
                cihoDlgClose(dlgId);
            }
            searchDataClick_P0507(0);
            resizeGrid(strTableP0507);
        }
        else {
            window.parent.cihoAlert(s.MSG, msgBox.dlgBtn.Ok);
        }
    } catch (e) { }
}

//验证预提支付
function authMyBills(tbId, dlgId) {
    try {
        var amount = $("#" + tbId).footerData("get").bczfje * 1;
        var maxAmount = window.top._MaxPerBill;
        if (amount >= maxAmount) {//"单据金额过大！"
            window.parent.cihoAlert(msg.other.djhjExtent, msgBox.dlgBtn.Ok);
            //alert("单据金额过大！");
            return false;
        }

        //验证时间
        var djdata = $("#djdate", "#" + dlgId).val();
        if (!djdata) {//"预提支付日期不能为空！"
            window.parent.cihoAlert(msg.dt.ytzfDtNOEmp, msgBox.dlgBtn.Ok, function () { $("#djdate", "#" + dlgId).select(); });
            return false;
        } else {
            var djdt = djdata.replace(/\-/, "/");
            if (djdt < window.top._startDate) {
                window.parent.cihoAlert(msg.other.djrqExtent, msgBox.dlgBtn.Ok, function () { $("#djdate", "#" + dlgId).select(); });
                return false;
            }
        }
        //验证账户
        var kmdm = $("#kmdm", "#" + dlgId).combobox("getValue");
        var kmmc = $("#kmdm", "#" + dlgId).combobox("getText");
        if (!kmdm && kmmc != alterdata.kmmc) {//"账户为空或者不存在！"
            window.parent.cihoAlert(msg.cw.inputzhEmp, msgBox.dlgBtn.Ok, function () { $("#kmdm", "#" + dlgId).combobox("focus"); });
            return false;
        }

        //验证费用经手人
        var zydm = $("#zydm", "#" + dlgId).combobox("getValue");
        var zymc = $("#zydm", "#" + dlgId).combobox("getText");
        if (!zydm && zymc != alterdata.zymc) {//"费用经手人为空或者不存在!"
            window.parent.cihoAlert(msg.cw.fyjsrNotEmp, msgBox.dlgBtn.Ok, function () { $("#zydm", "#" + dlgId).combobox("focus"); });
            return false;
        }

        //验证制单人
        var czydm = $("#czydm", "#" + dlgId).val();
        if (!czydm) {
            window.parent.cihoAlert(msg.other.inputZdr, msgBox.dlgBtn.Ok, function () { $("#czydm", "#" + dlgId)[0].select(); });
            return false;
        }

        /*
        //验证本次支付金额是否比可支付金额大
        var ids = $("#" + tbId).getDataIDs();
        for (var i = 0, len = ids.length; i < len; i++) {
        var item = $("#" + tbId).getRowData(ids[i]);
        if (item.mxguid || item.gzguid) {
        if (item.bczfje * 1 <= 0) {
        window.parent.cihoAlert(msg.other.dnh + (i + 1) + "】行的本次支付金额不能为负或0！", msgBox.dlgBtn.Ok);
        //alert("请选择第【" + (i + 1) + "】行的本次支付金额不能为负或0！");
        return false;
        }
        }
        }*/
        return true;
    } catch (e) { }
}

//grid的afterSaveCell事件
function afterSaveCell(rowId, cellname, value) {
    try {
        if (cellname == "bczfje") {
            var kzfje = $(this).getCell(rowId, "kzfjeyc") * 1;
            var bczfje = $(this).getCell(rowId, "bczfje") * 1;
            if (alterState == 1) {
                $(this).setCell(rowId, "kzfje", (kzfje - bczfje).toFixed(window.parent.strMoneyPlace));
            } else if (alterState == 2) {
                var bczfjeyc = $(this).getCell(rowId, "bczfjeyc") * 1;
                $(this).setCell(rowId, "kzfje", (kzfje + bczfjeyc - bczfje).toFixed(window.parent.strMoneyPlace));
            }
            sumGridTotal(this.id);
        }
    } catch (e) { }
}