import { FourTableType } from '@/pages/B_enterTibet/B1collect/type'
import { getTokenInfo } from '@/utils/storage'

type C22baseFormDataType = {
  pageSize: number
  pageNum: number
  num: string
  name: string
  type: string
  deptName: string
  userName: string
  startTime: string
  endTime: string
  status: string
  userType: string
}

export type C22InputKeyType = keyof C22baseFormDataType

export const C22baseFormData: C22baseFormDataType = {
  pageSize: 10,
  pageNum: 1,
  num: '',
  name: '',
  type: '',
  deptName: '',
  userName: '',
  startTime: '',
  endTime: '',
  status: '',
  userType: ''
}

// 藏品编辑模块(藏品登记模块/藏品删除模块)特有的按钮过滤
type C22tableBtnTxtType = '编辑' | '查看' | '撤回' | '审批'

export const C22tableBtnFu = (item: FourTableType) => {
  //申请单状态     申请人id      当前节点审批人     历史审批人       抄送人
  const { status, creatorId, currentAuditUserIds, auditUserIds, copyUserIds } = item
  let flagObj: { [K in C22tableBtnTxtType]: boolean } = {
    编辑: false,
    查看: false,
    撤回: false,
    审批: false
  }
  const myId = (getTokenInfo().user || { id: 0 }).id

  //  当前节点审批人
  const arr3 = (currentAuditUserIds || '').split(',').map(v => Number(v))
  // 历史审批人
  const arr4 = (auditUserIds || '').split(',').map(v => Number(v))

  // 抄送人
  const arr5 = (copyUserIds || '').split(',').map(v => Number(v))

  const obj = {
    1: () => {
      // 草稿
      // 申请人是我自己（提交人）
      if (myId === creatorId) flagObj['查看'] = flagObj['编辑'] = true
    },
    6: () => {
      //待提交
      // 申请人是我自己 =》查看   编辑
      if (myId === creatorId) flagObj['查看'] = flagObj['编辑'] = true
    },
    2: () => {
      // 待审批
      // 申请人是我自己（提交人）
      if (myId === creatorId) flagObj['查看'] = flagObj['撤回'] = true
      // 历史审批人有我 =》查看
      if (arr4.includes(myId)) flagObj['查看'] = true
      // 当前审批人有我 =》审批
      if (arr3.includes(myId)) flagObj['审批'] = true
      // 抄送人有我 =》查看
      if (arr5.includes(myId)) flagObj['查看'] = true
    },
    3: () => {
      //审批不通过
      // 申请人是我自己（提交人）
      if (myId === creatorId) flagObj['编辑'] = flagObj['查看'] = true
      // 历史审批人有我 =》查看
      if (arr4.includes(myId)) flagObj['查看'] = true
      // 抄送人有我 =》查看
      if (arr5.includes(myId)) flagObj['查看'] = true
    },
    4: () => {
      //已完成
      // 申请人是我自己 =》查看
      if (myId === creatorId) flagObj['查看'] = true
      // 历史审批人有我 =》查看
      if (arr4.includes(myId)) flagObj['查看'] = true
      // 抄送人有我 =》查看
      if (arr5.includes(myId)) flagObj['查看'] = true
    }
  }

  if (Reflect.get(obj, status)) Reflect.get(obj, status)()

  return flagObj
}

// 详情页 特有的按钮
type C22tableBtnTxtType2 = '编辑' | '提交' | '删除' | '撤回' | '审批' | '重新提交'

export const C22infoBtnFu = (item: FourTableType) => {
  //申请单状态     申请人id      当前节点审批人
  const { status, creatorId, currentAuditUserIds } = item

  let flagObj: { [K in C22tableBtnTxtType2]: boolean } = {
    编辑: false,
    提交: false,
    删除: false,
    撤回: false,
    审批: false,
    重新提交: false
  }
  const myId = (getTokenInfo().user || { id: 0 }).id

  //  当前节点审批人
  const arr3 = (currentAuditUserIds || '').split(',').map(v => Number(v))
  const obj = {
    1: () => {
      // 草稿
      // 我是申请人（提交人）
      if (creatorId === myId) flagObj['编辑'] = flagObj['提交'] = flagObj['删除'] = true
    },
    6: () => {
      //待提交
      // 我是申请人
      if (creatorId === myId) flagObj['提交'] = flagObj['编辑'] = flagObj['删除'] = true
    },
    2: () => {
      //待审批
      // 我是申请人
      if (creatorId === myId) flagObj['撤回'] = true
      // 当前审批人有我 =》审批
      if (arr3.includes(myId)) flagObj['审批'] = true
    },
    3: () => {
      //审批不通过
      // 我是申请人
      if (creatorId === myId) flagObj['编辑'] = flagObj['重新提交'] = true
    }
  }
  if (Reflect.get(obj, status)) Reflect.get(obj, status)()

  return flagObj
}
