import request from '@/utils/request';
import type { ApiResult, PageResult } from '@/api/index';
import type { CommissionRole, CommissionRoleParam } from './model';
import { MODULES_API_URL } from '@/config/setting';

/**
 * 分页查询分红角色
 */
export async function pageCommissionRole(params: CommissionRoleParam) {
  const res = await request.get<ApiResult<PageResult<CommissionRole>>>(
    MODULES_API_URL + '/shop/commission-role/page',
    {
      params
    }
  );
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 查询分红角色列表
 */
export async function listCommissionRole(params?: CommissionRoleParam) {
  const res = await request.get<ApiResult<CommissionRole[]>>(
    MODULES_API_URL + '/shop/commission-role',
    {
      params
    }
  );
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 添加分红角色
 */
export async function addCommissionRole(data: CommissionRole) {
  const res = await request.post<ApiResult<unknown>>(
    MODULES_API_URL + '/shop/commission-role',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 修改分红角色
 */
export async function updateCommissionRole(data: CommissionRole) {
  const res = await request.put<ApiResult<unknown>>(
    MODULES_API_URL + '/shop/commission-role',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 删除分红角色
 */
export async function removeCommissionRole(id?: number) {
  const res = await request.delete<ApiResult<unknown>>(
    MODULES_API_URL + '/shop/commission-role/' + id
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 批量删除分红角色
 */
export async function removeBatchCommissionRole(data: (number | undefined)[]) {
  const res = await request.delete<ApiResult<unknown>>(
    MODULES_API_URL + '/shop/commission-role/batch',
    {
      data
    }
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 根据id查询分红角色
 */
export async function getCommissionRole(id: number) {
  const res = await request.get<ApiResult<CommissionRole>>(
    MODULES_API_URL + '/shop/commission-role/' + id
  );
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}
