import apiTools from '@/api'
import i18n from '@/locale'
export default {
  namespaced: true,
  state: {
    showMessageNum: 0, //1.回收成功, 2.更新成功, 3.要設定的空間>目前學校有的 4.有不存在老師的ID
    isLoading: false,
    originalSpace: 0,
    schoolUsedSpace: 0, //學校其他已經使用的空間
    teacherUsedSpaceNum: 0, //目前老師已使用空間,
    maxFixedAssignedValue: 0, //資料進來，可分的最大值

    teacherSpaceData: [], //存放目前所有老師的資料狀態，讓前臺能及時給予更新回饋
    currentSelectedTeacherId: [], //目前所選存放的老師的id，勾選給空間用

    pieNumData: [
      { value: 0, name: i18n.t("serviceDriveAuth['學校已使用空間']") },
      { value: 0, name: i18n.t("serviceDriveAuth['已分配給教師空間']") },
      { value: 0, name: i18n.t("serviceDriveAuth['剩餘空間']") }
    ],
    isKeyInSpace: false //存放是否有進行手動輸入
  },
  getters: {
    getIsKeyInSpace(state) {
      return state.isKeyInSpace
    },
    getMsgNum(state) {
      return state.showMessageNum
    },
    getIsLoading(state) {
      return state.isLoading
    },
    getPieNumData(state) {
      return state.pieNumData
    },
    getOriginalSpace(state) {
      return state.originalSpace
    },
    getMaxFixedAssignedValue(state) {
      return state.maxFixedAssignedValue
    },
    getTeacherSpaceData(state) {
      return state.teacherSpaceData
    },
    getCurrentSelectedTeacherId(state) {
      return state.currentSelectedTeacherId
    }
  },
  mutations: {
    setIsKeyInSpace(state, status) {
      /*if(status==true)console.log('有進行手動輸入')
      else console.log('沒有進行手動輸入')*/
      state.isKeyInSpace = status
    },
    setCurrentSelectedTeacherId(state, data) {
      state.currentSelectedTeacherId = data
      //console.log(data)
    },
    setMsgNum(state, no) {
      state.showMessageNum = no
    },
    //回收時直接設為0
    setIsLoading(state, status) {
      state.isLoading = status
    },
    setTeacherUsedSpaceZero(state) {
      state.teacherUsedSpaceNum = 0
    },
    //存放DB初值，使用者未保存的時候使用

    setTeacherUsedSpaceNum(state, data) {
      let teacherUsedSpaceNum = 0
      data.forEach(element => {
        if (element.size != '-') teacherUsedSpaceNum += parseInt(element.size)
        else {
          teacherUsedSpaceNum += 0
        }
      })
      state.teacherUsedSpaceNum = teacherUsedSpaceNum
    },

    //處理介面上的即時數據

    resetTeacherSpaceUIData(state) {
      state.teacherSpaceData.forEach(element => {
        element.size = element.initSize
      })
    },

    setTeacherSpaceUIData(state, data) {
      state.teacherSpaceData = data
    },
    setTeacherSpaceUIDataZero(state) {
      state.teacherSpaceData.forEach(element => {
        element.size = 0
      })
    },
    updateTeacherSpaceUIData(state, data) {
      state.teacherSpaceData.forEach(element => {
        data.forEach(item => {
          if (element.id == item.id) {
            element.size = item.size
          }
        })
      })
    },

    setPieNumData(state, data) {
      state.originalSpace = data.avaliable
      state.schoolUsedSpace = data.usedSpace
      state.pieNumData = [
        { value: state.schoolUsedSpace, name: i18n.t("serviceDriveAuth['學校已使用空間']") },
        { value: state.teacherUsedSpaceNum, name: i18n.t("serviceDriveAuth['已分配給教師空間']") },
        { value: state.originalSpace - state.schoolUsedSpace - state.teacherUsedSpaceNum, name: i18n.t("serviceDriveAuth['剩餘空間']") }
      ]
      state.maxFixedAssignedValue = state.originalSpace - state.schoolUsedSpace
    },
    updatePieNumData(state) {
      state.pieNumData = [
        { value: state.schoolUsedSpace, name: i18n.t("serviceDriveAuth['學校已使用空間']") },
        { value: state.teacherUsedSpaceNum, name: i18n.t("serviceDriveAuth['已分配給教師空間']") },
        { value: state.originalSpace - state.schoolUsedSpace - state.teacherUsedSpaceNum, name: i18n.t("serviceDriveAuth['剩餘空間']") }
      ]
    }
    //處理介面上的即時數據
  },
  actions: {
    //初始化空間
    getInitialSpace(context, params) {
      let schoolCode = params.schoolCode
      //console.log(params)
      apiTools.spaceAuth.teacherSpace(schoolCode, 'baseSpace').then(
        res => {
          //console.log(res)
          context.commit('setTeacherUsedSpaceNum', params.originalTeachers)
          context.commit('setPieNumData', res)
          context.commit('setIsLoading', false)
        },
        err => {
          console.log(err)
        }
      )
    },
    //回收空間
    resetAllSpace(context, schoolCode) {
      apiTools.spaceAuth.teacherSpace(schoolCode, 'retract').then(
        res => {
          //console.log(res)
          context.commit('setTeacherUsedSpaceZero')
          context.commit('setTeacherSpaceUIDataZero')
          context.commit('updatePieNumData')
          context.commit('setIsLoading', false)
          context.commit('setMsgNum', 1)
        },
        err => {
          console.log(err)
        }
      )
    },
    //更新空間分配
    setTeacherSpace(context, params) {
      let schoolCode = params.schoolCode
      let teachers = params.teachers
      apiTools.spaceAuth.teacherSpace(schoolCode, 'upd', teachers).then(
        res => {
          //console.log(res)
          context.commit('updateTeacherSpaceUIData', teachers)
          context.commit('setTeacherUsedSpaceNum', context.state.teacherSpaceData)
          context.commit('updatePieNumData')
          context.commit('setIsLoading', false)
          context.commit('setMsgNum', 2)
          if (res.status == '1') {
            context.commit('setMsgNum', 3)
          }
          if (res.status == '2') {
            context.commit('setMsgNum', 4)
          }
        },
        err => {
          console.log(err)
        }
      )
    }
  }
}
