import type { AttachmentEntity, PageConfig } from '@vben/types';

import { useAccessStore } from '@vben/stores';
import { parseQueryValues } from '@vben/utils';

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

interface AttachmentPartialEntity
  extends Partial<Omit<AttachmentEntity, 'attid'>> {
  attid?: string;
}

interface AttachmentQueryParams extends AttachmentPartialEntity, PageConfig {}

/**
 * 附件信息_列表
 */
export async function getAttachmentListApi(params: AttachmentQueryParams) {
  return requestClient.post<any>('/api/query/list?pagevalue=97', {
    pageindex: params.pageindex,
    rows: params.rows,
    ...parseQueryValues({
      attlsh: params.attlsh,
      attmodel: params.attmodel,
    }),
  });
}

/**
 * 新增图片并返回id
 */
export async function addImageApi(data: FormData) {
  const token = useAccessStore().accessToken;
  return baseRequestClient.post<any>(
    '/api/attachment/addimg?pagevalue=93',
    data,
    {
      headers: {
        'Content-Type': 'multipart/form-data',
        token,
      },
    },
  );
}

/**
 * 添加附件记录并返回路径
 */
export async function addAttachmentApi(data: FormData) {
  const token = useAccessStore().accessToken;
  return baseRequestClient.post<any>(
    '/api/attachment/addtopath?pagevalue=94',
    data,
    {
      headers: {
        'Content-Type': 'multipart/form-data',
        token,
      },
    },
  );
}

/**
 * 删除附件记录
 */
export async function deleteAttachmentApi(data: { attid: string }) {
  return requestClient.post<any>('/api/attachment/del?pagevalue=95', {
    ...data,
  });
}

/**
 * 更新附件记录
 */
export async function updateAttachmentApi(data: { attid: string }) {
  return requestClient.post<any>('/api/attachment/up?pagevalue=96', {
    ...data,
  });
}
