import type { TableColumn } from '@/components/core/dynamic-table';
import { VehicleUsageStatusEnum } from '@/enums/vehicleEnum';
import { formatToDate } from '@/utils/dateUtil';
import { Tag } from 'ant-design-vue';
import dayjs from 'dayjs';

export type TableListItem = API.VehicleUsageEntity;
export type TableColumnItem = TableColumn<TableListItem>;
export const baseColumns: TableColumnItem[] = [
  {
    title: '年度',
    width: 80,
    fixed: 'left',
    dataIndex: 'year',
    formItemProps: {
      label: '年度',
      component: 'Select',
      componentProps: {
        fieldNames: {
          label: 'label',
          value: 'value',
        },
        options: Array.from({ length: dayjs().year() - 2010 }).map((_, index) => ({
          label: `${dayjs().year() - index}`,
          value: dayjs().year() - index,
        })),
      },
      colProps: {
        span: 4,
      },
    },
  },
  {
    title: '车辆名称及车牌号',
    width: 150,
    dataIndex: 'vehicle',
    fixed: 'left',
    formItemProps: {
      labelWidth: 150,

      label: '车辆名称及车牌号',
      span: 12,
    },
    customRender: ({ record }) => {
      return record?.vehicle?.label || '';
    },
  },
  {
    title: '申请人',
    dataIndex: 'applicant',
    formItemProps: {
      span: 10,
    },
  },
  {
    title: '出行司机',
    hideInSearch: true,
    dataIndex: 'driver',
  },
  {
    title: '随行人员',
    hideInSearch: true,
    dataIndex: 'partner',
  },
  {
    title: '当前车辆里程数(KM)',
    hideInSearch: true,
    dataIndex: 'currentMileage',
  },
  {
    title: '预计出行开始时间',
    width: 130,
    hideInSearch: true,
    align: 'center',
    dataIndex: 'expectedStartDate',
    customRender: ({ record }) => {
      return formatToDate(record.expectedStartDate);
    },
  },
  {
    title: '预计出行结束时间',
    width: 130,
    hideInSearch: true,
    align: 'center',
    dataIndex: 'expectedEndDate',
    customRender: ({ record }) => {
      return formatToDate(record.expectedEndDate);
    },
  },
  {
    title: '使用事由',
    dataIndex: 'purpose',
    hideInSearch: true,
  },
  {
    title: '实际回司时间',
    hideInSearch: true,
    width: 130,
    align: 'center',
    dataIndex: 'actualReturnTime',
    customRender: ({ record }) => {
      return formatToDate(record.actualReturnTime);
    },
  },
  {
    title: '回城车辆里程数(KM)',
    hideInSearch: true,
    dataIndex: 'returnMileage',
  },
  {
    title: '审核人',
    hideInSearch: true,
    dataIndex: 'reviewer',
  },
  {
    title: '备注',
    hideInSearch: true,
    dataIndex: 'remark',
  },

  {
    title: '审核状态',
    width: 100,
    align:'center',
    fixed: 'right',
    dataIndex: 'status',
    formItemProps: {
      component: 'Select',
      componentProps: {
        options: Object.values(VehicleUsageStatusEnum)
          .filter((value) => typeof value === 'number')
          .map((item) => formatStatus(item as VehicleUsageStatusEnum)),
      },
    },
    customRender: ({ record }) => {
      const { color, label } = formatStatus(record.status);
      return <Tag color={color}>{label}</Tag>;
    },
  },
];
export function formatStatus(status: VehicleUsageStatusEnum): {
  color: string;
  label: string;
  value: number;
} {
  switch (status) {
    case VehicleUsageStatusEnum.Pending:
      return {
        color: '#ccc',
        label: '待审核',
        value: VehicleUsageStatusEnum.Pending,
      };
    case VehicleUsageStatusEnum.Approved:
      return {
        color: 'green',
        label: '已通过',
        value: VehicleUsageStatusEnum.Approved,
      };
    case VehicleUsageStatusEnum.Rejected:
      return {
        color: 'red',
        label: '已拒绝',
        value: VehicleUsageStatusEnum.Rejected,
      };
    default:
      return {
        color: '#ccc',
        label: '待审核',
        value: VehicleUsageStatusEnum.Pending,
      };
  }
}
