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

import { z } from '#/adapter/form';
import { getRangePickerDefaultProps } from '#/utils';
import { useActionColumn } from '#/utils/table';

// ========== 新增/编辑表单 ==========
export function useFormSchema(): VbenFormSchema[] {
  return [
    {
      component: 'Input',
      fieldName: 'id',
      dependencies: { triggerFields: [''], show: () => false },
    },
    {
      fieldName: 'taskId',
      label: '交车任务',
      component: 'ApiSelect',
      componentProps: {
        placeholder: '请选择交车任务',
        api: () =>
          import('#/api/asset/delivery-task').then((m) =>
            m.getSimpleDeliveryTaskList(),
          ),
        labelField: 'taskCode',
        valueField: 'id',
        showSearch: true,
        filterOption: (input: string, option: any) =>
          (option?.label ?? '').toLowerCase().includes(input.toLowerCase()),
      },
      rules: z.number().min(1, { message: '请选择交车任务' }),
    },
    {
      fieldName: 'deliveryDate',
      label: '交车日期',
      component: 'DatePicker',
      componentProps: {
        placeholder: '请选择交车日期',
        showTime: true,
        format: 'YYYY-MM-DD HH:mm:ss',
        valueFormat: 'YYYY-MM-DD HH:mm:ss',
        style: { width: '100%' },
      },
      rules: z.string().min(1, { message: '请选择交车日期' }),
    },
    {
      fieldName: 'deliveryPerson',
      label: '交车人',
      component: 'Input',
      componentProps: { placeholder: '请输入交车人' },
      rules: z.string().min(1, { message: '请输入交车人' }),
    },
    {
      fieldName: 'deliveryLocation',
      label: '交车地点',
      component: 'Input',
      componentProps: { placeholder: '请输入交车地点' },
    },
    {
      fieldName: 'authorizedPersonName',
      label: '被授权人姓名',
      component: 'Input',
      componentProps: { placeholder: '请输入被授权人姓名' },
    },
    {
      fieldName: 'authorizedPersonPhone',
      label: '被授权人电话',
      component: 'Input',
      componentProps: { placeholder: '请输入被授权人电话' },
    },
    {
      fieldName: 'authorizedPersonIdCard',
      label: '被授权人身份证',
      component: 'Input',
      componentProps: { placeholder: '请输入被授权人身份证' },
    },
  ];
}

// ========== 搜索表单 ==========
export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'contractCode',
      label: '合同编码',
      component: 'Input',
      componentProps: { placeholder: '请输入合同编码' },
    },
    {
      fieldName: 'projectName',
      label: '项目名称',
      component: 'Input',
      componentProps: { placeholder: '请输入项目名称' },
    },
    {
      fieldName: 'customerName',
      label: '客户名称',
      component: 'Input',
      componentProps: { placeholder: '请输入客户名称' },
    },
    {
      fieldName: 'deliveryRegion',
      label: '交车区域',
      component: 'Input',
      componentProps: { placeholder: '请输入交车区域' },
    },
    {
      fieldName: 'deliveryDate',
      label: '交车时间',
      component: 'RangePicker',
      componentProps: getRangePickerDefaultProps(),
    },
    {
      fieldName: 'deliveryPerson',
      label: '交车人',
      component: 'Input',
      componentProps: { placeholder: '请输入交车人' },
    },
  ];
}

// ========== 待处理列表列 ==========
export function usePendingColumns(): VxeTableGridOptions['columns'] {
  return [
    {
      field: 'expectedDeliveryDate',
      title: '预计交车时间',
      minWidth: 200,
      formatter: ({ row }: { row: any }) => {
        const start = row.expectedDeliveryDateStart || '';
        const end = row.expectedDeliveryDateEnd || '';
        return end ? `${start} 至 ${end}` : start;
      },
    },
    { field: 'createTime', title: '任务发布时间', minWidth: 160, formatter: 'formatDateTime' },
    { field: 'contractCode', title: '合同编码', minWidth: 220 },
    { field: 'projectName', title: '项目名称', minWidth: 150 },
    { field: 'customerName', title: '客户名称', minWidth: 150 },
    { field: 'vehicleCount', title: '交车数量', minWidth: 100, align: 'center' },
    { field: 'deliveryRegion', title: '交车区域', minWidth: 120 },
    { field: 'deliveryLocation', title: '交车地点', minWidth: 150 },
    useActionColumn(3),
  ];
}

// ========== 历史记录列表列 ==========
export function useHistoryColumns(): VxeTableGridOptions['columns'] {
  return [
    {
      field: 'expectedDeliveryDate',
      title: '预计交车时间',
      minWidth: 200,
      formatter: ({ row }: { row: any }) => {
        const start = row.expectedDeliveryDateStart || '';
        const end = row.expectedDeliveryDateEnd || '';
        return end ? `${start} 至 ${end}` : start;
      },
    },
    { field: 'createTime', title: '任务发布时间', minWidth: 160, formatter: 'formatDateTime' },
    { field: 'completeTime', title: '交车完成时间', minWidth: 160, formatter: 'formatDateTime' },
    { field: 'deliveryPerson', title: '交车人', minWidth: 100 },
    { field: 'contractCode', title: '合同编码', minWidth: 220 },
    { field: 'projectName', title: '项目名称', minWidth: 150 },
    { field: 'customerName', title: '客户名称', minWidth: 150 },
    { field: 'vehicleCount', title: '交车数量', minWidth: 100, align: 'center' },
    { field: 'deliveryRegion', title: '交车区域', minWidth: 120 },
    { field: 'deliveryLocation', title: '交车地点', minWidth: 150 },
    useActionColumn(3, true),
  ];
}

// ========== 兼容旧引用 ==========
export function useGridColumns(): VxeTableGridOptions['columns'] {
  return usePendingColumns();
}
