function isNull(data) {
  if (!data) return true;
  if (JSON.stringify(data) === "{}") return true;
  if (JSON.stringify(data) === "[]") return true;
  return false;
}

/* 1 . 手机校验 */
const validatePhone = () => {
  return (rule, value, callback) => {
    if (!value) {
    } else {
      const reg = /^1[3|4|5|7|8][0-9]\d{8}$/;
      if (reg.test(value)) {
        callback();
      } else {
        return callback(new Error("请输入正确的手机号"));
      }
    }
  };
};
export const phoneRule = (rule,prop = 'phone') => {
  if (isNull(rule)) return [];
  const { require, validator } = rule && { ...rule };
  const result = [];
  if (require) {
    result.push({
      required: true,
      message: "请输入手机号",
      trigger: "blur"
    });
  }
  if (validator) {
    result.push(
      {
        min: 11,
        message: "手机号格式不正确",
        trigger: "blur"
      },
      {
        trigger: "blur",
        validator: validatePhone()
      }
    );
  }
  const resultObj = {};
  resultObj[prop] = result;
  return { ...resultObj }
};
/* 手机校验end */

/* 2. 身份证校验 bengin */
const validateIdentity = () => {
  return (rule, value, callback) => {
    if (!value) {
      // return callback(new Error('身份证号不能为空'));
    } else if (!/(^\d{15}$)|(^\d{17}(\d|X|x)$)/.test(value)) {
      callback(new Error("输入的身份证长度或格式错误"));
    }

    //身份证城市
    var aCity = {
      11: "北京",
      12: "天津",
      13: "河北",
      14: "山西",
      15: "内蒙古",
      21: "辽宁",
      22: "吉林",
      23: "黑龙江",
      31: "上海",
      32: "江苏",
      33: "浙江",
      34: "安徽",
      35: "福建",
      36: "江西",
      37: "山东",
      41: "河南",
      42: "湖北",
      43: "湖南",
      44: "广东",
      45: "广西",
      46: "海南",
      50: "重庆",
      51: "四川",
      52: "贵州",
      53: "云南",
      54: "西藏",
      61: "陕西",
      62: "甘肃",
      63: "青海",
      64: "宁夏",
      65: "新疆",
      71: "台湾",
      81: "香港",
      82: "澳门",
      91: "国外"
    };
    if (!aCity[parseInt(value?.substr(0, 2))]) { callback(new Error("身份证地区非法")); }
    // 出生日期验证
    var sBirthday = ( value.substr(6, 4) + "-" + Number(value.substr(10, 2)) + "-" + Number(value.substr(12, 2))).replace(/-/g, "/"),
    d = new Date(sBirthday);
    if ( sBirthday !== d.getFullYear() + "/" + (d.getMonth() + 1) + "/" + d.getDate()) {
      callback(new Error("身份证上的出生日期非法"));
    }

    // 身份证号码校验
    var sum = 0,
      weights = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2],
      codes = "10X98765432";
    for (var i = 0; i < value.length - 1; i++) {
      sum += value[i] * weights[i];
    }
    var last = codes[sum % 11]; //计算出来的最后一位身份证号码
    if (value[value.length - 1] !== last) {
      callback(new Error("输入的身份证号非法"));
    }
    callback();
  };
};
export const identityCardRule = (rule,prop = 'idEntityCard') => {
  if (isNull(rule)) return [];
  const { require, validator } = rule && { ...rule };
  const result = [];
  if (require) {
    result.push({
      required: true,
      message: "请输入身份证号",
      trigger: "blur"
    });
  }
  if (validator) {
    result.push({
      trigger: "blur",
      validator: validateIdentity()
    });
  }

  const resultObj = {};
  resultObj[prop] = result;
  return { ...resultObj }
};
/* 身份证校验 end */

/* 3车牌号校验 */
const validateCarnumber = () => {
  return (rule, value, callback) => {
    const ptreg =
      /^[京津冀晋蒙辽吉黑沪苏浙皖闽赣鲁豫鄂湘桂琼川贵云渝藏陕甘青宁新粤]{1}[ABCDEFGHJKLMNOPQRSTUVWXY]{1}[0-9A-Z]{5}$/u;
    const xnyreg =
      /^[京津冀晋蒙辽吉黑沪苏浙皖闽赣鲁豫鄂湘桂琼川贵云渝藏陕甘青宁新粤]{1}[A-Z]{1}(([0-9]{5}[DABCEFGHJK]$)|([DABCEFGHJK][A-HJ-NP-Z0-9][0-9]{4}$))/; // 2021年新能源车牌不止有DF
    if (!value) {
      callback(new Error("请选择车牌号"));
    } else {
      if (value.length === 7) {
        if (!ptreg.test(value)) {
          callback(new Error("请输入正确的普通车牌号"));
        } else {
          callback();
        }
      } else if (value.length === 8) {
        if (!xnyreg.test(value)) {
          callback(new Error("请输入正确的新能源车牌号"));
        } else {
          callback();
        }
      } else {
        callback(new Error("车牌号错误"));
      }
    }
  };
};
export const carNumberRule = (rule,prop = "carnumber") => {
  if (isNull(rule)) return [];
  const { require, validator } = rule && { ...rule };
  const result = [];
  if (require) {
    result.push({
      required: true,
      message: "请输入车牌号",
      trigger: "blur"
    });
  }
  if (validator) {
    result.push({
      trigger: "blur",
      validator: validateCarnumber()
    });
  }
  // return {
  //   carnumber: result
  // };
  const resultObj = {};
  resultObj[prop] = result;
  return { ...resultObj }
};
/* 3车牌号校验end */

/* 4民族校验 */
export const nationSelectRule = (rule,prop = 'nation') => {
  if (isNull(rule)) return [];
  const { require } = rule && { ...rule };
  const result = [];
  if (require) {
    result.push({
      required: true,
      message: "请选择民族",
      trigger: "change"
    });
  }
  const resultObj = {};
  resultObj[prop] = result;
  return { ...resultObj }
};

/* 5 组织机构 */
export const frameWorkRule = (rule,prop = 'frameWork') => {
  if (isNull(rule)) return [];
  const { require } = rule && { ...rule };
  const result = [];
  if (require) {
    result.push({
      required: true,
      message: "请选择组织机构",
      trigger: "change"
    });
  }
  // return {
  //   frameWork: result
  // };
  const resultObj = {};
  resultObj[prop] = result;
  return { ...resultObj }
};

/* 6 选择地址 */
export const addressSelectRule = (rule,prop = 'addredd') => {
  if (isNull(rule)) return [];
  const { require } = rule && { ...rule };
  const result = [];
  if (require) {
    result.push({
      required: true,
      message: "请选择地址",
      trigger: "change"
    });
  }
  const resultObj = {};
  resultObj[prop] = result;
  return { ...resultObj }
};

/* 7.Email */
const validateEmail = () => {
  return (rule, value, callback) => {
    const reg = /^([a-zA-Z0-9]+[-_\.]?)+@[a-zA-Z0-9]+\.[a-z]+$/;
    if (!value) {
    } else {
      if (!reg.test(value)) {
        return callback(new Error("请输入正确的邮箱地址"));
      } else {
        callback();
      }
    }
  };
};
export const emailRule = (rule,prop = "email") => {
  if (isNull(rule)) return [];
  const { require, validator } = rule && { ...rule };
  const result = [];
  if (require) {
    result.push({
      required: true,
      message: "请输入邮箱",
      trigger: "blur"
    });
  }
  if (validator) {
    result.push({
      trigger: "change",
      validator: validateEmail()
    });
  }
  // return {
  //   email: result
  // };
  const resultObj = {};
  resultObj[prop] = result;
  return { ...resultObj }
};

/* 5 组织机构 */
export const departmentRule = (rule,prop = 'department') => {
  if (isNull(rule)) return [];
  const { require } = rule && { ...rule };
  const result = [];
  if (require) {
    result.push({
      required: true,
      message: "请选择部门",
      trigger: "change"
    });
  }
  // return {
  //   frameWork: result
  // };
  const resultObj = {};
  resultObj[prop] = result;
  return { ...resultObj }
};
