import type { VbenFormSchema } from '#/adapter/form';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { EnergyHydrogenRecordApi } from '#/api/energy/hydrogen-record';

import { useActionColumn } from '#/utils/table';
import { getRangePickerDefaultProps } from '#/utils';

export const SOURCE_TYPE_OPTIONS = [
  { label: 'Excel导入', value: 1, color: 'blue' },
  { label: 'API同步', value: 2, color: 'green' },
  { label: '手动录入', value: 3, color: 'default' },
];

export const MATCH_STATUS_OPTIONS = [
  { label: '待匹配', value: 0, color: 'orange' },
  { label: '已匹配', value: 1, color: 'green' },
  { label: '匹配失败', value: 2, color: 'red' },
];

/** 搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'stationId',
      label: '加氢站',
      component: 'Input',
      componentProps: {
        placeholder: '请输入加氢站',
        allowClear: true,
      },
    },
    {
      fieldName: 'plateNumber',
      label: '车牌号',
      component: 'Input',
      componentProps: {
        placeholder: '请输入车牌号',
        allowClear: true,
      },
    },
    {
      fieldName: 'hydrogenDate',
      label: '加氢日期',
      component: 'RangePicker',
      componentProps: {
        ...getRangePickerDefaultProps(),
        class: 'w-full',
      },
    },
    {
      fieldName: 'matchStatus',
      label: '匹配状态',
      component: 'Select',
      componentProps: {
        options: MATCH_STATUS_OPTIONS,
        placeholder: '请选择匹配状态',
        allowClear: true,
      },
    },
    {
      fieldName: 'sourceType',
      label: '数据来源',
      component: 'Select',
      componentProps: {
        options: SOURCE_TYPE_OPTIONS,
        placeholder: '请选择数据来源',
        allowClear: true,
      },
    },
  ];
}

/** 表格列配置 */
export function useGridColumns(): VxeTableGridOptions<EnergyHydrogenRecordApi.Record>['columns'] {
  return [
    {
      field: 'stationName',
      title: '站点名称',
      minWidth: 150,
      fixed: 'left',
    },
    {
      field: 'plateNumber',
      title: '车牌号',
      minWidth: 120,
    },
    {
      field: 'hydrogenDate',
      title: '加氢日期',
      minWidth: 120,
      formatter: 'formatDate',
      align: 'center',
    },
    {
      field: 'hydrogenQuantity',
      title: '加氢量(KG)',
      minWidth: 100,
      align: 'right',
    },
    {
      field: 'unitPrice',
      title: '单价',
      minWidth: 80,
      align: 'right',
    },
    {
      field: 'amount',
      title: '金额',
      minWidth: 100,
      align: 'right',
    },
    {
      field: 'mileage',
      title: '里程数',
      minWidth: 80,
      align: 'right',
    },
    {
      field: 'sourceType',
      title: '数据来源',
      minWidth: 100,
      align: 'center',
      slots: { default: 'sourceType' },
    },
    {
      field: 'matchStatus',
      title: '匹配状态',
      minWidth: 100,
      align: 'center',
      slots: { default: 'matchStatus' },
    },
    useActionColumn(2),
  ];
}
