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

import { computed, ref } from 'vue';

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

import { useVbenForm, z } from '#/adapter/form';
import { apiAdd, apiUpdate } from '#/api/hr/phoneBook';

import { getDictOptions } from '#/utils/dict';
import { defaultFormValueGetter, useBeforeCloseDiff } from '#/utils/popup';

const emit = defineEmits<{
  success: [];
}>();
const formData = ref<PhoneBookApi.PhoneBook>();

const formSchema: VbenFormSchema[] = [
  {
    component: 'Input',
    fieldName: 'plate',
    label: '板块',
    rules: 'required',
  },
  {
    component: 'Input',
    fieldName: 'firstLevelDepartment',
    label: '一级部门',
    rules: 'required',
  },
  {
    component: 'Input',
    fieldName: 'secondLevelDepartment',
    label: '二级部门',
    rules: 'required',
  },
  {
    component: 'Input',
    fieldName: 'thirdLevelDepartment',
    label: '三级部门',
    rules: 'required',
  },
  {
    component: 'Input',
    fieldName: 'deptId',
    label: '部门ID',
    rules: 'required',
  },
  {
    component: 'Input',
    fieldName: 'position',
    label: '主岗位',
    rules: 'required',
  },
  {
    component: 'Input',
    fieldName: 'name',
    label: '姓名',
    rules: 'required',
  },
  {
    component: 'Input',
    fieldName: 'phoneNumber',
    label: '手机号码',
    rules: 'required',
  },
  {
    component: 'Input',
    fieldName: 'officePhone',
    label: '办公电话',
    rules: 'required',
  },
  {
    component: 'Input',
    fieldName: 'shortLine',
    label: '短线',
    rules: 'required',
  },
  {
    component: 'Textarea',
    fieldName: 'remarks',
    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<PhoneBookApi.PhoneBook>();
    if (data) {
      formData.value = data;
      await formApi.setValues(formData.value);
    } else {
      formApi.resetForm();
    }
    await markInitialized();
  },
});

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

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

const getDrawerTitle = computed(() =>
  formData.value?.id ? '修改电话本管理' : '新增电话本管理',
);
</script>
<template>
  <Drawer class="w-full max-w-[800px]" :title="getDrawerTitle">
    <BasicForm class="mx-4" />
  </Drawer>
</template>
