import { ref, toRefs, isRef } from "vue"
import { getSysDictByCode } from "@/api/sysDict" //引入封装数字字典接口
/**
 * 获取字典数据
 */
export function getDict(...args) {
  const res = ref({})
  return (() => {
    args.forEach((d, index) => {
      res.value[d] = []
      getSysDictByCode({
        dictCode: d
      }).then(result => {
        res.value[d] = result.itemList.map(p => ({
          label: p.zdmc,
          value: p.dm,
          elTagType: p.dictType
        }))
      })
    })
    return toRefs(res.value)
  })()
}
//翻译字典内容
export function setDict(dm, options) {
  let value = ""
  options.forEach(v => {
    if (v.value == dm) {
      value = v.label
    }
  })
  return value
}
/**
 * 设置级联选择器回显
 * @param {*} id 选中ID
 * @param {*} array 级联数据树
 * @param {*} childDeptList 子集变量
 */
export function setCascader(id, array, childDeptList = "childDeptList", fun) {
  if (array) {
    array.forEach(item => {
      if (item.childDeptList && item.id != id) {
        fun(item.id)
        setCascader(id, item.childDeptList, childDeptList, fun)
      } else if (item.childDeptList && item.id == id) {
        fun(item.id)
      } else if (!item.childDeptList && item.id == id) {
        fun(item.id)
      }
    })
  }
}
/**
 * 当type=1时获取出生日期,type=2时获取性别,type=3时获取年龄 all
 * @param {*} IdCard
 * @param {*} type
 * @returns
 */
export function IdCard(IdCard, type) {
  let user = {
    birthday: "",
    sex: "",
    age: ""
  }
  if (type === 1 || type == "all") {
    //获取出生日期
    let birthday = IdCard.substring(6, 10) + "-" + IdCard.substring(10, 12) + "-" + IdCard.substring(12, 14)
    if (type == "all") {
      user.birthday = birthday
    } else {
      return birthday
    }
  }
  if (type === 2 || type == "all") {
    //获取性别
    if (parseInt(IdCard.substr(16, 1)) % 2 === 1) {
      if (type == "all") {
        user.sex = "男"
      } else {
        return "男女"
      }
    } else {
      if (type == "all") {
        user.sex = "女"
      } else {
        return "女"
      }
    }
  }
  if (type === 3 || type == "all") {
    //获取年龄
    var ageDate = new Date()
    var month = ageDate.getMonth() + 1
    var day = ageDate.getDate()
    var age = ageDate.getFullYear() - IdCard.substring(6, 10) - 1
    if (IdCard.substring(10, 12) < month || (IdCard.substring(10, 12) === month && IdCard.substring(12, 14) <= day)) {
      age++
    }
    if (age <= 0) {
      age = 1
    }
    if (type == "all") {
      user.age = age
    } else {
      return age
    }
  }
  return user
}
/**
 *翻译字典数据
 * @export
 * @param {*} dm
 * @param {*} array
 */
export function getDictValue(dm, array) {
  let item = array.value.find(item => {
    if (item.value) {
      return item.value == dm;
    } else if (item.dm) {
      return item.dm == dm;
    }
  })
  return item ? item.label : ""
}

/** 获取多个字典值(一个值也可以) 字典内容 value-label
 * @param {String} 要查的值
 * @param {Array} dict 字典内容
 */
export function getMultiDictVal(values, dict) {
  if (typeof values === 'string' && values?.length) values = values.split(',')
  if (!Array.isArray(values)) return ''
  if (isRef(dict)) dict = dict.value
  if (!Array.isArray(dict)) return ''

  return values.map(v => {
    const item = dict.find(item => item.value === v);
    return item ? item.label : v;
  }).join(',');
}

