import { uploadBuildingExcel, submitBuildingExcel, uploadBuildingTreeExcel, submitBuildingTreeExcel } from '@/api/batchImport'

const batchImport = {
  namespaced: true,
  state: {
    total: '',
    temlist: [],
    treeTotal: '',
    treeTemlist: []
  },

  mutations: {
    SET_LIST: (state, list) => {
      state.temlist = list
    },
    SET_TOTAL: (state, total) => {
      state.total = total
    },
    SET_TREE_LIST: (state, list) => {
      state.treeTemlist = list
    },
    SET_TREE_TOTAL: (state, total) => {
      state.treeTotal = total
    }
  },

  actions: {
    UploadBuildingExcel({ commit }, file) {
      return new Promise((resolve, reject) => {
        uploadBuildingExcel({ file: file.raw }).then(response => {
          if (response.code === '0') {
            const data = response.data
            commit('SET_LIST', data.list)
            commit('SET_TOTAL', data.total)
          }
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },
    SubmitBuildingExcel({ commit }, file) {
      return new Promise((resolve, reject) => {
        submitBuildingExcel({ file: file.raw }).then(response => {
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },
    UploadBuildingTreeExcel({ commit }, file) {
      return new Promise((resolve, reject) => {
        uploadBuildingTreeExcel({ file: file.raw }).then(response => {
          if (response.code === '0') {
            const data = response.data
            commit('SET_TREE_LIST', data.list)
            commit('SET_TREE_TOTAL', data.total)
          }
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },
    SubmitBuildingTreeExcel({ commit }, file) {
      return new Promise((resolve, reject) => {
        submitBuildingTreeExcel({ file: file.raw }).then(response => {
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    }
  }
}

export default batchImport
