/* * 版 本 PIT-ADMS V7.0.3 敏捷开发框架
 * Copyright (c) 2013-2018 Hexagon PPM
 * 创建人：超级管理员
 * 日  期：2022-06-01 15:08
 * 描  述：数据字典
 */
var dataItemID = request('dataItemID');
var itemCode = request('itemCode');
var upDataItemDetailID = request('upDataItemDetailID');
var ProjectId = request('ProjectId');

var keyValue = request('keyValue');
var acceptClick;
var bootstrap = function ($, learun) {
    "use strict";
    var selectedRow = top.selectedDataItemRow;

    var page = {
        init: function () {
            page.initData();
            page.bind();
        },
        bind: function () {
            $('#UpDataItemDetailID').lrselect({
                url: top.$.rootUrl + '/ZZDT_EC/ec_dataitem/GetTreeData',//获取数据地址
                param: { ProjectId: ProjectId, isShowCheck: false, DataItemID: dataItemID, DataItemDetailID: keyValue }, //请求后台参数
                type: 'tree',//数据展示类型：                1.default：普通；2.tree：树形数据；3. treemultiple：树形多选；multiple：普通多选
                allowSearch: true,
                maxHeight: 225
            }).lrselectSet(upDataItemDetailID);//lrselectSet获取下拉框选择的值，传入UpObjectTypeID
            /*检测重复项*/
            $('#DataItemName').on('blur', function () {
                $.lrExistField(keyValue, 'DataItemName', top.$.rootUrl + '/ZZDT_EC/ec_dataitem/ExistDetailItemName', { itemCode: dataItemID, ProjectId: ProjectId });
            });
            $('#DataItemCode').on('blur', function () {
                $.lrExistField(keyValue, 'DataItemCode', top.$.rootUrl + '/ZZDT_EC/ec_dataitem/ExistDetailItemCode', { itemCode: dataItemID, ProjectId: ProjectId  });
            });
        },
        initData: function () {
            if (!!selectedRow) {
                keyValue = selectedRow.DataItemDetailID || '';
                $('#form').lrSetFormData(selectedRow);
            }
            else {
                $('#DataItemID').val(dataItemID);
                $('#UpDataItemDetailID').val(upDataItemDetailID);
            }
        }
    };
    // 保存数据
    acceptClick = function (callBack) {
        var formData = $('#form').lrGetFormData();
        if (!formData.OrderID) {
            $('#OrderID').val('-1');
            formData.OrderID = -1;
        }

        if (!$('#form').lrValidform()) {
            return false;
        }
        //文字特殊字符及最大长度检测
        if (!verify()) {
            return false;
        }

        if (!keyValue) {
            formData.ProjectId = ProjectId;
        }
        var postData = {
            strEntity: JSON.stringify(formData)
        }
        $.lrSaveForm(top.$.rootUrl + '/ZZDT_EC/ec_dataitem/SaveDetailForm?keyValue=' + keyValue + "&itemCode=" + itemCode + "&ProjectId=" + ProjectId, postData, function (res) {
            // 保存成功后才回调
            if (!!callBack) {
                callBack();
            }
        });
    };

    page.init();
}

//文字特殊字符及最大长度检测
function verify() {
    var msg = checkStr($("#DataItemName").val(), 200);
    if (msg) {
        $.lrValidformMessage($("#DataItemName"), msg);
        $('#DataItemName').click();
        return false;
    }
    msg = checkStr($("#DataItemCode").val(), 200);
    if (msg) {
        $.lrValidformMessage($("#DataItemCode"), msg);
        $('#DataItemCode').click();
        return false;
    }
    msg = checkStr($("#Remark").val(), 500);
    if (msg) {
        $.lrValidformMessage($("#Remark"), msg);
        $('#Remark').click();
        return false;
    }
    return true;
}