//打开退票单
function openTp(rowId) {
    try {
        var item = $("#" + strTableP0401).getRowData(rowId);

        var dlgId = strDivP040103, tbId = strTableP040103;
        var $dlgId = $("#" + dlgId);
        var state = item.zt == "已退票" ? 2 : 1;                       //如果为已退票，则修改。如果是其他，则为新增
        $dlgId.data("state", state);

        //创建jqgrid
        creatDiv_ShowDlgHaveTbl(dlgId, "进项发票退票", true, false, tbId, 400);
        //$("#" + dlgId).data("djbh", item.djbh);

        //创建单据头部的编号
        $("#" + dlgId + "_pop_box_top").html('<br/><h3>发票编号：' + item.fpbh + '</h3>');

        //创建保存按钮
        var strItemHtml = "<div class=\"clr\"></div>";
        strItemHtml += "<div class=\"box_btn_width\"><div class=\"box_btn\">";
        strItemHtml += "<div class=\"btn01\"><a id=\"btnSaveTp\"  style=\"cursor:pointer\">保存</a></div>";
        strItemHtml += "</div></div>";
        $(".pop_box", $dlgId).append(strItemHtml);

        // 创建表单元素
        var strForm = "<ul>";
        strForm += '<li class="inp_70">退票日期：<input type="text"  name="tpdate" id="tpdate" maxlength="50"   onkeypress="return checkValidate(event)" onpaste="return validatePaste()"/></li>';
        strForm += '<li class="inp_70">制单人：<input type="text"  name="czydm" id="czydm" maxlength="50"  disabled="disabled"/></li>';
        strForm += '<li class="bz">备注：<input type="text"  name="bz" id="bz" onkeypress="return checkValidate(event)" onpaste="return validatePaste()"/></li>';
        strForm += "</ul><br/>";
        $("#" + dlgId + "_divform").append(strForm);

        //为退票日期绑定日期控件
        inputAndBindData("tpdate", false, $dlgId, function () { $("#" + dlgId).data("change", true); });
        $("#btnSaveTp", $dlgId).click(saveP040103);

        //赋初始值
        if (state == 1) {       //新增
            $("#czydm", $dlgId).val(window.parent.objLoginInfo.name);
            $("#tpdate", $dlgId).val(getNowDate());
            $dlgId.data("data", { djbh: item.djbh });
        } else {                //修改

            //去后台请求修改信息
            var s = ParseData(window.external.DoOptions(tbId, "GetList", '{"djbh":"' + item.djbh + '"}'));
            if (s && s.ReturnCode == 0 && s.Records instanceof Array && s.Records.length > 0) {

                //为退票日期、备注、制单人赋值
                $("#tpdate", $dlgId).attr("disabled", true).val(s.Records[0].tpdate);
                $("#bz", $dlgId).val(escape(s.Records[0].a1));
                $("#czydm", $dlgId).val(s.Records[0].czymc);
                $dlgId.data("data", { djbh: item.djbh, czydm: s.Records[0].czydm, czymc: s.Records[0].czymc });
            } else {
                window.parent.cihoAlert(s.MSG, msgBox.dlgBtn.Ok);
            }
            s = null;
        }
        $("#tpdate,#bz", $dlgId).change(function () { $("#" + dlgId).data("change", true); });
        $dlgId.data("change", false);

        setTimeout('$("#bz","#"+strDivP040103).focus();', 100);
        item = null;
    } catch (e) { }
}

//保存退票单
function saveP040103() {
    try {
        var tbId = strTableP040103, dlgId = strDivP040103;
        var $dlgId = $("#" + dlgId);

        //验证退票日期
        var tpdate = $("#tpdate", $dlgId).val();           //退票日期
        if (!trim(tpdate)) {
            window.parent.cihoAlert("请输入退票日期！", msgBox.dlgBtn.Ok, function () {
                $("#tpdate", $dlgId).focus();
                $dlgId = null;
            });
            return;
        }

        var djdt = tpdate.replace(/\-/g, "/");
        if (djdt < window.top._startDate) {
            window.parent.cihoAlert(msg.other.tprqExtent, msgBox.dlgBtn.Ok, function () {
                $("#tpdate", $dlgId).focus();
                $dlgId = null;
            });
            return false;
        }

        var state = $dlgId.data("state");
        var data = $dlgId.data("data");

        if (checkDatePattern(tpdate)) {
            var bz = $("#bz", $dlgId).val();                        //备注

            //组织退票json数据
            var jsonStr = '{"djbh":"' + data.djbh + '","tpdate":"' + tpdate + '","a1":"' + trim(bz) + '",';
            if (state == 1) {                                                                    //新增
                jsonStr += '"czydm":"' + window.parent.objLoginInfo.code + '"}';
            } else {                                                                              //修改
                jsonStr += '"czydm":"' + data.czydm + '"}';
            }

            //执行保存
            var s = ParseData(window.external.DoOptions(tbId, "Add", jsonStr));
            if (s.ReturnCode == 0) {
                $("#" + tbId).setCell(selRowId, "zt", "2");
                cihoDlgClose(dlgId);
                searchDataClick_P0401(0);
            }
        } else {
            window.parent.cihoAlert(msg.dt.dtTypeErr, msgBox.dlgBtn.Ok);
        }
    } catch (e) { }
}