import dict from "./dict";
import $moment from './monent';

const confirm = (content, title, config) => {
  let ops = {content}
  if (typeof title == 'object') {
    ops = {...ops, ...title}
  } else ops = {...ops, title: title || "提示"}
  return new Promise((resolve, reject) => {
    uni.showModal({
      ...ops, ...config, success: res => {
        if (res?.confirm) {
          resolve()
        } else if (res?.cancel) {
          reject()
        }
      }
    })
  })
}

/**
 * 获取年龄
 * @param code
 */
const calcAge = (code) => {
  let birthday
  if (typeof code == 'string' && code.length == 18) {
    birthday = $moment(code.substring(6, 14), 'YYYYMMDD')
  } else if (typeof code == 'object') {
    birthday = code
  }
  return Math.ceil($moment().year() - $moment(birthday).year())
}


export default {
  dict,
  confirm,
  calcAge,
  dateFormat: (time, format) => {
    return $moment(time).format(format || 'YYYY-MM-DD').replace("Invalid Date", "")
  },
  formatName: (name) => {
    return Array.from(name)?.slice(-2)?.toString() || "";
  },
  loading: title => {
    uni.showLoading({
      title: title ? title : '加载中',
      mask: true
    })
  },
  hideLoading: () => {
    uni.hideLoading()
  }
}
