import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)

const store = {
    state: {
        params: {},
        selectCrumbs: {},
        allCrumbs: [],
        phoneNature: [],
        natureArr: {},
        roleTreeList: {}, // 角色树/用于Bi控制
        bigScreenNature: {
            natureArr: []
        }, // bi报表大屏模式
        biPreviewMap: {}, // 预览特殊属性：加载框；data等
        dialogEditors: [{}], // 编辑器弹窗（存个数）
        biDSUrls: {},
        snapshotData: [], // 编辑器快照数据
        snapshotIndex: -1, // 快照索引
        curComponent: null, // 当前激活的元素
        curComponentZIndex: null,
        biNatureArrIds: {}, //bi报表的控件id字典
        selectData: {},
        biSelectTable: {}, // 选中单元格时，保存Table
        screenData: [],
        dataSource: [], //数据源
        targetSource: [], //指标数据源
        // bi报表手机版数据
        biMobileObj: {},
        // bi报表手机版是否触发数据改变
        // biMobileIsDataChange: false,
        // bi报表是否新增的标识
        isBIAdd: {},
        mobileMouseRightTop: 0,
        mobileMouseRightLeft: 0,
        mobileMouseRightShow: false,
    },
    mutations: {
        setparams(status, data) {
            status.params = data
        },

        // fd表单界面设计器结束
        setDataSource(status, value) { // 数据源
            status.dataSource = value
        },
        setTargetSource(status, value) { // 指标库
            status.targetSource = value
        },
        removeTargetSource(status, value) { //清空指标库
            status.targetSource = []
        },
        setRoleTreeList(status, value) {
            status.roleTreeList = value
        },
        setPhoneNature(status, value) { // BI自定义报表组件区域
            status.phoneNature = value
        },
        setBIAdd(status, data) { // BI报表是否新增
            if (status.dialogEditors.length == 0) {
                return
            }
            if (typeof data == 'boolean') {
                const i = 'd' + (status.dialogEditors.length - 1)
                status.isBIAdd[i] = data
            } else {
                status.isBIAdd = data
            }
        },
        // 保存预览时的URL
        setBiDSUrls(status, data) {
            if (status.dialogEditors.length == 0) {
                return
            }
            if (Array.isArray(data)) {
                const i = 'd' + (status.dialogEditors.length - 1)
                status.biDSUrls[i] = data
            } else {
                status.biDSUrls = data
            }
        },
        setDialogEditors(status, data) { // BI报表是否新增
            status.dialogEditors = data
        },
        // BI预览保存loading和数据
        setBiPreviewMap(status, { id, pMap, formId }) {
            //改成有index的字典对象,由此应对多设计区的页面
            // if (status.dialogEditors.length == 0) {
            //     return
            // }
            const i = formId || 'd' + (status.dialogEditors.length - 1)
            if (!status.biPreviewMap[i]) {
                status.biPreviewMap[i] = {}
            }
            if (!status.biPreviewMap[i][id]) {
                status.biPreviewMap[i][id] = {
                    loading: false,
                    data: [],
                    cols: []
                }
            }
            status.biPreviewMap[i][id] = pMap
            status.biPreviewMap = {...status.biPreviewMap }
        },
        // BI预览保存loading和数据
        setAllBiPreviewMap(status, biPreviewMap) {
            status.biPreviewMap = {...biPreviewMap }
        },
        setNatureArr(status, data) { // BI自定义报表组件区域
            //改成有index的字典对象,由此应对多设计区的页面
            if (status.dialogEditors.length == 0) {
                return
            }
            if (Array.isArray(data)) {
                const i = 'd' + (status.dialogEditors.length - 1)
                status.natureArr[i] = []
                status.natureArr[i] = data
                status.natureArr = {...status.natureArr }
            } else {
                status.natureArr = data
            }
        },
        setBISelectTable(status, data) {
            status.biSelectTable = data
        },
        setBigScreenNature(status, data) { // BI自定义报表组件区域-大屏模式
            status.bigScreenNature = data
        },
        recordSnapshot(state) {
            // 添加新的快照
            // state.snapshotData[++state.snapshotIndex] = utils.deepCopy(state.bigScreenNature.natureArr)
            // // 在 undo 过程中，添加新的快照时，要将它后面的快照清理掉
            // if (state.snapshotIndex < state.snapshotData.length - 1) {
            // state.snapshotData = state.snapshotData.slice(0, state.snapshotIndex + 1)
            // }
        },
        setCurComponent(state, { component, zIndex }) {
            state.curComponent = component
            state.curComponentZIndex = zIndex
        },
        setBigArr(state, { index, selectData }) {
            state.bigScreenNature.natureArr[index] = selectData
        },
        // setShapeStyle(state, { top, left, width, height }) {
        //     if (top) state.curComponent.style.top = top
        //     if (left) state.curComponent.style.left = left
        //     if (width) state.curComponent.style.width = width
        //     if (height) state.curComponent.style.height = height
        //     state.bigScreenNature.natureArr[state.curComponentZIndex] = state.curComponent
        //     state.bigScreenNature = {...state.bigScreenNature }
        // },
        setShapePosStyle(state, { key, value }) {
            state.curComponent.style[key] = value
        },
        setBiNatureArrIds(status, data) { // BI自定义报表ID集合
            status.biNatureArrIds = data
        },

        // deleteComponent(state) {
        //     state.bigScreenNature.natureArr.splice(state.curComponentZIndex, 1)
        //     state.bigScreenNature = {...state.bigScreenNature }
        // },
        copyComponent(state, newId) {
            let newComponent = {...state.curComponent }
            newComponent.id = newId
            newComponent.style.left = newComponent.style.left + newComponent.style.width
            state.bigScreenNature.natureArr.push(newComponent)
            state.bigScreenNature = {...state.bigScreenNature }
        },
        upComponent() {
            // 上移图层 zIndex，表示元素在数组中越往后
            let { bigScreenNature, curComponentZIndex } = state
            if (curComponentZIndex < bigScreenNature.natureArr.length - 1) {
                swap(bigScreenNature.natureArr, curComponentZIndex, curComponentZIndex + 1)
                state.bigScreenNature = {...state.bigScreenNature }
            } else {
                console.log('已经到顶了', 'error')
            }
        },

        downComponent(state) {
            // 下移图层 zIndex，表示元素在数组中越往前
            let { bigScreenNature, curComponentZIndex } = state
            if (curComponentZIndex > 0) {
                swap(bigScreenNature.natureArr, curComponentZIndex, curComponentZIndex - 1)
                state.bigScreenNature = {...state.bigScreenNature }
            } else {
                console.log('已经到底了', 'error')
            }
        },

        topComponent(state) {
            // 置顶
            let { bigScreenNature, curComponentZIndex } = state
            if (curComponentZIndex < bigScreenNature.natureArr.length - 1) {
                swap(bigScreenNature.natureArr, curComponentZIndex, bigScreenNature.natureArr.length - 1)
                state.bigScreenNature = {...state.bigScreenNature }
            } else {
                console.log('已经到顶了', 'error')
            }
        },

        bottomComponent(state) {
            // 置底
            let { bigScreenNature, curComponentZIndex } = state
            if (curComponentZIndex > 0) {
                swap(bigScreenNature.natureArr, curComponentZIndex, 0)
                state.bigScreenNature = {...state.bigScreenNature }
            } else {
                console.log('已经到底了', 'error')
            }
        },
        setSelectData(status, data) { // 更新选择项
            status.selectData = data
        },
        saveArr(status, { data, biModel }) { // 更新选择项与组件区域关联
            if (biModel == 'mobile') {
                handlerItem(data, status.phoneNature)
            } else {
                handlerItem(data, status.natureArr['d' + (status.dialogEditors.length - 1)])
            }
        },
        setScreenData(status, data) { // 更新筛选框
            status.screenData = data
        },
        // bi报表手机版父节点id
        setBiMobileObj(state, v) {
            state.biMobileObj = v
        },
        // // bi报表手机版是否触发数据改变
        // setBiMobileIsDataChange(state, v) {
        //     state.biMobileIsDataChange = v
        // },
        // 手机右键
        setMobileMouseRight(state, { left, top, show }) {
            state.mobileMouseRightTop = top
            state.mobileMouseRightLeft = left
            state.mobileMouseRightShow = show
        }
    },
    actions: {},
    modules: {},
    getters: {}
}
export function swap(arr, i, j) {
    const temp = arr[i]
    arr[i] = arr[j]
    arr[j] = temp
}
// 用于BI自定义报表数据
export function handlerItem(data, arr) {
    if (!setSelectedItem(data, arr)) {
        for (let natu of arr) {
            if (natu.type === 'tab') {
                for (let tab of natu.tabs) {
                    handlerItem(data, tab.natureArr)
                }
            } else if (natu.type === 'table') {
                handlerItem(data, natu.cols)
            } else if (natu.type === 'panel') {
                handlerItem(data, natu.natureArr)
            } else if (natu.type === 'element' && natu.state == '1') {
                handlerItem(data, natu.dataSourceData)
            } else if (natu.type === 'col' && natu.children && natu.children.length) {
                handlerItem(data, natu.children)
            }
        }
    }
}
// 用于BI自定义报表数据-找item
export function setSelectedItem(data, arr) {
    let result = false
    if (arr) {
        for (let i = 0; i < arr.length; i++) {
            if (arr[i].id === data.id) {
                arr[i] = data
                result = true
                break
            }
        }
    }
    return result
}

// console.dir(Vue);
// console.dir(Vue.$store);
// Vue.$store.bi = store;
export default store