import { VxeUI } from 'vxe-table'
import XEUtils from 'xe-utils'

import dayjs from '#/plugins/dayjs'
import num from '#/utils/num'
import str from '#/utils/str'

// 正整数
export const pattern_pos_int = String.raw`^[1-9]\d*$`
export const pattern_phone = String.raw`^1[3-9]\d{9}$`
export const rule_phone = [
  {
    required: true,
    pattern: pattern_phone,
    message: '请输入正确的手机号',
  },
]

function preValidate(params: any) {
  const rule = params.rule
  const itemValue = params.data[params.field]
  if (!itemValue) {
    if (rule.required) {
      return new TypeError(rule.message || '请输入')
    }
    return undefined
  }
  return itemValue
}

// 表单校验器
export const rangeValidator = (min: any, max: any) => {
  return (params: any) => {
    let itemValue = preValidate(params)
    if (itemValue === undefined || itemValue instanceof TypeError) {
      return itemValue
    }
    itemValue = Number(itemValue)
    if (Number.isNaN(itemValue)) {
      return new TypeError('请输入正常的数值')
    }
    let msg = ''
    if (min && max) {
      msg = `请输入${min}到${max}之间的数值`
    } else if (min) {
      msg = `请输入不小于${min}的数值`
    } else if (max) {
      msg = `请输入不大于${max}的数值`
    }
    if (min && itemValue < min) {
      return new TypeError(msg)
    }
    if (max && itemValue > max) {
      return new TypeError(msg)
    }
  }
}

export const yearValidator = (params: any) => {
  const itemValue = preValidate(params)
  if (itemValue === undefined || itemValue instanceof TypeError) {
    return itemValue
  }
  // 判断是否为正确的年份
  if (!/^[1-9]\d{3}$/.test(itemValue)) {
    return new TypeError('请输入正确的年份')
  }
}

// 格式化日期，默认 yyyy-MM-dd HH:mm:ss
VxeUI.formats.add('formatDate', {
  cellFormatMethod({ cellValue }, format) {
    return XEUtils.toDateString(cellValue, format || 'yyyy-MM-dd HH:mm:ss')
  },
})

VxeUI.formats.add('trimZero', {
  cellFormatMethod({ cellValue }) {
    return num.trimZero(cellValue)
  },
})

VxeUI.formats.add('money', {
  cellFormatMethod({ cellValue }) {
    return num.formatMoney(cellValue)
  },
})

VxeUI.formats.add('timeGap', {
  cellFormatMethod({ cellValue }) {
    return dayjs(cellValue).fromNow()
  },
})

VxeUI.formats.add('vipEndTime', {
  cellFormatMethod(params: any) {
    const { cellValue, row } = params
    if (!cellValue) {
      return row.activeFlag ? '已激活（未登录过）' : '未激活'
    }
    const res = dayjs(cellValue).fromNow()
    if (res.endsWith('前')) {
      return `${res}（已过期）`
    }
    return res
  },
})

VxeUI.formats.add('redaction', {
  cellFormatMethod({ cellValue }) {
    return str.redaction(cellValue)
  },
})

VxeUI.formats.add('redactionPhone', {
  cellFormatMethod({ cellValue }) {
    return str.redactionPhone(cellValue)
  },
})

VxeUI.formats.add('redactionRemark', {
  cellFormatMethod({ cellValue }) {
    return str.redactionRemark(cellValue)
  },
})

// 保留2位小数
VxeUI.formats.add('fixed2', {
  cellFormatMethod({ cellValue }) {
    return num.toFixed2(Number(cellValue))
  },
})

// 数字变百分比
VxeUI.formats.add('num2percent', {
  cellFormatMethod({ cellValue }) {
    return num.toPercent(cellValue)
  },
})
