import utils from '../../utils/utils'
// import eventBus from 'ts-jcpt/packages/utils/eventBus'
import biUtils from '../../utils/biUtils'
import _ from 'lodash'
const dragEventMixins = {
    data() {
        return {
            dragDom: null,
            dropdom: null,
            contentRect: null,
            dragType: '',
            dragTarget: {},
            dragTargetItem: {},
            businessNatureArr: {},
            containerHeight: 100 // 高度
        }
    },
    mounted: function() {
        this.containerHeight = this.$refs.gridMainLayout.$el.offsetHeight
        this.$eventBus.$on('dragstart', this.dragstart) //用于指标的移动监听
        this.$eventBus.$on('dragend', this.dragend) //用于指标的移动监听
        this.contentRect = this.$refs.biUserDefinedIconDragView.getBoundingClientRect()
    },
    methods: {
        // 拖拽开始
        dragstart(ev, type, item) {
            this.uuid = utils.uuid(6)
            this.draging = true
            this.isDragEnd = false
            this.dragTarget = ev.target
            this.dragTargetItem = item
            this.dragType = type
        },
        // // 查找相同的Id
        // findDuplicateId(uuid){
        //   if(this.natureArr.length==0){
        //     return uuid
        //   }
        //   let biNatureArrIds = Object.keys(this.$store.state.bi.biNatureArrIds)
        //   if(biNatureArrIds.length==0){ // 未存值

        //   }
        // },
        // getN
        // 拖拽结束
        dragend(ev, type, item) {
            this.isDragEnd = true
                // 属性最后值未渲染成功，则删除默认坑位
            if (this.natureArr && this.natureArr[this.natureArr.length - 1] && !this.natureArr[this.natureArr.length - 1].type) {
                this.natureArr.splice(this.natureArr.length - 1, 1) //删除数组最后一个（默认坑位）
                this.setStoreNatureArr()
            }
            // 去掉多出来的默认内容 
            if (this.$refs.gridMainLayout && this.$refs.gridMainLayout.$refs.gridLayout && this.$refs.gridMainLayout.$refs.gridLayout.isDragging) {
                this.$refs.gridMainLayout.$refs.gridLayout.isDragging = false
            }

        },
        // 触发over事件
        dragOver(e) {
            e.preventDefault()
            if (this.dragType == 'target') {
                this.moverTarget(event)
                return false;
            }
            if (!this.dragDom) {
                return
            }
            this.setPositionDebonce()(e)
        },
        // 移动的目标位置
        setPositionDebonce() {
            return _.debounce(event => {
                this.dragDom.dispatchEvent(
                    new PointerEvent('pointermove', {
                        clientX: event.clientX,
                        clientY: event.clientY,
                        pointerType: 'mouse',
                        bubbles: true
                    })
                )
            }, 150)
        },
        // 重绘图
        calcScaleHeight(h) {
            const scaleHeight =
                (this.containerHeight - 7 * (100 + 1)) / 100 // 新比例
            let height = h === Infinity ?
                h :
                Math.round(
                    10 * h + Math.max(0, h - 1) * 7
                ) //旧比例10下的高度
            h = Math.round(
                (height + 7) / (scaleHeight + 7)
            )
            return h
        },
        // 进入放置区
        dropEnterHandler(event) {
            event.preventDefault()
            if (!this.dragType || this.dragType == 'target') {
                return false;
            }
            if (!this.draging) return
            this.draging = false
            let dragLay = {}
            if (this.dragType == 'screen') {
                dragLay = {
                    x: 0,
                    y: 0,
                    w: this.dragTargetItem.state == 8 ? 36 : 6,
                    h: this.dragTargetItem.state == 8 ? 7 : 2,
                    i: this.uuid,
                    id: this.uuid,
                    isSelect: false,
                    HPosition: true,
                }
                if (this.dragTargetItem.state == 10) { // 过滤树
                    dragLay.h = 30
                    dragLay.w = 8
                    dragLay.HPosition = false
                }
            } else if (this.dragType == 'element') {
                dragLay = {
                    x: 0,
                    y: 0,
                    w: this.dragTargetItem.state == 3 ? 1 : 6,
                    h: this.dragTargetItem.state == 3 ? 3 : 8,
                    i: this.uuid,
                    id: this.uuid,
                    isSelect: false,
                }
            } else if (this.dragType == 'filter') { //过滤器类型
                dragLay = {
                    x: 0,
                    y: 0,
                    w: 36,
                    h: 6,
                    value: '',
                    i: this.uuid,
                    id: this.uuid,
                    isSelect: false,
                    simple: true,
                    controlType: this.dragTargetItem.title,
                    HPosition: true
                }
            } else {
                dragLay = {
                    x: 0,
                    y: 0,
                    w: 12,
                    h: 10,
                    i: this.uuid,
                    id: this.uuid
                }
                if (this.dragType == 'biElement' && this.dragTargetItem.state == '4') { //计时器
                    dragLay.w = 7
                    dragLay.h = 3
                    dragLay.HPosition = true
                    dragLay.groundColor = 'rgba(255, 0, 0, 0)'
                    dragLay.textColor = '#02d9fd'
                }
            }
            dragLay.h = dragLay.HPosition ? dragLay.h : this.calcScaleHeight(dragLay.h)
                //增加默认坑位
            this.natureArr.push(dragLay)
            this.setStoreNatureArr()
            this.$nextTick(() => {
                this.dragDom = this.$refs.gridMainLayout.$refs[dragLay.i][0].$refs.biReportLayout
                    // this.dragDom.parentElement.style.transform = `translate3d(${event.clientX -
                    //         this.contentRect.x -
                    //         20}px, ${event.clientY - this.contentRect.y - 20}px, 0px)`
                if (!this.dragDom) {
                    return
                }
                this.dragDom.dispatchEvent(
                    new PointerEvent('pointerdown', {
                        clientX: event.clientX,
                        clientY: event.clientY,
                        pointerType: 'mouse',
                        bubbles: true
                    })
                )
            })
        },
        setStoreNatureArr() {
            if (this.biModel == 'mobile') {
                this.$store.commit('setPhoneNature', [...this.natureArr])
            } else {
                this.$store.commit('setNatureArr', [...this.natureArr])
            }
        },
        // 成功放置
        dropHandler(event) {
            if (!this.dragType) {
                return false;
            }
            this.isDragEnd = true
            if (this.dragType == 'target') {
                this.dropTarget()
                return false;
            }
            // 给坑位填值
            let dragLay = this.natureArr[this.natureArr.length - 1]
            let pos = this.getPageLocation(event)
            let H = dragLay.h
            this.setDataNature(dragLay)
            dragLay.h = (H != dragLay.h && !dragLay.HPosition) ? this.calcScaleHeight(dragLay.h) : dragLay.h
            this.dropdom = this.getDropdom(pos, 'biUserDefined__item')
            if (this.dropdom && (this.dropdom.id == 'tab' || this.dropdom.id == 'panel')) {
                let uuid = this.dropdom.getAttribute('uuid')
                this.setNatureArr(this.natureArr, this.dropdom.id, uuid, dragLay)
            }
            this.setStoreNatureArr()
            if (this.dragDom) {
                this.dragDom.dispatchEvent(
                    new PointerEvent('pointerup', {
                        clientX: event.clientX,
                        clientY: event.clientY,
                        pointerType: 'mouse',
                        bubbles: true
                    })
                )
            }

            if (this.dragType == 'business') {
                this.getBusinessOther({...dragLay })
            }
        },
        // 寻找自然控件处理容器
        setNatureArr(natureArr, type, uuid, dragLay) {
            for (let lay of natureArr) {
                if (lay.type == 'tab') {
                    for (let tab of lay.tabs) {
                        this.setNatureArr(tab.natureArr, type, uuid, dragLay)
                    }
                }
                if (lay.type == 'panel') {
                    this.setNatureArr(lay.natureArr, type, uuid, dragLay)
                }
                if (lay.i === uuid) {
                    if (type == 'tab') {
                        for (let tab of lay.tabs) {
                            if (tab.index == lay.activeIndex) {
                                dragLay.isContanier = true // 标明在容器中
                                tab.natureArr.push(dragLay)
                                break
                            }
                        }
                    } else {
                        dragLay.isContanier = true // 标明在容器中
                        lay.natureArr.push(dragLay)
                    }
                    this.natureArr.splice(this.natureArr.length - 1, 1) //删除数组最后一个（默认坑位）
                    this.setStoreNatureArr()
                    return
                }
            }
        },
        // 点击编辑区
        gridMainClick(ev) {
            this.$eventBus.$emit('resetOtherActive', {}) //清除其他高亮
            this.$eventBus.$emit('itemselectchange', { target: {}, select: false })
            return false
        },
        //根据类型给控件设初始属性
        setDataNature(d) {
            d.type = this.dragType
            switch (this.dragType) {
                case 'chart':
                    d.chartType = this.dragTargetItem.state;
                    d.chartValue = this.dragTargetItem.code;
                    d.cate = this.dragTargetItem.cate;
                    d.state = this.dragTargetItem.state;
                    d.showBorder = true
                    d.point = true
                    d.controlType = '图表';
                    d.emptyText = '图表暂无数据'
                        // d.controlTitle = '标题';
                    d.textConfig = [{ name: '差', value: '25', i: utils.uuid(16) }, { name: '中', value: '45', i: utils.uuid(16) }, { name: '良好', value: '65', i: utils.uuid(16) }, { name: '优', value: '80', i: utils.uuid(16) }]
                    d.fieldsXIdList = [{
                        id: '',
                        i: utils.uuid(16),
                        fieldsXId: '',
                        fieldsXName: ''
                    }];
                    if (d.state == 4 || d.state == 17 || d.state == 18 || d.state == 19) {
                        d.h = 12
                        d.w = 16
                    }
                    // d.height = 110 - 54
                    break;
                case 'panel':
                    // d.h = 12
                    // d.w = 12
                    d.chartType = this.dragTargetItem.state;
                    d.chartValue = this.dragTargetItem.code;
                    d.state = this.dragTargetItem.state;
                    d.controlType = '容器';
                    d.controlTitle = '标题';
                    d.natureArr = [];
                    d.titleColor = '#222';
                    d.titleFontSize = 14;
                    d.borderTop = 1;
                    d.titleBackground = '#fff'
                    d.backgroundColor = '#fff'
                    d.borderBottom = 1;
                    d.showBorder = true
                        // d.borderChoose = [{ name: 'name1', value: 1 },
                        //     { name: 'name2', value: 2 },
                        //     { name: 'name3', value: 3 },
                        //     { name: 'name4', value: 4 }
                        // ]
                    break;
                case 'tab':
                    d.h = 36
                    d.w = 36
                    d.activeIndex = '0' //当前tab的激活页
                    d.tabIndex = 0 // 当前tab的下标
                    d.tabs = [];
                    d.tabs[0] = {
                        title: '标题',
                        id: '0' + d.id,
                        index: '0',
                        natureArr: [],
                        dataSourceData: []
                    }
                    d.controlTitle = '标题';
                    d.controlType = 'tab';
                    break;
                case 'table':
                    d.controlType = '表格'
                    d.border = 'full'
                    d.showHeader = true
                    d.stripe = false
                    d.data = []
                    d.offPage = false
                    d.rownumbers = true
                    d.backgroundColor = '#fff'
                    d.anotherBackgroundColor = ''
                    d.cols = biUtils.getColNullFn(d.id)
                    break;
                case 'screen':
                    d.h = 2
                    d.HPosition = true // 代表高度已经是绝对化了
                    d.placeholder = this.dragTargetItem.title
                    if (this.dragTargetItem.state == 1) {
                        d.placeholder = '请选择时间';
                        d.dateType = 'range'
                    }
                    //附初值
                    if (this.dragTargetItem.state == 2 || this.dragTargetItem.state == 3) {
                        d.dataType = '1'
                    }
                    d.state = this.dragTargetItem.state
                    if (d.state == '7' || d.state == '4' || d.state == '5') {
                        d.w = 3
                    }
                    if (d.state == '10') { // 科室筛选器
                        d.h = 30
                        d.w = 8
                        d.showBorder = true
                        d.groundColor = '#fff'
                        d.HPosition = false
                    }
                    d.controlName = this.dragTargetItem.controlName
                    d.controlType = this.dragTargetItem.title
                    d.dataSourceData = [{
                        id: utils.uuid(16),
                        i: utils.uuid(16),
                        dataSourceId: '',
                        dataSourceCol: '',
                        dataSourceDateType: ''
                    }];
                    break;
                case 'element':
                    d.state = this.dragTargetItem.state
                    d.controlName = this.dragTargetItem.controlName
                    d.controlType = this.dragTargetItem.title
                    if (d.state == '1') { //快捷链接
                        d.dataSourceId = ''
                        d.colNumber = '1'
                        d.iconW = '80'
                        d.iconH = '80'
                        d.align = 'center'
                        d.showStyle = 'topBottom'
                        d.dataSourceData = [{
                            id: utils.uuid(16),
                            i: utils.uuid(16),
                            showName: '显示名称',
                            linkUrl: '',
                            showIcon: '',
                            enterType: 'DLL',
                            meauName: '',
                            tokenName: '',
                            userCode: '',
                            deptCode: '',
                            sysName: '',
                            sysCode: '',
                            controlType: '链接项'
                        }];
                    }
                    if (d.state == '2') { //方块签
                        d.dataSourceId = ''
                        d.colNumber = '1'
                        d.dataSourceData = [{
                            id: utils.uuid(16),
                            i: utils.uuid(16),
                            dataSourceId: '',
                            showName: '',
                            selectDataSourceCode: '',
                            showValue: '',
                            unit: '条',
                            fontColor: '#0079fe',
                            fontSize: '14px',
                            backgroundColor: '',
                        }];
                    }
                    if (d.state == '3') { //分割线
                        d.h = 3
                        d.w = 1
                        d.dataSourceId = ''
                        d.colNumber = '1'
                        d.linesHeight = '100%'
                        d.linesWidth = 0
                        d.groundColor = '#f3f8fd'
                        d.direction = '0'
                    }
                    if (d.state == '5') { //日程
                        d.h = 22
                        d.w = 13
                        d.showType = 'month'
                    }
                    if (d.state == '4') { //多媒体
                        d.h = 10
                        d.w = 12
                        d.groundColor = '#ccc'
                        d.attrType = 'picture'
                        d.showType = 'tile'
                        d.direction = 'horizontal'
                        d.isSource = 'nohas'
                        d.interval = 1000
                        d.titleName = ''
                        d.linkUrl = ''
                        d.carouselList = [
                            { id: utils.uuid(16), i: utils.uuid(16), linkUrl: '', titleName: '', showPicture: '' }
                        ]
                    }
                    if (d.state == '6') { //人物块状
                        d.h = 12
                        d.w = 14
                        d.groundColor = ''
                        d.borderColor = '#c7c7c7'
                        d.colNumber = 3
                        d.iconField = ''
                        d.itemHeight = 70
                        d.itemRadius = 4
                        d.iconList = [{ iconUrl: '', iconValue: '', i: utils.uuid(16) }]
                        d.dataSourceData = [
                            { id: utils.uuid(16), i: utils.uuid(16), showName: '', textAlign: "left", field: '', fontColor: '#333', fontSize: 12, backgroundColor: '' }
                        ]
                    }
                    break;
                case 'biElement':
                    d.state = this.dragTargetItem.state
                    d.controlName = this.dragTargetItem.controlName
                    d.controlType = this.dragTargetItem.title
                    if (d.state == 1) { // 指标项
                        d.w = 17
                        d.h = 15
                        d.chartId = '1'
                        d.chartType = '1';
                        d.chartValue = '003'
                        d.point = true
                        d.smooth = false
                        d.isSlider = false
                        d.viewType = 'chart'
                    } else if (d.state == 2) { // 轮播表
                        d.w = 17
                        d.h = 15
                        d.rowNum = 5
                        d.waitTime = 2000
                        d.carousel = 'single'
                        d.sort = true
                        d.groundColor = '#fff'
                        d.rowNumbers = true
                    } else if (d.state == 3) {
                        d.w = 36
                        d.h = 2
                        d.ancherName = '锚点标题'
                        d.fontColor = 'var(--light-font-grey)'
                        d.fontSize = '14'
                    } else if (d.state == 5) { //指标贴
                        d.HPosition = true
                        d.w = 6
                        d.h = 8
                    } else if (d.state == 7) { //指标贴
                        d.HPosition = true
                        d.w = 12
                        d.h = 8
                    } else if (d.state == 6) { //层级指标贴
                        d.HPosition = true
                        d.w = 20
                        d.h = 18
                        d.itemBackground1 = 'rgba(0, 121, 251, 0.29)'
                        d.itemBackground2 = 'rgba(230, 162, 60, 0.35)'
                    }
                    break;
                case 'ratherTab': //比较页签
                    d.h = 8
                    d.w = 36
                    d.nature = {}
                    d.nature.fixation = 2
                    d.nature.data = []
                    d.nature.source = []
                    d.controlType = this.dragTargetItem.title;
                    break;
                case 'business': //业务组件
                    d.h = 24
                    d.w = 24
                    d.versionCode = this.dragTargetItem.versionCode
                    d.groundColor = 'var(--light-back-grey)'
                    d.bid = this.dragTargetItem.bid
                    d.bTitle = this.dragTargetItem.title
                        // d.natureArr = JSON.parse(this.dragTargetItem.other)
                    d.controlType = '业务组件';
                    break;
            }
        },
        // 获取业务组件信息
        getBusinessOther(dragLay) {
            this.$nextTick(() => {
                const _this = this
                this.$api({
                        url: '/ts-bi/api/BiReportDesign/' + dragLay.bid,
                        method: 'get',
                        loading: false
                    })
                    .then(function(res) {
                        // _this.natureArr[_this.natureArr.length - 1].natureArr = JSON.parse(res.object[0].other).natureArr
                        _this.natureArr = _this.setNatureArrByid(_this.natureArr, dragLay.id, JSON.parse(res.object[0].other).natureArr)
                        _this.setStoreNatureArr()
                    })
                    .catch(function(error) {
                        // 请求失败
                    })
            })

        },
        setNatureArrByid(natureArr, id, newNatureArrs) {
            for (let i = 0; i < natureArr.length; i++) {
                if (natureArr[i].id === id) {
                    natureArr[i].natureArr = newNatureArrs
                    return natureArr
                } else {
                    if (natureArr[i].type === 'tab') {
                        for (let tab of natureArr[i].tabs) {
                            if (tab.index == natureArr[i].activeIndex) {
                                let natus = this.setNatureArrByid(tab.natureArr, id, newNatureArrs)
                                if (natus) {
                                    tab.natureArr = natus
                                    return natureArr
                                }
                            }
                        }
                    } else if (natureArr[i].type === 'panel') {
                        let natus = this.setNatureArrByid(natureArr[i].natureArr, id, newNatureArrs)
                        if (natus) {
                            natureArr[i].natureArr = natus
                            return natureArr
                        }
                    }
                }
            }
            return null
        },
        //获取当前鼠标停留的元素
        getDropdom(pos, dropClass) {
            let index = -1
            let _dom = this.$refs.biUserDefinedIconDragView.getElementsByClassName(dropClass)
            return this.getDropdomChild(_dom, dropClass, pos)
        },
        getDropdomChild(_dom, dropClass, pos) {
            for (let i = 0; i < _dom.length; i++) {
                // 存在内嵌控件
                if (_dom[i].getElementsByClassName(dropClass) && _dom[i].getElementsByClassName(dropClass).length) {
                    let domI = this.getDropdomChild(_dom[i].getElementsByClassName(dropClass), dropClass, pos)
                    if (domI != null) {
                        return domI
                    }
                }
                let p = _dom[i].getBoundingClientRect()
                let w = _dom[i].offsetWidth
                let h = _dom[i].offsetHeight
                if (pos.x > p.x && pos.x < p.x + w && pos.y > p.y && pos.y < p.y + h) {
                    return _dom[i]
                }
            }
        },
        //获取指标元素当前鼠标停留的元素
        getDropdomTarget(pos, dropClass) {
            let index = -1
            let _dom = this.$refs.biUserDefinedIconDragView.getElementsByClassName(dropClass)
            for (let i = 0; i < _dom.length; i++) {
                let p = _dom[i].getBoundingClientRect()
                let w = _dom[i].offsetWidth
                let h = _dom[i].offsetHeight
                if (pos.x > p.x && pos.x < p.x + w && pos.y > p.y && pos.y < p.y + h) {
                    index = i
                }
            }
            if (index == -1) {
                return null
            }
            return _dom[index] // 当前鼠标停留的元素
        },
        getPageLocation(event) {
            let e = event || window.event
            let scrollX =
                document.documentElement.scrollLeft || document.body.scrollLeft
            let scrollY =
                document.documentElement.scrollTop || document.body.scrollTop
            let x = e.pageX || e.clientX + scrollX
            let y = e.pageY || e.clientY + scrollY
            return {
                x: x,
                y: y
            }
        },
        // 移动指标元素
        moverTarget(event) {
            let pos = this.getPageLocation(event)
            this.dropdom = this.getDropdomTarget(pos, 'vxe-header--column')
            if (this.dropdom) {
                this.$eventBus.$emit('resetOtherActive', {}) //清除其他高亮
                this.dropdom.classList.add('biUserDefined-table__active')
            }
        },
        // 放置指标元素
        async dropTarget() {
            const _this = this
            if (this.dropdom) {
                let tableDom = this.getTableId(this.dropdom)
                if (tableDom) {
                    let tableId = tableDom.id
                    let result = this.getColByNatureArrByIndex(this.natureArr, tableId, this.dropdom.cellIndex - 1)
                    if (!result.colItem) { // 未拖至正确的位置
                        return
                    }
                    let selectColItem = result.colItem
                    selectColItem.type = 'col'
                    if (this.dragTargetItem.queryName) { // 数据源数据
                        if (this.dragTargetItem.children) { //一整个字段被拖
                            if (this.dragTargetItem.children.length + 1 > result.natu.cols.length) {
                                // let a = this.dragTargetItem.children.length - result.natu.cols.length
                                for (let i = result.natu.cols.length; i < this.dragTargetItem.children.length + 1; i++) {
                                    result.natu.cols.push({
                                        id: utils.uuid(16),
                                        title: '',
                                        align: 'center',
                                        field: 'col' + utils.uuid(6),
                                        // sortable: false,
                                        width: 60,
                                        fontSize: 12,
                                        fontColor: '#333',
                                        clickEvent: [],
                                        backgroundColor: '',
                                        pid: result.natu.cols[1].id,
                                        resizable: false
                                    })
                                }

                            }
                            if (result.natu.dataSourceId && result.natu.dataSourceId != this.dragTargetItem.children[0].parent.id) {
                                await _this.clearCoverDSId(result, selectColItem, '不属于同一个数据源，若需更改数据源，会导致单元格配置清空或被覆盖，是否继续更改？') // 清空并覆盖
                            } else if (result.natu.dataSource && result.natu.dataSource.resultName && result.natu.dataSource.resultName != this.dragTargetItem.children[0].parent.resultName) { // 存在数据源，不可更改
                                await _this.clearCoverDSId(result, selectColItem, '不属于同一个结果集，若需更改结果集，会导致单元格配置清空或被覆盖，是否继续更改')
                            } else if (result.natu.dataSourceId || result.natu.dataSource) {
                                await _this.clearCoverDSId(result, selectColItem, '已有该数据源字段,您确定要覆盖之前的字段吗？')
                            } else {
                                _this.coverDSId(result)
                            }
                        } else { //单个字段被拖
                            if (result.natu.dataSourceId && result.natu.dataSourceId != this.dragTargetItem.parent.id) { // 存在数据源，不可更改
                                await _this.clearOneDSId(result, selectColItem, '不属于同一个数据源，若需更改数据源，会导致单元格配置清空或被覆盖，是否继续更改？')
                            } else if (result.natu.dataSource && result.natu.dataSource.resultName && result.natu.dataSource.resultName != this.dragTargetItem.parent.resultName) { // 存在数据源，不可更改
                                await _this.clearOneDSId(result, selectColItem, '不属于同一个结果集，若需更改结果集，会导致单元格配置清空或被覆盖，是否继续更改？')
                            } else {
                                _this.coverOneDSId(result, selectColItem)
                            }
                            if (!result.natu.selectDataSourceCodeOption) {
                                result.natu.selectDataSourceCodeOption = []
                                for (let code of this.dragTargetItem.parent.columns) {
                                    result.natu.selectDataSourceCodeOption.push({ name: code.name })
                                }
                            }
                        }
                    } else { // 纬度指标
                        if (result.natu.dataSourceId && result.natu.dataSourceId != '000') { // 存在数据源，不可更改
                            this.$utils.msg(this, '不属于同一个数据源，请重新操作')
                            return false
                        }
                        result.natu.dataSourceName = '维度指标'
                        result.natu.dataSourceId = '000'
                        selectColItem.measureName = this.dragTargetItem.measureName
                        selectColItem.title = this.dragTargetItem.name || this.dragTargetItem._name
                        selectColItem.isCommon = this.dragTargetItem.isCommon || false
                        selectColItem.targetId = this.dragTargetItem.uuid || this.dragTargetItem._id
                        selectColItem.targetName = this.dragTargetItem.name || this.dragTargetItem._name
                        if (selectColItem.isCommon) { // 通用指标
                            selectColItem = this.setDataCommonTarget(selectColItem)
                        }
                    }
                    this.setStoreNatureArr()
                    this.dragType = ''
                }
            }
        },

        //清空并覆盖
        async clearCoverDSId(result, selectColItem, confirmText) {
            const _this = this
            await this.$mModal.confirm(confirmText).then(type => {
                if (type == 'confirm') {
                    result.natu.cols.forEach(eve => {
                        if (eve.title && eve.title != '#') {
                            eve.title = ''
                            eve.targetId = null
                            eve.targetName = null
                            eve.columnType = null
                        }
                    })
                    _this.coverDSId(result)
                        // this.setStoreNatureArr()
                    this.$eventBus.$emit('itemselectchange', { target: selectColItem, select: true });
                    _this.dropdom.classList.add('biUserDefined-table__active')
                }
            })

        },
        // 覆盖表格数据源
        coverDSId(result) {
            result.natu.selectDataSourceCodeOption = []
            for (let i = 0; i < this.dragTargetItem.children.length; i++) {
                result.natu.dataSourceName = this.dragTargetItem.children[0].parent.queryName
                result.natu.dataSourceId = this.dragTargetItem.children[0].parent.id
                result.natu.dataSource = JSON.parse(JSON.stringify(this.dragTargetItem.children[0].parent))
                result.natu.cols[i].title = this.dragTargetItem.children[i].name
                result.natu.cols[i].targetId = this.dragTargetItem.children[i].name
                result.natu.cols[i].targetName = this.dragTargetItem.children[i].name
                result.natu.cols[i].columnType = this.dragTargetItem.children[i].type
                result.natu.cols[i].type = 'col'
                result.natu.selectDataSourceCodeOption.push({ name: this.dragTargetItem.children[i].name })
            }
        },
        // 清空并覆盖一个字段
        async clearOneDSId(result, selectColItem, confirmText) {
            const _this = this
            await this.$mModal
                .confirm(confirmText)
                .then(type => {
                    if (type == 'confirm') { // 更改
                        result.natu.selectDataSourceCodeOption = []
                        result.natu.cols.forEach(eve => {
                            if (eve.title && eve.title != '#') {
                                eve.title = ''
                                eve.targetId = null
                                eve.targetName = null
                                eve.columnType = null
                            }
                        })
                        _this.coverOneDSId(result, selectColItem)
                    } else { // 不更改
                        return false
                    }
                })
        },
        // 覆盖一个
        coverOneDSId(result, selectColItem) {
            result.natu.dataSourceName = this.dragTargetItem.parent.queryName
            result.natu.dataSourceId = this.dragTargetItem.parent.id
            result.natu.dataSource = JSON.parse(JSON.stringify(this.dragTargetItem.parent))
            selectColItem.title = this.dragTargetItem.name
            selectColItem.targetId = this.dragTargetItem.name
            selectColItem.targetName = this.dragTargetItem.name
            selectColItem.columnType = this.dragTargetItem.type
            selectColItem.backgroundColor = ''
        },
        getTableId(startTag) {
            if (!(startTag instanceof HTMLElement)) return console.error('receive only HTMLElement')
            if (startTag.parentElement && startTag.parentElement.getAttribute('type') != 'table') {
                return this.getTableId(startTag.parentElement)
            } else {
                return startTag.parentElement;
            }
        },
        //获取通用指标
        setDataCommonTarget(selectColItem) {
            var rt = {
                id: '',
                name: '',
                i: utils.uuid(16),
                model: ''
            }
            selectColItem.relationTarget = [rt];
            if (selectColItem.targetId == '1' || selectColItem.targetId == '4' || selectColItem.targetId == '7') {
                let newRt = {...rt }
                newRt.i = utils.uuid(16) + '1'
                selectColItem.relationTarget.push(newRt);
            }
            return selectColItem
        }
    },
    beforeDestroy: function() {
        this.$eventBus.$off('dragstart', this.dragstart) //用于指标的移动监听
            // eventBus.$off('clearScreenView', this.clearScreenView)//清空筛选框
    }
}
export default dragEventMixins