import {
  getEntityTree,
  getTentityType,
  updateTentityType,
  deleteTentityType,
  getFindList,
  getTentityStructByEntityId,
  getAllTentityTypeManyByClass,
  //seven one
  deleteRecord,
  getRecordList,
  insertRecord,
  updateRecord,
  //seven two
  insertRecordTab,
  updateRecordTab,
  copyEntity
} from "@/api/system-management/templateSetting"
import {
  message
} from '@/utils/common'
const getDefaultState = () => {
  return {
    ////>>>>>>    third    <<<<<<////
    thirdTree: [],
    // 实体模版临时数据存储
    dictionariesFormVal: {},
    //结构下拉框的值
    downTypeList: [],
    leftList: [],

    ////>>>>>>fourth<<<<<<////
    componentList: [],
    sevenList: [],
    //////
    sevenLoading: false
  }
}
const state = getDefaultState()
const mutations = {
  TREE_LIST(state, data) {
    state.thirdTree = data.list
  },
  TENTITY_TYPE(state, data) {
    state.dictionariesFormVal = {
      ...data.data.packTentityType,
      isType: 1,
      id: data.id,
    }
  },
  FIND_LIST(state, data) {
    state.downTypeList = data.list
  },
  DATA_TAIL_LIST(state, data) {
    data.tentityStructList.forEach((item) => {
      if (item.columnCanNull === "true") {
        item.columnCanNull = true;
      } else {
        item.columnCanNull = false;
      }
    });
    state.leftList = data.tentityStructList;
  },
  TYPE_MANY_BY_CLASS(state, data) {
    state.componentList = data.list
  },
  //seven//
  SEVEN_LIST(state, data) {
    state.sevenList = data.list
    state.sevenLoading = false
  },
  SAVE_TITLE(state, data) {
    message('success', '保存成功', 3000)
  },
  UPDATA_RECORD(state, data) {
    message('success', '修改成功', 3000)
  },
  DELETE_RECORD(state, data) {
    message('success', '删除成功', 3000)
  },
  INSERT_TAB(state, data) {
    message('success', '保存成功', 3000)
  },
  UPDATA_TAB(state, data) {
    message('success', '修改成功', 3000)
  },
  COPY_ENTITY(state, data) {
    message('success', '复制实体成功', 3000)
  },

}
const actions = {
  async getEntityTree({
    commit,
    dispatch
  }, data) {
    await getEntityTree(data).then(res => {
      try {
        commit('TREE_LIST', res.body)
      } catch (err) {
        throw new Error(err)
      }
    })
  },
  async getTentityType({
    commit,
    dispatch
  }, data) {
    await getTentityType(data).then(res => {
      try {
        let info = {
          'data': res.body,
          'id': data.id
        }
        commit('TENTITY_TYPE', info)
      } catch (err) {
        throw new Error(err)
      }
    })
  },
  async updateTentityType({
    commit,
    dispatch
  }, data) {
    await updateTentityType(data).then(res => {
      try {} catch (err) {
        throw new Error(err)
      }
    })
  },
  async deleteTentityType({
    commit,
    dispatch
  }, data) {
    await deleteTentityType(data).then(res => {
      try {} catch (err) {
        throw new Error(err)
      }
    })
  },
  async getFindList({
    commit,
    dispatch
  }, data) {
    await getFindList(data).then(res => {
      try {
        commit('FIND_LIST', res.body)
      } catch (err) {
        throw new Error(err)
      }
    })
  },
  async getTentityStructByEntityId({
    commit,
    dispatch
  }, data) {
    await getTentityStructByEntityId(data).then(res => {
      try {
        commit('DATA_TAIL_LIST', res.body)
      } catch (err) {
        throw new Error(err)
      }
    })
  },
  async getAllTentityTypeManyByClass({
    commit,
    dispatch
  }, data) {
    await getAllTentityTypeManyByClass(data).then(res => {
      try {
        commit('TYPE_MANY_BY_CLASS', res.body)
      } catch (err) {
        throw new Error(err)
      }
    })
  },
  async getRecordList({
    commit,
    dispatch
  }, data) {
    await getRecordList(data).then(res => {
      try {
        commit('SEVEN_LIST', res.body)
      } catch (err) {
        throw new Error(err)
      }
    })
  },
  async insertRecord({
    commit,
    dispatch
  }, data) {
    await insertRecord(data).then(res => {
      try {
        commit('SAVE_TITLE', res.body)
      } catch (err) {
        throw new Error(err)
      }
    })
  },
  async updateRecord({
    commit,
    dispatch
  }, data) {
    await updateRecord(data).then(res => {
      try {
        commit('UPDATA_RECORD', res.body)
      } catch (err) {
        throw new Error(err)
      }
    })
  },
  async deleteRecord({
    commit,
    dispatch
  }, data) {
    await deleteRecord(data).then(res => {
      try {
        commit('DELETE_RECORD', res.body)
      } catch (err) {
        throw new Error(err)
      }
    })
  },
  async insertRecordTab({
    commit,
    dispatch
  }, data) {
    await insertRecordTab(data).then(res => {
      try {
        commit('INSERT_TAB', res.body)
      } catch (err) {
        throw new Error(err)
      }
    })
  },
  async insertRecordTab({
    commit,
    dispatch
  }, data) {
    await insertRecordTab(data).then(res => {
      try {
        commit('INSERT_TAB', res.body)
      } catch (err) {
        throw new Error(err)
      }
    })
  },
  async updateRecordTab({
    commit,
    dispatch
  }, data) {
    await updateRecordTab(data).then(res => {
      try {
        commit('UPDATA_TAB', res.body)
      } catch (err) {
        throw new Error(err)
      }
    })
  },
  async copyEntity({
    commit,
    dispatch
  }, data) {
    await copyEntity(data).then(res => {
      try {
        commit('COPY_ENTITY', res.body)
      } catch (err) {
        throw new Error(err)
      }
    })
  },

}
export default {
  namespaced: true,
  state,
  mutations,
  actions
}
