import request from '@/utils/request';
import type { ApiResult, PageResult } from '@/api';
import type { BCPlan, BCPlanParam } from '@/api/apps/bc/plan/model';
/**
 * 分页查询计划
 */
export async function pageBCPlan(params: BCPlanParam) {
  const res = await request.get<ApiResult<PageResult<BCPlan>>>(
    '/apps/bc-plan/page',
    {
      params
    }
  );
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 查询计划列表
 */
export async function listBCPlan(params?: BCPlanParam) {
  const res = await request.get<ApiResult<BCPlan[]>>('/apps/bc-plan', {
    params
  });
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 添加计划
 */
export async function addBCPlan(data: BCPlan) {
  const res = await request.post<ApiResult<unknown>>('/apps/bc-plan', data);
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 修改计划
 */
export async function updateBCPlan(data: BCPlan) {
  const res = await request.put<ApiResult<unknown>>('/apps/bc-plan', data);
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 绑定计划
 */
export async function bindBCPlan(data: BCPlan) {
  const res = await request.put<ApiResult<unknown>>('/apps/bc-plan/bind', data);
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 删除计划
 */
export async function removeBCPlan(id?: number) {
  const res = await request.delete<ApiResult<unknown>>('/apps/bc-plan/' + id);
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 批量删除计划
 */
export async function removeBatchBCPlan(data: (number | undefined)[]) {
  const res = await request.delete<ApiResult<unknown>>('/apps/bc-plan/batch', {
    data
  });
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}
