import { globalApi } from '@/api'
import { calcIndexPathByCurrentRouter } from '@/utils'

// 获取菜单数据
const GET_MEUMS = 'GET_MEUMS'

// 显示设置快捷菜单
const SHOW_EDIT_MENUS = 'SHOW_EDIT_MENUS'

// 隐藏设置快捷菜单
const HIDE_EDIT_MENUS = 'HIDE_EDIT_MENUS'

// 边侧菜单
const SIDEBAR_UNFOLD = 'SIDEBAR_UNFOLD'

// 基本信息
const GET_PROJECT_INFO = 'GET_PROJECT_INFO'

//
const CALC_TOUTER_INDEX = 'CALC_TOUTER_INDEX'

const HEADER_HISTORY = 'HEADER_HISTORY'

const MENU_HISTORY_JSON = 'MENU_HISTORY_JSON'

const MEDIA_LIST = 'MEDIA_LIST'

const CHOOSE_ITEM = 'CHOOSE_ITEM'

const state = {
  menus: [],
  editMenus: false,
  projectInfo: null,
  currentPath: [],
  sidebarUnfold: true, // false:边侧菜单收缩, true:边侧菜单展开
  headerHistory: [],
  menuHistoryJson: {
    name: '',
    value: []
  },
  mediaList: '',
  chooseItem: ''
}

const actions = {
  async getMenus({
    commit, dispatch
  }, parmas) {
    let res = await globalApi.getMenus(parmas)
    let menus = res.data.data
    dispatch('calcIndexPathByrouter', menus)
    commit(GET_MEUMS, menus)
  },
  showEditMenus({ commit }) {
    commit(SHOW_EDIT_MENUS, true)
  },
  hideEditMenus({ commit }) {
    commit(HIDE_EDIT_MENUS, false)
  },
  setSidebarUnfold({ commit }, type) {
    commit(SIDEBAR_UNFOLD, type)
  },

  calcIndexPathByrouter ({commit}, menus) {
    const currentRoute = location.href
    const indexPath = calcIndexPathByCurrentRouter(menus, currentRoute)
    console.log('this is indexPath %O', indexPath)
    commit(CALC_TOUTER_INDEX, indexPath)
    // calcIndexPathByCurrentRouter(menus)
  },

  async getProjectInfo({ commit, dispatch }) {
    let res = await globalApi.getProjectInfo()
    let projectInfo = res.data
    commit(GET_PROJECT_INFO, projectInfo)
  }
}

const getters = {
  getMenus(state) {
    return state.menus
  },
  getEditMenus(state) {
    return state.editMenus
  },
  getSidebarUnfoldInfo(state) {
    return state.sidebarUnfold
  },
  getProjectInfo(state) {
    return state.projectInfo
  },
  getCurrentMenus(state) {
    return state.currentMenus
  },
  getCurrentPath (state) {
    return state.curredsntPath
  }
}

const mutations = {
  [CHOOSE_ITEM](state, chooseItem) {
    state.chooseItem = chooseItem
  },
  [MEDIA_LIST](state, mediaList) {
    state.mediaList = mediaList
  },
  [MENU_HISTORY_JSON](state, history) {
    const {name, value} = history
    // 判断是否是数组
    let list = Array.isArray(value) ? value : JSON.parse(value)
    // 取前15条
    if (list.length > 15) {
      list = list.slice(0, 15)
    }
    state.menuHistoryJson = {
      name,
      value: list
    }
    // 存入localStorage
    localStorage.setItem(name, list)
  },
  [HEADER_HISTORY](state, history) {
    state.headerHistory = [...history]
    try {
      if (localStorage.history) {
        localStorage.history = history
      }
    } catch (error) {

    }
  },
  [GET_MEUMS](state, menus) {
    state.menus = [...menus]
  },

  [SHOW_EDIT_MENUS](state, status) {
    state.editMenus = status
  },
  [HIDE_EDIT_MENUS](state, status) {
    state.editMenus = status
  },
  [SIDEBAR_UNFOLD](state, type) {
    state.sidebarUnfold = type
  },
  [GET_PROJECT_INFO](state, projectInfo) {
    state.projectInfo = { ...projectInfo }
  },
  [CALC_TOUTER_INDEX](state, indexPath) {
    console.log('this is CALC_TOUTER_INDEX %o', indexPath)
    state.currentPath = [...indexPath]
  }
}

export default {
  state,
  mutations,
  actions,
  getters
}
