/* eslint-disable no-empty */
// import { branchQueryPage } from '@/api/specialSetting'
import { getSubject, getTeacher,findEducationUsers,getEmployeeOrgan } from '@/api/buildTeam'
import { getSchool,queryEmployByOrganId } from '@/api/systemManage'
import { vipGroupCategory } from "@/api/vipSeting"
import { findTechnician } from '@/api/repairManager'

const loadings = {}

/**
 *
 * 为避免重复请求全局参数，将需要使用的全局参数放到vuex中缓存
 *
 * 使用:
 *
 * 按照需要直接使用 this.$store.dispatch('action', force: Bool 是否强制刷新)
 *
 * 直接从this.$store.state.name 中获取数据
 */

export default {
  state: {
    branchs: [],
    subjects: [],
    teachers: [],
    schools: [],
    vipGroupCategory: [],
    educations:[],
    technician: [],
    employs:[],
  },
  mutations: {
    commit_branchs: (state, branchs) => {
      state.branchs = branchs
    },
    commit_subjects: (state, subjects) => {
      state.subjects = subjects
    },
    commit_teachers: (state, teachers) => {
      state.teachers = teachers.map(teacher=>{
        return {
          ...teacher,
          id:teacher.id,
          userId:teacher.id,
          userName:teacher.realName,
          realName:teacher.realName
        }
      })
    },
    commit_schools: (state, schools) => {
      state.schools = schools
    },
    commit_vip_group_category: (state, vipGroupCategory) => {
      state.vipGroupCategory = vipGroupCategory
    },
    commit_educations: (state, educations)=>{
      state.educations = educations
    },
    commit_technician: (state, technician) => {
      state.technician = technician
    },
    commit_employs:(state,employs)=>{
      state.employs = employs.map(emloys=>{
        return {
          ...emloys,
          id:emloys.id,
          userId:emloys.id,
          userName:emloys.realName,
          realName:emloys.realName
        }
      })
    }
  },
  actions: {
    async setBranchs ({ commit, state }, force) {
      if ((!state.branchs.length || force === true) && !loadings.commit_branchs) {
        loadings.commit_branchs = getEmployeeOrgan()
        try {
          const res = await loadings.commit_branchs
          commit('commit_branchs', res.data)
        } catch (error) { }
        loadings.commit_branchs = false
      }
    },
    async setSubjects ({ commit, state }, force) {
      if ((!state.subjects.length || force === true) && !loadings.commit_subjects) {
        loadings.commit_subjects = getSubject({rows: 9999, tenantId: '1'})
        try {
          const res = await loadings.commit_subjects
          commit('commit_subjects', res.data)
        } catch (error) { }
        loadings.commit_subjects = false
      }
    },
    async setTeachers ({ commit, state }, force) {
      if ((!state.teachers.length || force === true) && !loadings.commit_teachers) {
        loadings.commit_teachers = getTeacher()
        try {
          const res = await loadings.commit_teachers
          commit('commit_teachers', res.data)
        } catch (error) { }
        loadings.commit_teachers = false
      }
    },
    async setSchools ({ commit, state }, force) {
      if ((!state.schools.length || force === true) && !loadings.commit_schools) {
        loadings.commit_schools = getSchool()
        try {
          const res = await loadings.commit_schools
          commit('commit_schools', res.data)
        } catch (error) { }
        loadings.commit_schools = false
      }
      return loadings.commit_schools
    },
    async setVipGroupCategory ({ commit, state }, force) {
      if ((!state.vipGroupCategory.length || force === true) && !loadings.commit_vip_group_category) {
        loadings.commit_vip_group_category = vipGroupCategory()
        try {
          const res = await loadings.commit_vip_group_category
          commit('commit_vip_group_category', res.data)
        } catch (error) { }
        loadings.commit_vip_group_category = false
      }
    },
    async setEducations({commit,state},force){
      if ((!state.educations.length || force === true) && !loadings.commit_educations) {
        loadings.commit_educations = findEducationUsers()
        try {
          const res = await loadings.commit_educations
          commit('commit_educations', res.data)
        } catch (error) { }
        loadings.commit_educations = false
      }
    },
    async setTechnician({commit,state},force){
      if ((!state.technician.length || force === true) && !loadings.commit_technician) {
        loadings.commit_technician = findTechnician()
        try {
          const res = await loadings.commit_technician
          commit('commit_technician', res.data)
        } catch (error) {}
        loadings.commit_technician = false
      }

    },
    async setEmploys({commit,state},force){
      if ((!state.employs.length || force === true) && !loadings.commit_employs) {
        loadings.commit_employs = queryEmployByOrganId({rows:99999})
        try {
          const res = await loadings.commit_employs
          commit('commit_employs', res.data.rows)
        } catch (error) {}
        loadings.commit_employs = false
      }

    }
  }
}
