<template>
  <div>
    <PageWrapper
      title="第一次使用请修改当前用户密码，待修改完成后退出登录，才能使用本系统!"
      content="修改成功后会自动退出当前登录！"
      class="p-4"
      :contentStyle="{ margin: '16px 0 ' }"
    >
      <div class="py-8 bg-white flex flex-col justify-center items-center">
        <BasicForm @register="register" />
        <div class="flex justify-center">
          <a-button @click="resetFields"> 重置 </a-button>
          <a-button class="!ml-4" type="primary" @click="handleSubmit"> 确认 </a-button>
        </div>
      </div>
    </PageWrapper>
  </div>
</template>
<script lang="ts">
  import { defineComponent } from 'vue';
  import { PageWrapper } from '/@/components/Page';
  import { BasicForm, useForm } from '/@/components/Form';
  import { USER_INFO_KEY } from '/@/enums/cacheEnum';
  import { getAuthCache } from '/@/utils/auth';
  import { formSchema } from './pwd.data';
  import { resetPassword } from '/@/api/system/system';
  import { useMultipleTabStore } from '/@/store/modules/multipleTab';
  import { useUserStore } from '/@/store/modules/user';
  import { useAppStore } from '/@/store/modules/app';
  import { usePermissionStore } from '/@/store/modules/permission';
  import { useMessage } from '/@/hooks/web/useMessage';
  export default defineComponent({
    name: 'ChangePassword',
    components: { BasicForm, PageWrapper },
    setup() {
      const [register, { validate, resetFields }] = useForm({
        size: 'large',
        labelWidth: 100,
        showActionButtonGroup: false,
        schemas: formSchema,
      });
      const tabStore = useMultipleTabStore();
      const userStore = useUserStore();
      const appStore = useAppStore();
      const permissionStore = usePermissionStore();
      const { createMessage } = useMessage();
      const userInfo = getAuthCache(USER_INFO_KEY);
      async function handleSubmit() {
        try {
          const values = await validate();
          const { passwordOld, passwordNew } = values;
          const params = {
            userId: userInfo.userId,
            password: passwordOld,
            resetPassword: passwordNew,
          };

          await resetPassword(params).then((result) => {
            if (result.data) {
              createMessage.success('修改成功');
              // /dashboard/analysis
              setTimeout(function () {
                localStorage.clear();
                appStore.resetAllState();
                permissionStore.resetState();
                tabStore.resetState();
                userStore.resetState();
                location.reload();
              }, 500);
            }
          });
        } catch (error) {}
      }
      return { register, resetFields, handleSubmit };
    },
  });
</script>
