import {
  getData,
  getFindList,
  deleteData,
  getBoxType,
  addData,
  updateData,
  addDocToBox,
  detail,
  detailDelete
} from "@/api/intelligent-warehouse/warehouse-management/fileBox-management.js"
import {
  getClassTreeByClassName
} from "@/api/system-management/archiveSettings"
import {
  getButtonList
} from "@/api/user"
import {
  message
} from '@/utils/common'
const getDefaultState = () => {
  return {
    tableLoading: false,
    tableData: [],
    findLIst: [],
    boxLIst: [],
    pagination: {
      pageSize: 10,
      pageNum: 1,
      total: 0,
    },
    treeLoading: false,
    treeData: [],
    detailTableData: [],
    showButtons: {
      add: false,
      edit: false,
      del: false,
      addDel: false,
      ware: false
    },
  }
}
const state = getDefaultState()
const mutations = {
  GET_DATA(state, data) {
    state.tableData = data.list
    state.tableLoading = false
    state.pagination.total = data.total
  },
  ADD_DATA(state, data) {
    message('success', '新增成功', 3000)
  },
  UPDATE_DATA(state, data) {
    message('success', '修改成功', 3000)
  },
  DELECT_DATA(state, data) {
    message('success', '删除成功', 3000)
  },
  FIND_LIST(state, data) {
    state.findLIst = data.list
  },
  BOX_TYPE(state, data) {
    state.boxLIst = data.list
  },
  GET_TREE(state, data) {
    state.treeData = data.list
    state.treeLoading = false
  },
  ADD_BOX(state, data) {
    message('success', '操作成功', 3000)
  },
  DETAIL(state, data) {
    state.detailTableData = data
  },
  DETAIL_DEL(state, data) {
  },
  GET_BUTTON_LIST(state, data) {
    data.forEach(item => {
      state.showButtons[item.key] = true
    })
  }
}
const actions = {

  async getData({
    commit,
    dispatch
  }, data) {
    await getData(data).then(res => {
      try {
        commit('GET_DATA', res.body)
      } catch (err) {
        throw new Error(err)
      }
    })
  },
  async addData({
    commit,
    dispatch
  }, data) {
    await addData(data).then(res => {
      try {
        commit('ADD_DATA', res.body)
      } catch (err) {
        throw new Error(err)
      }
    })
  },
  async updateData({
    commit,
    dispatch
  }, data) {
    await updateData(data).then(res => {
      try {
        commit('UPDATE_DATA', res.body)
      } catch (err) {
        throw new Error(err)
      }
    })
  },
  async deleteData({
    commit,
    dispatch
  }, data) {
    await deleteData(data).then(res => {
      try {
        commit('DELECT_DATA', res.body)
      } 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 getBoxType({
    commit,
    dispatch
  }, data) {
    await getBoxType(data).then(res => {
      try {
        commit('BOX_TYPE', res.body)
      } catch (err) {
        throw new Error(err)
      }
    })
  },
  async getClassTreeByClassName({
    commit,
    dispatch
  }, data) {
    await getClassTreeByClassName(data).then(res => {
      try {
        commit('GET_TREE', res.body)
      } catch (err) {
        throw new Error(err)
      }
    })
  },
  async addDocToBox({
    commit,
    dispatch
  }, data) {
    await addDocToBox(data).then(res => {
      try {
        commit('ADD_BOX', res.body)
      } catch (err) {
        throw new Error(err)
      }
    })
  },
  async detail({
    commit,
    dispatch
  }, data) {
    return await detail(data).then(res => {
      try {
        commit('DETAIL', res)
        return res
      } catch (err) {
        throw new Error(err)
      }
    })
  },
  async detailDelete({
    commit,
    dispatch
  }, data) {
    await detailDelete(data).then(res => {
      try {
        commit('DETAIL_DEL', res)
      } catch (err) {
        throw new Error(err)
      }
    })
  },
  async getButtonList({
    commit
  }, params) {
    await getButtonList(params).then(res => {
      commit('GET_BUTTON_LIST', res.body.list)
    })
  }
}
export default {
  namespaced: true,
  state,
  mutations,
  actions
}
