import { http } from '@/utils/http/axios'
import type { PageParam, PictureSortParam, SetTFParam } from '/#/api'
import { ContentTypeEnum } from '@/enums/httpEnum'

/**
 * 获取任务包列表
 * @returns
 */
export async function getPackageList(params: PageParam) {
  const res = await http.request({
    url: `/ocr/ocrPackagetask/list`,
    method: 'get',
    params,
  })

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

/**
 * 获取某个任务包内任务列表
 * @param id 任务包id
 * @param params
 * @returns
 */
export async function getPackageTaskList(packageid: string, params: PageParam) {
  const res = await http.request({
    url: `/backstage/jifen/ocrtaskchildpicture/listbypackageid/${packageid}`,
    method: 'get',
    params,
  })

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

/**
 * 获取某个任务详情(字段信息)
 * @param id 任务id
 * @param packageid 任务包id
 * @returns
 */
export async function getTaskDetailInfo(taskId: string, packageid: string) {
  const res = await http.request({
    url: `/backstage/jifen/ocrtaskchildpicture/getdata/${taskId}/${packageid}`,
    method: 'get',
  })

  return res.data
}

/**
 *  获取某个任务详情(相似图片列表)
 * @param packageid 任务包id
 * @param taskchildpictureid 任务详情的id
 * @param params
 * @returns
 */
export async function getTaskDetailPictureList(packageid: string, taskchildpictureid: string, params: PageParam & PictureSortParam) {
  const res = await http.request({
    url: `/backstage/jifen/ocrtaskchildpicture/listbypictureid/${packageid}/${taskchildpictureid}`,
    method: 'get',
    params,
  })

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

  // 精简一下数据
  const list = records.map((item) => {
    return {
      id: item.id,
      pictureid: item.pictureid,
      imgurl: item.ocrPicture.imgurl,
      iztrueorfalse: item.iztrueorfalse,
      states: item.states,
      history: hasHistory(item.ocpictureid, item.picturecompare),
    }
  })
  return {
    pageCount: pages,
    data: list,
  }
}

// ocpictureid 比 picturecompare数据多，说明有历史数据
function hasHistory(ocpictureid: string, picturecompare: string) {
  const ids = ocpictureid.split(',')
  const compares = picturecompare.split(',')
  return ids.length > compares.length
}

/**
 * 设置真假
 * @param params
 * @returns
 */
export async function setTF(params: SetTFParam) {
  return await http.request({
    url: `/backstage/jifen/ocrtaskchildpicture/ordertrueorfalse`,
    method: 'post',
    params,
    headers: { 'Content-Type': ContentTypeEnum.FORM_DATA },
  })
}

/**
 * 清除之前标记的真假
 * @param id
 * @returns
 */
export async function clearTF(id: string, childId: string): Promise<any> {
  return await http.request({
    url: `/backstage/jifen/ocrtaskchildpicture/clearmark`,
    method: 'post',
    params: { packageid: id, taskchildpictureid: childId },
    headers: { 'Content-Type': ContentTypeEnum.FORM_DATA },
  })
}

/**
 * 将任务包内的任务按相似度排序
 * @param id
 * @returns
 */
export async function sort(id: string): Promise<any> {
  return await http.request({
    url: `/ocr/ocrPackagetask/listorderbytask`,
    method: 'get',
    params: { packageid: id },
  })
}
