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

export interface PadLockVO {
  lockId?: number;
  id?: number; // 兼容字段
  lockCode?: string;
  lockName: string;
  lockNfc?: string;
  hardwareId?: number;
  hardwareName?: string;
  lockTypeId?: number;
  lockTypeName?: string;
  lockSpec?: string;
  exStatus?: string; // 状态：'0' 或 '1'
  exRemark?: string; // 备注
  enableFlag?: string;
  remark?: string;
  createBy?: string;
  createTime?: Date | string;
  updateBy?: string;
  updateTime?: Date | string;
}

export interface PageParam {
  pageNo: number;
  pageSize: number;
  lockCode?: string;
  lockName?: string;
  lockNfc?: string;
  lockTypeId?: number;
  enableFlag?: string;
}

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

// 挂锁管理 API
export const padLockApi = {
  // 查询挂锁-列表
  listPadLock: async (params: PageParam): Promise<PageResponse<PadLockVO>> => {
    const response: any = await request.get({ 
      url: '/iscs/lock/getLockPage', 
      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<PadLockVO>;
  },

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

  // 新增挂锁
  addPadLock: async (data: PadLockVO): Promise<void> => {
    await request.post({ 
      url: '/iscs/lock/insertLock', 
      data 
    });
  },

  // 修改挂锁信息
  updatePadLock: async (data: PadLockVO): Promise<void> => {
    await request.put({ 
      url: '/iscs/lock/updateLock', 
      data 
    });
  },

  // 删除挂锁信息
  delPadLock: async (ids: number | number[] | string): Promise<void> => {
    const idsParam = typeof ids === 'string' ? ids : (Array.isArray(ids) ? ids.join(',') : ids);
    await request.delete({
      url: `/iscs/lock/deleteLockList?ids=${idsParam}`,
    });
  },
};

