import request from '@/utils/request';
import { withCreditUserScope } from '@/api/credit/utils/data-scope';

import type { ApiResult, PageResult } from '@/api';
import type { CreditSupplier, CreditSupplierParam } from './model';

/**
 * 分页查询供应商
 */
export async function pageCreditSupplier(params: CreditSupplierParam) {
  const res = await request.get<ApiResult<PageResult<CreditSupplier>>>(
    '/credit/credit-supplier/page',
    { params: withCreditUserScope(params) }
  );
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 查询供应商列表
 */
export async function listCreditSupplier(params?: CreditSupplierParam) {
  const res = await request.get<ApiResult<CreditSupplier[]>>(
    '/credit/credit-supplier',
    { params: withCreditUserScope(params) }
  );
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 添加供应商
 */
export async function addCreditSupplier(data: CreditSupplier) {
  const res = await request.post<ApiResult<unknown>>(
    '/credit/credit-supplier',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 修改供应商
 */
export async function updateCreditSupplier(data: CreditSupplier) {
  const res = await request.put<ApiResult<unknown>>(
    '/credit/credit-supplier',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 删除供应商
 */
export async function removeCreditSupplier(id?: number) {
  const res = await request.delete<ApiResult<unknown>>(
    '/credit/credit-supplier/' + id
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 批量删除供应商
 */
export async function removeBatchCreditSupplier(data: (number | undefined)[]) {
  const res = await request.delete<ApiResult<unknown>>(
    '/credit/credit-supplier/batch',
    {
      data
    }
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 根据id查询供应商
 */
export async function getCreditSupplier(id: number) {
  const res = await request.get<ApiResult<CreditSupplier>>(
    '/credit/credit-supplier/' + id
  );
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 导入供应商
 */
export async function importCreditSupplier(file: File, companyId?: number) {
  const formData = new FormData();
  formData.append('file', file);
  if (companyId != null) {
    formData.append('companyId', String(companyId));
  }
  const res = await request.post<ApiResult<unknown>>(
    '/credit/credit-supplier/import',
    formData,
    {
      headers: {
        'Content-Type': 'multipart/form-data'
      }
    }
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}
