/* eslint-disable no-empty */
import Cookies from 'js-cookie'
import { hasIndexErrData } from '@/views/main/api'
import { permission } from '@/utils/directivePage'

const state = {
  sidebar: {
    opened: Cookies.get('sidebarStatus') ? !!+Cookies.get('sidebarStatus') : true,
    withoutAnimation: false
  },
  device: 'desktop',
  status: {
    indexErrData: false,
    remindMatterData: false,
    musicPatrol: false,
    studentInfo: false,
    teacherInfo: false,
    attendanceServe:false
  },
}

const mutations = {
  TOGGLE_SIDEBAR: state => {
    // state.sidebar.opened = !state.sidebar.opened
    state.sidebar.opened = true;
    state.sidebar.withoutAnimation = false
    if (state.sidebar.opened) {
      Cookies.set('sidebarStatus', 1)
    } else {
      Cookies.set('sidebarStatus', 0)
    }
  },
  CLOSE_SIDEBAR: (state, withoutAnimation) => {
    Cookies.set('sidebarStatus', 0)
    state.sidebar.opened = false
    state.sidebar.withoutAnimation = withoutAnimation
  },
  TOGGLE_DEVICE: (state, device) => {
    state.device = device
  },
  COMMIT_DOT_STATUS: (state, status) => {
    const { musicPatrol, studentInfo, teacherInfo,attendanceServe } = status
    state.status = {
      ...status,
      indexErrData: musicPatrol || studentInfo || teacherInfo || attendanceServe
    }
    toggleDot(state.status)
  }
}

const toggleDot = status => {
  const { indexErrData, remindMatterData } = status
  const dotStatus =  indexErrData || remindMatterData
  const el = document.querySelector('.menu-wrapper a[href="#/main/main"] span')
  if (el) {
    if (dotStatus) {
      el.classList.add('main-dot')
    } else {
      el.classList.remove('main-dot')
    }
  }
}

const actions = {
  toggleSideBar ({ commit }) {
    commit('TOGGLE_SIDEBAR')
  },
  closeSideBar ({ commit }, { withoutAnimation }) {
    commit('CLOSE_SIDEBAR', withoutAnimation)
  },
  toggleDevice ({ commit }, device) {
    commit('TOGGLE_DEVICE', device)
  },
  async setDotStatus({ commit }) {
    if (permission('hasIndexErrData')) {
      try {
        const res = await hasIndexErrData()
        commit('COMMIT_DOT_STATUS', res.data)
      } catch (error) {}
    }
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
