<template>
  <div class="flex flex-col items-center justify-center py-8 bg-white">
    <BasicForm @register="register" />
    <div class="flex justify-center">
      <a-button @click="resetFields" class="w-25 !rounded"> {{ t('重置') }} </a-button>
      <a-button class="!ml-4 w-25 !rounded" type="primary" @click="handleSubmit">
        {{ t('确认') }}
      </a-button>
    </div>
  </div>
</template>
<script lang="ts">
  import { defineComponent, inject, ref } from 'vue';
  import { BasicForm, useForm, FormSchema } from '/@/components/Form';

  import { updatePassword } from '/@/api/system/user';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useUserStore } from '/@/store/modules/user';
  const { t } = useI18n();
  const pwdScore = ref(0);
  export const formSchema: FormSchema[] = [
    {
      field: 'oldPassword',
      label: t('当前密码'),
      component: 'InputPassword',
      required: true,
      colProps: {
        span: 24,
      },
    },
    {
      field: 'newPassword',
      label: t('新密码'),
      component: 'StrengthMeter',
      componentProps: {
        placeholder: t('新密码'),
        onChange: (_, score) => {
          pwdScore.value = score;
        },
      },
      rules: [
        {
          validator: (_, value) => {
            if (!value) {
              return Promise.reject('请输入新密码');
            }
            if (pwdScore.value < 50) {
              return Promise.reject('密码强度设置过低，请至少保证中等强度。');
            }
            return Promise.resolve();
          },
          trigger: 'change',
        },
      ],
      colProps: {
        span: 24,
      },
    },
    {
      field: 'confirmPassword',
      label: t('确认密码'),
      component: 'InputPassword',
      colProps: {
        span: 24,
      },

      dynamicRules: ({ values }) => {
        return [
          {
            required: true,
            validator: (_, value) => {
              if (!value) {
                return Promise.reject(t('密码不能为空'));
              }
              if (value !== values.newPassword) {
                return Promise.reject(t('两次输入的密码不一致!'));
              }
              return Promise.resolve();
            },
          },
        ];
      },
    },
  ];

  export default defineComponent({
    name: 'ChangePassword',
    components: { BasicForm },
    setup() {
      const [register, { validate, resetFields }] = useForm({
        showActionButtonGroup: false,
        schemas: formSchema,
        layout: 'vertical',
      });
      const userStore = useUserStore();
      const keycloak = inject('keycloak');
      async function handleSubmit() {
        try {
          const values = await validate();
          // const { passwordOld, passwordNew } = values;

          // TODO custom api
          let res = await updatePassword(values);
          if (res) {
            userStore.confirmLoginOut('修改密码成功，是否退出系统重新登陆？');

            if (keycloak) {
              keycloak.logout();
            }
          }
        } catch (error) {}
      }

      return { register, resetFields, handleSubmit, t };
    },
  });
</script>
