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

import { computed, ref } from 'vue';

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

import { useVbenForm } from '#/adapter/form';
import { apiAdd, apiDetail, apiUpdate } from '#/api/hr/employeeFlow';
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: 'applyCode',
    label: '申请编号',
    disabled: true,
    // rules: 'required',
  },
  {
    component: 'Select',
    componentProps: {},
    fieldName: 'flowType',
    label: '审批类型',
    disabled: true,
    // rules: 'selectRequired',
  },
  {
    component: 'Input',
    fieldName: 'employeeId',
    label: '员工信息ID',
    disabled: true,
    // rules: 'required',
  },
  // {
  //   component: 'DatePicker',
  //   fieldName: 'startDate',
  //   label: '开始时间',
  //   // rules: 'required',
  // },
  // {
  //   component: 'DatePicker',
  //   fieldName: 'endDate',
  //   label: '结束时间',
  //   // rules: 'required',
  // },
  // {
  //   component: 'RadioGroup',
  //   componentProps: {
  //     buttonStyle: 'solid',
  //     optionType: 'button',
  //   },
  //   fieldName: 'status',
  //   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 { id } = drawerApi.getData<EmployeeFlowApi.EmployeeFlow>() as {
      id?: number;
    };
    isUpdate.value = !!id;
    if (isUpdate.value && id) {
      const data = await apiDetail(id);
      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<EmployeeFlowApi.EmployeeFlow>();
    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>
