import store from '@/store'

// 拟票环节标识
const PROPOSED_VOTE_LINK = 'trunk_user_task_register'

/**
 * 控制组件是否展示
 * @param {String} mark 组件标识
 * @return {boolean}
 */
export const isDisplay = mark => {
  if (!mark) return true // 如果没传组件标识,直接 return true
  const TICKET_INFO = store.getters['useApprovalProcessInfo']
  const ALL_LINKS = store.getters['useApprovalProcessRules']
  const CURRENT_LINK = TICKET_INFO?.currentTaskDefKey
  let LINK_CODE = ''
  // 如果没有 CURRENT_LINK 为拟票环节
  if (ALL_LINKS && ALL_LINKS.length) {
    !!CURRENT_LINK && CURRENT_LINK !== '拟票' && CURRENT_LINK !== '创建'
      ? (LINK_CODE = CURRENT_LINK)
      : (LINK_CODE = PROPOSED_VOTE_LINK)
    const current_link_info = ALL_LINKS.find(link => link.linkCode === LINK_CODE)
    if (current_link_info?.components?.length) {
      const current_comp = current_link_info.components.some(comp => comp.name === mark)
      if (current_comp) return true
    }
  }
  return false
}

/**
 * 控制字段是否可编辑
 * @description switch: true/false 编辑/隐藏。如果 defaultStatus: true 则 fields 里配置的字段均为不可编辑的字段
 * @param {String} field 需要控制的字段
 * @return {boolean}
 */
export const isEdit = field => {
  if (!field) return true // 如果没传字段,直接 return true
  const TICKET_INFO = store.getters['useApprovalProcessInfo']
  const ALL_LINKS = store.getters['useApprovalProcessRules']
  const CURRENT_LINK = TICKET_INFO?.currentTaskDefKey
  let LINK_CODE = ''
  // 如果没有 CURRENT_LINK 为拟票环节
  if (ALL_LINKS && ALL_LINKS.length) {
    !!CURRENT_LINK && CURRENT_LINK !== '拟票' && CURRENT_LINK !== '创建'
      ? (LINK_CODE = CURRENT_LINK)
      : (LINK_CODE = PROPOSED_VOTE_LINK)
    const current_link_info = ALL_LINKS.find(link => link.linkCode === LINK_CODE)
    if (current_link_info?.defaultStatus) {
      if (current_link_info?.components?.length) {
        const current_field = current_link_info.components.find(comp => {
          return !!comp.fields.find(fieldItem => fieldItem.fieldName === field)
        })
        return !current_field
      }
    }
    if (current_link_info?.components?.length) {
      const current_field = current_link_info.components.find(comp => {
        return !!comp.fields.find(fieldItem => fieldItem.fieldName === field && fieldItem.switch)
      })
      if (current_field) {
        return true
      }
    }
  }
  return false
}

/**
 * 控制字段是否隐藏
 * @description switch: true/false 编辑/隐藏
 * @param {String} field 需要控制的字段
 * @return {boolean}
 */
export const isHide = field => {
  if (!field) return false // 如果没传字段,直接 return false,不隐藏
  const TICKET_INFO = store.getters['useApprovalProcessInfo']
  const ALL_LINKS = store.getters['useApprovalProcessRules']
  const CURRENT_LINK = TICKET_INFO?.currentTaskDefKey
  let LINK_CODE = ''
  if (ALL_LINKS && ALL_LINKS.length) {
    !!CURRENT_LINK && CURRENT_LINK !== '拟票' && CURRENT_LINK !== '创建'
      ? (LINK_CODE = CURRENT_LINK)
      : (LINK_CODE = PROPOSED_VOTE_LINK)
    const current_link_info = ALL_LINKS.find(link => link.linkCode === LINK_CODE)
    if (current_link_info?.components?.length) {
      const current_field = current_link_info.components.find(comp => {
        return !!comp.fields.find(fieldItem => fieldItem.fieldName === field && !fieldItem.switch)
      })
      if (current_field) {
        return true
      }
    }
  }
  return false
}

/**
 * 控制当前字段是否加深颜色
 * @description switch: true/false 编辑/隐藏
 * @param {String} field 需要控制的字段
 * @return {boolean}
 */
export const isDark = field => {
  if (!field) return true // 如果没传字段,直接 return true,加深颜色
  const TICKET_INFO = store.getters['useApprovalProcessInfo']
  const ALL_LINKS = store.getters['useApprovalProcessRules']
  const CURRENT_LINK = TICKET_INFO?.currentTaskDefKey

  let LINK_CODE = ''
  // 如果没有 CURRENT_LINK 为拟票环节
  if (ALL_LINKS && ALL_LINKS.length) {
    !!CURRENT_LINK && CURRENT_LINK !== '拟票' && CURRENT_LINK !== '创建'
      ? (LINK_CODE = CURRENT_LINK)
      : (LINK_CODE = PROPOSED_VOTE_LINK)
    const current_link_info = ALL_LINKS.find(link => link.linkCode === LINK_CODE)
    if (current_link_info?.defaultStatus) {
      if (current_link_info?.components?.length) {
        const current_field = current_link_info.components.find(comp => {
          return !!comp.fields.find(fieldItem => fieldItem.fieldName === field)
        })
        return !current_field
      }
    }
    if (current_link_info?.components?.length) {
      const current_field = current_link_info.components.find(comp => {
        return !!comp.fields.find(fieldItem => fieldItem.fieldName === field && fieldItem.switch)
      })
      if (current_field) {
        return true
      }
    }
  }
  return false
}

/**
 * 控制当前字段是否可执行
 * @description execute: true/false 可执行/不可执行
 * @param {String} field 需要控制的字段
 * @return {boolean}
 */
export const isExecute = field => {
  const TICKET_INFO = store.getters['useApprovalProcessInfo']
  const ALL_LINKS = store.getters['useApprovalProcessRules']
  const CURRENT_LINK = TICKET_INFO?.currentTaskDefKey

  let LINK_CODE = ''
  // 如果没有 CURRENT_LINK 为拟票环节
  if (ALL_LINKS && ALL_LINKS.length) {
    CURRENT_LINK ? (LINK_CODE = CURRENT_LINK) : (LINK_CODE = PROPOSED_VOTE_LINK)
    const current_link_info = ALL_LINKS.find(link => link.linkCode === LINK_CODE)
    if (current_link_info?.components?.length) {
      const current_field = current_link_info.components.find(comp => {
        return !!comp.executeAble.find(fieldItem => fieldItem.fieldName === field && fieldItem.switch)
      })
      if (current_field) return true
    }
  }
  return false
}

/**
 * 控制当前环节是否是在执行之后的环节，如果是就展示已执行
 * @param {String} field 需要控制的字段
 * @return {boolean} true/false - 之后/之前
 */
export const isExecuteAfter = field => {
  const TICKET_INFO = store.getters['useApprovalProcessInfo']
  const ALL_LINKS = store.getters['useApprovalProcessRules']
  const CURRENT_LINK = TICKET_INFO?.currentTaskDefKey

  let LINK_CODE = ''
  let CURRENT_EXECUTEABLE_LINK = ''
  // 如果没有 CURRENT_LINK 为拟票环节
  // 如果当前票已完成, 直接返回true
  if (ALL_LINKS && ALL_LINKS.length) {
    CURRENT_LINK ? (LINK_CODE = CURRENT_LINK) : (LINK_CODE = PROPOSED_VOTE_LINK)
    ALL_LINKS.forEach(link => {
      if (link?.components?.length) {
        link.components.forEach(component => {
          if (component?.executeAble?.length) {
            component.executeAble.forEach(exec => {
              if (exec.fieldName === field && exec.switch) {
                CURRENT_EXECUTEABLE_LINK = link.linkCode
              }
            })
          }
        })
      }
    })

    if (CURRENT_EXECUTEABLE_LINK) {
      const current_executeable = intercept(CURRENT_EXECUTEABLE_LINK)
      const current_link = intercept(LINK_CODE)
      if (current_link > current_executeable) {
        return true
      }
    }
  }
  return false
}

/**
 * 截取环节标识最后一位或者两位数字
 * @param {String} str 环节标识
 * @return {*}
 */
function intercept(str) {
  const index = str.lastIndexOf('_')
  const res = str.substring(index + 1, str.length)
  return Number(res)
}
