import { request } from '../../utils/axios';

// 隔离点 VO 类型
export interface SegregationPointVO {
  id?: number; // 详情接口返回的 id 字段
  pointId?: number;
  pointCode: string;
  pointName: string;
  pointIcon?: string;
  pointPicture?: string;
  pointNfc: string | null; // pointNfc 可能为 null
  workstationId: number;
  lotoId: number | null; // lotoId 可能为 null
  powerType?: string;
  pointSerialNumber?: string | null; // pointSerialNumber 可能为 null
  remark?: string;
  createTime?: Date;
  // 扩展字段（用于显示）
  workstationName?: string;
  lotoName?: string;
  machineryId?: number;
  machineryName?: string;
  switchStatus?: number | string | null;
}

// 分页参数类型
export interface PageParam {
  current?: number;
  size?: number;
  pageNo?: number; // 兼容旧参数
  pageSize?: number; // 兼容旧参数
  pointCode?: string;
  pointName?: string;
  workstationId?: number;
  lotoId?: number;
  machineryId?: number;
  powerType?: string;
  [key: string]: any;
}

// 分页响应类型
export interface PageResponse<T> {
  list: T[];
  total: number;
  records?: T[]; // 兼容其他响应格式
  data?: {
    list: T[];
    total: number;
  };
}

// 隔离点管理 API
export const segregationPointApi = {
  // 查询隔离点列表（分页）
  getIsIsolationPointPage: async (params?: PageParam): Promise<PageResponse<SegregationPointVO>> => {
    // 从 params 中提取查询参数，排除分页参数 current 和 size
    const { current, size, pageNo, pageSize, ...queryParams } = params || {};
    
    const response = await request.get({ 
      url: '/iscs/isolation-point/getIsolationPointPage', 
      params: {
        pageNo: pageNo || current || 1,
        pageSize: pageSize || size || 10,
        ...queryParams,
      }
    });
    
    // 处理响应数据格式
    if (response && typeof response === 'object') {
      if ('data' in response && response.data) {
        return response.data;
      } else if ('list' in response || 'records' in response) {
        return {
          list: response.list || response.records || [],
          total: response.total || 0,
        };
      }
    }
    return response;
  },

  // 查询隔离点详情
  selectIsIsolationPointById: async (id: number): Promise<SegregationPointVO> => {
    const response = await request.get({ 
      url: '/iscs/isolation-point/selectIsolationPointById', 
      params: { id } 
    });
    return response?.data || response;
  },

  // 新增隔离点
  addinsertIsIsolationPoint: async (data: SegregationPointVO): Promise<void> => {
    return await request.post({ 
      url: '/iscs/isolation-point/insertIsolationPoint', 
      data 
    });
  },

  // 修改隔离点
  updateIsIsolationPoint: async (data: SegregationPointVO): Promise<void> => {
    return await request.put({ 
      url: '/iscs/isolation-point/updateIsolationPoint', 
      data 
    });
  },

  // 删除隔离点
  deleteIsIsolationPointByPointIds: async (pointIds: number | number[]): Promise<void> => {
    const ids = Array.isArray(pointIds) ? pointIds.join(',') : pointIds;
    return await request.delete({
      url: '/iscs/isolation-point/deleteIsolationPointList',
      params: { ids },
    });
  },

  // 查询车间列表
  getWorkshopList: async () => {
    return await request.get({ url: '/mes/md/workshop/listAll' });
  },

  // 查询工作区域列表
  getWorkareaList: async (workshopId: number) => {
    return await request.get({
      url: '/iscs/workarea/getIsWorkareaList',
      params: { workshopId },
    });
  },
};

