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

import { ref } from 'vue';

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

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

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

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

const formSchema: VbenFormSchema[] = [
  {
    label: '主键',
    fieldName: 'id',
    component: 'Input',
    dependencies: {
      show: () => false,
      triggerFields: [''],
    },
  },
  {
    component: 'Input',
    fieldName: 'name',
    label: '姓名',
    disabled: true,
  },
  {
    component: 'Input',
    fieldName: 'deptName',
    label: '部门',
    disabled: true,
  },
  {
    component: 'RadioGroup',
    componentProps: {
      buttonStyle: 'solid',
      optionType: 'button',
      options: getDictOptions(HrDictEnum.HR_CONTRACT_FORM),
    },
    fieldName: 'contractForm',
    label: '合同形式',
    rules: 'required',
  },
  {
    component: 'DatePicker',
    componentProps: {
      format: 'YYYY-MM-DD',
      valueFormat: 'YYYY-MM-DD',
      getVxePopupContainer,
    },
    fieldName: 'contractStartDate',
    label: '合同开始时间',
    rules: 'required',
  },
  {
    component: 'DatePicker',
    componentProps: {
      format: 'YYYY-MM-DD',
      valueFormat: 'YYYY-MM-DD',
      getVxePopupContainer,
    },
    fieldName: 'contractEndDate',
    label: '合同截止时间',
    rules: 'required',
  },
  // {
  //   component: 'Input',
  //   fieldName: 'contractEntity',
  //   label: '合同主体',
  //   rules: 'required',
  // },
  {
    component: 'Textarea',
    // componentProps: {
    //   buttonStyle: 'solid',
    //   optionType: 'button',
    // },
    fieldName: 'contractSigningStatus',
    label: '合同签订情况',
    rules: 'required',
  },
  // {
  //   component: 'Input',
  //   fieldName: 'dictType',
  //   label: '字典类型',
  //   rules: 'required',
  // },
  // {
  //   component: 'Input',
  //   fieldName: 'description',
  //   label: '描述',
  //   rules: 'required',
  // },
  {
    component: 'Textarea',
    fieldName: 'remark',
    label: '申请备注',
    formItemClass: 'items-start',
  },
];

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

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

const [BasicModal, modalApi] = useVbenModal({
  fullscreenButton: false,
  onBeforeClose,
  onClosed: handleClosed,
  onConfirm: onSubmit,
  onOpenChange: async (isOpen) => {
    if (!isOpen) {
      return null;
    }
    modalApi.modalLoading(true);

    const data = modalApi.getData() as { id?: number; name?: string };
    if (data) {
      formData.value = data;
      await formApi.setValues(formData.value);
    }
    await markInitialized();

    modalApi.modalLoading(false);
  },
});

async function onSubmit() {
  const { valid } = await formApi.validate();
  if (valid) {
    modalApi.lock();
    const data =
      await formApi.getValues<EmployeeInfoApi.EmployeeRenewalContractApplyBo>();
    try {
      await applyRenewalContract(data);
      resetInitialized();
      emit('success');
      modalApi.close();
    } finally {
      modalApi.unlock();
    }
  }
}

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

// const getModalTitle = computed(() =>
//   formData.value?.id ? '修改字典类型' : '新增字典类型',
// );
</script>
<template>
  <BasicModal title="合同续约申请">
    <BasicForm class="mx-4" />
  </BasicModal>
</template>
