<template>
  <BasicModal
    v-bind="$attrs"
    @register="register"
    @ok="handSubmit"
    :title="t('routes.staff.updateBtn')"
    @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 { reMyselfPassword } from '/@/api/staff/list';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { useUserStore } from '/@/store/modules/user';

  const { createMessage } = useMessage();

  export default defineComponent({
    components: { BasicModal, BasicForm },
    props: {
      userData: { type: Object },
    },
    setup(props) {
      const { t } = useI18n();
      const userStore = useUserStore();
      const getUserInfo = userStore.getUserInfo;
      const preventAutoFill = ref(true);
      const schemas: FormSchema[] = [
        {
          field: 'id',
          component: 'Input',
          label: 'id',
          show: false,
        },
        {
          field: 'oldPassword',
          component: 'Input',
          label: '原密码',
          labelWidth: 120,
          required: true,
          colProps: { span: 18 },
          componentProps:{
            readonly:preventAutoFill.value,
          }
        },
        {
          field: 'password',
          component: 'StrengthMeter',
          label: '新密码',
          labelWidth: 120,
          required: true,
          rules: [
            {
              required: true,
              // @ts-ignore
              validator: async (rule, 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:{
            maxLength: 16,
            minLength: 8,
            readonly:preventAutoFill.value,
            placeholder:"请输入8-16位数字、字母大小写组合"
          },
          colProps: { span: 18 },
        },
      ];

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

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

      function onDataReceive(data) {
        preventAutoFill.value = false
        setTimeout(()=>{
          updateSchema([{
            field: 'oldPassword',
            componentProps:{
              readonly:false,
            }
          },{
            field: 'password',
            componentProps:{
              readonly:false,
            }
          }])
        },500)
        setFieldsValue({
          id: data.id,
        });

      }
      async function handSubmit() {
        console.log('getUserInfo',getUserInfo)
        const { id } = getUserInfo
        const { password,oldPassword } = await validate();
        await reMyselfPassword({ 
          id, 
          newPassword: encodeStr(window.btoa(password)),
          oldPassword: encodeStr(window.btoa(oldPassword)),
         });
        createMessage.success(t('common.optSuccess'));
        resetFields();
        userStore.logout(true);
      }
      function handleVisibleChange(v) {
        v && props.userData && nextTick(() => onDataReceive(props.userData));
      }

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