<script lang="ts" setup>
import type { VbenFormSchema } from '#/adapter/form';
import type { TrainingApi } from '#/api/hr/training';

import { computed, ref } from 'vue';

import { useVbenDrawer } from '@vben/common-ui';

import { useVbenForm } from '#/adapter/form';
import { apiAdd, apiUpdate } from '#/api/hr/training';
import { defaultFormValueGetter, useBeforeCloseDiff } from '#/utils/popup';

const emit = defineEmits<{
  success: [];
}>();
const isUpdate = ref(false);

const formSchema: VbenFormSchema[] = [
  {
    component: 'Input',
    dependencies: {
      show: () => false,
      triggerFields: [''],
    },
    fieldName: 'id',
    label: 'id',
  },
  // {
  //   component: 'Input',
  //   fieldName: 'employeeId',
  //   label: '员工信息ID',
  //   rules: 'required',
  // },
  {
    component: 'Input',
    fieldName: 'name',
    label: '姓名',
    rules: 'required',
  },
  {
    component: 'Input',
    fieldName: 'idCardNumber',
    label: '身份证号',
    rules: 'required',
  },
  {
    component: 'Input',
    fieldName: 'trainingCourse',
    label: '培训课程',
    rules: 'required',
  },
  {
    component: 'Input',
    fieldName: 'trainingInstitution',
    label: '培训机构',
    rules: 'required',
  },
  {
    component: 'Input',
    fieldName: 'trainer',
    label: '培训讲师',
    // rules: 'required',
  },
  {
    component: 'DatePicker',
    fieldName: 'trainingTime',
    label: '参训时间',
    rules: 'required',
  },
  {
    component: 'DatePicker',
    fieldName: 'serviceEndDate',
    label: '服务期',
    rules: 'required',
  },
  {
    component: 'Input',
    fieldName: 'serviceYears',
    label: '服务年限',
    rules: 'required',
  },
  {
    component: 'Input',
    fieldName: 'trainingAmount',
    label: '培训金额',
    rules: 'required',
  },
  {
    component: 'Textarea',
    fieldName: 'remark',
    label: '备注',
    // rules: 'required',
  },
];

const [BasicForm, formApi] = useVbenForm({
  commonConfig: {
    componentProps: {
      class: 'w-full',
    },
    formItemClass: 'col-span-2',
    labelWidth: 90,
  },
  schema: formSchema,
  showDefaultActions: false,
  wrapperClass: 'grid-cols-2',
});

const { onBeforeClose, markInitialized, resetInitialized } = useBeforeCloseDiff(
  {
    initializedGetter: defaultFormValueGetter(formApi),
    currentGetter: defaultFormValueGetter(formApi),
  },
);

const [Drawer, drawerApi] = useVbenDrawer({
  onBeforeClose,
  onClosed: handleClosed,
  onConfirm: onSubmit,
  async onOpenChange(isOpen) {
    if (!isOpen) {
      return null;
    }
    const data = drawerApi.getData<TrainingApi.Training>();
    isUpdate.value = !!data?.id;
    if (isUpdate.value) {
      await formApi.setValues(data);
    } else {
      formApi.resetForm();
    }
    await markInitialized();
  },
});

async function onSubmit() {
  const { valid } = await formApi.validate();
  if (valid) {
    drawerApi.lock();
    const data = await formApi.getValues<TrainingApi.Training>();
    try {
      await (isUpdate.value ? apiUpdate(data) : apiAdd(data));
      resetInitialized();
      emit('success');
      drawerApi.close();
    } finally {
      drawerApi.unlock();
    }
  }
}

async function handleClosed() {
  await formApi.resetForm();
  resetInitialized();
}

const getDrawerTitle = computed(() =>
  isUpdate.value ? '修改员工培训档案' : '新增员工培训档案',
);
</script>
<template>
  <Drawer class="w-full max-w-[800px]" :title="getDrawerTitle">
    <BasicForm class="mx-4" />
  </Drawer>
</template>
