import type { FormSchemaGetter } from '#/adapter/form';
import type { VxeGridProps } from '#/adapter/vxe-table';

import { selectAllSysStation } from '#/api/system/infoEntry/stationInfo/stationInfo';

// 获取过滤后的岗位列表
import { getFilteredPositions } from '../../../../utils/positionFilter';

export const querySchema: FormSchemaGetter = () => [
  {
      component: 'ApiSelect',
      fieldName: 'stationId',
      label: '油站',
      componentProps: {
        placeholder: '请选择油站',
        clearable: true,
        api: async () => {
          const resp = await selectAllSysStation();
          const data = resp || [];
          return Array.isArray(data)
            ? data.map((item: any) => ({
                label: item.stationName,
                value: item.id.toString(),
              }))
            : [];
        },
        labelField: 'label',
        valueField: 'value',
      },
    },
  {
    component: 'Select',
    componentProps: {
      options: [
        { label: '按时完成', value: 2 },
        { label: '逾期完成', value: 1 },
      ],
    },
    fieldName: 'completionStatus',
    label: '状态',
  },
  {
    component: 'DatePicker',
    componentProps: {
      type: 'daterange',
      format: 'YYYY-MM-DD HH:mm:ss',
      valueFormat: 'YYYY-MM-DD HH:mm:ss',
      startPlaceholder: '开始日期',
      endPlaceholder: '结束日期',
    },
    fieldName: 'processTime',
    label: '处理时间',
  },
  {
    component: 'Select',
    componentProps: {
      options: [
        { label: '否', value: 0 },
        { label: '是', value: 1 },
      ],
    },
    fieldName: 'isComment',
    label: '评论',
  },
  {
    component: 'ApiSelect',
    fieldName: 'position',
    label: '岗位',
    componentProps: {
      placeholder: '请选择岗位',
      clearable: true,
      api: getFilteredPositions,
      labelField: 'label',
      valueField: 'value',
    },
  },
  {
    component: 'Select',
    componentProps: {
      options: [
        { label: '系统', value: 0 },
        { label: '新增', value: 1 },
      ],
    },
    fieldName: 'createType',
    label: '创建',
  },
  {
    component: 'Input',
    componentProps: {
      placeholder: '请输入执行人',
    },
    fieldName: 'executorName',
    label: '执行人',
  },
];

/**
 * 映射表单字段到API字段
 */
export function mapFormToApi(formValues: any, page: any) {
  const apiParams = {
    pageNum: page.currentPage,
    pageSize: page.pageSize,
  };
  
  // 油站ID
  if (formValues.stationId) {
    apiParams.stationId = formValues.stationId;
  }
  
  // 完成状态
  if (formValues.completionStatus !== undefined) {
    apiParams.completionStatus = formValues.completionStatus;
  }
  
  // 处理时间
  if (formValues.processTime) {
    apiParams.processTimeStart = formValues.processTime[0];
    apiParams.processTimeEnd = formValues.processTime[1];
  }
  
  // 是否评论
  if (formValues.isComment !== undefined) {
    apiParams.isComment = formValues.isComment;
  }
  
  // 岗位
  if (formValues.position) {
    apiParams.position = formValues.position;
  }
  
  // 创建类型
  if (formValues.createType !== undefined) {
    apiParams.createType = formValues.createType;
  }
  
  // 执行人姓名
  if (formValues.executorName) {
    apiParams.executorName = formValues.executorName;
  }
  
  return apiParams;
}

/**
 * 映射API响应数据到页面显示字段
 */
export function mapApiToView(data: any[]) {
  return data.map(item => ({
    ...item,
    // 处理需要转换的字段
  }));
}

export const columns: VxeGridProps['columns'] = [
  {
    field: 'action',
    fixed: 'left',
    slots: { default: 'action' },
    resizable: false,
    title: '操作',
    width: 180,
  },
  {
    title: '任务名称',
    field: 'taskName',
    minWidth: 150,
  },
  {
    title: '油站',
    field: 'stationName',
    minWidth: 120,
  },
  {
    title: '执行人',
    field: 'executorName',
    minWidth: 120,
  },
  {
    title: '岗位',
    field: 'positionName',
    minWidth: 120,
  },
  {
    title: '评论',
    field: 'isComment',
    minWidth: 100,
    align: 'center',
    formatter: (cellValue: number) => {
      return cellValue === 1 ? '已评' : '未评';
    },
  },
  {
    title: '截止时间',
    field: 'deadline',
    minWidth: 180,
  },
  {
    title: '处理时间',
    field: 'processTime',
    minWidth: 180,
  },
  {
    title: '完成状态',
    field: 'completionStatus',
    minWidth: 120,
    align: 'center',
    formatter: ({ row }) => {
      return row.completionStatus === 2 ? '按时完成' : '逾期完成';
    },
  },
  {
    title: '创建',
    field: 'createType',
    minWidth: 100,
    align: 'center',
    formatter: (cellValue: number) => {
      return cellValue === 0 ? '系统' : '新增';
    },
  },
];

