import { sendEmail, sendSms } from "@/service/Api.service";
import { iFormItem } from "cqk-sy-ui/sy/components/BusinessForm";
import { validPassword } from "../EditPassword/config";

export const generateFormList = (data: any, confirm: any) => {
  return [
    {
      type: "text",
      label: data.phone ? "手机号" : "邮箱",
      showLabel: true,
      defaultValue: data.phone ? data.phone : data.email,
    },
    {
      type: "passwordInput",
      showLabel: true,
      label: "输入密码",
      placeholder: "请输⼊6-20位数字、字⺟组合的密码",
      rule: [
        {
          validator(rule: any, value: any, callback: any) {
            const result = validPassword(value);
            if (!result)
              callback(new Error("密码不符合规则，请输入6~20位数字和字母组合"));
            else callback();
          },
        },
      ],
    },
    {
      type: "codeInput",
      showLabel: true,
      label: "验证码",
      placeholder: "请输入6位验证码",
      maxLen: 6,
      rule: [
        {
          required: true,
          message: "请输入验证码",
          trigger: "blur",
        },
      ],
      async handleClickSendCode(roleForm, formEl) {
        let res;
        if (data.phone) {
          res = await sendSms({
            phone: data.phone,
            template_id: 1,
          });
        } else {
          // 邮箱
          res = await sendEmail({
            email: data.email,
            template_id: 1,
          });
        }
        return !!res;
      },
    },
    {
      type: "submitButton",
      showLabel: true,
      text: "设置密码",
      btnProps: {
        type: "primary",
      },
      styles:
        "height:38px;margin-top: 10px;margin-left: 50px; padding: 12px 20px;",
      async onSubmit(valid, data, setError) {
        if (valid) {
          confirm(
            {
              pwd: data.输入密码,
              code: data.验证码,
              email: data.邮箱,
              phone: data.手机号,
            },
            setError
          );
        }
      },
    },
  ] as iFormItem[];
};
