import { pickBy } from 'lodash-es'
import { http } from '@/utils/http/axios'
import type { CheckParam, PageParam, QueryPictureParam, UploadParam } from '/#/api'
import { ContentTypeEnum } from '@/enums/httpEnum'
import { notEmpty } from '@/utils'
import { formatToDate2 } from '@/utils/dateUtil'

/**
 * 获取查重列表
 * @param params
 * @returns
 */
export async function getDeduplicationList(params: PageParam & QueryPictureParam): Promise<any> {
  const res = await http.request({
    url: `/ocr/ocrPicture/list`,
    method: 'get',
    params,
  })

  const { data: { records } } = res
  return {
    data: records,
  }
}

/**
 * 上传图片
 * @param params
 * @returns
 */
export async function upload(params: UploadParam): Promise<{ data: string }> {
  return http.request({
    url: `/ocr/ocrPackagetask/upload`,
    method: 'post',
    params,
    headers: { 'Content-Type': ContentTypeEnum.FORM_DATA },
  })

  // return new Promise((r, e) => {
  //   r({ url: 'https://picx.zhimg.com/70/v2-615f0ca70562a95edff5b10b393c18e4_1440w.avis?source=172ae18b&biz_tag=Post' })
  // })
}

/**
 * 查询个人笔记
 * @returns
 */
export async function queryNote() {
  return http.request({
    url: `/ocr/ocrBooknote/query`,
    method: 'get',
  })
}

/**
 * 保存个人笔记
 * @param note
 * @returns
 */
export async function saveNote(note: string) {
  return http.request({
    url: `/ocr/ocrBooknote/add`,
    method: 'post',
    params: { notecontent: note },
    headers: { 'Content-Type': ContentTypeEnum.FORM_DATA },
  })
}

/**
 * 一键查重
 * @param note
 * @returns
 */
export async function oneClickCheck(params: Partial<CheckParam> = { search_history: '0' }) {
  const notEmptyParams = pickBy(params, notEmpty)

  Object.keys(notEmptyParams).forEach((key) => {
    const val = notEmptyParams[key]

    if (Array.isArray(val))
      notEmptyParams[key] = val.join(',')
  })

  return http.request({
    url: `/ocr/ocrPicture/createorder`,
    method: 'get',
    params: notEmptyParams,
    headers: { 'Content-Type': ContentTypeEnum.FORM_DATA },
  })
}

/**
 * 获取图片列表
 * @param params
 * @returns
 */
export async function getPictureList(params: any): Promise<any> {
  const notEmptyParams = pickBy(params, notEmpty)

  Object.keys(notEmptyParams).forEach((key) => {
    const val = notEmptyParams[key]

    if (key === 'izyear') {
      const start = formatToDate2(val[0])
      const end = formatToDate2(val[1])
      notEmptyParams[key] = `${start}-${end}`
    }

    if (Array.isArray(val))
      notEmptyParams[key] = val.join(',')
  })

  const res = await http.request({
    url: `/ocr/ocrPicture/list`,
    method: 'get',
    params: notEmptyParams,
    headers: { 'Content-Type': ContentTypeEnum.FORM_DATA },
  })

  const { data: { records, pages } } = res
  return {
    pageCount: pages,
    data: records,
  }
}
