//打开报溢单
function openBYD(djbh) {
    try {
        var dlgId = strDivP030302, tbId = strTableP030302, state = djbh ? 2 : 1;

        creatDiv_ShowDlgHaveTbl(dlgId, "报溢单", false, true, tbId, 650);                        //创建窗口
        createElements(tbId, dlgId);                                                             //创建input标签和grid
        initGridSetP030302(tbId, dlgId);                                                         //为grid绑定事件
        initInputsProp(dlgId, tbId);                                                             //为表单元素初始化属性

        var $dlgId = $("#" + dlgId), $tbId = $("#" + tbId);
        $dlgId.data("state", state);

        //获取数据
        var mylist = [];
        if (state == 1) {                                               //新增页头页脚默认值
            $dlgId.data("data", { djbh: djbh });
            $("#djdate", $dlgId).val(getNowDate());
            $("#czymc", $dlgId).attr("disabled", true).val(window.parent.objLoginInfo.name);
        } else if (state == 2) {                                        //修改，页头页脚初始值
            var data = ParseData(window.external.DoOptions(tbId, "GetList", '{ "djbh": "' + djbh + '" }'));
            if (data && data.ReturnCode == 0) {
                $("#idMust", $dlgId).append('NO:<span id="djbh">' + djbh + '</span>');
                $("#djdate", $dlgId).attr("disabled", true).val(data.djdate);

                //为仓库赋值
                var $ck = $("#ckmc", $dlgId);
                $ck.combobox("setValue", data.ckdm);
                if ($ck.combobox("getText") == data.ckdm) {
                    $ck.combobox("setText", data.ckmc);
                }

                //为职员赋值
                var $zy = $("#zymc", $dlgId);
                $zy.combobox("setValue", data.zydm);
                if ($zy.combobox("getText") == data.zydm) {
                    $zy.combobox("setText", data.zymc);
                }

                $("#czymc", $dlgId).val(data.czymc);
                $("#a4", $dlgId).val(escape(data.a4));
                if (data.Records instanceof Array) { mylist = data.Records };
                $dlgId.data("data", { djbh: djbh, ckdm: data.ckdm, ckmc: data.ckmc, zydm: data.zydm, zymc: data.zymc, czydm: data.czydm, czymc: data.czymc });
                $ck = $zy = null;
            } else {
                window.parent.cihoAlert(data.MSG, msgBox.dlgBtn.Ok);
            }
        }
        loadGridData(tbId, mylist);
        if (state == 2) {
            compareData = getCompareData(tbId, cols);
        }

        $dlgId.data("change", false);

        setTimeout('$("#a4", "#" + strDivP030302).focus()', 100);

        $(".layoutpanel").css("z-index", "50000009");

        $dlgId = $tbId = null;
    } catch (e) { }
}

//设置grid事件和属性
function initGridSetP030302(tbId, dlgId) {
    $("#" + tbId).setGridParam({
        "afterSaveCell": function (rowId, cellname, value) {            //单元格保存后要执行的事件（计算和）
            if (cellname == "sl" || cellname == "dj") {
                var ypNum = $(this).getCell(rowId, "sl");
                var ypPrice = $(this).getCell(rowId, "dj");
                if (/^\d+(\.\d+)?$/.test(ypNum) && /^\d+(\.\d+)?$/.test(ypPrice)) {
                    $(this).setCell(rowId, "je", ypNum * ypPrice);
                } else {
                    $(this).setCell(rowId, "je", null);
                }
                sumGridTotal(this.id);
            }
        },
        "afterEditCell": function (rowId, cellname, value, iRow, iCol) {
            try {
                cellX = iRow; cellY = iCol;
                var item = $("#" + rowId + " :input[name='" + cellname + "']", this);
                switch (cellname) {
                    case "spmc":                //药品名称下拉框
                        item.combobox({ valueField: "dm", textField: "mc", width: _iWidth120 });

                        //为药品combobox加载数据
                        var yplist = getComboboxData("Tspxx");
                        yplist.push({ "mc": "<font color='red'>添加药品</font>", "dm": "-1" });
                        item.combobox("loadData", yplist);

                        //为药品combobox赋值
                        var ypdm = $(this).getCell(rowId, "spdm");
                        item.combobox("setValue", ypdm);
                        if (item.combobox("getText") == ypdm) {
                            item.combobox("setText", value);
                        }
                        item.combobox('focus');

                        //为药品combobox设置事件
                        item.combobox({
                            onChange: function (newValue) {
                                var $tbId = $("#" + tbId);
                                if (newValue) {
                                    $tbId.setCell(rowId, "spdm", newValue);
                                } else {
                                    $tbId.setCell(rowId, "spdm", null);
                                    $tbId.setCell(rowId, "dj", null);
                                    $tbId.setCell(rowId, "je", null);
                                }
                                changeZt(tbId, rowId);
                                changeRowPrice(dlgId, tbId, rowId);
                            },
                            onSelect: function (item) {
                                if (item.dm == "-1") {
                                    ciho.IniData({ id: "divAddType", title: "药品", table: "Tflxx" }, { model: "Tspxx", jquery: $(this) });
                                    $(this).combobox("setValue", "");
                                }
                            },
                            afterChange: function (newValue, newText) {
                                if (newValue == "-1") {
                                    ciho.IniData({ id: "divAddType", title: "药品", table: "Tflxx" }, { model: "Tspxx", jquery: $(this) });
                                    ciho.showTypeDialog();
                                    $(this).combobox("setValue", "");
                                }
                            }
                        });

                        break;
                    case "sl":
                        item.val(value);
                        item.autoNumeric({ mDec: window.parent.strCountPlace, aSep: "" }).focus(function () { this.select(); }).css("ime-mode", "disabled");
                        item.change(function () { changeZt(tbId, rowId); });
                        break;
                }
                $(".layoutpanel").css("z-index", "50000009");
                item = null;
            } catch (e) { }
        }
    });
    addColFormater(tbId);
    $("#" + tbId).data("records", []);
    bindDelEvent(tbId);
    $("#" + tbId).setGridHeight(141);
}