import request from '@/utils/request';
import type { ApiResult, PageResult } from '@/api';
import type { ClinicPrescription, ClinicPrescriptionParam } from './model';

/**
 * 分页查询处方主表

 */
export async function pageClinicPrescription(params: ClinicPrescriptionParam) {
  const res = await request.get<ApiResult<PageResult<ClinicPrescription>>>(
    '/clinic/clinic-prescription/page',
    {
      params
    }
  );
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 查询处方主表
列表
 */
export async function listClinicPrescription(params?: ClinicPrescriptionParam) {
  const res = await request.get<ApiResult<ClinicPrescription[]>>(
    '/clinic/clinic-prescription',
    {
      params
    }
  );
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 添加处方主表

 */
export async function addClinicPrescription(data: ClinicPrescription) {
  const res = await request.post<ApiResult<unknown>>(
    '/clinic/clinic-prescription',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 修改处方主表

 */
export async function updateClinicPrescription(data: ClinicPrescription) {
  const res = await request.put<ApiResult<unknown>>(
    '/clinic/clinic-prescription',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 删除处方主表

 */
export async function removeClinicPrescription(id?: number) {
  const res = await request.delete<ApiResult<unknown>>(
    '/clinic/clinic-prescription/' + id
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 批量删除处方主表

 */
export async function removeBatchClinicPrescription(
  data: (number | undefined)[]
) {
  const res = await request.delete<ApiResult<unknown>>(
    '/clinic/clinic-prescription/batch',
    {
      data
    }
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 根据id查询处方主表

 */
export async function getClinicPrescription(id: number) {
  const res = await request.get<ApiResult<ClinicPrescription>>(
    '/clinic/clinic-prescription/' + id
  );
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}
