import { request } from 'umi';
import type {
  AddressTableListParams,
  LoginLogListParams,
  NewsListParams,
  NewsParams
} from './data';
import service from '@/services/http/service';


export async function queryNewsAllList(params: NewsListParams) {

  console.log("queryNewsAllList: ", params)
  let result = await service.post('/api/v1/news/list', {
    all: true,
    tag: params.tag,
    order_asc: params.order_asc,
    is_deleted: params.is_deleted,
    search: params.main_title,
    "page_no": params.currentPage! - 1,
    "page_size": params.pageSize
  });

  return Promise.resolve({
    data: result,
    success: true,
  })
}


export async function queryNewsDraftList(params: NewsListParams) {

  console.log("queryNewsDraftList: ", params)
  let result = await service.post('/api/v1/news/draft/list', {
    tag: params.tag,
    order_asc: params.order_asc,
    search: params.main_title,
    "page_no": params.currentPage! - 1,
    "page_size": params.pageSize
  });

  return Promise.resolve({
    data: result,
    success: true,
  })
}


export async function newsPublishSub(id: number) {

  console.log("newsPublishSub: ", id)
  let result = await service.post('/api/v1/news/publish', {
    "id": id,
  });


  return Promise.resolve({
    data: result,
    success: true,
  })
}

// {
//   "category": "分类",
//   "main_title": "主标题",
//   "sub_title": "副标题",
//   "summary": "摘要信息",
//   "keywords": [
//     "Bitcoin",
//     "Ethereum",
//     "Filecoin"
//   ],
//   "seo_keywords": [
//     "Bitcoin",
//     "Ethereum",
//     "Filecoin"
//   ],
//   "tags": [
//     "#Blockchain",
//     "#AI",
//     "#Coincap"
//   ],
//   "image_url": "https://www.baidu.com/images/asrw2342342342.png",
//   "content": "这是我自己新增的一条新闻，新增后自动进入草稿箱，在未发布前只存在于草稿箱中；提交完成后返回草稿箱数据ID；草稿箱URI：/api/v1/news/draft/list"
// }
export async function addNewsItem(params: NewsParams) {

  console.log("addNewsItem: ", params)
  let result = await service.post('/api/v1/news/add', {
    ...params
  });

  return Promise.resolve({
    data: result.data.draft_id,
    success: true,
  })
}



// 通过新闻模版copy至草稿箱
export async function copyNewsItem(params: NewsParams) {

  console.log("editNewsItem: ", params)
  let result = await service.post('/api/v1/news/edit', {
    ...params
  });

  return Promise.resolve({
    data: result.data.draft_id,
    success: true,
  })
}




export async function removeNews(params: { ids: number[] }) {

  console.log("removeNews: ", params)

  let result = await service.post('/api/v1/news/delete', {
    ids: params.ids,
  });

  return Promise.resolve({
    data: result,
    success: true,
  })



}



export async function removeNewsDraft(params: { ids: number[] }) {

  console.log("removeNewsDraft: ", params)

  let result = await service.post('/api/v1/news/draft/delete', {
    ids: params.ids,
  });

  return Promise.resolve({
    data: result,
    success: true,
  })

}

// export async function updateMember(params: MemberTableListItem) {
//   return request('/api/member/member/update', {
//     method: 'POST',
//     data: {
//       ...params,
//     },
//   });
// }

export async function queryAddress(params: AddressTableListParams) {
  return request('/api/member/address/list', {
    method: 'POST',
    data: {
      ...params,
    },
  });
}

export async function queryLoginLog(params: LoginLogListParams) {
  return request('/api/member/loginlog/list', {
    method: 'POST',
    data: {
      ...params,
    },
  });
}
