import { B1listType } from '@/pages/B1ledger/data'
import { createHashHistory } from 'history'
import { getTokenInfo } from './storage'
const history = createHashHistory()
export default history

// 权限失效，回登录页，带上回跳路径
export const loginOutFu = () => {
  const urlAll = window.location.href
  const urlArr = urlAll.split('/#/')

  if (!urlAll.includes('/login')) {
    if (urlArr[1]) {
      history.push(`/login?back=${urlArr[1]}`)
    } else history.push('/login')
  }
}

// 新窗口打开藏品详情页面
export const openLink = (path: string) => {
  const urlAll = window.location.href
  const qian = urlAll.split('/#/')[0]
  window.open(`${qian}/#${path}`, '_blank')
}

// 监听路由
let routerLength = 0
history.listen((_: any, listener: any) => {
  if (listener === 'PUSH') {
    routerLength += 1
  } else if (listener === 'POP') {
    if (routerLength >= 1) routerLength -= 1
  }
})

// 点击返回
export const backPageFu = (path: string) => {
  if (routerLength) history.go(-1)
  else history.push(path)
}

// 列表按钮的权限-审批
// 订单状态为待审批，且当前用户为当前节点审批人时
export const aduitBtnRoleFu = (item: B1listType) => {
  const userInfo = getTokenInfo().user

  let flag = false

  if (item.status === 1 && userInfo) {
    if (item.currentAuditUserIds) {
      const idsArr = item.currentAuditUserIds.split(',').map(v => Number(v))

      if (idsArr.includes(userInfo.id)) {
        flag = true
      }
    }
  }

  return flag
}

// 列表按钮的权限-重新提交
// 申请状态为未通过，且当前用户为订单创建人时
export const resubmitBtnRoleFu = (item: B1listType) => {
  const userInfo = getTokenInfo().user

  let flag = false

  if (item.status === 2 && userInfo) {
    if (userInfo.id === item.creatorId) flag = true
  }

  return flag
}
