
const { t } = useI18n()

const lengObj = {
  all: { exp: '^.', message: t('Avue.crud.rule.all') },
  number: { exp: '^\d', message: t('Avue.crud.rule.number') },
  alphabet: { exp: '^[a-zA-Z]', message: t('Avue.crud.rule.alphabet') },
  chinese: { exp: '^[\u4e00-\u9fa5]', message: t('Avue.crud.rule.chinese') }
}
export const patternObj = {
  phone: /^1[3-9]\d{9}$/,//手机号码
  email: /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/,//电子邮箱
  idCard: /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/,//身份证号码
  postalCode: /^\d{6}$/,//邮政编码
  chineseStr: /^[\u4e00-\u9fa5]+$/,//中文
  url: /^(https?:\/\/)?(www\.)?([a-zA-Z0-9-]+\.)+[a-zA-Z]{2,}(\/\S*)?$/,//网址
  money: /^-?\d+(\.\d{2})?$/,//金额
  getLeng: (min, max, type) => {
    if (!min && min !== 0) min = false
    if (!max && max !== 0) max = false
    let exp = lengObj[type].exp
    let message = lengObj[type].message
    if (min && max) {
      exp = `${exp}{${min},${max}}$`
      message = `${t('Avue.tip.input')}${min} ~ ${max}${t('Avue.control.selectAppend')}${message}`
    }
    else if (min) {
      exp = `${exp}{${min},}$`
      message = `${t('Avue.crud.rule.minTip')}${min}${t('Avue.control.selectAppend')}${message}`
    }
    else if (max) {
      exp = `${exp}{0,${max}}$`
      message = `${t('Avue.crud.rule.maxTip')}${max}${t('Avue.control.selectAppend')}${message}`
    }
    return { message, pattern: new RegExp(exp) }
  }
}
export const rulesVerify = [
  { label: '唯一校验', tip: '校验当前值在数据库表内唯一', subTip: '校验当前值在表格内唯一', type: 'only', },
  { label: '字符长度校验', type: 'leng', controlType: 'length' },
  { label: '手机号码', type: 'phone', msg: t('Avue.crud.rule.msg.phone') },
  { label: '电子邮箱', type: 'email', msg: t('Avue.crud.rule.msg.email') },
  { label: '身份证号码', type: 'idCard', msg: t('Avue.crud.rule.msg.idCard') },
  { label: '邮政编码', type: 'postalCode', msg: t('Avue.crud.rule.msg.postalCode') },
  { label: '中文', type: 'chineseStr', msg: t('Avue.crud.rule.msg.chineseStr') },
  { label: '网址', type: 'url', msg: t('Avue.crud.rule.msg.url') },
  { label: '金额', type: 'money', msg: t('Avue.crud.rule.msg.money') },
  { label: '自定义正则表达式', tip: '例如：^[1-9]\d*$', type: 'customRegExp', controlType: 'regExp' },
  { label: '自定义校验规则', type: 'customRule', controlType: 'MEditor' }
]
