
import { Notification, MessageBox, Message } from 'element-ui'

const attribute = {
  state: {
    attributeList: [],
    groupList: []
  },

  mutations: {
    SET_ATTRIBUTE_LIST: (state, list) => {
      state.attributeList = list
    },
    SET_GROUP_LIST: (state, list) => {
      state.groupList = list
    },
    PUSH_ATTRIBUTE: (state, item) => {
      state.attributeList.push(item)
    },
    PUSH_GROUP: (state, item) => {
      state.groupList.push(item)
    },
    UPDATE_ATTRIBUTE: (state, {item, idx}) => {
      state.attributeList[idx] = item
    },
    UPDATE_GROUP: (state, {item, idx}) => {
      state.groupList[idx] = item
    },
    DELETE_ATTRIBUTE: (state, idx) => {
      state.attributeList.splice(idx,1);
    },
    DELETE_GROUP: (state, idx) => {
      state.groupList.splice(idx,1);
    },
    GET_ATTRIBUTE_ITEM: (state, idx) => {
      return state.attributeList[idx]
    },
    GET_GROUP_ITEM: (state, idx) => {
      return state.groupList[idx]
    }
  },

  actions: {
    // 初始化 属性和分组数据
    InitAttributeAndGroup({ commit, state }, data) {
      commit('SET_ATTRIBUTE_LIST', data.attrList)
      commit('SET_GROUP_LIST', data.groupList)
      console.log('res:', state)
    },
    // 新增 属性
    AddAttribute({ commit, state }, attr) {
      commit('PUSH_ATTRIBUTE', attr)
    },
    // 新增 分组
    AddGroup({ commit, state }, data) {
      commit('PUSH_GROUP', data)
    },
    // 获取 属性 单个信息
    GetAttributeItem({ commit, state }, idx) {
      return new Promise((resolve, reject) => {
        resolve(state.attributeList[idx])
      })
    },
     // 获取 分组 单个信息
    GetGroupItem({ commit, state }, idx) {
      return new Promise((resolve, reject) => {
        resolve(state.groupList[idx])
      })
    },
    // 修改 属性
    EditAttribute({ commit, state }, param) {
      commit('UPDATE_ATTRIBUTE', param)
    },
    // 修改 属性
    EditGroup({ commit, state }, param) {
      commit('UPDATE_GROUP', param)
    },
    // 删除分组 删除分组 判断分组是否有属性，有属性就不能删除
    DeleteGroup({ commit, state }, idx) {
      let groupItem = state.groupList[idx];
      let result = state.attributeList.filter(v => {
        if (v['cmdKey'] === groupItem['cmdKey']) {
          return true
        }
      })
      if (!result) {
        commit('DELETE_GROUP', idx)
      } else {
        Message.error('当前分组已关联参数，无法删除！');
      }
    },
    // 删除属性
    DeleteAttribute({ commit, state }, idx) {
      commit('DELETE_ATTRIBUTE', idx)
    }

  }
}

export default attribute
