import {selectList,batchDelete,deleteList,addList,updateList} from "@/api/archiveProveService.js";
  import { getButtonList } from "@/api/user"
  import {
    message
  } from '@/utils/common'
  const getDefaultState = () => {
    return {
      tableData: [],
      loading: false,
      pagination: {
        pageSize: 10,
        pageNum: 1,
        total: 0,
      },
      showButtons: {
        add: false,
        edit: false,
        del: false,
      },
    }
  }
  const state = getDefaultState()
  const mutations = {
    GET_DATA(state, data) {
      state.tableData = data.list
      state.pagination.total = data.total
      state.loading = false
    },
    ADD_LIST(state, data) {
      // state.tableData = data.list
      // state.pagination.total = data.total
      // state.loading = false
    },
    UPDATE_LIST(state, data) {
      // state.tableData = data.list
      // state.pagination.total = data.total
      // state.loading = false
    },
    DELETE(state, data) {
      // state.tableData = data.list
      // state.pagination.total = data.total
      // state.loading = false
    },
    GET_BUTTON_LIST(state, data) {
      data.forEach(item => {
        state.showButtons[item.key] = true
      })
    }
  
  }
  const actions = {
    async getData({
      commit,
      dispatch
    }, data) {
      await selectList(data).then(res => {
        try {
          commit('GET_DATA', res.body)
        } catch (err) {
          throw new Error(err)
        }
      })
    },
    // 批量
    async batchDelete({commit,dispatch},data){
        await batchDelete(data).then(res=>{
            try {
                commit('DELETE', res.body)
            } catch (error) {
                throw new Error(err)
            }
        })
    },
    // 单个
    async deleteList({commit,dispatch},data){
        await deleteList(data).then(res=>{
            try {
                commit('DELETE', res.body)
            } catch (error) {
                throw new Error(err)
            }
        })
    },
    // 新增
    async addList({commit,dispatch},data){
        await addList(data).then(res=>{
            try {
                commit('ADD_LIST', res.body)
            } catch (error) {
                throw new Error(err)
            }
        })
    },
    // 修改
    async updateList({commit,dispatch},data){
        await updateList(data).then(res=>{
            try {
                commit('UPDATE_LIST', res.body)
            } catch (error) {
                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
  }
  