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

export interface PadLockTypeVO {
  lockTypeId?: number;
  id?: number; // 兼容字段
  parentTypeId?: number;
  lockTypeCode?: string;
  lockTypeName: string;
  lockTypeIcon?: string;
  lockTypeImg?: string;
  lockTypeSpec?: string;
  hardwareTypeId?: number;
  enableFlag?: string;
  remark?: string;
  createBy?: string;
  createTime?: Date;
  updateBy?: string;
  updateTime?: Date;
  children?: PadLockTypeVO[]; // 树形结构
}

export interface PageParam {
  pageNo: number;
  pageSize: number;
  lockTypeCode?: string;
  lockTypeName?: string;
  enableFlag?: string;
}

// 分页响应类型
export interface PageResponse<T> {
  list: T[];
  total: number;
}

// 挂锁类型管理 API
export const padLockTypeApi = {
  // 查询挂锁类型-列表
  listPadLockType: async (params: PageParam): Promise<PageResponse<PadLockTypeVO>> => {
    const response: any = await request.get({ 
      url: '/iscs/lock-type/getLockTypePage', 
      params 
    });
    
    // 处理响应数据格式
    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 as any).list || (response as any).records || [],
          total: (response as any).total || 0,
        };
      }
    }
    return response as PageResponse<PadLockTypeVO>;
  },

  // 获取挂锁类型详细信息
  getPadLockTypeInfo: async (id: number): Promise<PadLockTypeVO> => {
    const response: any = await request.get({
      url: '/iscs/lock-type/selectLockTypeById',
      params: { id }
    });
    return (response?.data || response) as PadLockTypeVO;
  },

  // 新增挂锁类型
  addPadLockType: async (data: PadLockTypeVO): Promise<void> => {
    await request.post({ 
      url: '/iscs/lock-type/insertLockType', 
      data 
    });
  },

  // 修改挂锁类型信息
  updatePadLockType: async (data: PadLockTypeVO): Promise<void> => {
    await request.put({ 
      url: '/iscs/lock-type/updateLockType', 
      data 
    });
  },

  // 删除挂锁类型信息
  delPadLockType: async (ids: number | number[]): Promise<void> => {
    const idsParam = Array.isArray(ids) ? ids.join(',') : ids;
    await request.delete({
      url: '/iscs/lock-type/deleteLockTypeList',
      params: { ids: idsParam },
    });
  },
};

