import type { VbenFormSchema } from '#/adapter/form';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { ArchiveDocumentApi } from '#/api/archive/document';

import { DICT_TYPE } from '@vben/constants';
import { getDictOptions } from '@vben/hooks';
import { handleTree } from '@vben/utils';

import { getCategoryList } from '#/api/archive/category';

/** 表单类型 */
export type FormType = 'create' | 'detail' | 'edit';

/** 档案表单配置 */
export function useFormSchema(formType: FormType): VbenFormSchema[] {
  const readonly = formType === 'detail';
  // 档案编号、来源、状态、归档人由后端维护，仅编辑/详情可见
  const showBackendFields = formType !== 'create';

  return [
    {
      fieldName: 'id',
      component: 'Input',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'no',
      label: '档案编号',
      component: 'Input',
      dependencies: {
        triggerFields: [''],
        show: () => showBackendFields,
      },
      componentProps: {
        disabled: true,
        placeholder: '系统自动生成',
      },
    },
    {
      fieldName: 'status',
      label: '档案状态',
      component: 'Select',
      dependencies: {
        triggerFields: [''],
        show: () => showBackendFields,
      },
      componentProps: {
        options: getDictOptions(DICT_TYPE.ARCHIVE_DOCUMENT_STATUS, 'number'),
        disabled: true,
      },
    },
    {
      fieldName: 'title',
      label: '档案标题',
      component: 'Input',
      componentProps: {
        allowClear: true,
        disabled: readonly,
        placeholder: '请输入档案标题',
      },
      rules: 'required',
      formItemClass: 'col-span-2',
    },
    {
      fieldName: 'categoryId',
      label: '档案分类',
      component: 'ApiTreeSelect',
      componentProps: {
        allowClear: true,
        api: async () => handleTree(await getCategoryList({ status: 0 })),
        fieldNames: { label: 'name', value: 'id', children: 'children' },
        placeholder: '请选择档案分类',
        showSearch: true,
        treeDefaultExpandAll: true,
        disabled: readonly,
      },
      rules: 'selectRequired',
    },
    {
      fieldName: 'secretLevel',
      label: '密级',
      component: 'Select',
      componentProps: {
        options: getDictOptions(DICT_TYPE.ARCHIVE_SECRET_LEVEL, 'number'),
        placeholder: '请选择密级',
        disabled: readonly,
      },
      rules: 'selectRequired',
    },
    {
      fieldName: 'carrierType',
      label: '载体',
      component: 'Select',
      componentProps: {
        options: getDictOptions(DICT_TYPE.ARCHIVE_CARRIER_TYPE, 'number'),
        placeholder: '请选择载体',
        disabled: readonly,
      },
      rules: 'selectRequired',
    },
    {
      fieldName: 'archiveDate',
      label: '归档日期',
      component: 'DatePicker',
      componentProps: {
        allowClear: true,
        format: 'YYYY-MM-DD',
        valueFormat: 'YYYY-MM-DD',
        placeholder: '请选择归档日期',
        disabled: readonly,
      },
      rules: 'required',
    },
    {
      fieldName: 'shelfNo',
      label: '书架号',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '如 A架',
        disabled: readonly,
      },
    },
    {
      fieldName: 'layerNo',
      label: '层号',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '如 2层',
        disabled: readonly,
      },
    },
    {
      fieldName: 'columnNo',
      label: '列号',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '如 3列',
        disabled: readonly,
      },
    },
    {
      fieldName: 'boxNo',
      label: '册号/档案盒号',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '如 档案盒-001',
        disabled: readonly,
      },
    },
    {
      fieldName: 'storePlace',
      label: '存放位置',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '如 3号库房A架2层',
        disabled: readonly,
      },
      formItemClass: 'col-span-2',
    },
    {
      fieldName: 'tags',
      label: '关键词',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '多个关键词用英文逗号分隔，便于检索',
        disabled: readonly,
      },
      formItemClass: 'col-span-2',
    },
    {
      fieldName: 'summary',
      label: '摘要',
      component: 'Textarea',
      componentProps: {
        rows: 2,
        placeholder: '请输入摘要',
        disabled: readonly,
      },
      formItemClass: 'col-span-2',
    },
    {
      fieldName: 'sourceType',
      label: '来源类型',
      component: 'Select',
      dependencies: {
        triggerFields: [''],
        show: () => showBackendFields,
      },
      componentProps: {
        options: getDictOptions(DICT_TYPE.ARCHIVE_SOURCE_TYPE, 'number'),
        disabled: true,
      },
    },
    {
      fieldName: 'sourceNo',
      label: '来源单号',
      component: 'Input',
      dependencies: {
        triggerFields: [''],
        show: () => showBackendFields,
      },
      componentProps: {
        disabled: true,
      },
    },
    {
      fieldName: 'archivistName',
      label: '归档人',
      component: 'Input',
      dependencies: {
        triggerFields: [''],
        show: () => showBackendFields,
      },
      componentProps: {
        disabled: true,
      },
    },
    {
      fieldName: 'remark',
      label: '备注',
      component: 'Textarea',
      componentProps: {
        rows: 2,
        placeholder: '请输入备注',
        disabled: readonly,
      },
      formItemClass: 'col-span-2',
    },
    {
      fieldName: 'blobIds',
      label: '电子文件',
      component: 'FileUpload',
      componentProps: {
        valueKey: 'id',
        maxNumber: 10,
        multiple: true,
        showDescription: !readonly,
        disabled: readonly,
      },
      formItemClass: 'col-span-2',
    },
  ];
}

/** 列表的搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'no',
      label: '档案编号',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '请输入档案编号',
      },
    },
    {
      fieldName: 'title',
      label: '档案标题',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '请输入档案标题',
      },
    },
    {
      fieldName: 'categoryId',
      label: '档案分类',
      component: 'ApiTreeSelect',
      componentProps: {
        allowClear: true,
        api: async () => handleTree(await getCategoryList({ status: 0 })),
        fieldNames: { label: 'name', value: 'id', children: 'children' },
        placeholder: '请选择档案分类',
        showSearch: true,
        treeDefaultExpandAll: true,
      },
    },
    {
      fieldName: 'secretLevel',
      label: '密级',
      component: 'Select',
      componentProps: {
        options: getDictOptions(DICT_TYPE.ARCHIVE_SECRET_LEVEL, 'number'),
        allowClear: true,
        placeholder: '请选择密级',
      },
    },
    {
      fieldName: 'carrierType',
      label: '载体',
      component: 'Select',
      componentProps: {
        options: getDictOptions(DICT_TYPE.ARCHIVE_CARRIER_TYPE, 'number'),
        allowClear: true,
        placeholder: '请选择载体',
      },
    },
    {
      fieldName: 'sourceType',
      label: '来源类型',
      component: 'Select',
      componentProps: {
        options: getDictOptions(DICT_TYPE.ARCHIVE_SOURCE_TYPE, 'number'),
        allowClear: true,
        placeholder: '请选择来源类型',
      },
    },
    {
      fieldName: 'sourceNo',
      label: '来源单号',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '请输入来源单号',
      },
    },
    {
      fieldName: 'tags',
      label: '关键词',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '请输入关键词',
      },
    },
    {
      fieldName: 'status',
      label: '档案状态',
      component: 'Select',
      componentProps: {
        options: getDictOptions(DICT_TYPE.ARCHIVE_DOCUMENT_STATUS, 'number'),
        allowClear: true,
        placeholder: '请选择档案状态',
      },
    },
    {
      fieldName: 'archiveDate',
      label: '归档日期',
      component: 'RangePicker',
      componentProps: {
        allowClear: true,
        format: 'YYYY-MM-DD',
        valueFormat: 'YYYY-MM-DD',
        placeholder: ['开始日期', '结束日期'],
      },
    },
  ];
}

/** 表格列配置 */
export function useGridColumns(): VxeTableGridOptions<ArchiveDocumentApi.Document>['columns'] {
  return [
    {
      field: 'no',
      title: '档案编号',
      minWidth: 170,
    },
    {
      field: 'title',
      title: '档案标题',
      minWidth: 220,
    },
    {
      field: 'categoryName',
      title: '档案分类',
      minWidth: 120,
    },
    {
      field: 'secretLevel',
      title: '密级',
      minWidth: 90,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.ARCHIVE_SECRET_LEVEL },
      },
    },
    {
      field: 'carrierType',
      title: '载体',
      minWidth: 110,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.ARCHIVE_CARRIER_TYPE },
      },
    },
    {
      field: 'sourceType',
      title: '来源',
      minWidth: 90,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.ARCHIVE_SOURCE_TYPE },
      },
    },
    {
      field: 'sourceNo',
      title: '来源单号',
      minWidth: 160,
    },
    {
      field: 'storePlace',
      title: '存放位置',
      minWidth: 170,
    },
    {
      field: 'archiveDate',
      title: '归档日期',
      minWidth: 120,
    },
    {
      field: 'status',
      title: '档案状态',
      minWidth: 100,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.ARCHIVE_DOCUMENT_STATUS },
      },
    },
    {
      field: 'hasAttachment',
      title: '电子文件',
      minWidth: 100,
      slots: { default: 'hasAttachment' },
    },
    {
      title: '操作',
      width: 200,
      fixed: 'right',
      slots: { default: 'actions' },
    },
  ];
}
