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

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

// ========== 新增/编辑表单 ==========
export function useFormSchema(): VbenFormSchema[] {
  return [
    {
      component: 'Input',
      fieldName: 'id',
      dependencies: { triggerFields: [''], show: () => false },
    },
    {
      fieldName: 'contractId',
      label: '选择项目名称',
      component: 'ApiSelect',
      componentProps: {
        placeholder: '请选择或输入项目名称',
        api: () =>
          import('#/api/asset/contract').then((m) =>
            m.getSimpleContractList(),
          ),
        labelField: 'projectName',
        valueField: 'id',
        showSearch: true,
        filterOption: (input: string, option: any) =>
          (option?.label ?? '').toLowerCase().includes(input.toLowerCase()),
      },
      rules: z.number().min(1, { message: '请选择项目名称' }),
    },
    {
      fieldName: 'contractCode',
      label: '合同编码',
      component: 'Input',
      componentProps: { placeholder: '选择项目后自动填充', disabled: true },
    },
    {
      fieldName: 'customerName',
      label: '客户名称',
      component: 'Input',
      componentProps: { placeholder: '选择项目后自动填充', disabled: true },
    },
    {
      fieldName: 'deliveryRegion',
      label: '交车区域',
      component: 'Input',
      componentProps: { placeholder: '选择项目后自动填充', disabled: true },
    },
    {
      fieldName: 'deliveryLocation',
      label: '交车地点',
      component: 'Input',
      componentProps: { placeholder: '选择项目后自动填充', disabled: true },
    },
    {
      fieldName: 'expectedDeliveryDateStart',
      label: '预计交车开始日期',
      component: 'DatePicker',
      componentProps: {
        placeholder: '请选择日期',
        format: 'YYYY-MM-DD',
        valueFormat: 'YYYY-MM-DD',
      },
      rules: 'required',
    },
    {
      fieldName: 'expectedDeliveryDateEnd',
      label: '预计交车结束日期',
      component: 'DatePicker',
      componentProps: {
        placeholder: '请选择日期（可选）',
        format: 'YYYY-MM-DD',
        valueFormat: 'YYYY-MM-DD',
      },
    },
    {
      fieldName: 'billingStartDate',
      label: '开始计费日期',
      component: 'DatePicker',
      componentProps: {
        placeholder: '请选择开始计费日期',
        format: 'YYYY-MM-DD',
        valueFormat: 'YYYY-MM-DD',
      },
      rules: 'required',
    },
    {
      fieldName: 'needReturn',
      label: '已交车辆是否需要还车',
      component: 'Checkbox',
      renderComponentContent: () => ({ default: () => '不需要还车' }),
    },
  ];
}

// ========== 搜索表单 ==========
export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'taskCode',
      label: '交车任务编码',
      component: 'Input',
      componentProps: { placeholder: '请输入交车任务编码' },
    },
    {
      fieldName: 'contractCode',
      label: '合同编码',
      component: 'Input',
      componentProps: { placeholder: '请输入合同编码' },
    },
    {
      fieldName: 'customerName',
      label: '客户名称',
      component: 'Input',
      componentProps: { placeholder: '请输入客户名称' },
    },
  ];
}

// ========== 主表列配置（按合同分组） ==========
export function useGridColumns(): VxeTableGridOptions['columns'] {
  return [
    { type: 'expand', width: 60, fixed: 'left' },
    { field: 'contractCode', title: '合同编码', minWidth: 220, fixed: 'left' },
    { field: 'projectName', title: '项目名称', minWidth: 150 },
    { field: 'customerName', title: '客户名称', minWidth: 150 },
    { field: 'businessDeptName', title: '业务部门', minWidth: 120 },
    { field: 'businessManagerName', title: '业务负责人', minWidth: 100 },
    { field: 'startDate', title: '合同生效日期', minWidth: 120 },
    { field: 'endDate', title: '合同结束日期', minWidth: 120 },
    useActionColumn(1),
  ];
}

// ========== 子表列配置（交车任务明细） ==========
export function useSubGridColumns(): VxeTableGridOptions['columns'] {
  return [
    { field: 'taskCode', title: '交车任务编码', minWidth: 260 },
    {
      field: 'taskStatus',
      title: '任务状态',
      minWidth: 100,
      formatter: ({ cellValue }: { cellValue: number }) => cellValue === 1 ? '挂起' : '激活',
    },
    {
      field: 'deliveryStatus',
      title: '交车状态',
      minWidth: 100,
      formatter: ({ cellValue }: { cellValue: number }) => cellValue === 1 ? '已交车' : '未交车',
    },
    {
      field: 'expectedDeliveryDateStart',
      title: '预计交车日期',
      minWidth: 200,
      formatter: ({ row }: { row: any }) => {
        const start = row.expectedDeliveryDateStart || '';
        const end = row.expectedDeliveryDateEnd || '';
        return end ? `${start} 至 ${end}` : start;
      },
    },
    { field: 'vehicleCount', title: '交车数量', minWidth: 100, align: 'center' },
    { field: 'billingStartDate', title: '开始计费日期', minWidth: 120 },
    { field: 'creator', title: '创建人', minWidth: 100 },
    { field: 'createTime', title: '创建时间', minWidth: 120, formatter: 'formatDateTime' },
    useActionColumn(3, true, 'subActions'),
  ];
}
