import _ from 'lodash'
const dragColMixins = {
    data() {
        return {}
    },
    mounted: function() {
        this.$eventBus.$on('deleteCol', this.deleteColHandler) //删除列
        this.$eventBus.$on('addCol', this.addColHandler) //插入列
    },
    methods: {
        deleteColHandler(item) { // 删除列

            if (item.pid) {
                let colItem = this.getColByNatureArr(this.natureArr, item.pid, item.field)
                colItem.cols.splice(colItem.i, 1)
                this.$eventBus.$emit('itemselectchange', { target: {}, select: false });
                this.$store.commit('setSelectData', {})
                this.$store.commit('setNatureArr', [...this.natureArr])
            } else {
                this.$utils.msg(this, '序列号列不允许删除')
            }
        },
        addColHandler(item) { // 插入列
            if (item.pid) {
                let colItem = this.getColByNatureArr(this.natureArr, item.pid, item.field)
                let uuid = this.$utils.uuid(16)
                let d = {
                    title: '',
                    id: uuid,
                    align: 'center',
                    field: 'col' + uuid,
                    width: 60,
                    pid: item.pid,
                    clickEvent: [],
                    resizable: false
                }
                colItem.cols.splice(colItem.i, 0, d);
                this.$store.commit('setNatureArr', [...this.natureArr])
            } else {
                this.$utils.msg(this, '序列号列不允许插入')
            }
        },
        // 获取表格项（处理存在tab页签的项）
        getTablesItem(tablesItem, pid, field) {
            if (this.$refs.gridMainLayout.$refs.tsTable) {
                tablesItem = this.getTablesItemPlus(this.$refs.gridMainLayout.$refs.tsTable, pid, field)
            }
            if (Object.keys(tablesItem).length === 0) {
                for (let gridMainItem of this.$refs.gridMainLayout.$refs.tabMainLayout) {
                    if (gridMainItem.$refs.tsTable && gridMainItem.$refs.tsTable.length > 0) {
                        tablesItem = this.getTablesItemPlus(gridMainItem.$refs.tsTable, pid, field)
                        if (Object.keys(tablesItem).length != 0) {
                            return tablesItem
                        }
                    }
                }
            }
            return tablesItem
        },
        getTablesItemPlus(tsTables, pid, field) {
            let tablesItem = {}
            for (let table of tsTables) {
                if (table.$refs.tstable.id === pid) {
                    let col = table.$refs.tstable.getColumnByField(field)
                    let index = table.$refs.tstable.getColumnIndex(col)
                    let fullColumn = table.getTableColumn().fullColumn
                    tablesItem.i = index
                    tablesItem.fullColumn = fullColumn
                    tablesItem.table = table
                    return tablesItem
                }
            }
            return tablesItem
        },
        getColByNatureArr(natureArr, pid, field) {
            let colItem = {}
            for (let natu of natureArr) {
                if (natu.type === 'tab') {
                    for (let tab of natu.tabs) {
                        if (tab.index == natu.activeIndex) {
                            let result = this.getColByNatureArr(tab.natureArr, pid, field)
                            if (Object.keys(result).length > 0) {
                                return result
                            }
                            return
                        }
                    }
                } else if (natu.type === 'panel') {
                    let result = this.getColByNatureArr(natu.natureArr, pid, field)
                    if (Object.keys(result).length > 0) {
                        return result
                    }
                } else if (natu.type == 'table' || natu.id === pid) { //新增的col的id不一致
                    for (let i = 0; i < natu.cols.length; i++) {
                        if (natu.cols[i].field == field) {
                            colItem.i = i
                            colItem.cols = natu.cols
                            return colItem
                        }
                    }
                    break
                }
            }
            return colItem
        },
        //通过下标找表格列（含tab的情况）
        getColByNatureArrByIndex(natureArr, pid, index) {
            for (let natu of natureArr) {
                if (natu.type === 'tab') {
                    for (let tab of natu.tabs) {
                        if (tab.index == natu.activeIndex) {
                            let result = this.getColByNatureArrByIndex(tab.natureArr, pid, index)
                            if (Object.keys(result).length > 0) {
                                return result
                            }
                        }
                    }
                } else if (natu.type == 'panel') {
                    let result = this.getColByNatureArrByIndex(natu.natureArr, pid, index)
                    if (Object.keys(result).length > 0) {
                        return result
                    }
                } else {
                    if (natu.id === pid) {
                        return { colItem: natu.cols[index], natu: natu }
                    }
                }
            }
            return {}
        },
    },
    beforeDestroy: function() {
        this.$eventBus.$off('deleteCol', this.deleteColHandler) //删除列
        this.$eventBus.$off('addCol', this.addColHandler) //插入列
    }
}
export default dragColMixins