import request from '@/utils/request';
import type { ApiResult, PageResult } from '@/api';
import type {
  Certificate,
  CertificateParam
} from '@/api/love/certficate/model';
/**
 * 分页查询证件
 */
export async function pageCertificate(params: CertificateParam) {
  const res = await request.get<ApiResult<PageResult<Certificate>>>(
    '/love/certificate/page',
    {
      params
    }
  );
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 查询证件列表
 */
export async function listCertificate(params?: CertificateParam) {
  const res = await request.get<ApiResult<Certificate[]>>('/love/certificate', {
    params
  });
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 添加证件
 */
export async function addCertificate(data: Certificate) {
  const res = await request.post<ApiResult<unknown>>('/love/certificate', data);
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 修改证件
 */
export async function updateCertificate(data: Certificate) {
  const res = await request.put<ApiResult<unknown>>('/love/certificate', data);
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 绑定证件
 */
export async function bindCertificate(data: Certificate) {
  const res = await request.put<ApiResult<unknown>>(
    '/love/certificate/bind',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 批量添加设备
 */
export async function addBatchCertificate(data: Certificate[]) {
  const res = await request.post<ApiResult<unknown>>(
    '/love/certificate/batch',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 删除证件
 */
export async function removeCertificate(id?: number) {
  const res = await request.delete<ApiResult<unknown>>(
    '/love/certificate/' + id
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 批量删除证件
 */
export async function removeBatchCertificate(data: (number | undefined)[]) {
  const res = await request.delete<ApiResult<unknown>>(
    '/love/certificate/batch',
    {
      data
    }
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}
