import { queryStr } from '@/utils/query';
import { request } from 'umi';
import { api } from '../api'
export interface AddTag {
  id?: string,
  tagName: string,
  type: string
}
export interface MediaList {
  describeInfo?: string,
  groupId?: string,
  id?: string,
  mediaAuthor?: string,
  mediaContent?: string,
  mediaContentSourceUrl?: string,
  mediaDigest?: string,
  mediaTagIds?: string | string[],
  mediaThumbMediaId?: string,
  mediaType?: string,
  title?: string,
  url?: string
}
export interface GetMediaList {
  mediaTagId?: string,
  pageNum: string,
  pageSize: string,
  mediaType: string,
  tagIds?: string | string[],
  groupId?: string,
}

export interface CreateGraphic {
  groupId: number,
  mediaTagIds: number[],
  sysMediaGraphics: {
    author: string,
    content: string,
    contentSourceUrl?: string,
    digest?: string,
    needOpenComment?: boolean,
    onlyFansCanComment?: boolean,
    showCoverPic?: boolean,
    title: string,
    url: string
  }[]
}
export interface EditGraphic extends CreateGraphic {
  id: string,
  appId: string
}
export interface GetGraphicList {
  mediaType?: string,
  groupId?: string,
  pageNum: number,
  pageSize: number,
  tagIds?: number[]
}
// export interface GetWeChatData {
//   appId: string,
//   count: string | number,
//   offset: string
// }
export interface GetWeChatData {
  mpId: number,
  mediaType: string,
  pageNum: number,
  pageSize: number
}
export interface GetWeChatITData {
  appId: string,
  count: string | number,
  offset: string,
  type: string
}
// export async function getGzhData() {
//   return request('/api/data/gzhsc');
// }
/**获取上传接口 */
export async function getFileUrl(params: { type: string }) {
  return request(`${api}/system/oss/ossUpload?type=${params.type}`);
}
/*************************标签**********************************/
/**新增标签 */
export async function addTag(params?: AddTag) {
  return request(api + '/system/mediaTag/create', {
    method: 'POST',
    data: params,
  });
}
/**编辑标签 */
export async function editTag(params?: AddTag) {
  return request(api + '/system/mediaTag/edit', {
    method: 'POST',
    data: params,
  });
}

/**删除标签 */
export async function delTag(params: { id: string }) {
  return request(`${api}/system/mediaTag/${params.id}`, {
    method: 'DELETE',
  });
}
/**获取标签列表*/
export async function getTagList(params: { type: string }) {
  return request(`${api}/system/mediaTag/list?type=${params.type}`);
}
/*************************分类**********************************/
/**新增标签 */
export async function addClass(params?: { groupName: string }) {
  return request(api + '/system/sysMediaGroup/create', {
    method: 'POST',
    data: params,
  });
}
/**编辑标签 */
export async function editClass(params?: { groupName: string, id: string }) {
  return request(api + '/system/sysMediaGroup/edit', {
    method: 'POST',
    data: params,
  });
}

/**删除标签 */
export async function delClass(params: { id: string }) {
  return request(`${api}/system/sysMediaGroup/${params.id}`, {
    method: 'DELETE',
  });
}
/**获取标签列表*/
export async function getClass(params: any) {
  return request(`${api}/system/sysMediaGroup?type=${params?.type}`);
}
/*************************素材**********************************/
/** 新增非图文本地素材*/
export async function addMediaList(params?: MediaList) {
  return request(api + '/system/SysMedia/create', {
    method: 'POST',
    data: params,
  });
}
/** 编辑非图文本地素材*/
export async function editMediaList(params?: MediaList) {
  return request(api + '/system/SysMedia/edit', {
    method: 'POST',
    data: params,
  });
}
/** 新增图文本地素材*/
export async function createGraphic(params: CreateGraphic) {
  return request(api + '/system/SysMedia/createGraphic', {
    method: 'POST',
    data: params,
  });
}
/** 新增图文微信素材*/
export async function createGraphicMaterial(params: CreateGraphic) {
  return request(api + '/system/SysMedia/addWxGraphicMaterial', {
    method: 'POST',
    data: params,
  });
}
/** 编辑图文本地素材*/
export async function editGraphic(params: EditGraphic) {
  return request(api + '/system/SysMedia/editGraphic', {
    method: 'POST',
    data: params,
  });
}
/** 编辑图文微信素材*/
export async function editGraphicWxMaterial(params: EditGraphic) {
  return request(api + '/system/SysMedia/updateWxMaterial', {
    method: 'POST',
    data: params,
  });
}
/** 获取图文素材*/
export async function getGraphicList(params: GetGraphicList) {
  return request(`${api}/system/SysMedia/graphicList${queryStr(params)}`);
}
/** 获取本地素材*/
export async function getMediaList(params: GetMediaList) {
  return request(`${api}/system/SysMedia/list${queryStr(params)}`);
}
/** 删除本地素材*/
export async function delMediaList(params: { id: string }) {
  return request(`${api}/system/SysMedia/${params.id}`, {
    method: 'DELETE',
  });
}
/**同步本地图文素材 */
export async function syncPermanentGraphicMedias(params: { appId: string[], mediaId: string }) {
  return request(api + '/system/SysMedia/syncPermanentGraphicMaterial', {
    method: 'POST',
    data: params,
  });
}
/**同步本地非图文素材 */
export async function syncMedias(params: { appId: string[], mediaId: string }) {
  return request(api + '/system/SysMedia/syncPermanent', {
    method: 'POST',
    data: params,
  });
}
/**获取微信图文素材 */
export async function getWeChatITData(params: GetWeChatITData) {
  return request(`${api}/system/SysMedia/batchGetMaterialNews${queryStr(params)}`);
}


// /**获取微信非图文素材 */
// export async function getWeChatData(params: GetWeChatData) {
//   return request(`${api}/system/SysMedia/batchGetMateria${queryStr(params)}`);
// }
/**获取微信非图文素材 */
export async function getWeChatData(params: GetWeChatData) {
  let { mpId, ...param } = params
  return request(`${api}/system/mp/media/list/${mpId}`, {
    method: 'POST',
    data: param,
  });
}
/**更新公众号下素材*/
export async function updateMediaCache(params: { mpId: number, mediaType: string }) {
  let { mpId, mediaType } = params
  return request(`${api}/system/mp/media/syncMedia/${mpId}/${mediaType}`, {
    method: 'PUT'
  })
}
/** 删除微信素材*/
export async function delMediawx(id: number) {
  return request(`${api}/system/mp/media/${id}`, {
    method: 'DELETE',
  });
}
/**微信素材详情*/
export async function getWxMediaInfo(params: { id: number, isCon?: boolean }) {
  let { id, isCon = true } = params
  return request(`${api}/system/mp/media/${id}/${isCon}`);
}

/**导入文章 */
export async function exportMediaByUrl(params: { url: string }) {
  return request(`${api}/system/SysMedia/exportMediaByUrl${queryStr(params)}`);
}
export async function exportMediaByUrl1(url: string) {
  return fetch(`${api}/system/mp/media/parseWxNewsByUrl?url=${url}`, {
    headers: { ['Authorization']: 'Bearer ' + sessionStorage.getItem('Admin-Token') },
  });
}




/**本地素材详情*/
export async function getGraphicDetail(params: { id: string }) {
  return request(`${api}/system/SysMedia/graphicDetail/${params.id}`);
}
/**查询图文同步结果 */
export async function syncResult(params: any) {
  return request(api + '/system/SysMedia/syncResult', {
    method: 'POST',
    data: params,
  });
}
/**批量上传非图文素材 */
export async function bantchAdd(params: any) {
  return request(`${api}/system/SysMedia/bantchAdd`, {
    method: 'PUT',
    data: params
  })
}
/**oss删除 */
export async function delOss(params: any) {
  return request(`${api}/system/oss/${params}`, {
    method: 'DELETE',
    // data: params
  });
}



//=========================新接口===================
// belongUser 0 公共 1 个人
/**获取本地素材列表 */
export async function bdSysMediaList(params: { parentId?: any, belongUser: 0 | 1, pageNum: number, pageSize: number, fileType: "image" | "news" | "voice" | "video" }) {
  const { belongUser, ...param } = params
  return request(api + `/system/SysMedia/list/${belongUser}`, {
    method: 'POST',
    data: param,
  });
}
/**修改本地素材 */
export async function bdSysMediaEdit(params: { belongUser: 0 | 1, sysMediaId: number, fileType: "image" | "news" | "voice" | "video" }) {
  const { belongUser, sysMediaId, ...param } = params
  return request(api + `/system/SysMedia/${belongUser}/${sysMediaId}`, {
    method: 'PUT',
    data: param
  });
}
/**新增本地素材 */
export async function bdSysMediaAdd(params: { belongUser: 0 | 1, parentId: number, fileType: "image" | "news" | "voice" | "video" }) {
  const { belongUser, ...param } = params
  return request(api + `/system/SysMedia/${belongUser}`, {
    method: 'POST',
    data: param,
  });
}
/**新增微信素材*/
export async function wxSysMediaAdd(params: { mediaType: "image" | "news" | "voice" | "video", localMediaId: number, mpId: number, news: any }) {
  return request(api + `/system/mp/media`, {
    method: 'POST',
    data: params,
  });
}
/**删除本地素材 */
export async function delMedia(sysMediaId: any) {
  return request(`${api}/system/SysMedia/${sysMediaId}`, {
    method: 'DELETE',
  });
}
/**获取本地素材详情*/
export async function getMedia(sysMediaId: any) {
  return request(`${api}/system/SysMedia/${sysMediaId}`, {
    method: 'GET',
  });
}
/**同步素材 */
export async function syncMedia(params: { sysMediaId: number, mpIds: number }) {
  const { mpIds, sysMediaId } = params
  return request(api + `/system/SysMedia/syncToMp/${sysMediaId}/${mpIds}`, {
    method: 'PUT',
  });
}
/**返回微信媒体ID的同步接口 */
export async function syncForSend(params: { sysMediaId: number, mpIds: number }) {
  const { mpIds, sysMediaId } = params
  return request(api + `/system/SysMedia/syncForSend/${sysMediaId}/${mpIds}`, {
    method: 'PUT',
  });
}

/**获取素材管理的公众号*/
export async function getWxlist(sysMediaId: number) {
  return request(api + `/system/SysMedia/getBindMps/${sysMediaId}`, {
    method: 'GET'
  })
}
/**编辑素材关联的公众号 */
export async function editWxlist(params: { sysMediaId: number, mpIds: any }) {
  const { sysMediaId, mpIds } = params
  return request(api + `/system/SysMedia/bindMediaMps/${sysMediaId}`, {
    method: 'POST',
    data: mpIds
  })
}
/**获取素材文件夹目录树*/
export async function getFolderTree(props: { belongUser: any, fileType: any }) {
  let { belongUser, fileType } = props
  return request(api + `/system/SysMedia/folderTree/${belongUser}/${fileType}`, {
    method: 'GET'
  })
}
/*改变文件位置*/
export async function editMediaFolder(params: { sysMediaId: number, folderId: number }) {
  const { folderId, sysMediaId } = params
  return request(api + `/system/SysMedia/configMediaFolder/${folderId}/${sysMediaId}`, {
    method: 'PUT',
  });
}
/** 排序 */
export async function configSortApi({ sysMediaId, sort }: { sysMediaId: number, sort: number }) {
  return request(api + `/system/SysMedia/configMediaSort/${sysMediaId}/${sort}`, {
    method: 'PUT'
  });
}