//==============工资支付==============
//state:1 修改
//state:2 新增
function openGZZF(djbh, djdate) {
    try {
        var tbId = strTableP050803;
        var dlgId = strDivP050803;
        var $dlgId = $("#" + dlgId);
        creatDiv_ShowDlgHaveTbl(dlgId, "工资支付单", false, false, tbId, 600);

        var config = ParseData(window.external.DoOptions(tbId, "GetTableConfig", "{}"));
        var header = createInputs(config.pageHeader);
        var footer = createInputs(config.pageFooter);
        $("#" + dlgId + "_divform").append(header+'<table id="' + tbId + '"></table>'+footer);
        createDjGrid(tbId, config.colNames, config.colModel);
		$("#" + tbId).setLabel("rn", "序号");
		
        var strItemHtml = "<div class=\"clr\"></div>";
        strItemHtml += "<div class=\"box_btn_width\">";
        strItemHtml += "<div class=\"box_btn\">";
        strItemHtml += "<div class=\"btn01\"><a id=\"btnSaveGZZF\"  style=\"cursor:pointer\">保存退出</a></div>";
        strItemHtml += "</div></div>";
        $(".pop_box", "#" + dlgId).append(strItemHtml);

        initInputsPropGZZF(tbId, dlgId);
        initGridSet(tbId, dlgId);

        if (djbh) { var state = 2; } else { var state = 1; }
        $dlgId.data("state", state);
        var mylist = [];
        if (state == 2) {                                                                                 // 修改，为标签赋值
            $("#idMust", "#" + dlgId).text("NO:" + djbh);
            var record = ParseData(window.external.DoOptions(tbId, "GetList", '{"djbh":"' + djbh + '","cztype":"2","djdate":"' + djdate + '"}'));
            if (record.ReturnCode == 0) {
                $("#bz", "#" + dlgId).val(escape(record.bz));
                $("#czydm", "#" + dlgId).val(record.czymc);
                $("#djdate", "#" + dlgId).val(record.djdate).attr("disabled", true);

                var $km = $("#kmdm", "#" + dlgId);
                $km.combobox("setValue", record.kmdm);
                if ($km.combobox("getText") == record.kmdm) {
                    $km.combobox("setText", record.zhmc);
                }
                if (record.Records instanceof Array) {
                    mylist = record.Records;
                }
                $dlgId.data("data", { "czydm": record.czydm, "czymc": record.czymc, "kmdm": record.kmdm, "kmmc": record.zhmc, "djbh": record.djbh });
            } else {
                window.parent.cihoAlert(record.MSG, msgBox.dlgBtn.Ok);
            }

        } else {                           //新增，为标签设置默认值
            $dlgId.data("data", {});
            $("#djdate", "#" + dlgId).val(getNowDate());
            $("#czydm", "#" + dlgId).val(window.parent.objLoginInfo.name);
            $("#kmdm", "#" + dlgId).combobox("setValue", "");
            var record = ParseData(window.external.DoOptions(tbId, "GetList", '{"djbh":"","cztype":"1","djdate":"' + getNowDate() + '"}'));
            if (record.ReturnCode == 0) {
                if (record.Records instanceof Array) {
                    mylist = record.Records;
                }
            } else {
                window.parent.cihoAlert(record.MSG, msgBox.dlgBtn.Ok);
            }
        }
        loadGridData(tbId, mylist);
        $("#btnSaveGZZF").click(afterSaveClick);
        $("#" + dlgId).data("change", false);
        $(".layoutpanel").css("z-index", "50000009");
        setTimeout(function () { $("#bz", "#" + dlgId).focus(); }, 100);
    } catch (e) { }
}

function afterSaveClick() {
    if (!authMyBillsP050803()) { return; }
    saveGZZF();
    return false;
}

function saveGZZF() {
    try {

        var tbId = strTableP050803;
        var dlgId = strDivP050803;
        var $tbId = $("#" + tbId);
        var $dlgId = $("#" + dlgId);
        var state = $dlgId.data("state");
        var data = $dlgId.data("data");
        var inputs = [];

        var $kmdm = $("#kmdm", "#" + dlgId);
        var kmdm = $kmdm.combobox("getValue");
        var kmmc = $kmdm.combobox("getText");
        if (kmdm) {
            inputs.push('"kmdm":"' + kmdm + '"');
        } else if (!kmdm && kmmc == data.kmmc) {
            inputs.push('"kmdm":"' + data.kmdm + '"');
        }
        var djdate = $dlgId.find("#djdate").val();
        inputs.push('"djdate":"' + djdate + '"');
        var bz = trim($dlgId.find("#bz").val());
        inputs.push('"bz":"' + bz + '"');
        if (state == 1) {               //新增
            inputs.push('"czydm":"' + window.parent.objLoginInfo.code + '"');
        } else if (state) {
            inputs.push('"czydm":"' + data.czydm + '"');
            inputs.push('"djbh":"' + data.djbh + '"');
        }
        var ids = $tbId.getDataIDs();
        var records = $tbId.data("records");
        for (var i = 0; i < ids.length; i++) {
            var item = $tbId.getRowData(ids[i]);
            if (item.zydm) {
                if (state == 1) {
                    var record = '{"zydm":"' + item.zydm + '","sfgz":"' + item.sfgz + '"}';
                } else if (state == 2) {//修改进来的
                    if (item.sfgz) {
                        var record = '{"mxguid":"' + item.mxguid + '","zydm":"' + item.zydm + '","sfgz":"' + item.sfgz + '","flag":"' + item.flag + '"}';
                    } else {
                        var record = '{"mxguid":"' + item.mxguid + '","flag":"3"}';
                    }
                }
                records.push(record);
            }
        }
        var jsonStr = '{' + inputs + ',"Records":[' + records + ']}';

        var s;
        if (state == 1) {//新增
            s = ParseData(window.external.DoOptions(tbId, "Add", jsonStr));
        } else if (state == 2) {//修改
            s = ParseData(window.external.DoOptions(tbId, "Edit", jsonStr));
        }
        if (s && s.ReturnCode == 0) {
            cihoDlgClose(dlgId);
            searchDataClick_P0508(0);
            resizeGrid(strTableP0508);
        }
        else {
            window.parent.cihoAlert(s.MSG, msgBox.dlgBtn.Ok);
        }
    } catch (e) { }
}


function authMyBillsP050803() {
    try {
        var tbId = strTableP050803;
        var dlgId = strDivP050803;
        var data = $("#" + dlgId).data("data");
        $("#" + tbId).saveCell(cellX, cellY);
        var $djdate = $("#djdate", "#" + dlgId);
        var djdate = $djdate.val();
        if (!djdate || !checkDatePattern(djdate)) {
            window.parent.cihoAlert("请输入支付日期！", msgBox.dlgBtn.Ok, function () { $djdate[0].select(); data = $djdate = djdate = null; });
            return false;
        } else {
            var djdate = djdate.replace(/\-/g, "/");
            if (djdate < window.top._startDate) {
                window.parent.cihoAlert(msg.other.zfrqExtent, null, function () { $djdate[0].select(); data = $djdate = djdate = null; });
                return false;
            }
        }

        var $km = $("#kmdm", "#" + dlgId);
        var kmdm = $km.combobox("getValue");
        var kmmc = $km.combobox("getText");
        if (!kmdm && kmmc != data.kmmc) {
            window.parent.cihoAlert(msg.other.inputFkzh, msgBox.dlgBtn.Ok, function () {
                $km.combobox('focus');
            });
            return false;
        }

        var sfgz = isNaN($("#" + tbId).footerData("get").sfgz * 1) ? 0 : $("#" + tbId).footerData("get").sfgz * 1;
        var yfgz = isNaN($("#" + tbId).footerData("get").yfhj * 1) ? 0 : $("#" + tbId).footerData("get").yfhj * 1;
        if (sfgz * 1 <= 0) {
            window.parent.cihoAlert("请至少输入一条数据！", msgBox.dlgBtn.Ok);
            return false;
        }

        var $tbId = $("#" + tbId);
        var ids = $("#" + tbId).getDataIDs();
        var rows = [];
        for (var i = 0; i < ids.length; i++) {
            var item = $tbId.getRowData(ids[i]);
            if (item.zydm && item.ye * 1 < 0) {
                rows.push(i);
            }
        }
        if (rows.length > 0) {
            window.parent.cihoAlert("应发工资应该大于实发工资！", msgBox.dlgBtn.Ok, function () { saveGZZF(); data = $djdate = $km = $tbId = ids = rows = null; });
            return false;
        }
        return true;
    } catch (e) { }
}

//为操作按钮注册事件
function bindDelEvent(tbId) {
    $("#" + tbId).delegate("tr[id]:visible", "hover", function (e) {
        if (e.type == "mouseenter") {
            var btn = '<a style="color:red;cursor:pointer;text-decoration:none" onclick="delDjRow(\'' + tbId + '\',' + this.id + ');return false; ">删除</a>';
            $("#" + tbId).setCell(this.id, "cz", btn);
        }
        if (e.type == "mouseleave") {
            $("#" + tbId).setCell(this.id, "cz", " ");
        }
    });
}

function initInputsPropGZZF(tbId, dlgId, djdate) {
    $("#djdate,#kmdm,#czydm", "#" + dlgId).parent().addClass("inp_70");
    $("#bz", "#" + dlgId).parent().addClass("bz");
    $("#czydm", "#" + dlgId).attr("disabled", true);
    $("#kmdm", "#" + dlgId).combobox({
        valueField: "dm",
        textField: "mc",
        width: _iWidth100,
        onSelect: function (option) {
            if (option.dm == "-1") {
                ciho.IniData({ id: "divAddType", title: "付款账户", table: "Tflxx" }, { model: "Tkjkm", jquery: $(this) });
                $(this).combobox("setValue", "");
            }
            changeZt(tbId);
        },
        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) });
    $("#bz", "#" + dlgId).change(function () { changeZt(tbId); }).focus(function () { $(this).select(); });
    function djdateChange() {
        var $djdate = $("#djdate", "#" + dlgId);
        var djdate = $djdate.val();
        changeZt(tbId);
        if (checkDatePattern(djdate)) {
            var mylist = [];
            $("#" + tbId).clearGridData();
            var record = ParseData(window.external.DoOptions(tbId, "GetList", '{"djbh":"","cztype":"1","djdate":"' + djdate + '"}'));
            if (record.ReturnCode == 0 && record.Records instanceof Array) {
                mylist = record.Records;
            } else {
                window.parent.cihoAlert(record.MSG, msgBox.dlgBtn.Ok);
            }
            loadGridData(tbId, mylist);
        }
    }
    inputAndBindData("djdate", false, "#" + dlgId, djdateChange);
    $("#djdate", "#" + dlgId).focus(function () { $(this).select(); });
    $("#" + tbId).data("records", []);
}