<template>
  <BasicModal
    v-bind="$attrs"
    @register="register"
    @ok="handSubmit"
    :title="t('routes.system.password')"
    :min-height="100"
    @visible-change="handleVisibleChange"
    @cancel="resetFields"
  >
    <div class="pt-3px pr-3px">
      <BasicForm @register="registerForm" />
    </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 { updatePasswordApi } 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('sys.login.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(t('code.3011'));
                }
                if (!regPos.test(value)) {
                  /* eslint-disable-next-line */
                  return Promise.reject(t('code.3011'));
                }
                return Promise.resolve();
              },
              trigger: 'change',
            },
          ],
          componentProps:{
            placeholder: t('code.3011'),
            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 updatePasswordApi({ 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>
