import { findChildren, getTreeDataList } from "@/utils/index";
import Store from '@/store'

export const detectionChartType = (chartType, form) => {
  let op = false
  let cg = form.setting.type === chartType.value
  if (chartType.type === 1 && form.schema.y.field) {
    op = true
  }
  if (chartType.type === 3 && (form.schema.y.field || form.schema.x.type !== 6)) {
    op = true
  }
  if (chartType.type === 2 && !form.schema.y.field) {
    op = true
  }
  if (chartType.value === 18 && form.schema.x.field !== 28 && form.schema.x.expand !== 17) {
    op = true
  }
  if ((chartType.value === 21 || chartType.value === 23) && form.schema.x.expand !== 18) {
    op = true
  }
  if (chartType.value === 17) {
    op = false
  }
  if (chartType.value === 30) {
    op = false
  }
  if (chartType.value === 13) {
    op = false
  }
  return {
    op,
    cg
  }
}

export const getFormPermissions = (form) => {
  if ((form.schema.x.field && form.schema.x.type < 90) || (form.schema.y.field && form.schema.y.type < 90) || form.schema.x.type === 96 || form.schema.y.type === 96) {
    return 1
  } else {
    return form.permissions
  }
}

export const getLineDataArr = (form, selected, count) => {
  let arr = [], line = form.setting.config.line
  for (let x of selected.x) {
    if (form.schema.x.type === 6) {
      const xn = getTreeDataList(form.source.x, []).find(item => item.name === x)
      x = xn.id
    }
    let sum = 0
    for (let y of selected.y) {
      if (form.schema.y.type === 6) {
        const yn = getTreeDataList(form.source.y, []).find(item => item.name === y)
        y = yn.id
      }
      sum += count[x] !== undefined ? count[x][y] || 0 : 0
    }
    switch (line.operator) {
      case 0:
        arr.push((sum / selected.y.length).toFixed(2))
        break
      case 1:
        let s1 = count[x] !== undefined ? count[x][line.first] || 0 : 0
        let s2 = line.second === '0' ? sum : count[x] !== undefined ? count[x][line.second] || 0 : 0
        if (s2) {
          arr.push((s1 / s2).toFixed(2))
        } else {
          arr.push(0)
        }
        break
      case 2:
        arr.push(sum)
        break
    }
  }
  return arr
}

export const getTreeNameByIds = (treeList, ids) => {
  let tree = getTreeDataList(treeList, [])
  return tree.filter(item => ids.indexOf(item.id) !== -1).map(item => item.name)
}

export const getXAxisName = (patentField, fieldId, expandId) => {
  let field = patentField.find(item => item.id === fieldId)
  if (field) {
    if (field.expand) {
      let expand = field.expand.find(item => item.id === expandId)
      return `${field.name}（${expand.name}）`
    } else {
      return field.name
    }
  }
  return "名称"
}

export const getSourceName = (fieldId, expandId, num) => {
  if(fieldId.type == 'DateTime'){
    if(fieldId.field == 'PD' && expandId){
      return `公开${expandId}度`
    }
    if(fieldId.field == 'GD' && expandId){
      return `授权${expandId}度`
    }
    if(fieldId.field == 'AD' && expandId){
      return `申请${expandId}度`
    }
  }
  // if (fieldId === 25 && expandId === 12) {
  //   return '申请年度'
  // }
  // if (fieldId === 25 && expandId === 10) {
  //   return '申请季度'
  // }
  // if (fieldId === 25 && expandId === 9) {
  //   return '申请月度'
  // }
  // if (fieldId === 25 && expandId === 11) {
  //   return '申请半年度'
  // }
  // if (fieldId === 25 && expandId === 13) {
  //   return '每2年申请'
  // }
  // if (fieldId === 25 && expandId === 14) {
  //   return '每3年申请'
  // }
  // if (fieldId === 25 && expandId === 15) {
  //   return '每5年申请'
  // }
  // if (fieldId === 26 && expandId === 12) {
  //   return '公开年度'
  // }
  // if (fieldId === 26 && expandId === 10) {
  //   return '公开季度'
  // }
  // if (fieldId === 26 && expandId === 9) {
  //   return '公开月度'
  // }
  // if (fieldId === 26 && expandId === 11) {
  //   return '公开半年度'
  // }
  // if (fieldId === 26 && expandId === 13) {
  //   return '每2年公开'
  // }
  // if (fieldId === 26 && expandId === 14) {
  //   return '每3年公开'
  // }
  // if (fieldId === 26 && expandId === 15) {
  //   return '每5年公开'
  // }
  // if (fieldId === 27 && expandId === 12) {
  //   return '授权年度'
  // }
  // if (fieldId === 27 && expandId === 10) {
  //   return '授权季度'
  // }
  // if (fieldId === 27 && expandId === 9) {
  //   return '授权月度'
  // }
  // if (fieldId === 27 && expandId === 11) {
  //   return '授权半年度'
  // }
  // if (fieldId === 27 && expandId === 13) {
  //   return '每2年授权'
  // }
  // if (fieldId === 27 && expandId === 14) {
  //   return '每3年授权'
  // }
  // if (fieldId === 27 && expandId === 15) {
  //   return '每5年授权'
  // }
  const field = Store.getters.patentField.find(item => item.id === fieldId)
  if (!field) {
    return ''
  }
  const expand = field.expand ? field.expand.find(item => item.id === expandId) : null
  if (expand) {
    return `${field.name}-${expand.name}`
  } else {
    return `${field.name}`
  }
}