import type { VbenFormSchema } from '#/adapter/form';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';

import { VehicleApplicationApi } from '#/api/vehicle/application';

/** 用车申请状态选项 */
export const StatusOptions = [
  { label: '草稿', value: VehicleApplicationApi.Status.DRAFT },
  { label: '审批中', value: VehicleApplicationApi.Status.APPROVING },
  { label: '已通过', value: VehicleApplicationApi.Status.APPROVED },
  { label: '已驳回', value: VehicleApplicationApi.Status.REJECTED },
];

export function getStatusName(status?: number) {
  return StatusOptions.find((item) => item.value === status)?.label ?? '-';
}

/** 新增/修改表单 */
export function useFormSchema(formType: 'create' | 'edit' | 'detail') {
  const disabled = formType === 'detail';
  return [
    {
      fieldName: 'id',
      component: 'Input',
      dependencies: { triggerFields: [''], show: () => false },
    },
    {
      fieldName: 'useStartTime',
      label: '用车开始时间',
      component: 'DatePicker',
      componentProps: {
        showTime: true,
        valueFormat: 'YYYY-MM-DD HH:mm:ss',
        placeholder: '请选择用车开始时间',
        style: { width: '100%' },
        disabled,
      },
      rules: 'required',
    },
    {
      fieldName: 'useEndTime',
      label: '用车结束时间',
      component: 'DatePicker',
      componentProps: {
        showTime: true,
        valueFormat: 'YYYY-MM-DD HH:mm:ss',
        placeholder: '请选择用车结束时间',
        style: { width: '100%' },
        disabled,
      },
      rules: 'required',
    },
    {
      fieldName: 'departure',
      label: '出发地',
      component: 'Input',
      componentProps: {
        placeholder: '请输入出发地',
        maxlength: 128,
        disabled,
      },
      rules: 'required',
    },
    {
      fieldName: 'destination',
      label: '目的地',
      component: 'Input',
      componentProps: {
        placeholder: '请输入目的地',
        maxlength: 128,
        disabled,
      },
      rules: 'required',
    },
    {
      fieldName: 'passengers',
      label: '随行人数',
      component: 'InputNumber',
      componentProps: {
        min: 1,
        precision: 0,
        placeholder: '请输入随行人数',
        class: '!w-full',
        disabled,
      },
    },
    {
      fieldName: 'vehicleNo',
      label: '车牌号',
      component: 'Input',
      componentProps: {
        placeholder: '选填，如 苏F12345',
        maxlength: 32,
        disabled,
      },
    },
    {
      fieldName: 'reason',
      label: '用车事由',
      component: 'Textarea',
      componentProps: {
        placeholder: '请输入用车事由',
        autoSize: { minRows: 2, maxRows: 4 },
        disabled,
      },
      formItemClass: 'col-span-2',
      rules: 'required',
    },
    {
      fieldName: 'remark',
      label: '备注',
      component: 'Textarea',
      componentProps: {
        placeholder: '请输入备注',
        autoSize: { minRows: 2, maxRows: 3 },
        disabled,
      },
      formItemClass: 'col-span-2',
    },
  ] as VbenFormSchema[];
}

/** 列表搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'no',
      label: '申请单号',
      component: 'Input',
      componentProps: { placeholder: '请输入申请单号', allowClear: true },
    },
    {
      fieldName: 'applicantName',
      label: '申请人',
      component: 'Input',
      componentProps: { placeholder: '请输入申请人', allowClear: true },
    },
    {
      fieldName: 'destination',
      label: '目的地',
      component: 'Input',
      componentProps: { placeholder: '请输入目的地', allowClear: true },
    },
    {
      fieldName: 'status',
      label: '状态',
      component: 'Select',
      componentProps: {
        options: StatusOptions,
        placeholder: '请选择状态',
        allowClear: true,
      },
    },
  ];
}

/** 列表列 */
export function useGridColumns(): VxeTableGridOptions['columns'] {
  return [
    { field: 'no', title: '申请单号', width: 175, fixed: 'left' },
    { field: 'applicantName', title: '申请人', width: 110 },
    {
      field: 'useStartTime',
      title: '用车开始',
      width: 165,
      formatter: 'formatDateTime',
    },
    {
      field: 'useEndTime',
      title: '用车结束',
      width: 165,
      formatter: 'formatDateTime',
    },
    { field: 'departure', title: '出发地', minWidth: 140 },
    { field: 'destination', title: '目的地', minWidth: 140 },
    { field: 'passengers', title: '随行人数', width: 95 },
    { field: 'reason', title: '用车事由', minWidth: 180, showOverflow: 'tooltip' },
    { field: 'vehicleNo', title: '车牌号', width: 110 },
    {
      field: 'status',
      title: '状态',
      width: 100,
      slots: { default: 'status' },
    },
    {
      title: '操作',
      width: 220,
      fixed: 'right',
      slots: { default: 'actions' },
    },
  ];
}
