<template>
  <BasicModal
    v-bind="$attrs"
    @register="register"
    @ok="handSubmit"
    title="重置密码"
    :min-height="100"
    @visible-change="handleVisibleChange"
    @cancel="resetFields"
  >
    <div class="pt-3px pr-3px">
      <BasicForm @register="registerForm" />
      <span style="margin-left:100px;color:#9f9c9c">请设置新密码，设置后，管理员可使用新密码登录。</span>
    </div>
  </BasicModal>
</template>
<script lang="ts">
  import { useI18n } from '/@/hooks/web/useI18n';
  import { encodeStr } from '/@/utils/encodeUtil';
  import { defineComponent, nextTick, ref } from 'vue';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { BasicForm, FormSchema, useForm } from '/@/components/Form/index';
  import { ReUcenterUserPassword } from '/@/api/staff/list';
  import { useMessage } from '/@/hooks/web/useMessage';

  const { createMessage } = useMessage();

  export default defineComponent({
    components: { BasicModal, BasicForm },
    props: {
      userData: { type: Object },
    },
    setup(props) {
      const { t } = useI18n();
      const preventAutoFill = ref(true);
      const schemas: FormSchema[] = [
        {
          field: 'id',
          component: 'Input',
          label: 'id',
          show: false,
        },
        {
          field: 'password',
          component: 'StrengthMeter',
          label: t('routes.staff.password'),
          labelWidth: 100,
          required: true,
          rules: [
            {
              required: true,
              // @ts-ignore
              validator: async (rule, value) => {
                console.log('value',value)
                const regPos = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[a-zA-Z\d]{8,}$/; // 非中文
                if (!value) {
                  return Promise.reject('请输入8-16位数字、字母大小写组合');
                }
                if (!regPos.test(value)) {
                  /* eslint-disable-next-line */
                  return Promise.reject('请输入8-16位数字、字母大小写组合');
                }
                return Promise.resolve();
              },
              trigger: 'change',
            },
          ],
          componentProps:{
            placeholder:"请输入8-16位数字、字母大小写组合",
            maxLength: 16,
            minLength: 8,
          },
          colProps: { span: 18 },
        },
      ];

      const [
        registerForm,
        {
          setFieldsValue,
          validate,
          resetFields,
          // setProps
        },
      ] = useForm({
        labelWidth: 120,
        schemas,
        showActionButtonGroup: false,
        actionColOptions: {
          span: 24,
        },
      });

      const [register, { closeModal }] = useModalInner((data) => {
        data && onDataReceive(data);
      });

      function onDataReceive(data) {
        setFieldsValue({
          id: data.id,
        });
      }
      async function handSubmit() {
        const { id, password } = await validate();
        await ReUcenterUserPassword({ id, newPassword: encodeStr(window.btoa(password)) });
        createMessage.success(t('common.optSuccess'));
        resetFields();
        closeModal();
      }
      function handleVisibleChange(v) {
        v && props.userData && nextTick(() => onDataReceive(props.userData));
      }

      return {
        handSubmit,
        register,
        t,
        schemas,
        registerForm,
        handleVisibleChange,
        resetFields,
      };
    },
  });
</script>
