import request from '@/utils/request';
import type { ApiResult, PageResult } from '@/api';
import type {
  SecurityRecord,
  SecurityRecordParam
} from '@/api/tower/security-record/model';
/**
 * 分页查询仓库
 */
export async function pageSecurityRecord(params: SecurityRecordParam) {
  const res = await request.get<ApiResult<PageResult<SecurityRecord>>>(
    '/tower/tower-security-record/page',
    {
      params
    }
  );
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 查询仓库列表
 */
export async function listSecurityRecord(params?: SecurityRecordParam) {
  const res = await request.get<ApiResult<SecurityRecord[]>>(
    '/tower/tower-security-record',
    {
      params
    }
  );
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 添加仓库
 */
export async function addSecurityRecord(data: SecurityRecord) {
  const res = await request.post<ApiResult<unknown>>(
    '/tower/tower-security-record',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 修改仓库
 */
export async function updateSecurityRecord(data: SecurityRecord) {
  const res = await request.put<ApiResult<unknown>>(
    '/tower/tower-security-record',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 绑定仓库
 */
export async function bindSecurityRecord(data: SecurityRecord) {
  const res = await request.put<ApiResult<unknown>>(
    '/tower/tower-security-record/bind',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 删除仓库
 */
export async function removeSecurityRecord(id?: number) {
  const res = await request.delete<ApiResult<unknown>>(
    '/tower/tower-security-record/' + id
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 批量删除仓库
 */
export async function removeBatchSecurityRecord(data: (number | undefined)[]) {
  const res = await request.delete<ApiResult<unknown>>(
    '/tower/tower-security-record/batch',
    {
      data
    }
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}
