import { sendEmail, sendSms } from "@/service/Api.service";
import { filterPhone, phoneReg } from "cqk-sy-ui";
import { iFormItem } from "cqk-sy-ui/sy/components/BusinessForm";
import { ElMessage } from "element-plus";

export const generateStep1FormList = (
  data: { phone: string; email: string },
  submit: any
) => {
  return [
    {
      type: "text",
      label: data.phone ? "原手机号" : "当前邮箱",
      defaultValue: data.phone ? filterPhone(data.phone) : data.email,
      showLabel: true,
    },
    {
      type: "codeInput",
      showLabel: true,
      placeholder: "请输入验证码",
      maxLen: 6,
      label: "验证码",
      rule: [
        {
          required: true,
          message: "请输入验证码",
        },
      ],
      async handleClickSendCode(roleForm: any, formEl) {
        let res = null;
        if (data.phone) {
          res = await sendSms({
            phone: data.phone,
            template_id: 3,
          });
        } else {
          // 邮箱
          res = await sendEmail({
            email: data.email,
            template_id: 3,
          });
        }
        if (res) {
          ElMessage({
            message: "发送成功",
            type: "success",
          });
          return true;
        } else return false;
      },
    },
    {
      type: "submitButton",
      text: "下一步",
      btnProps: {
        type: "primary",
      },
      styles: "margin-top: 10px;margin-left: 50px;",
      async onSubmit(valid, data, setError) {
        if (valid) {
          data = {
            phone: data.原手机号,
            email: data.当前邮箱,
            code: data.验证码,
          };
          submit(data, setError);
        }
      },
    },
  ] as iFormItem[];
};

export const generateStep2FormList = (submit: any) => {
  return [
    {
      type: "input",
      showLabel: true,
      label: "新手机号",
      placeholder: "请输入新手机号",
      maxLen: 11,
      rule: [
        {
          pattern: phoneReg,
          message: "请输入正确的手机号",
          trigger: "blur",
        },
        {
          required: true,
          message: "请输入手机号",
          trigger: "blur",
        },
      ],
    },
    {
      type: "codeInput",
      showLabel: true,
      placeholder: "请输入验证码",
      maxLen: 6,
      rule: [
        {
          required: true,
          message: "请输入验证码",
        },
      ],
      label: "验证码",
      async handleClickSendCode(roleForm: any, formEl) {
        let res = null;
        res = await sendSms({
          phone: roleForm.新手机号,
          template_id: 3,
        });
        if (res) {
          ElMessage({
            message: "发送成功",
            type: "success",
          });
          return true;
        } else return false;
      },
    },
    {
      type: "submitButton",
      text: "提交修改",
      btnProps: {
        type: "primary",
      },
      styles: "margin-top: 10px;margin-left: 50px;",
      async onSubmit(valid, data, setError) {
        if (valid) {
          data = {
            phone: data.新手机号,
            code: data.验证码,
          };
          submit(data, setError);
        }
      },
    },
  ] as iFormItem[];
};
