import { PAGE_SIZE } from '@/const/config/page'
import UserService from '@/service/UserManagementService'
import StaffService from '@/service/StaffService'
import { Module } from 'vuex'
import { iListItem, iPlatformModuleState } from './types'

export default <Module<iPlatformModuleState, {}>>{
  namespaced: true,
  state: {
    list: [] as iListItem[],
    end: undefined,
    firstBranch: undefined,
    firstSubBranch: undefined,
    level: undefined,
    location: '',
    page: 1,
    page_size: PAGE_SIZE,
    phone: '',
    secondBranch: undefined,
    secondSubBranch: undefined,
    start: undefined,
    userName: '',
    total: 0,
    loading: false,
    uuid: '',
    role: ''
  },
  mutations: {
    save(state, payload) {
      Object.assign(state, payload)
    },
  },
  actions: {
    async query({ state, commit }, payload) {
      state.loading = true
      const ret = await UserService.getInstance().adminQueryBankAdmins({
        phone: state.phone,
        end: state.end as number,
        page: state.page as number,
        page_size: state.page_size as number,
        start: state.start as number,
        user_name: state.userName,
      })
      if (ret.code === 200) {
        commit('save', {
          list: ret.data.item,
          total: ret.data.total,
        })
        state.loading = false
      }
    },
    async queryUser({ state, commit }, payload) {
      state.loading = true
      console.log(state)
      StaffService.getInstance().queryStaff({
        end: state.end,
        first_branch: state.firstBranch,
        first_sub_branch: state.firstSubBranch,
        location: state.location,
        page: state.page,
        page_size: state.page_size,
        phone: state.phone,
        second_branch: state.secondBranch,
        second_sub_branch: state.secondSubBranch,
        start: state.start,
        user_name: state.userName,
        level: state.level,
        role: state.role + ''
      }).then(res => {
        if (res.code === 200) {
          commit('save', {
            list: res.data.item,
            total: res.data.total
          })
        }
        state.loading = false
      })
    },
    async queryNext({ state, commit }, payload) {
      state.loading = true
      StaffService.getInstance().getNextLevel({
        uuid: state.uuid,
        role: state.role
      }).then(res => {
        if (res.code === 200) {
          commit('save', {
            list: res.data.item,
            total: res.data.total
          })
        }
        state.loading = false
      })
    }
  },
}
