//打开报损单
function openBSD(djbh) {
    try {
        var dlgId = strDivP030301, tbId = strTableP030301, state = djbh ? 2 : 1;

        creatDiv_ShowDlgHaveTbl(dlgId, "报损/赠送单", false, true, tbId, 700);   //创建弹出窗口
        createElements(tbId, dlgId);                                            //创建表单元素
        initGridSet_P030301(tbId, dlgId);                                       //为grid绑定事件
        initInputsProp(dlgId, tbId);                                            //初始化表单元素

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

        var mylist = [];
        if (state == 1) {                                                     //新增页头页脚默认值
            $dlgId.data("data", {});
            $("#djdate", $dlgId).val(getNowDate());
            $("#czymc", $dlgId).attr("disabled", true).val(window.parent.objLoginInfo.name);
            $("#djtypebh", $dlgId).combobox("setValue", "701");
        } 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);
                var $zy = $("#zymc", $dlgId);
                $ck.combobox("setValue", data.ckdm);
                $zy.combobox("setValue", data.zydm);
                if ($ck.combobox("getText") == data.ckdm) {
                    $ck.combobox("setText", data.ckmc);
                }
                if ($zy.combobox("getText") == data.zydm) {
                    $zy.combobox("setText", data.zymc);
                }

                $("#djtypebh", $dlgId).combobox("setValue", data.djtypebh);
                $("#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", "#" + strDivP030301).focus()', 100);
        $(".layoutpanel").css("z-index", "50000009");
        $dlgId = $tbId = null;
    } catch (e) { }
}

//设置grid事件和属性
function initGridSet_P030301(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: _iWidth80 });

                        //为药品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 "mxzymc":
                        item.combobox({ valueField: "dm", textField: "mc", width: _iWidth80 });

                        //为职员combobox加载数据
                        var zylist = getComboboxData("TZyxx");
                        zylist.push({ "mc": "<font color='red'>添加销售员</font>", "dm": "-1" });
                        item.combobox("loadData", zylist);

                        //为职员combobox设置值
                        var mxzydm = $(this).getCell(rowId, "mxzydm");
                        item.combobox("setValue", mxzydm);
                        if (item.combobox("getText") == mxzydm) {
                            item.combobox("setText", value);
                        }
                        item.combobox('focus');

                        //为职员combobox初始化事件
                        item.combobox({
                            onChange: function (newValue) {
                                var $tbId = $("#" + tbId);
                                if (newValue) {
                                    $tbId.setCell(rowId, "mxzydm", newValue);
                                }
                                else {
                                    $tbId.setCell(rowId, "mxzydm", null);
                                }
                                changeZt(tbId, rowId);
                            },
                            onSelect: function (item) {
                                if (item.dm == "-1") {
                                    ciho.IniData({ id: "divAddType", title: "销售员", table: "Tflxx" }, { model: "TZyxx", jquery: $(this) });
                                    $(this).combobox("setValue", "");
                                }
                            },
                            afterChange: function (newValue, newText) {
                                if (newValue == "-1") {
                                    ciho.IniData({ id: "divAddType", title: "销售员", table: "Tflxx" }, { model: "TZyxx", jquery: $(this) });
                                    ciho.showTypeDialog();
                                    $(this).combobox("setValue", "");
                                }
                            }
                        });
                        break;
                    case "khmc":
                        item.combobox({ valueField: "dm", textField: "mc", width: _iWidth80 });

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

                        //为客户combobox设置值
                        var khdm = $(this).getCell(rowId, "khdm");
                        item.combobox("setValue", khdm);
                        item.combobox("setText", value);
                        if (item.combobox("getText") == khdm) {
                            item.combobox("setText", value);
                        }

                        item.combobox('focus');

                        //为客户combobox初始化事件
                        item.combobox({
                            onChange: function (newValue) {
                                var $tbId = $("#" + tbId);
                                if (newValue) {
                                    $tbId.setCell(rowId, "khdm", newValue);
                                }
                                else {
                                    $tbId.setCell(rowId, "khdm", null);
                                }
                                changeZt(tbId, rowId);
                            },
                            onSelect: function (item) {
                                if (item.dm == "-1") {
                                    ciho.IniData({ id: "divAddType", title: "客户", table: "Tflxx" }, { model: "Tkhxx", jquery: $(this) });
                                    $(this).combobox("setValue", "");
                                }
                            },
                            afterChange: function (newValue, newText) {
                                if (newValue == "-1") {
                                    ciho.IniData({ id: "divAddType", title: "客户", table: "Tflxx" }, { model: "Tkhxx", jquery: $(this) });
                                    ciho.showTypeDialog();
                                    $(this).combobox("setValue", "");
                                } else {
                                    $(this).combobox("setText", newText.replace(/&nbsp;/g, ""));
                                }
                            }
                        });
                        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);
    bindDelEvent(tbId);
    $("#" + tbId).data("records", []);
    $("#" + tbId).setGridHeight(141);
}