import type { VbenFormSchema } from '#/adapter/form';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { EnergyQuotaApi } from '#/api/energy/quota';

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

import { z } from '#/adapter/form';
import {
  getLineOptions,
  getMachineryOptions,
  getWorkshopOptions,
} from '#/api/energy/meter';

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

/** 新增/修改考核指标的表单 */
export function useFormSchema(formType: FormType): VbenFormSchema[] {
  return [
    {
      fieldName: 'id',
      component: 'Input',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'name',
      label: '指标名称',
      component: 'Input',
      componentProps: {
        placeholder: '如 1#车间用电日定额',
      },
      rules: 'required',
    },
    {
      fieldName: 'energyType',
      label: '能源类型',
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: getDictOptions(DICT_TYPE.MES_EM_ENERGY_TYPE, 'number'),
        placeholder: '请选择能源类型',
      },
      rules: 'selectRequired',
    },
    {
      fieldName: 'objectType',
      label: '考核对象类型',
      component: 'Select',
      componentProps: {
        options: getDictOptions(DICT_TYPE.MES_EM_QUOTA_OBJECT_TYPE, 'number'),
        placeholder: '请选择考核对象类型',
      },
      help: '对象越具体优先级越高：设备 > 产线 > 车间 > 全厂',
      rules: 'selectRequired',
    },
    {
      fieldName: 'workshopId',
      label: '考核车间',
      component: 'ApiSelect',
      componentProps: {
        allowClear: true,
        api: getWorkshopOptions,
        labelField: 'name',
        placeholder: '请选择考核车间',
        valueField: 'id',
      },
      dependencies: {
        triggerFields: ['objectType'],
        show: (values) => values.objectType === 1,
      },
    },
    {
      fieldName: 'lineId',
      label: '考核产线',
      component: 'ApiSelect',
      componentProps: {
        allowClear: true,
        api: getLineOptions,
        labelField: 'name',
        placeholder: '请选择考核产线',
        valueField: 'id',
      },
      dependencies: {
        triggerFields: ['objectType'],
        show: (values) => values.objectType === 2,
      },
    },
    {
      fieldName: 'machineryId',
      label: '考核设备',
      component: 'ApiSelect',
      componentProps: {
        allowClear: true,
        api: getMachineryOptions,
        labelField: 'name',
        placeholder: '请选择考核设备',
        valueField: 'id',
      },
      dependencies: {
        triggerFields: ['objectType'],
        show: (values) => values.objectType === 3,
      },
    },
    {
      fieldName: 'periodType',
      label: '考核周期',
      component: 'Select',
      componentProps: {
        options: getDictOptions(DICT_TYPE.MES_EM_QUOTA_PERIOD, 'number'),
        placeholder: '请选择考核周期',
      },
      help: '日定额按当日用量比较；月定额按当月累计用量比较',
      rules: 'selectRequired',
    },
    {
      fieldName: 'quotaValue',
      label: '定额值',
      component: 'InputNumber',
      componentProps: {
        class: '!w-full',
        min: 0,
        precision: 2,
        placeholder: '请输入定额值',
      },
      rules: 'required',
    },
    {
      fieldName: 'unit',
      label: '计量单位',
      component: 'Input',
      componentProps: {
        placeholder: '如 kWh、t、L、m³',
      },
      help: '选择能源类型后自动带入默认单位',
    },
    {
      fieldName: 'warnRatio',
      label: '预警比例(%)',
      component: 'InputNumber',
      componentProps: {
        class: '!w-full',
        min: 0,
        precision: 2,
        placeholder: '请输入预警比例',
      },
      help: '实际用量超过「定额 × (1 + 比例)」触发警告',
      rules: z.number().default(10),
    },
    {
      fieldName: 'alarmRatio',
      label: '严重预警比例(%)',
      component: 'InputNumber',
      componentProps: {
        class: '!w-full',
        min: 0,
        precision: 2,
        placeholder: '请输入严重预警比例',
      },
      help: '超过该比例时预警级别升级为「严重」',
      rules: z.number().default(20),
    },
    {
      fieldName: 'momWarnRatio',
      label: '环比阈值(%)',
      component: 'InputNumber',
      componentProps: {
        class: '!w-full',
        min: 0,
        precision: 2,
        placeholder: '请输入环比异常阈值',
      },
      help: '较上一周期增幅超过该值时触发环比异常预警',
      rules: z.number().default(30),
    },
    {
      fieldName: 'yoyWarnRatio',
      label: '同比阈值(%)',
      component: 'InputNumber',
      componentProps: {
        class: '!w-full',
        min: 0,
        precision: 2,
        placeholder: '请输入同比异常阈值',
      },
      help: '较去年同期增幅超过该值时触发同比异常预警',
      rules: z.number().default(30),
    },
    {
      fieldName: 'status',
      label: '状态',
      component: 'RadioGroup',
      componentProps: {
        buttonStyle: 'solid',
        optionType: 'button',
        options: getDictOptions(DICT_TYPE.MES_EM_METER_STATUS, 'number'),
      },
      rules: z.number().default(1),
    },
    {
      fieldName: 'remark',
      label: '备注',
      component: 'Textarea',
      formItemClass: 'col-span-3',
      componentProps: {
        placeholder: '请输入备注',
        rows: 3,
        disabled: formType === 'detail',
      },
    },
  ];
}

/** 列表的搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'name',
      label: '指标名称',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '请输入指标名称',
      },
    },
    {
      fieldName: 'energyType',
      label: '能源类型',
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: getDictOptions(DICT_TYPE.MES_EM_ENERGY_TYPE, 'number'),
        placeholder: '请选择能源类型',
      },
    },
    {
      fieldName: 'objectType',
      label: '考核对象类型',
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: getDictOptions(DICT_TYPE.MES_EM_QUOTA_OBJECT_TYPE, 'number'),
        placeholder: '请选择考核对象类型',
      },
    },
    {
      fieldName: 'periodType',
      label: '考核周期',
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: getDictOptions(DICT_TYPE.MES_EM_QUOTA_PERIOD, 'number'),
        placeholder: '请选择考核周期',
      },
    },
    {
      fieldName: 'status',
      label: '状态',
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: getDictOptions(DICT_TYPE.MES_EM_METER_STATUS, 'number'),
        placeholder: '请选择状态',
      },
    },
  ];
}

/** 列表的字段 */
export function useGridColumns(): VxeTableGridOptions<EnergyQuotaApi.Quota>['columns'] {
  return [
    { field: 'name', title: '指标名称', minWidth: 180 },
    {
      field: 'energyType',
      title: '能源类型',
      width: 100,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.MES_EM_ENERGY_TYPE },
      },
    },
    {
      field: 'objectType',
      title: '对象类型',
      width: 100,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.MES_EM_QUOTA_OBJECT_TYPE },
      },
    },
    { field: 'objectName', title: '考核对象', minWidth: 140 },
    {
      field: 'periodType',
      title: '考核周期',
      width: 100,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.MES_EM_QUOTA_PERIOD },
      },
    },
    {
      field: 'quotaValue',
      title: '定额值',
      width: 130,
      align: 'right',
      slots: {
        default: 'quotaValue',
      },
    },
    {
      field: 'warnRatio',
      title: '预警比例',
      width: 100,
      align: 'right',
      formatter: ({ cellValue }) => `${cellValue ?? 0}%`,
    },
    {
      field: 'alarmRatio',
      title: '严重比例',
      width: 100,
      align: 'right',
      formatter: ({ cellValue }) => `${cellValue ?? 0}%`,
    },
    {
      field: 'momWarnRatio',
      title: '环比阈值',
      width: 100,
      align: 'right',
      formatter: ({ cellValue }) => `${cellValue ?? 0}%`,
    },
    {
      field: 'yoyWarnRatio',
      title: '同比阈值',
      width: 100,
      align: 'right',
      formatter: ({ cellValue }) => `${cellValue ?? 0}%`,
    },
    {
      field: 'status',
      title: '状态',
      width: 90,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.MES_EM_METER_STATUS },
      },
    },
    {
      field: 'createTime',
      title: '创建时间',
      width: 180,
      formatter: 'formatDateTime',
    },
    {
      title: '操作',
      width: 170,
      fixed: 'right',
      slots: {
        default: 'actions',
      },
    },
  ];
}
