import type { ProColumns } from '@ant-design/pro-components';
import type { VehicleItem, VehicleStatus, VehicleType } from './data';

type IntlLike = {
  formatMessage: (descriptor: { id: string; defaultMessage: string }) => string;
};

export const getStatusValueEnum = (
  intl: IntlLike,
): Record<VehicleStatus, { text: string }> => ({
  active: {
    text: intl.formatMessage({
      id: 'vehicle.status.active',
      defaultMessage: '在运营',
    }),
  },
  inactive: {
    text: intl.formatMessage({
      id: 'vehicle.status.inactive',
      defaultMessage: '已停用',
    }),
  },
  maintenance: {
    text: intl.formatMessage({
      id: 'vehicle.status.maintenance',
      defaultMessage: '维护中',
    }),
  },
  retired: {
    text: intl.formatMessage({
      id: 'vehicle.status.retired',
      defaultMessage: '已退役',
    }),
  },
});

export const getTypeValueEnum = (
  intl: IntlLike,
): Record<VehicleType, { text: string }> => ({
  car: {
    text: intl.formatMessage({
      id: 'vehicle.type.car',
      defaultMessage: '轿车',
    }),
  },
  truck: {
    text: intl.formatMessage({
      id: 'vehicle.type.truck',
      defaultMessage: '卡车',
    }),
  },
  van: {
    text: intl.formatMessage({
      id: 'vehicle.type.van',
      defaultMessage: '厢式车',
    }),
  },
  bus: {
    text: intl.formatMessage({
      id: 'vehicle.type.bus',
      defaultMessage: '客车',
    }),
  },
  other: {
    text: intl.formatMessage({
      id: 'vehicle.type.other',
      defaultMessage: '其他',
    }),
  },
});

export type VehicleColumnsParams = {
  intl: IntlLike;
  statusEnum: Record<VehicleStatus, { text: string }>;
  typeEnum: Record<VehicleType, { text: string }>;
  onEdit: (record: VehicleItem) => void;
  onDelete: (record: VehicleItem) => void;
};

export const createVehicleColumns = ({
  intl,
  statusEnum,
  typeEnum,
  onEdit,
  onDelete,
}: VehicleColumnsParams): ProColumns<VehicleItem>[] => {
  return [
    {
      title: intl.formatMessage({
        id: 'vehicle.columns.plateNumber',
        defaultMessage: '车牌号',
      }),
      dataIndex: 'plateNumber',
      width: 160,
    },
    {
      title: intl.formatMessage({
        id: 'vehicle.columns.brand',
        defaultMessage: '品牌',
      }),
      dataIndex: 'brand',
      width: 140,
    },
    {
      title: intl.formatMessage({
        id: 'vehicle.columns.model',
        defaultMessage: '车型',
      }),
      dataIndex: 'model',
      width: 160,
    },
    {
      title: intl.formatMessage({
        id: 'vehicle.columns.type',
        defaultMessage: '车辆类型',
      }),
      dataIndex: 'type',
      width: 120,
      valueEnum: typeEnum,
    },
    {
      title: intl.formatMessage({
        id: 'vehicle.columns.status',
        defaultMessage: '状态',
      }),
      dataIndex: 'status',
      width: 120,
      valueEnum: statusEnum,
    },
    {
      title: intl.formatMessage({
        id: 'vehicle.columns.ownerName',
        defaultMessage: '车主/使用人',
      }),
      dataIndex: 'ownerName',
      width: 160,
      ellipsis: true,
    },
    {
      title: intl.formatMessage({
        id: 'vehicle.columns.createdAt',
        defaultMessage: '创建时间',
      }),
      dataIndex: 'createdAt',
      width: 180,
      valueType: 'dateTime',
      hideInSearch: true,
    },
    {
      title: intl.formatMessage({
        id: 'common.action',
        defaultMessage: '操作',
      }),
      valueType: 'option',
      width: 160,
      render: (_, record) => [
        <a key="edit" onClick={() => onEdit(record)}>
          {intl.formatMessage({ id: 'common.edit', defaultMessage: '编辑' })}
        </a>,
        <a key="delete" onClick={() => onDelete(record)}>
          {intl.formatMessage({
            id: 'common.delete',
            defaultMessage: '删除',
          })}
        </a>,
      ],
    },
  ];
};

