<template>
  <BasicModal v-bind="$attrs" @register="registerModal" title="修改手机号" @ok="handleSubmit" width="600px">
    <BasicForm @register="registerForm" />
  </BasicModal>
</template>
<script lang="ts" setup>
  import { ref, unref, defineExpose } from 'vue';
  import { rules } from '/@/utils/helper/validator';
  import { defHttp } from '/@/utils/http/axios';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { BasicForm, useForm } from '/@/components/Form/index';
  import { useMessage } from '/@/hooks/web/useMessage';
  // 声明Emits
  const emit = defineEmits(['register']);
  const $message = useMessage();
  const formRef = ref();
  const username = ref('');
  //表单配置
  const [registerForm, { resetFields, validate, clearValidate }] = useForm({
    schemas: [
      // {
      //   label: '旧密码',
      //   field: 'oldphone',
      //   component: 'Input',
      //   //动态自定义正则，values: 当前表单的所有值
      //   dynamicRules: ({ values }) => {
      //     console.log('values:', values);
      //     //需要return
      //     return [
      //       {
      //         //默认开启表单检验
      //         required: true,
      //         // value 当前手机号输入的值
      //         validator: (_, value) => {
      //           //需要return 一个Promise对象
      //           return new Promise((resolve, reject) => {
      //             if (!value) {
      //               reject('请输入手机号！');
      //             }
      //             //验证手机号是否正确
      //             let reg = /^1[3456789]\d{9}$/;
      //             if (!reg.test(value)) {
      //               reject('请输入正确手机号！');
      //             }
      //             resolve();
      //           });
      //         },
      //       },
      //     ];
      //   },
      // },
      {
        label: '新手机号',
        field: 'phone',
        component: 'Input',
        componentProps: {
          placeholder: '请输入新手机号',
        },
        //动态自定义正则，values: 当前表单的所有值
        dynamicRules: ({ values }) => {
          console.log('values:', values);
          //需要return
          return [
            {
              //默认开启表单检验
              required: true,
              // value 当前手机号输入的值
              validator: (_, value) => {
                //需要return 一个Promise对象
                return new Promise((resolve, reject) => {
                  if (!value) {
                    reject('请输入手机号！');
                  }
                  //验证手机号是否正确
                  let reg = /^1[3456789]\d{9}$/;
                  if (!reg.test(value)) {
                    reject('请输入正确手机号！');
                  }
                  resolve();
                });
              },
            },
          ];
        },
      },
    ],
    showActionButtonGroup: false,
  });
  //表单赋值
  const [registerModal, { setModalProps, closeModal }] = useModalInner();

  //表单提交事件
  async function handleSubmit() {
    try {
      const values = await validate();
      setModalProps({ confirmLoading: true });
      //提交表单
      let params = Object.assign({ userId: unref(username) }, values);
      defHttp.post({ url: '/sys/user/changePhone', params }, { isTransformResponse: false }).then((res) => {
        if (res.success) {
          $message.createMessage.success('修改成功');
          //关闭弹窗
          closeModal();
        } else {
          $message.createMessage.warning(res.message);
        }
      });
    } finally {
      setModalProps({ confirmLoading: false });
    }
  }

  async function show(name) {
    if (!name) {
      $message.createMessage.warning('当前系统无登录用户!');
      return;
    } else {
      username.value = name;
      await setModalProps({ visible: true });
      await resetFields();
      await clearValidate();
    }
  }

  defineExpose({
    show,
  });
</script>
