import { TypeI5Tree } from '@/pages/Isystem/I5organization/data'
import store from '@/store'

// 树结构 通过最后一级id得到前面的id或者名称

export function treeLastIdFindFatherFu(
  tree: TypeI5Tree[],
  targetId: string,
  returnType: 'id' | 'name'
): string[] {
  let pathNodes: TypeI5Tree[] = []

  // 递归遍历函数
  const traverse = (nodes: TypeI5Tree[], currentPath: TypeI5Tree[]): boolean => {
    for (const node of nodes) {
      const newPath = [...currentPath, node]

      if (node.id === targetId) {
        pathNodes = newPath
        return true
      }

      if (node.children?.length) {
        const found = traverse(node.children, newPath)
        if (found) return true
      }
    }
    return false
  }

  traverse(tree, [])

  // 根据返回类型转换结果
  return returnType === 'id' ? pathNodes.map(n => n.id) : pathNodes.map(n => n.name)
}

// -------------------处理所属部门数据-------------------
let buMenTree: TypeI5Tree[] = []
export const buMenRes = (list: any[]) => {
  buMenTree = store.getState().I5organization.treeData
  let arr = list.map((v: any) => ({
    ...v,
    deptNameRes: v.deptId
      ? treeLastIdFindFatherFu(buMenTree, v.deptId + '', 'name').join(' / ')
      : '-'
  }))
  return arr
}

// 筛选下拉框数据
export const selectObj = {
  订单审批状态: [
    { value: 1, label: '草稿' },
    { value: 2, label: '待审批' },
    { value: 3, label: '审批不通过' },
    { value: 4, label: '已完成' }
  ],
  藏品入库申请状态: [
    { value: 1, label: '草稿' },
    { value: 2, label: '待审批' },
    { value: 3, label: '申请不通过' },
    { value: 4, label: '已完成' },
    { value: 5, label: '待盘点' },
    { value: 6, label: '待提交' }
  ],
  藏品入藏状态: [
    // status
    { value: 1, label: '入馆中' },
    { value: 2, label: '待入馆' },
    { value: 3, label: '已入馆' },
    { value: 4, label: '入藏中' },
    { value: 5, label: '登记中' },
    { value: 6, label: '已入藏' },
    { value: 7, label: '注销中' },
    { value: 8, label: '已删除' },
    { value: 9, label: '待登记' }
  ],
  藏品库存状态: [
    // siteStatus
    { value: 1, label: '待入库' },
    { value: 2, label: '入库中' },
    { value: 3, label: '已入库' },
    { value: 4, label: '出库中' },
    { value: 5, label: '已出库' },
    { value: 6, label: '移库中' },
    { value: 7, label: '盘点中' }
  ],
  藏品属性状态: [
    // isEdit
    { value: 0, label: '可编辑' },
    { value: 1, label: '修改中' }
  ],
  启用状态: [
    { value: 1, label: '启用' },
    { value: 0, label: '禁用' }
  ],
  藏品级别: [
    { value: '一级', label: '一级' },
    { value: '二级', label: '二级' },
    { value: '三级', label: '三级' },
    { value: '一般', label: '一般' },
    { value: '未定级', label: '未定级' }
  ],
  完残程度: [
    { value: '完整', label: '完整' },
    { value: '基本完整', label: '基本完整' },
    { value: '残缺', label: '残缺' },
    { value: '严重残缺(含缺失部件)', label: '严重残缺(含缺失部件)' }
  ],
  保存状态: [
    { value: '状态稳定，不需修复', label: '状态稳定，不需修复' },
    { value: '部分损腐，需要修复', label: '部分损腐，需要修复' },
    { value: '腐蚀损毁严重，急需修复', label: '腐蚀损毁严重，急需修复' },
    { value: '严重残缺(已修复)', label: '严重残缺(已修复)' }
  ],
  质量范围: [
    { value: '<0.01 kg', label: '<0.01 kg' },
    { value: '0.01-1 kg', label: '0.01-1 kg' },
    { value: '1-50 kg', label: '1-50 kg' },
    { value: '50-100 kg', label: '50-100 kg' },
    { value: '100-1000 kg', label: '100-1000 kg' },
    { value: '>1000 kg', label: '>1000 kg' }
  ],
  质量单位: [
    { value: 'g', label: 'g' },
    { value: 'kg', label: 'kg' }
  ],
  入藏时间范围: [
    { value: '1949.10.1前', label: '1949.10.1前' },
    { value: '1949.10.1-1965', label: '1949.10.1-1965' },
    { value: '1966-1976', label: '1966-1976' },
    { value: '1977-2000', label: '1977-2000' },
    { value: '2001至今', label: '2001至今' }
  ],
  文物来源: [
    { value: '征集购买', label: '征集购买' },
    { value: '接受捐赠', label: '接受捐赠' },
    { value: '依法交换', label: '依法交换' },
    { value: '拨交', label: '拨交' },
    { value: '移交', label: '移交' },
    { value: '旧藏', label: '旧藏' },
    { value: '发掘', label: '发掘' },
    { value: '采集', label: '采集' },
    { value: '拣选', label: '拣选' },
    { value: '其他', label: '其他' }
  ],
  附件类型: [
    { value: 'img', label: '图像' },
    { value: 'model', label: '3D' },
    { value: 'audio', label: '音频' },
    { value: 'video', label: '视频' },
    { value: 'doc', label: '文档' },
    { value: 'other', label: '其他' }
  ],
  图片类型: [
    { value: '正视图', label: '正视图' },
    { value: '俯视图', label: '俯视图' },
    { value: '侧视图', label: '侧视图' },
    { value: '全景图', label: '全景图' },
    { value: '局部图', label: '局部图' },
    { value: '底部图', label: '底部图' }
  ],
  维护类型: [
    { value: 'SG', label: '事故登记' },
    { value: 'XZ', label: '现状登记' },
    { value: 'XF', label: '修复登记' }
  ],
  校验结果: [
    { value: 1, label: '成功' },
    { value: 0, label: '失败' }
  ],
  展览状态: [
    { value: 1, label: '展览中' },
    { value: 2, label: '归还中' },
    { value: 3, label: '已归还' }
  ]
}

// 详情页申请类型-获取字典数据
export const getDictFu = (key: string) => {
  const dictAll = store.getState().I2dict.dictAll
  const list = dictAll.filter(v => v.name === key)
  return list ? list[0].children : []
}

// -------------------级联回显-------------------
export const resJiLianFu = (idTemp: string, type?: 'tag', isNull?: string) => {
  if (idTemp) {
    const dictAll = type ? store.getState().E1tag.treeData : store.getState().I2dict.dictAll

    const idArr = idTemp.split(',')
    const id = idArr[idArr.length - 1]
    let arr = treeLastIdFindFatherFu(dictAll, id, 'name')

    if (!type) {
      if (arr.length >= 2) arr = arr.slice(1)
    }

    if (arr && arr.length) return arr.join(' / ')
    else return isNull || '（空）'
  } else return isNull || '（空）'
}

// 藏品标签回显 、文创标签回显
export function resTagFu(idString: string, type: '藏品' | '文创'): any {
  const dataList =
    type === '藏品' ? store.getState().E1tag.treeData : store.getState().E3label.treeData

  // 1. 构建ID到名称的映射
  const idToNameMap = new Map<string, string>()

  const buildMap = (nodes: TypeI5Tree[]) => {
    nodes.forEach(node => {
      idToNameMap.set(node.id, node.name)
      if (node.children) buildMap(node.children)
    })
  }
  buildMap(dataList)

  // 2. 解析ID字符串并映射为名称
  return idString
    .split(';')
    .map(part => {
      const ids = part.split(',').map(id => id.trim())
      const targetId = ids[ids.length - 1] // 取路径最后一个ID
      return idToNameMap.get(targetId) || ''
    })
    .filter(name => name) // 过滤空值（可选，根据需要调整）
    .join('；') // 使用中文分号拼接
}

// size字段拼接
export const myTableTransferSize = (item: any) => {
  let danWei = 'cm'

  let txt1 = item.sizeL ? `通长${item.sizeL}` : ''
  let txt2 = item.sizeW ? `通宽${item.sizeW}` : ''
  let txt3 = item.sizeH ? `通高${item.sizeH}` : ''

  txt1 = txt1 ? txt1 + danWei : ''
  txt2 = txt2 ? txt2 + danWei : ''
  txt3 = txt3 ? txt3 + danWei : ''

  let arr = [txt1, txt2, txt3]
  arr = arr.filter(v => v)

  if (!txt1 && !txt2 && !txt3) return '（空）'
  else return arr.join(' - ')
}
