import router from '@/router'
const state = {
  tags: [
    // 默认打开路由标签
    {
      path: '/index/index',
      fullPath: '/index/index',
      name: 'NewCostList',
      meta: {
        title: 'TCO成本管理'
      }
    }
  ], // 标签
  keepAliveIncludes: ['NewCostList']
}

const arr = ['add', 'edit', 'scfx', 'ggdy', 'zscqxq', 'rzxq', 'bzyx', 'bzljxz', 'gysj', 'cpplfb', 'jgsj', 'dzsj', 'zscqsq', 'wlbj', 'km', 'cpzl', 'spddxq', 'spxdxqMx', 'dj', 'bzsj', 'bl', 'skuinfo', 'sczl', 'xxlr', 'rz', 'zlsh', 'spxd', 'structureBom', 'packingBom', 'prlog']
let str = ''

const mutations = {
  // 新增标签
  ADD_NAV_TAG: (state, tag) => {
    if (tag.name == 'AddSpu') {
      tag.currType = 'cre'
    }
    if (tag.name == 'EditSku') {
      tag.currType = 'editskucre'
    }
    if (tag.name == 'EditSpu') {
      tag.currType = 'editspucre'
    }
    if (tag.name == 'ProductList') {
      tag.currType = 'productlistcre'
    }
    state.tags.push(tag)
  },

  // 替换标签
  REPLACE_NAV_TAG: (state, tag) => {
    if (tag.name == 'AddSpu') {
      tag.currType = 'rep'
    }
    if (tag.name == 'EditSku') {
      tag.currType = 'editskurep'
    }
    if (tag.name == 'EditSpu') {
      tag.currType = 'editspurep'
    }
    if (tag.name == 'ProductList') {
      tag.currType = 'productlistrep'
    }
    if (arr.includes(str)) {
      const index = state.tags.findIndex(it => it.path === tag.path)
      state.tags.splice(index, 1, tag)
    } else {
      const index = state.tags.findIndex(it => it.fullPath === tag.fullPath)
      state.tags.splice(index, 1, tag)
    }
  },

  // 删除标签
  RM_NAV_TAG: (state, index) => {
    state.tags = state.tags.filter((it, i) => {
      return i !== index || it.name === 'NewCostList'
    })
  },

  // 删除所有标签
  RM_NAV_ALLTAG: (state, index) => {
    state.tags = [{
      path: '/index/index',
      fullPath: '/index/index',
      name: 'NewCostList',
      meta: {
        title: 'TCO成本管理'
      }
    }]
    state.keepAliveIncludes = ['NewCostList']
  }
}

// 不需要加载数据的路由
// let unneedLoadDataRoutes = []

const actions = {
  // 新增标签
  addNavTag: (context, route) => {
    // 跳转至新打开页面
    if (route.name == '404') {
      return false
    }
    // router.push({ path: tag.fullPath })
    // if (!context.state.tags.some(v => v.name === tag.name)) {
    const tag = {
      path: route.path,
      fullPath: route.fullPath,
      name: route.name,
      meta: route.meta,
      query: route.query
    }
    let index = route.fullPath.lastIndexOf("/")
    str = route.fullPath.substring(index + 1, route.fullPath.length);
    if (str.indexOf('?') != -1) {
      let index = str.lastIndexOf("?")
      str = str.substring(0, index);
    }
    if (!context.state.tags.some(v => v.fullPath === route.fullPath)) {
      // 标签栏没有，新增页面标签
      if (arr.includes(str)) {
        if (!context.state.tags.some(v => v.path == route.path)) {
          context.commit('ADD_NAV_TAG', tag)
        } else {
          context.commit('REPLACE_NAV_TAG', tag)
        }
      } else {
        context.commit('ADD_NAV_TAG', tag)
      }
    } else {
      // 标签栏有，替换标签
      context.commit('REPLACE_NAV_TAG', tag)
    }

    // 为组件增加执行钩子，所有数据请求在该钩子函数下调用 （推荐方案）
    // 解决keepalive多页签情况下，同组件不同参数中数据请求回调
    // Object.defineProperty(route.matched[route.matched.length - 1].instances, 'default', {
    //   configurable: true,
    //   set(target) {
    //     if (target) {
    //       if (!unneedLoadDataRoutes.some(it => it == route.fullPath)) {
    //         typeof target.$options.routeMounted == 'function' && target.$options.routeMounted.call(target, route)
    //         unneedLoadDataRoutes.push(route.fullPath)
    //       }
    //     }
    //   }
    // })
  },

  // 删除标签
  removeNavTag: (context, index) => {
    let nextIndex = index - 1
    var overallVar = context
    if (context.state.tags.length - 1 > index) {
      nextIndex = index + 1
    }
    // 最后一个页面标签不允许删除
    if (nextIndex === -1) return false
    // router.push({ path: context.state.tags[nextIndex].path })
    router.push(context.state.tags[nextIndex])
    context.commit('RM_NAV_TAG', index)
  },
  // 删除所有标签
  removeAllNavTag: (context, index) => {
    context.commit('RM_NAV_ALLTAG')
  }
}

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