import type { FormSchemaGetter } from '#/adapter/form';
import type { VxeGridProps } from '#/adapter/vxe-table';
import { selectAllSysStation } from '#/api/system/infoEntry/stationInfo/stationInfo';
import { getCompetitiveStationList } from '#/api/archive/operationsManagement/operationsManagement';
// 岗位列表接口
import { requestClient } from '#/api/request';

// 获取岗位列表
const getPostList = async () => {
  const resp = await requestClient.get('/system/post/list');
  return resp || [];
};
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: 'ApiSelect',
    componentProps: {
      api: async () => {
        const resp = await getCompetitiveStationList({});
        const data = resp?.rows || [];
        return Array.isArray(data)
          ? data.map((item: any) => ({ label: item.competitorName, value: item.id }))
          : [];
      },
      labelField: 'label',
      valueField: 'value',
      placeholder: '请选择竟对油站',
    },
    fieldName: 'competitorId',
    label: '竟对油站',
  },
  {
    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: 'surveyStartTime',
    label: '处理时间',
  },
  {
    component: 'Select',
    componentProps: {
      options: [
        { label: '否', value: 0 },
        { label: '是', value: 1 },
      ],
    },
    fieldName: 'isComment',
    label: '评论',
  },
  {
    component: 'ApiSelect',
    fieldName: 'position',
    label: '岗位',
    componentProps: {
      placeholder: '请选择岗位',
      clearable: true,
      api: async () => {
        const data = await getPostList();
        return Array.isArray(data.rows)
          ? data.rows.map((item: any) => ({
              label: item.postName || item.name,
              value: item.postId,
            }))
          : [];
      },
      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,
  };
  
  // 我方油站
  if (formValues.stationId) {
    apiParams.stationId = formValues.stationId;
  }
  
  // 竟对油站
  if (formValues.competitorId) {
    apiParams.competitorId = formValues.competitorId;
  }
  
  // 完成状态
  if (formValues.completionStatus !== undefined) {
    apiParams.completionStatus = formValues.completionStatus;
  }
  
  // 调查开始时间
  if (formValues.surveyStartTime) {
    apiParams.processStartTime = formValues.surveyStartTime[0];
    apiParams.processEndTime = formValues.surveyStartTime[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: '处理时间',
    fixed: 'left',
    field: 'processTime',
    minWidth: 180,
  },
  {
    title: '我方油站',
    fixed: 'left',
    field: 'stationName',
    minWidth: 130,
  },
  {
    title: '竟对油站',
    fixed: 'left',
    field: 'competitorName',
    minWidth: 130,
  },
  {
    title: '调查开始时间',
    field: 'surveyStartTime',
    minWidth: 180,
  },
  {
    title: '调查结束时间',
    field: 'surveyEndTime',
    minWidth: 180,
  },
  {
    title: '汽油车',
    field: 'gasoline',
    children: [
      {
        title: '进站车次',
        field: 'gasolineVehicleCount',
        minWidth: 130,
      },
      {
        title: '加油升数差',
        field: 'gasolineVolumeDiff',
        minWidth: 130,
      },
    ],
  },
  {
    title: '柴油车',
    field: 'diesel',
    children: [
      {
        title: '进站车次',
        field: 'dieselVehicleCount',
        minWidth: 130,
      },
      {
        title: '加油升数差',
        field: 'dieselVolumeDiff',
        minWidth: 130,
      },
    ],
  },
  {
    title: '摩托车',
    field: 'motorcycle',
    children: [
      {
        title: '进站车次',
        field: 'motorcycleCount',
        minWidth: 130,
      },
      {
        title: '加油升数差',
        field: 'motorcycleVolumeDiff',
        minWidth: 130,
      },
    ],
  },
  {
    title: '气车',
    field: 'gas',
    children: [
      {
        title: '进站车次',
        field: 'gasVehicleCount',
        minWidth: 130,
      },
      {
        title: '加油升数差',
        field: 'gasVolumeDiff',
        minWidth: 130,
      },
    ],
  },
  {
    title: '时段预测销量差（升）',
    field: 'periodForecastDiff',
    minWidth: 170,
  },
  {
    title: '全天预测销量差（升）',
    field: 'dailyForecastDiff',
    minWidth: 170,
  },
  {
    title: '汽油调查销量（升）',
    field: 'gasolineSurveyVolume',
    minWidth: 170,
  },
  {
    title: '柴油调查销量（升）',
    field: 'dieselSurveyVolume',
    minWidth: 170,
  },
  {
    title: '是否是竞争油站',
    field: 'isCompetitor',
    minWidth: 130,
    formatter: (cellValue: number) => {
      return cellValue === 1 ? '是' : '否';
    },
  },
];


