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

/**
 * 分页查询客户跟进情况
 */
export async function pageShopDealerRecord(params: ShopDealerRecordParam) {
  const res = await request.get<ApiResult<PageResult<ShopDealerRecord>>>(
    MODULES_API_URL + '/shop/shop-dealer-record/page',
    {
      params
    }
  );
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 查询客户跟进情况列表
 */
export async function listShopDealerRecord(params?: ShopDealerRecordParam) {
  const res = await request.get<ApiResult<ShopDealerRecord[]>>(
    MODULES_API_URL + '/shop/shop-dealer-record',
    {
      params
    }
  );
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 添加客户跟进情况
 */
export async function addShopDealerRecord(data: ShopDealerRecord) {
  const res = await request.post<ApiResult<unknown>>(
    MODULES_API_URL + '/shop/shop-dealer-record',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 修改客户跟进情况
 */
export async function updateShopDealerRecord(data: ShopDealerRecord) {
  const res = await request.put<ApiResult<unknown>>(
    MODULES_API_URL + '/shop/shop-dealer-record',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 删除客户跟进情况
 */
export async function removeShopDealerRecord(id?: number) {
  const res = await request.delete<ApiResult<unknown>>(
    MODULES_API_URL + '/shop/shop-dealer-record/' + id
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 批量删除客户跟进情况
 */
export async function removeBatchShopDealerRecord(
  data: (number | undefined)[]
) {
  const res = await request.delete<ApiResult<unknown>>(
    MODULES_API_URL + '/shop/shop-dealer-record/batch',
    {
      data
    }
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 根据id查询客户跟进情况
 */
export async function getShopDealerRecord(id: number) {
  const res = await request.get<ApiResult<ShopDealerRecord>>(
    MODULES_API_URL + '/shop/shop-dealer-record/' + id
  );
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}
