import webapi from './webapi'
// import fetch from '@ttk/utils/fetch'
import { commit } from '@ttk/app-loader'
import { history } from '@ttk/router'
import MD5 from 'md5.js'

export function clearUserInfo(reduce, gf, data) {
  return async (dispatch) => {
    dispatch(commit('app-root/loginInfo', { token: '', depId: '' }))
    dispatch(commit('app-root/functionNav', { type: 'clearItem' }))
    dispatch(commit('app-root/iframeRoutes', { type: 'clearItem' }))
  }
}

export function goToLogin(reduce, gf, data) {
  return async () => {
    history.push(`${history.location.root}/app-login`)
  }
}

export function functionNav(reduce, gf, data) {
  return async (dispatch, getState) => {
    let res = await webapi.querySecFunctionNav(data)
    if (res.errorCode === '100100000018' || res.errorCode === '00000214' || res.errorCode === '010121') {
      dispatch(clearUserInfo())
      dispatch(goToLogin(null, null, null))
      return
    }
    function loopMenu(routes, menupath = []) {
      return routes.filter((item) => {
        let tmpPath = [...menupath]
        item.functioinId = item.functioinId + "" // Menu组件的key值是string类型
        item.parentId = item.parentId + ""
        item.menupath = menupath // 菜单路径
        if (!item.childSecFunctioinDTOs || !item.childSecFunctioinDTOs.length) {
          return item.functionType == "menu"
        }
        tmpPath.push(item.functioinId)
        item.childSecFunctioinDTOs = loopMenu(item.childSecFunctioinDTOs, tmpPath)
        return item.functionType == "menu"
      })
    }
    res = loopMenu(res)
    dispatch(commit('app-root/functionNav', { type: 'concatItem', data: res }))
  }
}

export function openTabAction(reduce, gf, data) {
  return async (dispatch, getState) => {
    dispatch(commit('app-root/tags', {type: 'addTag', data}))
    dispatch(commit('app-root/tags', {type: 'setCurrentTag', data}))
  }
}

export function closeTabAction(reduce, gf, data) {
  return async (dispatch, getState) => {
    console.log('history', reduce, gf, data, history.location)
    data = transformIframeData(data)
    console.log("closeTabAction", data);
    dispatch(commit('app-root/tags', { type: 'removeTag', data }))
    let { pathname, search } = history.location
    let lastTag = { url: "/" } // 删除后需跳转的标签地址
    let stateRoot = getState("app-root").toJS()
    if (stateRoot.tags.list.length > 0) {
      lastTag = stateRoot.tags.list[stateRoot.tags.list.length - 1]
    }
    if (pathname.indexOf(data.url) >= 0) {
      // 关闭的是当前打开的tab
      history.replace(lastTag.url)
      // history.goBack()
    } else {
      // 关闭的tab不是当前打开的tab
    }
  }
}

export function addTabAction() {
  return async (dispatch, getState) => { }
}
export function removeTabAction() {
  return async (dispatch, getState) => { }
}
export function removeLeftTabsAction(data) {
  return async (dispatch, getState) => {
    dispatch(commit('app-root/tags', { type: 'removeLeftTags', data }))
  }
}
export function removeRightTabsAction(data) {
  return async (dispatch, getState) => {
    dispatch(commit('app-root/tags', { type: 'removeRightTags', data }))
  }
}
export function removeOtherTabsAction(data) {
  return async (dispatch, getState) => {
    dispatch(commit('app-root/tags', { type: 'removeOtherTags', data }))
  }
}
export function removeAllTabAction() {
  return async (dispatch, getState) => {
    dispatch(commit('app-root/tags', { type: 'removeAllTags' }))
  }
}

export function openIframeTabAction(data) {
  return async (dispatch, getState) => {
    data = transformIframeData(data)
    // if (data.iframeUrl) {
    dispatch(commit('app-root/iframeRoutes', { type: 'addItem', data }))
    // }
    history.push(data.url)
  }
}

function transformIframeData(data) {
  let { iframeUrl, name, url } = data
  if (!iframeUrl || url) {
    return data
  }
  name = name || '新标签'
  url = new MD5().update(`iframeUrl=${iframeUrl}&name=${name}`).digest('hex')
  data = {
    functioinId: url,
    name: name,
    url: url,
    iframeUrl: iframeUrl
  }
  return data
}