import type { FormSchemaGetter } from '#/adapter/form';

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

import { getCompetitiveStationList } from '#/api/archive/operationsManagement/operationsManagement';
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: 0 },
        { label: '是', value: 1 },
      ],
    },
    fieldName: 'isCompetitor',
    label: '是否是竞争油站',
  },
  {
    component: 'DatePicker',
    componentProps: {
      type: 'daterange',
      format: 'YYYY-MM-DD HH:mm:ss',
      valueFormat: 'YYYY-MM-DD HH:mm:ss',
      startPlaceholder: '开始日期',
      endPlaceholder: '结束日期',
    },
    fieldName: 'surveyDate',
    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.isCompetitor !== undefined) {
    apiParams.isCompetitor = formValues.isCompetitor;
  }
  
  // 调查日期
  if (formValues.surveyDate) {
    apiParams.surveyDateStart = formValues.surveyDate[0];
    apiParams.surveyDateEnd = formValues.surveyDate[1];
  }
  
  return apiParams;
}

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


