import {
  getTree,
  getUsers,
  saveUser,
  updateUser,
  deleteUser,
  resetPassword
} from "@/api/jurisdiction/user"
import {
  message
} from '@/utils/common'
import {
  getButtonList
} from "@/api/user"
const getDefaultState = () => {
  return {
    tableData: [],
    loading: false,
    //机构
    // treeList: [],
    // JGstatus: false,
    //编辑新增
    addStatus: false,
    rules: {
      username: [{
        required: true,
        message: '请输入登录账号',
        trigger: 'blur'
      }],
      userChnName: [{
        required: true,
        message: '请输入用户名称',
        trigger: 'blur'
      }],
      markEnable: [{
        required: true,
        message: '请选择',
        trigger: 'blur'
      }],
      level: [{
        required: true,
        message: '请选择',
        trigger: 'change'
      }],
      idCard: [{
          required: true,
          message: '身份证不能为空',
          trigger: ['blur', 'change']
        },
        {
          pattern: /^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$|^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$/,
          message: '请输入正确的身份证',
          trigger: ['blur', 'change']
        },
      ],
    },
    page: {
      pageSize: 10,
      pageNum: 1,
      total: 0,
    },
    showButtons: {
      add: false,
      update: false,
      del: false,
      szmm: false
    }
  }
}
const state = getDefaultState()
const mutations = {
  GET_USERS(state, data) {
    state.tableData = data.list
    state.page.total = data.total
    state.loading = false
  },
  GET_TREE(state, data) {
    state.treeList = data.list
  },
  SAVE_USER(state, data) {
    state.addStatus = false
  },
  GET_BUTTON_LIST(state, data) {
    data.forEach(item => {
      state.showButtons[item.key] = true
    })
  }
}
const actions = {
  async getUsers({
    commit,
    dispatch
  }, data) {
    await getUsers(data).then(res => {
      try {
        commit('GET_USERS', res.body)
      } catch (err) {
        throw new Error(err)
      }
    })
  },
  async getTree({
    commit
  }, params) {
    await getTree(params).then(res => {
      try {
        commit('GET_TREE', res.body)
      } catch (err) {
        throw new Error(err)
      }
    })
  },
  async saveUser({
    commit
  }, params) {
    await saveUser(params).then(res => {
      try {
        message('success', '新增成功', 3000)
        commit('SAVE_USER', res.body)
      } catch (err) {
        throw new Error(err)
      }
    })
  },
  async deleteUser({
    commit
  }, params) {
    await deleteUser(params).then(res => {
      try {
        message('success', '删除成功', 3000)
      } catch (err) {
        throw new Error(err)
      }
    })
  },
  async resetPassword({
    commit
  }, params) {
    await resetPassword(params).then(res => {
      try {
        message('success', '密码重置为:EZyc@2024', 5000)
      } catch (err) {
        throw new Error(err)
      }
    })
  },
  async updateUser({
    commit
  }, params) {
    await updateUser(params).then(res => {
      try {
        commit('SAVE_USER', res.body)
        message('success', '修改成功', 3000)
      } 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
}
