import Api from '@/api';
import type { FormSchema } from '@/components/core/schema-form/';
import { DictEnum } from '@/enums/dictEnum';
import { VehicleUsageStatusEnum } from '@/enums/vehicleEnum';
import { useDictStore } from '@/store/modules/dict';
import dayjs from 'dayjs';
import { formatStatus } from './columns';
const { getDictItemsByCode } = useDictStore();
export const formSchemas = (isEdit?: boolean): FormSchema<API.VehicleUsageDto>[] => [
  {
    label: '年度',
    component: 'Select',
    field: 'year',
    colProps: {
      span: 6,
    },
    defaultValue: dayjs().year(),
    rules: [{ required: true, type: 'number' }],
    componentProps: () => ({
      fieldNames: {
        label: 'label',
        value: 'value',
      },
      defaultValue: dayjs().year(),
      options: Array.from({ length: dayjs().year() - 2010 }).map((_, index) => ({
        label: `${dayjs().year() - index}`,
        value: dayjs().year() - index,
      })),
    }),
  },

  {
    field: 'vehicleId',
    component: 'Select',
    label: '车辆名称及车牌号',
    rules: [{ required: true, type: 'number' }],
    labelWidth: 150,
    helpMessage: '如未找到对应车辆，请先去字典管理添加车辆。',
    colProps: {
      span: 16,
      offset: 1,
    },
    componentProps: ({ formInstance, schema }) => ({
      showSearch: true,
      filterOption: (input: string, option: any) => {
        return option.label.toLocaleLowerCase().indexOf(input.toLocaleLowerCase()) >= 0;
      },
      fieldNames: {
        label: 'label',
        value: 'value',
      },
      options: getDictItemsByCode(DictEnum.Vehicle).map((item) => ({
        value: item.id,
        label: item.label,
      })),
      getPopupContainer: () => document.body,
      defaultActiveFirstOption: true,
    }),
  },
  {
    field: 'applicant',
    component: 'Input',
    label: '申请人',
    rules: [{ required: true, type: 'string' }],
    colProps: {
      span: 8,
    },
  },
  {
    field: 'driver',
    component: 'Input',
    label: '出行司机',
    rules: [{ type: 'string' }],
    colProps: {
      span: 8,
    },
  },
  {
    field: 'partner',
    component: 'Input',
    label: '随行人员',
    rules: [{ type: 'string' }],
    colProps: {
      span: 8,
    },
  },
  {
    label: '预计出行开始时间',
    field: 'expectedStartDate',
    component: 'DatePicker',
    labelWidth: 150,
    rules: [{ type: 'date', required: true }],
    colProps: {
      span: 12,
    },
  },
  {
    label: '预计出行结束时间',
    field: 'expectedEndDate',
    labelWidth: 150,
    rules: [{ type: 'date', required: true }],
    component: 'DatePicker',
    colProps: {
      span: 12,
    },
  },
  {
    field: 'currentMileage',
    component: 'InputNumber',
    label: '当前车辆里程数(KM)',
    rules: [{ type: 'number' }],
    labelWidth: 150,
    colProps: {
      span: 24,
    },
  },

  {
    label: '回城车辆里程数(KM)',
    labelWidth: 150,
    vIf: isEdit,
    field: 'returnMileage',
    rules: [{ type: 'number' }],
    component: 'InputNumber',
    colProps: {
      span: 24,
    },
  },

  {
    label: '实际回司时间',
    field: 'actualReturnTime',
    labelWidth: 150,
    vIf: isEdit,
    rules: [{ type: 'date' }],
    component: 'DatePicker',
    colProps: {
      span: 12,
    },
  },

  {
    field: 'purpose',
    component: 'InputTextArea',
    label: '使用事由',
    rules: [{ type: 'string' }],
  },
  {
    field: 'reviewer',
    component: 'Input',
    label: '审核人',
    rules: [{ type: 'string' }],
    colProps: {
      span: 8,
    },
  },
  {
    field: 'status',
    label: '审核结果',
    component: 'Select',
    required: true,
    vIf: isEdit,
    defaultValue: 0,
    colProps: {
      span: 8,
    },
    componentProps: {
      allowClear: false,
      options: Object.values(VehicleUsageStatusEnum)
        .filter((value) => typeof value === 'number')
        .map((item) => formatStatus(item as VehicleUsageStatusEnum)),
    },
  },
  {
    label: '备注',
    field: 'remark',
    component: 'Input',
    colProps: {
      span: 8,
    },
  },
];
