import type { VbenFormSchema } from '#/adapter/form';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { MesPdCtcReelApi } from '#/api/mes/pd/ctc-reel';

import { markRaw } from 'vue';

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

import { PdCtcSelect } from '#/views/mes/pd/ctc/components';

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

/** 新增/修改盘具计算记录 */
export function useFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'id',
      component: 'Input',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'ctcId',
      label: '换位导线产品',
      component: markRaw(PdCtcSelect),
      componentProps: {
        placeholder: '请选择换位导线产品',
      },
      rules: 'selectRequired',
    },
    {
      fieldName: 'reelType',
      label: '盘具类型',
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: getDictOptions(DICT_TYPE.PRODUCT_REEL_TYPE, 'string'),
        placeholder: '请选择盘具类型',
      },
    },
    {
      fieldName: 'reelOuterDiameter',
      label: '盘具外径(mm)',
      component: 'InputNumber',
      componentProps: {
        class: '!w-full',
        min: 0,
        precision: 0,
        placeholder: '请输入盘具外径 d1',
      },
    },
    {
      fieldName: 'reelInnerDiameter',
      label: '盘芯内径(mm)',
      component: 'InputNumber',
      componentProps: {
        class: '!w-full',
        min: 0,
        precision: 0,
        placeholder: '请输入盘芯内径 d2',
      },
    },
    {
      fieldName: 'reelWidth',
      label: '盘宽(mm)',
      component: 'InputNumber',
      componentProps: {
        class: '!w-full',
        min: 0,
        precision: 0,
        placeholder: '请输入盘宽 l2',
      },
    },
    {
      fieldName: 'maxCapacity',
      label: '最大容量(km)',
      component: 'InputNumber',
      componentProps: {
        class: '!w-full',
        min: 0,
        precision: 4,
        placeholder: '请输入最大容量',
      },
    },
    {
      fieldName: 'recommendedLength',
      label: '推荐每盘长度(km)',
      component: 'InputNumber',
      componentProps: {
        class: '!w-full',
        min: 0,
        precision: 2,
        placeholder: '请输入推荐每盘长度',
      },
    },
    {
      fieldName: 'netWeight',
      label: '净重(kg)',
      component: 'InputNumber',
      componentProps: {
        class: '!w-full',
        min: 0,
        precision: 2,
        placeholder: '请输入净重',
      },
    },
    {
      fieldName: 'grossWeight',
      label: '毛重(kg)',
      component: 'InputNumber',
      componentProps: {
        class: '!w-full',
        min: 0,
        precision: 2,
        placeholder: '请输入毛重',
      },
    },
    {
      fieldName: 'calcFormula',
      label: '计算公式',
      component: 'Textarea',
      formItemClass: 'col-span-3',
      componentProps: {
        placeholder: '((d1-60)+(d2+10))×INT(l2/(宽+1))×(d1-d2-70)/(高+0.2)/1274',
        rows: 2,
      },
      help: '容量计算：((d1-60)+(d2+10))×INT(l2/(CTC宽+1))×(d1-d2-70)/(CTC高+0.2)/1274',
    },
    {
      fieldName: 'remark',
      label: '备注',
      component: 'Textarea',
      formItemClass: 'col-span-3',
      componentProps: {
        placeholder: '请输入备注',
        rows: 2,
      },
    },
  ];
}

/** 列表的搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'ctcId',
      label: '换位导线产品',
      component: markRaw(PdCtcSelect),
      componentProps: {
        allowClear: true,
        placeholder: '请选择换位导线产品',
      },
    },
    {
      fieldName: 'reelType',
      label: '盘具类型',
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: getDictOptions(DICT_TYPE.PRODUCT_REEL_TYPE, 'string'),
        placeholder: '请选择盘具类型',
      },
    },
  ];
}

/** 列表的字段 */
export function useGridColumns(): VxeTableGridOptions<MesPdCtcReelApi.CtcReel>['columns'] {
  return [
    { field: 'ctcOrderSpec', title: '换位导线规格', minWidth: 160 },
    {
      field: 'reelType',
      title: '盘具类型',
      width: 100,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.PRODUCT_REEL_TYPE },
      },
    },
    { field: 'reelOuterDiameter', title: '盘具外径(mm)', width: 110 },
    { field: 'reelInnerDiameter', title: '盘芯内径(mm)', width: 110 },
    { field: 'reelWidth', title: '盘宽(mm)', width: 100 },
    { field: 'maxCapacity', title: '最大容量(km)', width: 120 },
    { field: 'recommendedLength', title: '推荐长度(km)', width: 120 },
    { field: 'netWeight', title: '净重(kg)', width: 100 },
    { field: 'grossWeight', title: '毛重(kg)', width: 100 },
    {
      field: 'createTime',
      title: '创建时间',
      width: 170,
      formatter: 'formatDateTime',
    },
    {
      title: '操作',
      width: 220,
      fixed: 'right',
      slots: {
        default: 'actions',
      },
    },
  ];
}
