import { request } from '@/http/axios'
import type {
  CabinetDetailResponse,
  RentingCabinetDetailDTO,
  AvailableStorageCellDTO,
  StoreItemToCellCommand,
  OpenCellByPasswordCommand,
  ResetCellByPasswordCommand,
  CabinetCellEntity
} from './type'
import { OpenCabinetApiData } from '../shop/type'

/** 获取智能柜详情接口 */
export function getCabinetDetailApi(shopId: number) {
  return request<CabinetDetailResponse>({
    url: 'cabinet/detail',
    method: 'get',
    params: {
      shopId
    }
  })
}

/** 获取出租中的智能柜详情接口 */
export function getRentingCabinetDetailApi(shopId: number) {
  return request<ApiResponseData<RentingCabinetDetailDTO[]>>({
    url: 'cabinet/detail/renting',
    method: 'get',
    params: {
      shopId
    }
  })
}

/** 获取自己租用中的智能柜详情接口 */
export function getUserRentedCabinetListApi(corpid:string, ab98UserId: number) {
  return request<ApiResponseData<RentingCabinetDetailDTO[]>>({
    url: 'cabinet/detail/user',
    method: 'get',
    params: {
      corpid,
      ab98UserId
    }
  })
}

export function openCabinet(cabinetId: number, pinNo: number, data: OpenCabinetApiData) {
  return request<ApiResponseData<void>>({
    url: `cabinet/openCabinet/${cabinetId}/${pinNo}`,
    method: 'post',
    data
  })
}

export const configureGoodsCellsStock = (cellId: number, goodsId: number, stock: number) => {
  return request<ApiResponseData<void>>({
    url: `/cabinet/configureGoodsCellsStock/${cellId}/${goodsId}/${stock}`,
    method: 'put'
  });
};

export const changeGoodsCellsStock = (cellId: number, stock: number) => {
  return request<ApiResponseData<void>>({
    url: `/cabinet/changeGoodsCellsStock/${cellId}/${stock}`,
    method: 'put'
  });
};

export const clearGoodsCells = (cellId: number) => {
  return request<ApiResponseData<void>>({ 
    url: `/cabinet/clearGoodsCells/${cellId}`,
    method: 'put'
  });
};

export const resetCellById = (cellId: number) => {
  return request<ApiResponseData<void>>({
    url: `/cabinet/reset/${cellId}`,
    method: 'put'
  });
};

/** 获取可用暂存柜格口列表 */
export function availableStorageCells(shopId: number) {
  return request<ApiResponseData<AvailableStorageCellDTO[]>>({
    url: 'cabinet/availableStorageCells',
    method: 'get',
    params: { shopId }
  })
}

/** 存入物品分配格口 */
export function storeItemApi(command: StoreItemToCellCommand) {
  return request<ApiResponseData<CabinetCellEntity>>({
    url: 'cabinet/storeItem',
    method: 'post',
    data: command
  })
}

/** 根据密码开启格口 */
export function openByPassword(command: OpenCellByPasswordCommand) {
  return request<ApiResponseData<void>>({
    url: 'cabinet/openByPassword',
    method: 'post',
    data: command
  })
}

/** 重置格口状态 */
export function resetByPassword(command: ResetCellByPasswordCommand) {
  return request<ApiResponseData<void>>({
    url: 'cabinet/resetByPassword',
    method: 'post',
    data: command
  })
}

