import type { VbenFormSchema } from '#/adapter/form';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { EnergyStationPriceApi } from '#/api/energy/station-price';

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

export const PRICE_STATUS_OPTIONS = [
  { label: '生效中', value: 1, color: 'green' },
  { label: '已过期', value: 0, color: 'default' },
];

/** 新增/修改的表单 */
export function useFormSchema(): VbenFormSchema[] {
  return [
    {
      component: 'Input',
      fieldName: 'id',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'stationId',
      label: '加氢站',
      component: 'InputNumber',
      componentProps: {
        placeholder: '请输入加氢站ID',
        style: { width: '100%' },
      },
      rules: 'required',
    },
    {
      fieldName: 'customerId',
      label: '客户',
      component: 'InputNumber',
      componentProps: {
        placeholder: '请输入客户ID',
        style: { width: '100%' },
      },
      rules: 'required',
    },
    {
      fieldName: 'costPrice',
      label: '成本价(元/KG)',
      component: 'InputNumber',
      componentProps: {
        placeholder: '请输入成本价',
        min: 0,
        precision: 2,
        style: { width: '100%' },
      },
      rules: 'required',
    },
    {
      fieldName: 'customerPrice',
      label: '客户价(元/KG)',
      component: 'InputNumber',
      componentProps: {
        placeholder: '请输入客户价',
        min: 0,
        precision: 2,
        style: { width: '100%' },
      },
      rules: 'required',
    },
    {
      fieldName: 'effectiveDate',
      label: '生效日期',
      component: 'DatePicker',
      componentProps: {
        placeholder: '请选择生效日期',
        format: 'YYYY-MM-DD',
        valueFormat: 'YYYY-MM-DD',
      },
      rules: 'required',
    },
    {
      fieldName: 'expiryDate',
      label: '过期日期',
      component: 'DatePicker',
      componentProps: {
        placeholder: '请选择过期日期（可选）',
        format: 'YYYY-MM-DD',
        valueFormat: 'YYYY-MM-DD',
      },
    },
  ];
}

/** 搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'stationName',
      label: '站点名称',
      component: 'Input',
      componentProps: {
        placeholder: '请输入站点名称',
        allowClear: true,
      },
    },
    {
      fieldName: 'customerName',
      label: '客户名称',
      component: 'Input',
      componentProps: {
        placeholder: '请输入客户名称',
        allowClear: true,
      },
    },
    {
      fieldName: 'status',
      label: '状态',
      component: 'Select',
      componentProps: {
        options: PRICE_STATUS_OPTIONS,
        placeholder: '请选择状态',
        allowClear: true,
      },
    },
  ];
}

/** 表格列配置 */
export function useGridColumns(): VxeTableGridOptions<EnergyStationPriceApi.Price>['columns'] {
  return [
    {
      field: 'stationName',
      title: '站点名称',
      minWidth: 150,
    },
    {
      field: 'customerName',
      title: '客户名称',
      minWidth: 150,
    },
    {
      field: 'costPrice',
      title: '成本价',
      minWidth: 100,
      align: 'right',
      slots: { default: 'costPrice' },
    },
    {
      field: 'customerPrice',
      title: '客户价',
      minWidth: 100,
      align: 'right',
      slots: { default: 'customerPrice' },
    },
    {
      field: 'effectiveDate',
      title: '生效日期',
      minWidth: 120,
      formatter: 'formatDate',
    },
    {
      field: 'expiryDate',
      title: '过期日期',
      minWidth: 120,
      slots: { default: 'expiryDate' },
    },
    {
      field: 'status',
      title: '状态',
      minWidth: 100,
      slots: { default: 'status' },
    },
    {
      field: 'createTime',
      title: '创建时间',
      minWidth: 180,
      formatter: 'formatDateTime',
    },
    useActionColumn(2),
  ];
}
