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

import { DictEnum } from '@vben/constants';

import { selectAllSysStationAreaList } from '#/api/system/infoEntry/stationInfo/stationInfo';
import { getDictOptions } from '#/utils/dict';

// 查询表单schema
export const queryFormSchema: FormSchemaGetter = () => [
  {
    component: 'Input',
    fieldName: 'stationName',
    label: '场站名称',
    componentProps: {
      placeholder: '请输入场站名称',
    },
  },
  {
    component: 'ApiSelect',
    fieldName: 'areaId',
    label: '所属片区',
    componentProps: {
      placeholder: '请选择所属片区',
      api: async () => {
        const resp = await selectAllSysStationAreaList();
        const data = resp || [];
        return Array.isArray(data)
          ? data.map((item: any) => ({
              label: item.areaName,
              value: item.id,
            }))
          : [];
      },
      labelField: 'label',
      valueField: 'value',
    },
  },
  {
    component: 'Select',
    fieldName: 'typeCode',
    label: '场站类型',
    componentProps: {
      placeholder: '请选择场站类型',
      options: getDictOptions(DictEnum.STATION_TYPE),
    },
  },
];

// 表格列配置
export const tableColumns: VxeGridProps['columns'] = [
  {
    type: 'checkbox',
    width: 80,
  },
  {
    field: 'stationName',
    title: '场站名称',
    minWidth: 150,
  },
  {
    field: 'areaName',
    title: '所属片区',
    minWidth: 120,
  },
  {
    field: 'typeCode',
    title: '场站类型',
    minWidth: 120,
    slots: { default: 'typeCode' },
  },
  {
    field: 'status',
    title: '状态',
    minWidth: 100,
    slots: { default: 'status' }, // 使用插槽代替直接返回HTML字符串
  },
  {
    field: 'createTime',
    title: '创建时间',
    minWidth: 150,
  },
  {
    field: 'updateTime',
    title: '修改时间',
    minWidth: 150,
  },
  {
    field: 'createBy',
    title: '创建人',
    minWidth: 100,
  },
  {
    field: 'updateBy',
    title: '更新人',
    minWidth: 100,
  },
  {
    field: 'action',
    fixed: 'right',
    slots: { default: 'action' },
    title: '操作',
    width: 150,
  },
];

// 抽屉表单schema
export const drawerFormSchema: FormSchemaGetter = () => [
  {
    component: 'Input',
    dependencies: {
      show: () => false,
      triggerFields: [''],
    },
    fieldName: 'id',
  },
  {
    component: 'Input',
    fieldName: 'stationName',
    label: '场站名称',
    componentProps: {
      placeholder: '请输入场站名称',
      maxlength: 100,
    },
    rules: 'required',
  },
  {
    component: 'ApiSelect',
    fieldName: 'areaId',
    label: '所属片区',
    componentProps: {
      placeholder: '请选择所属片区',
      api: async () => {
        const resp: any = await selectAllSysStationAreaList();
        const data = resp?.data || [];
        return Array.isArray(data)
          ? data.map((item: any) => ({
              label: item.areaName,
              value: item.id,
            }))
          : [];
      },
      labelField: 'label',
      valueField: 'value',
    },
    rules: 'required',
  },
  {
    component: 'ApiSelect',
    componentProps: {
      placeholder: '请选择场站类型',
      options: getDictOptions(DictEnum.STATION_TYPE),
    },
    fieldName: 'typeCode',
    label: '场站类型',
    rules: 'required',
  },
  {
    component: 'RadioGroup',
    componentProps: {
      options: getDictOptions(DictEnum.SYS_NORMAL_DISABLE), // 使用SYS_NORMAL_DISABLE字典
      isButton: true,
    },
    fieldName: 'status',
    defaultValue: '1', // 启用状态
    label: '状态',
  },
];
