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

import { computed, ref } from 'vue';

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

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

import { HrDictEnum } from '../dict-enum';

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: 'jobPosition',
    label: '招揽岗位',
    rules: 'required',
  },
  {
    component: 'Input',
    fieldName: 'name',
    label: '姓名',
    rules: 'required',
  },
  {
    component: 'RadioGroup',
    componentProps: {
      buttonStyle: 'solid',
      optionType: 'button',
      options: getDictOptions(HrDictEnum.HR_USER_SEX),
    },
    defaultValue: '1',
    fieldName: 'gender',
    label: '性别',
    rules: 'required',
  },
  {
    component: 'Input',
    fieldName: 'idCardNumber',
    label: '身份证号',
    rules: 'required',
  },
  {
    component: 'Input',
    fieldName: 'education',
    label: '学历',
    rules: 'required',
  },
  {
    component: 'Input',
    fieldName: 'benchmarkEnterprise',
    label: '标杆企业',
    rules: 'required',
  },
  {
    component: 'Input',
    fieldName: 'school',
    label: '院校',
    rules: 'required',
  },
  {
    component: 'Input',
    fieldName: 'major',
    label: '专业',
    rules: 'required',
  },
  {
    component: 'Input',
    fieldName: 'phoneNumber',
    label: '联系方式',
    rules: 'required',
  },
  {
    component: 'DatePicker',
    fieldName: 'interviewDate',
    label: '面试时间',
    rules: 'required',
  },
  {
    component: 'Input',
    fieldName: 'interviewSituation',
    label: '面试情况',
    rules: 'required',
  },
  {
    component: 'Select',
    componentProps: {
      getVxePopupContainer,
      options: getDictOptions(HrDictEnum.HR_INTERVIEW_RESULT),
    },
    fieldName: 'interviewResult',
    label: '面试结果',
    rules: 'selectRequired',
  },
  {
    component: 'Input',
    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<InterviewProfileApi.InterviewProfile>();
    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<InterviewProfileApi.InterviewProfile>();
    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>
