<script setup lang="ts">
import type { SysUserApi } from '#/api/auth/user';

import { ref } from 'vue';

import { useVbenModal, z } from '@vben/common-ui';

import { Descriptions, DescriptionsItem } from 'ant-design-vue';

import { useVbenForm } from '#/adapter/form';
import { resetPassword } from '#/api/auth/user';

const emit = defineEmits<{ reload: [] }>();

const [BasicModal, modalApi] = useVbenModal({
  onClosed: handleClosed,
  onConfirm: handleSubmit,
  onOpenChange: handleOpenChange,
});

const [BasicForm, formApi] = useVbenForm({
  schema: [
    {
      component: 'Input',
      dependencies: {
        show: () => false,
        triggerFields: [''],
      },
      fieldName: 'id',
      label: '用户ID',
      rules: 'required',
    },
    {
      component: 'InputPassword',
      componentProps: {
        placeholder: '请输入新的密码, 密码长度为5 - 20',
      },
      fieldName: 'password',
      label: '新的密码',
      rules: z
        .string()
        .min(5, { message: '密码长度为5 - 20' })
        .max(20, { message: '密码长度为5 - 20' }),
    },
  ],
  showDefaultActions: false,
  commonConfig: {
    labelWidth: 80,
  },
});

const currentUser = ref<null | SysUserApi.User>(null);
async function handleOpenChange(open: boolean) {
  if (!open) {
    return null;
  }
  modalApi.modalLoading(true);

  const data = modalApi.getData<SysUserApi.User>();
  currentUser.value = data;
  await formApi.setValues({ id: data.id });

  modalApi.modalLoading(false);
}

async function handleSubmit() {
  try {
    modalApi.modalLoading(true);
    const { valid } = await formApi.validate();
    if (!valid) {
      return;
    }
    const data = await formApi.getValues();
    await resetPassword(data);
    emit('reload');
    handleClosed();
  } catch (error) {
    console.error(error);
  } finally {
    modalApi.modalLoading(false);
  }
}

async function handleClosed() {
  modalApi.close();
  await formApi.resetForm();
  currentUser.value = null;
}
</script>

<template>
  <BasicModal
    :close-on-click-modal="false"
    :fullscreen-button="false"
    title="重置密码"
  >
    <div class="flex flex-col gap-[12px]">
      <Descriptions v-if="currentUser" size="small" :column="1" bordered>
        <DescriptionsItem label="用户ID">
          {{ currentUser.id }}
        </DescriptionsItem>
        <DescriptionsItem label="用户名">
          {{ currentUser.name }}
        </DescriptionsItem>
        <DescriptionsItem label="昵称">
          {{ currentUser.username }}
        </DescriptionsItem>
      </Descriptions>
      <BasicForm />
    </div>
  </BasicModal>
</template>
