import type { VbenFormApi, VbenFormSchema } from '#/adapter/form';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';

import { h } from 'vue';

import { DICT_TYPE, MesAutoCodeRuleCode } from '#/packages/constants/src';
import { getDictOptions } from '#/packages/effects/hooks/src';

import { Button } from 'ant-design-vue';

import { generateAutoCode } from '#/api/mes/md/autocode/record';

export type VersionChangeFormType = 'create' | 'detail' | 'update';

export function useVersionChangeGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'code',
      label: '变更单号',
      component: 'Input',
      componentProps: { placeholder: '请输入变更单号' },
    },
    {
      fieldName: 'changeType',
      label: '变更类型',
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: getDictOptions(DICT_TYPE.MES_PD_VERSION_CHANGE_TYPE, 'number'),
        placeholder: '请选择变更类型',
      },
    },
  ];
}

export function useVersionChangeGridColumns(
  showAction = true,
): VxeTableGridOptions['columns'] {
  const columns: VxeTableGridOptions['columns'] = [
    { field: 'code', title: '变更单号', minWidth: 130 },
    {
      field: 'changeType',
      title: '变更类型',
      minWidth: 100,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.MES_PD_VERSION_CHANGE_TYPE },
      },
    },
    { field: 'fromVersion', title: '原版本', minWidth: 90 },
    { field: 'toVersion', title: '新版本', minWidth: 90 },
    { field: 'applicantName', title: '申请人', minWidth: 100 },
    {
      field: 'createTime',
      title: '申请时间',
      minWidth: 160,
      formatter: 'formatDateTime',
    },
  ];

  if (showAction) {
    columns.push({
      field: 'action',
      title: '操作',
      width: 180,
      fixed: 'right',
      slots: { default: 'actions' },
    });
  }

  return columns;
}

export function useVersionChangeFormSchema(
  formType: VersionChangeFormType,
  formApi?: VbenFormApi,
): VbenFormSchema[] {
  const readonly = formType === 'detail';
  return [
    {
      fieldName: 'id',
      component: 'Input',
      dependencies: { triggerFields: [''], show: () => false },
    },
    {
      fieldName: 'projectId',
      component: 'Input',
      dependencies: { triggerFields: [''], show: () => false },
    },
    {
      fieldName: 'code',
      label: '变更单号',
      component: 'Input',
      componentProps: { disabled: readonly, placeholder: '请输入变更单号' },
      rules: 'required',
      suffix:
        formType === 'detail'
          ? undefined
          : () =>
              h(
                Button,
                {
                  type: 'default',
                  onClick: async () => {
                    const code = await generateAutoCode(
                      MesAutoCodeRuleCode.PD_VERSION_CODE,
                    );
                    await formApi?.setFieldValue('code', code);
                  },
                },
                { default: () => '生成' },
              ),
    },
    {
      fieldName: 'changeType',
      label: '变更类型',
      component: 'Select',
      componentProps: {
        disabled: readonly,
        options: getDictOptions(DICT_TYPE.MES_PD_VERSION_CHANGE_TYPE, 'number'),
        placeholder: '请选择变更类型',
      },
      rules: 'selectRequired',
    },
    {
      fieldName: 'fromVersion',
      label: '原版本',
      component: 'Input',
      componentProps: { disabled: readonly, placeholder: '如 V1.0' },
      rules: 'required',
    },
    {
      fieldName: 'toVersion',
      label: '新版本',
      component: 'Input',
      componentProps: { disabled: readonly, placeholder: '如 V1.1' },
      rules: 'required',
    },
    {
      fieldName: 'changeReason',
      label: '变更原因',
      component: 'Textarea',
      componentProps: { disabled: readonly, placeholder: '请输入变更原因', rows: 2 },
      rules: 'required',
    },
    {
      fieldName: 'changeContent',
      label: '变更内容',
      component: 'Textarea',
      componentProps: { disabled: readonly, placeholder: '请输入变更内容', rows: 4 },
      rules: 'required',
    },
    {
      fieldName: 'remark',
      label: '备注',
      component: 'Textarea',
      componentProps: { disabled: readonly, placeholder: '请输入备注', rows: 2 },
    },
  ];
}
