import type { VbenFormSchema } from '#/adapter/form';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { QcReportTemplateApi } from '#/api/mes/qc/report/template';

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

import { DEFAULT_PAGE } from './designer/constants';

/** 新增/修改的表单 */
export function useFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'id',
      component: 'Input',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'templateCode',
      label: '模板编码',
      component: 'Input',
      componentProps: {
        placeholder: '请输入模板编码（字母/数字/下划线/中划线）',
        maxlength: 64,
      },
      rules: 'required',
    },
    {
      fieldName: 'templateName',
      label: '模板名称',
      component: 'Input',
      componentProps: {
        placeholder: '请输入模板名称',
        maxlength: 128,
      },
      rules: 'required',
    },
    {
      fieldName: 'industry',
      label: '适用行业',
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: getDictOptions(DICT_TYPE.QC_REPORT_INDUSTRY, 'string'),
        placeholder: '请选择适用行业',
      },
    },
    {
      fieldName: 'reportType',
      label: '报告类型',
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: getDictOptions(DICT_TYPE.MES_QC_TYPE, 'string'),
        placeholder: '请选择报告类型',
      },
    },
    {
      fieldName: 'pageSize',
      label: '纸张尺寸',
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: getDictOptions(DICT_TYPE.QC_REPORT_PAGE_SIZE, 'string'),
        placeholder: '请选择纸张尺寸',
      },
      // 绝大多数质检报告都是 A4 纵向，默认带出，省得每次选
      defaultValue: DEFAULT_PAGE.size,
    },
    {
      fieldName: 'orientation',
      label: '纸张方向',
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: getDictOptions(DICT_TYPE.QC_REPORT_ORIENTATION, 'string'),
        placeholder: '请选择纸张方向',
      },
      defaultValue: DEFAULT_PAGE.orientation,
    },
    {
      fieldName: 'status',
      label: '模板状态',
      component: 'RadioGroup',
      componentProps: {
        buttonStyle: 'solid',
        optionType: 'button',
        options: getDictOptions(DICT_TYPE.QC_REPORT_TEMPLATE_STATUS, 'number'),
      },
      defaultValue: 0,
      rules: 'required',
    },
    {
      fieldName: 'description',
      label: '模板说明',
      component: 'Textarea',
      componentProps: {
        placeholder: '请输入模板说明',
        rows: 3,
        maxlength: 255,
        showCount: true,
      },
    },
  ];
}

/** 列表的搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'templateCode',
      label: '模板编码',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '请输入模板编码',
      },
    },
    {
      fieldName: 'templateName',
      label: '模板名称',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '请输入模板名称',
      },
    },
    {
      fieldName: 'industry',
      label: '适用行业',
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: getDictOptions(DICT_TYPE.QC_REPORT_INDUSTRY, 'string'),
        placeholder: '请选择适用行业',
      },
    },
    {
      fieldName: 'reportType',
      label: '报告类型',
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: getDictOptions(DICT_TYPE.MES_QC_TYPE, 'string'),
        placeholder: '请选择报告类型',
      },
    },
    {
      fieldName: 'status',
      label: '模板状态',
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: getDictOptions(DICT_TYPE.QC_REPORT_TEMPLATE_STATUS, 'number'),
        placeholder: '请选择模板状态',
      },
    },
  ];
}

/** 列表的字段 */
export function useGridColumns(): VxeTableGridOptions<QcReportTemplateApi.Template>['columns'] {
  return [
    {
      field: 'templateCode',
      title: '模板编码',
      width: 160,
    },
    {
      field: 'templateName',
      title: '模板名称',
      minWidth: 200,
    },
    {
      field: 'industry',
      title: '适用行业',
      width: 120,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.QC_REPORT_INDUSTRY },
      },
    },
    {
      field: 'reportType',
      title: '报告类型',
      width: 140,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.MES_QC_TYPE },
      },
    },
    {
      field: 'pageSize',
      title: '纸张',
      width: 100,
      slots: { default: 'pageSetting' },
    },
    {
      field: 'currentVersion',
      title: '当前版本',
      width: 110,
      slots: { default: 'currentVersion' },
    },
    {
      field: 'status',
      title: '状态',
      width: 100,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.QC_REPORT_TEMPLATE_STATUS },
      },
    },
    {
      field: 'createTime',
      title: '创建时间',
      width: 180,
      formatter: 'formatDate',
    },
    {
      title: '操作',
      width: 280,
      fixed: 'right',
      slots: { default: 'actions' },
    },
  ];
}