import request from '@/utils/request';
import { AxiosPromise } from 'axios';
import { ClientForm, ClientQuery, ClientVO, KeyPair } from '@/api/system/client/types';

/**
 * 查询客户端管理列表
 * @param query
 * @returns {*}
 */

export const listClient = (query?: ClientQuery): AxiosPromise<ClientVO[]> => {
  return request({
    url: '/system/client/list',
    method: 'get',
    params: query
  });
};

/**
 * 查询客户端管理详细
 * @param id
 */
export const getClient = (id: string | number): AxiosPromise<ClientVO> => {
  return request({
    url: '/system/client/' + id,
    method: 'get'
  });
};

/**
 * 新增客户端管理
 * @param data
 */
export const addClient = (data: ClientForm) => {
  return request({
    url: '/system/client/add',
    method: 'post',
    data: data
  });
};

/**
 * 修改客户端管理
 * @param data
 */
export const updateClient = (data: ClientForm) => {
  return request({
    url: '/system/client/edit',
    method: 'post',
    data: data
  });
};

/**
 * 删除客户端管理
 * @param id
 */
export const delClient = (id: string | number | Array<string | number>) => {
  return request({
    url: '/system/client/remove/' + id,
    method: 'post'
  });
};

/**
 * 状态修改
 * @param clientId 客户端id
 * @param status 状态
 */
export function changeStatus(clientId: string, status: string) {
  const data = {
    clientId,
    status
  };
  return request({
    url: '/system/client/changeStatus',
    method: 'post',
    data: data
  });
}

/**
 * 生成SM2密钥对
 * @param clientId 客户端ID
 */
export function generateSm2KeyPair(clientId: string | number, encrypt: boolean = false) {
  return request<KeyPair>({
    url: '/system/client/generateSm2KeyPair',
    method: 'post',
    params: { clientId, encrypt }
  });
}

/**
 * 重置客户端缓存
 * @param forceRefresh 是否强制刷新
 */
export const resetClientCache = (forceRefresh: boolean = true) => {
  return request({
    url: '/system/client/resetCache',
    method: 'post',
    params: { forceRefresh }
  });
};
