/* * 版 本 PIT-ADMS V7.0.3 敏捷开发框架
 * Copyright (c) 2013-2018 Hexagon PPM
 * 创建人：超级管理员
 * 日  期：2022-03-07 13:44
 * 描  述：工程数据表
 */
var acceptClick;
var keyValue = request('keyValue');
var ProjectId = request('ProjectId');
var oldTagNumber = "";
var objectTypeNameEN = "";
var isEditFirst = true;
var bootstrap = function ($, learun) {
    "use strict";
    var page = {
        init: function () {
            $('.lr-form-wrap').lrscroll();
            page.bind();
            page.initData();
        },
        bind: function () {
            $('#DataStatus').lrDataItemSelect({ code: 'Be_DataStatus' });
            $('#ObjectTypeID').lrselect({
                url: top.$.rootUrl + '/ZZDT_EC/ec_objecttype/GetTreeData',
                param: { ProjectId: ProjectId, isShowCheck: false },
                type: 'tree',
                allowSearch: true,
                maxHeight: 225,
                select: function (item) {
                    if (item && item.hasChildren) {
                        learun.alert.warning('请选择最后一级！');
                        $('#ObjectTypeID').lrselectSet('');
                        return false;
                    } else {
                        if (keyValue) {
                            //有值，说明是编辑

                        }
                        else {
                            //新增
                            //只有勾选了自动生成流水号才需要从接口获取最新的位号
                            if (item.NodeExtData.IsSerialNumber == 1) {
                                //如果是编辑  需要判断是不是第一次加载
                                if (!keyValue || (keyValue && !isEditFirst)) {
                                    $.ajax({
                                        type: 'GET',
                                        url: top.$.rootUrl + "/api/ObjectTypeApi/GetLatestSerialNumber?rdo=" + Math.random(),
                                        data: { projectId: ProjectId, objectTypeID: item.id },
                                        traditional: true,
                                        success: function (data) {
                                            if (data.code == '200') {
                                                objectTypeNameEN = item.NodeExtData.ObjectTypeNameEN;
                                                $("#TagNumber").val(data.data);
                                            } else {
                                                learun.alert.error(data.info);
                                            }
                                        }, error: function (msg) {
                                            learun.alert.error(msg);
                                        }, complete: function (data) {
                                        }
                                    });
                                } else {
                                    if (keyValue && isEditFirst) {
                                        objectTypeNameEN = item.NodeExtData.ObjectTypeNameEN;
                                    }
                                }
                            } else {
                                objectTypeNameEN = "";
                                if (!isEditFirst) {
                                    $("#TagNumber").val("");
                                }
                            }
                        }

                    }
                    isEditFirst = false;
                }
            });
            $('#CaseID').lrselect({
                allowSearch: true,
                url: top.$.rootUrl + '/ZZDT_EC/ec_case/GetList',
                param: { queryJson: JSON.stringify({ ProjectId: ProjectId }) },
                value: 'CaseID',
                text: 'CaseName',
                title: 'CaseName'
            }).lrselectSet(0);   
        },
        initData: function () {
            if (!!keyValue) {
                $.lrSetForm(top.$.rootUrl + '/ZZDT_EC/ec_enginedata/GetFormData?keyValue=' + keyValue + "&ProjectId=" + ProjectId, function (data) {
                    for (var id in data) {
                        if (!!data[id].length && data[id].length > 0) {
                            $('#' + id).jfGridSet('refreshdata', data[id]);
                        }
                        else {
                            $('[data-table="' + id + '"]').lrSetFormData(data[id]);
                        }
                    }
                    oldTagNumber = $("#TagNumber").val();
                });
            } else {
                //默认为00001 外部区
                $('#DataStatus').lrselectSet("00001");
            }
        }
    };
    // 保存数据
    acceptClick = function (callBack) {
        if (!$('#form').lrValidform()) {
            return false;
        }
        //文字特殊字符及最大长度检测
        if (!verify()) {
            return false;
        }
        var formData = $('#form').lrGetFormData();
        if (formData.ObjectTypeID == '-1') {
            $.lrValidformMessage($("#ObjectTypeID"), "请选择对象类型！");
            $('#ObjectTypeID').click();
            return false;
        }
        formData.ObjectTypeNameEN = objectTypeNameEN;
        var postData = {
            strEntity: JSON.stringify(formData),
            ProjectId: ProjectId
        }
        if (oldTagNumber == $("#TagNumber").val()) {
            $.lrSaveForm(top.$.rootUrl + '/ZZDT_EC/ec_enginedata/SaveForm?keyValue=' + keyValue, postData, function (res) {
                // 保存成功后才回调
                if (!!callBack) {
                    callBack();
                }
            });
        } else {
            var IsRepeat = true;
            if (!!keyValue) {
                $.ajax({
                    type: 'GET',
                    async: false,
                    url: top.$.rootUrl + "/api/ObjectTypeApi/IsRepeatByTagNumber?rdo=" + Math.random(),
                    data: { projectId: ProjectId, oldTagNumber: oldTagNumber, newTagNumber: $("#TagNumber").val() },
                    traditional: true,
                    success: function (data) {
                        IsRepeat = (JSON.stringify(data.data) == "{}");
                    },
                    error: function (msg) {
                        top.learun.alert.error(msg);
                    }, complete: function (data) {
                    }
                });
                if (!IsRepeat) {
                    //判断位号是否重复 弹出提示框（覆盖、不覆盖、取消）
                    top.learun.language.get("位号重复，是否覆盖？", function (msg) {
                        top.layer.confirm(msg, {
                            btn: ['覆盖', '不覆盖', '取消'],
                            title: "提示",
                            icon: 0,
                            skin: 'lr-layer'
                        }, function (index) {
                            UpdateTagNumber("1", callBack)
                            top.layer.close(index); //再执行关闭
                        }, function (index) {
                            UpdateTagNumber("0", callBack)
                            top.layer.close(index); //再执行关闭
                        });
                    });
                    return false;
                }
            }
            $.lrSaveForm(top.$.rootUrl + '/ZZDT_EC/ec_enginedata/SaveForm?keyValue=' + keyValue, postData, function (res) {
                // 保存成功后才回调
                if (!!callBack) {
                    callBack();
                }
            });
        }
    };
    page.init();
}
//文字特殊字符及最大长度检测
function verify() {
    var msg = checkStr($("#TagNumber").val(), 200);
    if (msg) {
        $.lrValidformMessage($("#TagNumber"), msg);
        $('#TagNumber').click();
        return false;
    }
    return true;
}
function UpdateTagNumber(isCover, callBack) {
    $.ajax({
        type: 'POST',
        url: top.$.rootUrl + "/api/ObjectTypeApi/UpdateTagNumber?rdo=" + Math.random() + "&projectId=" + ProjectId + "&oldTagNumber=" + oldTagNumber + "&newTagNumber=" + $("#TagNumber").val() + "&isCover=" + isCover,
        traditional: true,
        dataType: 'json',
        success: function (data) {
            if (data.code == 200) {
                top.learun.alert.success(data.info);
                // 保存成功后才回调
                if (!!callBack) {
                    callBack();
                }
                top.learun.layerClose('form');
            } else {
                top.learun.alert.error(data.info);
            }
        },
        error: function (msg) {
            top.learun.alert.error(msg);
        }, complete: function (data) {

        }
    });
}