/*
 * @Author: pft 904755879@qq.com
 * @Date: 2023-06-21 14:55:10
 * @LastEditors: 潘凤涛 904755879@qq.com
 * @LastEditTime: 2023-09-06 09:11:07
 * @FilePath: /tel-admin-1/src/utils/hooks/form_DataPicker_format.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import dayjs from "dayjs";
import type { FormRules, FormInstance } from "element-plus";

/**格式化时间选择器传参*/
export function UseFormatDatePickerHook(dateArr) {
  return dateArr?.length === 2
    ? `${dayjs(dateArr[0]).format("YYYY-MM-DD")} - ${dayjs(dateArr[1]).format(
      "YYYY-MM-DD"
    )}`
    : "";
}

/** 表单验证: 身份证号*/
export function UseValidIdCardHook(IdCard: string) {
  const IdCardReg = new RegExp(
    /^[1-9]\d{5}(?:18|19|20)\d{2}(?:0[1-9]|10|11|12)(?:0[1-9]|[1-2]\d|30|31)\d{3}[\dXx]$/
  );
  return IdCardReg.test(IdCard);
}
/** 表单验证: 电话号*/
export function UseValidTelHook(Tel: string) {
  const TelReg = new RegExp(
    /^(?:(?:\+|00)86)?1(?:(?:3[\d])|(?:4[5-79])|(?:5[0-35-9])|(?:6[5-7])|(?:7[0-8])|(?:8[\d])|(?:9[1589]))\d{8}$/
  );
  return TelReg.test(Tel);
}
/** 表单验证: 银行卡*/
export function UseValidBankCardHook(BankCard: string) {
  const BankCardReg = new RegExp(/^[1-9]\d{9,29}$/);
  return BankCardReg.test(BankCard);
}
/** 表单验证: 创客号*/
export function UseValidMakerCodeHook(MakerCode: string) {
  const MakerCodeReg = new RegExp(/^K.{7}$|^k.{7}$/);
  return MakerCodeReg.test(MakerCode);
}

/**
 * 两个时间月份差
 * @param time1 2023-03-01
 * @param time2
 * @returns 间隔月份
 */
export function UseValidTimeRangeHook(time1, time2) {
  if (time1 && time2) {
    const t1 = dayjs(time1).format("YYYY-MM-DD");
    const t2 = dayjs(time2).format("YYYY-MM-DD");
    return Math.abs(dayjs(t1).diff(dayjs(t2), "month"));
  }
}

export async function UseValidElFormHook(
  formEl: FormInstance | undefined,
  fn: Function
) {
  if (!formEl) return console.log("表单节点为空");
  await formEl.validate((valid, fields) => {
    if (valid) {
      fn();
      console.log("验证成功");
    } else {
      console.log("error submit", fields);
    }
  });
}

export function UsePlaceEmptyString(param: [Object] | Object) {
  if (toString.call(param) == "[object Array]") {
    let res = null;
    res = param.map(obj => {
      const newObj = { ...obj }; // 创建新对象，以避免修改原始对象
      Object.keys(newObj).forEach(key => {
        if (
          (newObj[key] + "").trim() === "" ||
          newObj[key] === undefined ||
          newObj[key] === null
        ) {
          newObj[key] = "无";
        }
      });
      return newObj;
    });
    console.log("处理后的：", res);
    return res;
  } else if (toString.call(param) == "[object Object]") {
    // 递归获取对象值，转换为‘无‘
    function traverseObject(obj) {
      for (const key in obj) {
        if (Object.prototype.hasOwnProperty.call(obj, key)) {
          const value = obj[key];
          if (
            (value + "").trim() === "" ||
            value === undefined ||
            value === null
          ) {
            obj[key] = "无";
          }
          if (toString.call(obj[key]) === "[object Object]") {
            traverseObject(value);
          }
        }
      }
    }
    traverseObject(param);
    console.log("处理后的对象为:", param);

    return param;
  }
}
