import { smsPerson } from "@/service/Api.service";
import { iFormItem } from "cqk-sy-ui/sy/components/BusinessForm/types";
import { ElMessage } from "element-plus";

const checkIdNumber = (rule: any, value: any, callback: any) => {
  const checkReg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
  if (!checkReg.test(value)) callback(new Error("身份证号码格式不正确！"));
  else callback();
};

let checkCard = (rule: any, value: any, callback: any) => {
  const checkReg = /^(\d{16}|\d{17}|\d{19})$/;
  if (!checkReg.test(value)) callback(new Error("银行卡位数不正确！"));
  else callback();
};
// 校验手机号
let checkPhone = (rule: any, value: any, callback: any) => {
  const checkReg = /^[1][3,4,5,7,8,9][0-9]{9}$/;
  if (!checkReg.test(value)) callback(new Error("手机号格式不正确！"));
  else callback();
};

export const generateFormList = (submit: any) => {
  return [
    {
      type: "input",
      label: "真实姓名",
      showLabel: true,
      placeholder: "请输入您的真实姓名",
      rule: [{ required: true, message: "真实姓名不能为空", trigger: "blur" }],
    },
    {
      type: "input",
      showLabel: true,
      maxLen: 18,
      label: "身份证号",
      placeholder: "请输入您的身份证号",
      rule: [{ validator: checkIdNumber, trigger: "blur" }],
    },
    {
      type: "input",
      showLabel: true,
      placeholder: "请输入您的银行卡号",
      maxLen: 19,
      label: "银行卡号",
      rule: [{ validator: checkCard, trigger: "blur" }],
    },
    {
      type: "codeInput",
      showLabel: true,
      placeholder: "请输入您的银行卡预留手机号",
      label: "银行预留手机号",
      maxLen: 11,
      rule: [{ validator: checkPhone, trigger: "blur" }],
      async handleClickSendCode(roleForm: any, formEl) {
        const phone = roleForm.银行预留手机号;
        const res = await smsPerson(phone);
        if (res && res.code === 200) {
          ElMessage({
            message: "验证码发送成功！",
            type: "success",
          });
          return true;
        }
        return false;
      },
    },
    {
      type: "input",
      showLabel: true,
      placeholder: "请输入验证码",
      maxLen: 6,
      label: "验证码",
      rule: [{ required: true, message: "验证码不能为空", trigger: "blur" }],
    },
    {
      type: "submitButton",
      text: "提交审核",
      btnProps: {
        type: "primary",
      },
      styles:
        "height:38px;margin-top: 10px;margin-left: 50px; padding: 12px 20px;",
      async onSubmit(valid, data, setError) {
        data = {
          name: data.真实姓名,
          id_card: data.身份证号,
          bank_card: data.银行卡号,
          bank_phone: data.银行预留手机号,
          code: data.验证码,
        };
        submit(valid, data, setError);
      },
    },
  ] as iFormItem[];
};
