import store from '@/store'
const PROPOSED_VOTE_LINK = 'trunk_user_task_register' // 设定拟票环节标识
/**
 * 前提是将此函数的this指向当前调用的页面
 * params ruleName 当前页的规则名
 * @returns rules 需要验证的新规则
 */
export function setFormRules(ruleName) {
  const TICKET_INFO = store.getters['useTicketInfo'] // 当前票的详细信息,由对票据进行操作的时候传入 vuex
  const ALL_LINKS = store.getters['useTicketAllLinks'] // 当前票的所有环节,由对票据进行操作的时候传入 vuex
  const CURRENT_LINK = TICKET_INFO?.actPojo?.taskDefKey // 当前票的当前环节,由对票据进行操作的时候传入 vuex
  let LINK_CODE = ''
  let NEW_RULES = {}

  if (ALL_LINKS && ALL_LINKS.length) {
    !!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) {
      if (current_link_info?.defaultStatus) {
        if (current_link_info?.fields?.length) {
          current_link_info.fields.forEach(field => {
            if (this?.[ruleName][field.fieldName]) {
              this.$delete(this[ruleName], field.fieldName)
            }
          })
        }
        NEW_RULES = this[ruleName]
      } else {
        if (current_link_info?.fields?.length) {
          current_link_info.fields.forEach(field => {
            if (field?.switch) {
              if (this?.[ruleName][field.fieldName]) {
                NEW_RULES[field.fieldName] = this[ruleName][field.fieldName]
              }
            }
          })
        }
      }
    }
  }
  this[ruleName] = NEW_RULES

  // if (this.$refs[formRef]) {
  //   this.$refs[formRef].setRules(this[ruleName])
  // }
}
