/* * 版 本 PIT-ADMS V7.0.3 敏捷开发框架
 * Copyright (c) 2013-2018 Hexagon PPM
 * 创建人：超级管理员
 * 日  期：2022-03-07 13:44
 * 描  述：工程数据表
 */
var refreshGirdData;
var ProjectId = request('ProjectId');
var RelTypeID = request('RelTypeID');
var RelEngineData1ID = request('RelEngineData1ID');
var selectData = [];
var bootstrap = function ($, learun) {
    "use strict";
    var page = {
        init: function () {
            page.initGird();
            page.bind();
        },
        bind: function () {
            $('#UpObjectTypeID').lrselect({
                url: top.$.rootUrl + '/ZZDT_EC/ec_objecttype/GetTreeData',
                param: { ProjectId: ProjectId, isShowCheck: false },
                type: 'tree',
                allowSearch: true,
                maxHeight: 225
            });
            $('#DataStatus').lrDataItemSelect({ code: 'Be_DataStatus' });
            //查询
            $('#btn_Search').on('click', function () {
                page.search($('.lr-layout-tool-left').lrGetFormData());
            });
            // 刷新
            $('#lr_refresh').on('click', function () {
                location.reload();
            });
            //获取当前选中的数据
            $.ajax({
                url: top.$.rootUrl + '/ZZDT_EC/ec_enginedata_rel/GetList',
                data: { queryJson: JSON.stringify({ ProjectId: ProjectId, RelTypeID: RelTypeID, RelEngineDataID: RelEngineData1ID})},
                async: false,
                type: "GET",
                dataType: "json",
                success: function (data) {
                    if (data.code == 200 && data.data.length > 0)
                        selectData = data.data;
                },
                error: function () {
                },
                complete: function (data) {
                }
            });
            // 保存
            $('#lr_save').on('click', function () {
                var relEngineData2IDs = [];
                if (selectData) {
                    for (var i = 0; i < selectData.length; i++) {
                        if (selectData[i].RelEngineData1ID == RelEngineData1ID) {
                            relEngineData2IDs.push(selectData[i].RelEngineData2ID);
                        } else {
                            relEngineData2IDs.push(selectData[i].RelEngineData1ID);
                        }
                    }
                } 
                var postData = {
                    strEntity: JSON.stringify(relEngineData2IDs)
                }
                $.lrSaveForm(top.$.rootUrl + '/ZZDT_EC/ec_enginedata_rel/SaveForm?RelTypeID=' + RelTypeID + "&ProjectId=" + ProjectId + "&RelEngineData1ID=" + RelEngineData1ID, postData, function (res) {

                });
            });
        },
        // 初始化列表
        initGird: function () {
            $('#gridtable').jfGrid({
                url: top.$.rootUrl + '/ZZDT_EC/ec_enginedata/GetPageList',
                headData: [
                    { label: "位号", name: "TagNumber", width: 300, align: "center", sort: false },
                    { label: "对象类型", name: "FullPathCN", width: 300, align: "center", sort: false },
                    { label: "数据状态", name: "DataStatusName", width: 150, align: "center", sort: false },
                    { name: "", width: 1, sort: false }
                ],
                mainId: 'EngineDataID',
                isPage: true,
                isMultiselect: true,
                onSelectRow: function (rowData, isSelect) {
                    selectData = $.grep(selectData, function (item) {
                        return item.RelEngineData1ID != rowData.EngineDataID && item.RelEngineData2ID != rowData.EngineDataID;
                    });
                    if (isSelect) {
                        var param = {};
                        param.RelEngineData1ID = RelEngineData1ID;
                        param.RelEngineData2ID = rowData.EngineDataID;
                        selectData.push(param);
                    }
                },
                onRenderComplete: function (data) {
                    //默认已经选中的
                    for (var i = 0; i < data.length; i++) {
                        if (selectData && data[i].EngineDataID != RelEngineData1ID) {
                            var gdata = $.grep(selectData, function (item) {
                                return item.RelEngineData1ID == data[i].EngineDataID || item.RelEngineData2ID == data[i].EngineDataID
                            });
                            if (gdata && gdata.length > 0) {
                                $('#gridtable').jfGridSet("selectRow", data[i].EngineDataID);
                            }
                        }
                    }
                }
            });
            page.search();
        },
        search: function (param) {
            param = param || {};
            if (param.UpObjectTypeID == "-1") {
                param.UpObjectTypeID = "";
            }
            param.NotEngineDataID = RelEngineData1ID;
            param.ProjectId = ProjectId;
            $('#gridtable').jfGridSet('reload', { queryJson: JSON.stringify(param) });
        }
    };
    refreshGirdData = function () {
        $('#gridtable').jfGridSet('reload');
    };
    page.init();
}
