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

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

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

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

/** 能源类型默认计量单位与折标煤系数 */
export const ENERGY_DEFAULTS: Record<
  number,
  { coalFactor: number; unit: string }
> = {
  1: { coalFactor: 0.1229, unit: 'kWh' },
  2: { coalFactor: 0.2429, unit: 't' },
  3: { coalFactor: 1.2, unit: 'L' },
  4: { coalFactor: 1.33, unit: 'm³' },
};

/** 新增/修改计量点的表单 */
export function useFormSchema(formType: FormType): VbenFormSchema[] {
  return [
    {
      fieldName: 'id',
      component: 'Input',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'code',
      label: '计量点编码',
      component: 'Input',
      componentProps: {
        placeholder: '请输入计量点编码，如 EM-DL-001',
      },
      dependencies: {
        triggerFields: ['id'],
        componentProps: (values) => ({ disabled: !!values.id }),
      },
      rules: 'required',
    },
    {
      fieldName: 'name',
      label: '计量点名称',
      component: 'Input',
      componentProps: {
        placeholder: '请输入计量点名称',
      },
      rules: 'required',
    },
    {
      fieldName: 'energyType',
      label: '能源类型',
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: getDictOptions(DICT_TYPE.MES_EM_ENERGY_TYPE, 'number'),
        placeholder: '请选择能源类型',
      },
      help: '切换能源类型后，计量单位与折标煤系数会自动带入默认值',
      rules: 'selectRequired',
    },
    {
      fieldName: 'unit',
      label: '计量单位',
      component: 'Input',
      componentProps: {
        placeholder: '如 kWh、t、L、m³',
      },
      rules: 'required',
    },
    {
      fieldName: 'meterNo',
      label: '表计编号',
      component: 'Input',
      componentProps: {
        placeholder: '请输入现场表计编号',
      },
    },
    {
      fieldName: 'workshopId',
      label: '所属车间',
      component: 'ApiSelect',
      componentProps: {
        allowClear: true,
        api: getWorkshopOptions,
        labelField: 'name',
        placeholder: '请选择所属车间',
        valueField: 'id',
      },
    },
    {
      fieldName: 'lineId',
      label: '所属产线',
      component: 'ApiSelect',
      componentProps: {
        allowClear: true,
        api: getLineOptions,
        labelField: 'name',
        placeholder: '请选择所属产线',
        valueField: 'id',
      },
    },
    {
      fieldName: 'machineryId',
      label: '关联设备',
      component: 'ApiSelect',
      componentProps: {
        allowClear: true,
        api: getMachineryOptions,
        labelField: 'name',
        placeholder: '请选择重点用能设备',
        valueField: 'id',
      },
      help: '选择设备后，若未选车间，系统会按设备归属自动补齐车间',
    },
    {
      fieldName: 'ratio',
      label: '倍率',
      component: 'InputNumber',
      componentProps: {
        class: '!w-full',
        min: 0,
        precision: 4,
        placeholder: '请输入倍率',
      },
      help: '用量 = (期末读数 - 期初读数) × 倍率',
      rules: z.number().default(1),
    },
    {
      fieldName: 'initialReading',
      label: '初始读数',
      component: 'InputNumber',
      componentProps: {
        class: '!w-full',
        min: 0,
        precision: 2,
        placeholder: '请输入初始读数',
      },
      help: '第一条抄表记录的期初读数',
      rules: z.number().default(0),
    },
    {
      fieldName: 'coalFactor',
      label: '折标煤系数',
      component: 'InputNumber',
      componentProps: {
        class: '!w-full',
        min: 0,
        precision: 4,
        placeholder: '请输入折标煤系数',
      },
      help: '吨标煤/计量单位，默认按能源类型带入',
    },
    {
      fieldName: 'leakThreshold',
      label: '跑冒滴漏阈值',
      component: 'InputNumber',
      componentProps: {
        class: '!w-full',
        min: 0,
        precision: 2,
        placeholder: '请输入单日用量阈值',
      },
      help: '单日用量超过该值时触发「疑似跑冒滴漏」预警；留空则不检测',
    },
    {
      fieldName: 'chargeUserId',
      label: '责任人',
      component: 'ApiSelect',
      componentProps: {
        allowClear: true,
        api: getSimpleUserList,
        labelField: 'nickname',
        placeholder: '请选择责任人',
        valueField: 'id',
      },
    },
    {
      fieldName: 'installDate',
      label: '安装日期',
      component: 'DatePicker',
      componentProps: {
        class: '!w-full',
        disabled: formType === 'detail',
        format: 'YYYY-MM-DD',
        placeholder: '请选择安装日期',
        valueFormat: 'YYYY-MM-DD',
      },
    },
    {
      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,
      },
    },
  ];
}

/** 列表的搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'code',
      label: '计量点编码',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '请输入计量点编码',
      },
    },
    {
      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: 'workshopId',
      label: '所属车间',
      component: 'ApiSelect',
      componentProps: {
        allowClear: true,
        api: getWorkshopOptions,
        labelField: 'name',
        placeholder: '请选择所属车间',
        valueField: 'id',
      },
    },
    {
      fieldName: 'status',
      label: '状态',
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: getDictOptions(DICT_TYPE.MES_EM_METER_STATUS, 'number'),
        placeholder: '请选择状态',
      },
    },
  ];
}

/** 列表的字段 */
export function useGridColumns(): VxeTableGridOptions<EnergyMeterApi.Meter>['columns'] {
  return [
    {
      field: 'code',
      title: '计量点编码',
      minWidth: 140,
      slots: {
        default: 'code',
      },
    },
    { field: 'name', title: '计量点名称', minWidth: 170 },
    {
      field: 'energyType',
      title: '能源类型',
      width: 100,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.MES_EM_ENERGY_TYPE },
      },
    },
    { field: 'unit', title: '单位', width: 80 },
    { field: 'meterNo', title: '表计编号', width: 120 },
    { field: 'workshopName', title: '所属车间', width: 120 },
    { field: 'lineName', title: '所属产线', width: 120 },
    { field: 'machineryName', title: '关联设备', minWidth: 140 },
    {
      field: 'ratio',
      title: '倍率',
      width: 90,
      align: 'right',
    },
    {
      field: 'coalFactor',
      title: '折标煤系数',
      width: 110,
      align: 'right',
    },
    {
      field: 'leakThreshold',
      title: '滴漏阈值',
      width: 100,
      align: 'right',
    },
    { field: 'chargeUserName', title: '责任人', width: 100 },
    {
      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',
      },
    },
  ];
}
