import type { FormSchemaGetter } from '#/adapter/form';
import type { VxeGridProps } from '#/adapter/vxe-table';
import { DictEnum } from '@vben/constants';
import { getDictOptions } from '#/utils/dict';
// import { queryGasStationList } from '#/api/schedule/index';
import { selectAllSysStationAreaList } from '#/api/system/infoEntry/stationInfo/stationInfo';
import { getPostList } from '#/api/system/post/post';

export const querySchema: FormSchemaGetter = () => [
  {
    component: 'ApiSelect',
    componentProps: {
      api: selectAllSysStationAreaList,
      labelField: 'stationName',
      valueField: 'id',
      placeholder: '请选择油站',
      clearable: true,
    },
    fieldName: 'stationId',
    label: '油站',
  },
  {
    component: 'ApiSelect',
    componentProps: {
      api: getPostList,
      labelField: 'postName',
      valueField: 'postId',
      placeholder: '请选择岗位名称',
      resultField: 'rows',
      clearable: true,
      afterFetch: (data: any) => {
        return data.rows.filter(
          (item: any) => item.postId === 4 || item.postId === 5,
        );
      },
    },
    fieldName: 'executePosition',
    label: '岗位名称',
  },
  {
    component: 'DatePicker',
    componentProps: {
      type: 'month',
      format: 'YYYY-MM',
      valueFormat: 'YYYY-MM',
      placeholder: '请选择月份',
    },
    fieldName: 'month',
    label: '月份',
  },
];

// 生成动态日期列
export const generateColumns = (month: string): VxeGridProps['columns'] => {
  // 基础列配置
  const baseColumns = [
    {
      field: 'action',
      fixed: 'left',
      slots: { default: 'action' },
      resizable: false,
      title: '操作',
      width: 180,
    },
    {
      title: '姓名',
      field: 'nickName',
      fixed: 'left',
      resizable: false,
      minWidth: 130,
    },
    {
      title: '岗位',
      field: 'postName',
      fixed: 'left',
      resizable: false,
      minWidth: 130,
    },
  ];

  // 解析月份
  const [year, monthNum] = month.split('-').map(Number);
  //@ts-ignore
  const date = new Date(year, monthNum - 1, 1);

  // 星期几的中文映射
  const weekDays = ['日', '一', '二', '三', '四', '五', '六'];

  // 生成日期列
  const dateColumns = [];
  let day = 1;

  // 获取当月的天数
  //@ts-ignore
  const daysInMonth = new Date(year, monthNum, 0).getDate();

  for (let i = 0; i < daysInMonth; i++) {
    // 获取当前日期的星期
    const weekDay = weekDays[date.getDay()];
    // 生成标题，如 "1四"
    const title = `${day}${weekDay}`;
    // 生成字段名，如 "day1"
    const field = `day${day}`;

    dateColumns.push({
      title,
      field,
      minWidth: 130,
      slots: { default: 'Select' },
    });

    day++;
    date.setDate(day);
  }

  // 合并基础列和日期列
  //@ts-ignore
  return [...baseColumns, ...dateColumns];
};
