<template>
  <basic-modal v-bind="$attrs" @register="registerModal" :title="title" @ok="handleSubmit" width="600px" data-cy="changePasswordModal">
    <basic-form @register="registerForm" />
  </basic-modal>
</template>
<script lang="ts" setup>
  import { rules } from '@/utils/helper/validator';
  import { BasicModal, useModalInner } from '@/components/Modal';
  import { useForm, BasicForm } from '@/components/Form';
  import { useMessage } from '@/hooks/web/useMessage';
  import accountService from '@/api-service/account/account.service';
  <%_ if (enableTranslation) { _%>
  import { useI18n } from '@/hooks/web/useI18n';
  import { useLocaleStore } from '@/store/modules/locale';
  <%_ } _%>

  <%_ if (enableTranslation) { _%>
  const localeStore = useLocaleStore();
  const { t } = useI18n();

  <%_ } _%>
  // 声明Emits
  const emit = defineEmits(['register']);
  const $message = useMessage();
  const username = ref('');
  const title = ref(<% if (enableTranslation) { %>t('layout.changePassword.changePassword')<% } else { %>'t$t('layout.changePassword.changePassword')$t$'<% } %>);
  //表单配置
  const [registerForm, { resetFields, validate, clearValidate }] = useForm({
    schemas: [
      {
        label: <% if (enableTranslation) { %>t('layout.changePassword.oldPassword')<% } else { %>'t$t('layout.changePassword.oldPassword')$t$'<% } %>,
        field: 'currentPassword',
        component: 'InputPassword',
        required: true,
      },
      {
        label: <% if (enableTranslation) { %>t('layout.changePassword.newPassword')<% } else { %>'t$t('layout.changePassword.newPassword')$t$'<% } %>,
        field: 'newPassword',
        component: 'StrengthMeter',
        componentProps: {
          placeholder: <% if (enableTranslation) { %>t('layout.changePassword.pleaseEnterNewPassword')<% } else { %>'t$t('layout.changePassword.pleaseEnterNewPassword')$t$'<% } %>,
        },
        rules: [
          {
            required: true,
            message: <% if (enableTranslation) { %>t('layout.changePassword.pleaseEnterNewPassword')<% } else { %>'t$t('layout.changePassword.pleaseEnterNewPassword')$t$'<% } %>,
          },
        ],
      },
      {
        label: <% if (enableTranslation) { %>t('layout.changePassword.confirmNewPassword')<% } else { %>'t$t('layout.changePassword.confirmNewPassword')$t$'<% } %>,
        field: 'confirmpassword',
        component: 'InputPassword',
        dynamicRules: ({ values }) => rules.confirmPassword(values, true),
      },
    ],
    showActionButtonGroup: false,
    wrapperCol: null,
    <%_ if (enableTranslation) { _%>
    labelWidth: localeStore.getLocale == 'zh_CN' ? 100 : 160,
    <%_ } else { _%>
    labelWidth: 100,
    <%_ } _%>
  });
  //表单赋值
  const [registerModal, { setModalProps, closeModal }] = useModalInner();

  //表单提交事件
  async function handleSubmit() {
    try {
      const values = await validate();
      setModalProps({ confirmLoading: true });
      //提交表单
      let params = Object.assign({ login: unref(username) }, values);
      accountService.changePassword(params).then(() => {
        $message.createMessage.success('修改密码成功');
        closeModal();
      }).catch(() => {
        $message.createMessage.error('修改密码失败');
      });
    } catch (e ) {
      console.log(e);
    } finally {
      setModalProps({ confirmLoading: false });
    }
  }

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

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