import type { PageParam, PageResult } from '@vben/request';

import { requestClient } from '#/api/request';

export namespace CustomerApi {
  /** 客户信息 */
  export interface Customer {
    id: number; // 主键
    name?: string; // 客户名称
    code?: string; // 客户编号
    type?: number; // 客户类型
    areaId?: number; // 地区
    contacts?: string; // 联系人
    role: string; // 角色
    phone: string; // 联系电话
    email: string; // 邮箱
    remark: string; // 备注
  }

  export interface Dashboard {
    customerCount: number;
    projectCount: number;
    licenseCount: number;
    userCount: number;
    contractCount: number;
    signingRate: number;
  }
}

/** 查询首页详情 */
export function dashboard() {
  return requestClient.get<CustomerApi.Dashboard>(
    `/license/customer/dashboard`,
  );
}

/** 查询客户分页 */
export function getCustomerPage(params: PageParam) {
  return requestClient.get<PageResult<CustomerApi.Customer>>(
    '/license/customer/page',
    { params },
  );
}

/** 查询客户列表 */
export function getCustomerList() {
  return requestClient.get<CustomerApi.Customer[]>('/license/customer/list');
}

/** 查询可选择的客户列表 */
export function getLicenseCustomerList() {
  return requestClient.get<CustomerApi.Customer[]>(
    '/license/customer/license-list',
  );
}

/** 查询客户详情 */
export function getCustomer(id: number) {
  return requestClient.get<CustomerApi.Customer>(
    `/license/customer/get?id=${id}`,
  );
}

/** 新增客户 */
export function createCustomer(data: CustomerApi.Customer) {
  return requestClient.post('/license/customer/create', data);
}

/** 修改客户 */
export function updateCustomer(data: CustomerApi.Customer) {
  return requestClient.put('/license/customer/update', data);
}

/** 删除客户 */
export function deleteCustomer(id: number) {
  return requestClient.delete(`/license/customer/delete?id=${id}`);
}

/** 导出客户 */
export function exportCustomer(params: any) {
  return requestClient.download('/license/customer/export-excel', params);
}

/** 下载导入模板 */
export function importTemplate() {
  return requestClient.download('/license/customer/get-import-template');
}

/** 导入客户 */
export function importCustomer(file: File, updateSupport: boolean) {
  return requestClient.upload('/license/customer/import', {
    file,
    updateSupport,
  });
}

/** 客户名称是否唯一 */
export async function isCustomerNameUnique(
  name: string,
  id?: CustomerApi.Customer['id'],
) {
  return requestClient.get<boolean>('/license/customer/name-unique', {
    params: { id, name },
  });
}

/** 客户编号是否唯一 */
export async function isCustomerCodeUnique(
  code: number,
  id?: CustomerApi.Customer['id'],
) {
  return requestClient.get<boolean>('/license/customer/code-unique', {
    params: { id, code },
  });
}

/** 查询当前最大sn */
export async function getMaxSn() {
  return requestClient.get<boolean>('/license/customer/max-sn');
}
