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

import { ContentTypeEnum } from '@/enums/httpEnum'

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

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

/**
 * 任务包列表主图-新 
 * @returns
 */
export async function getPackagePicture(params: PageParam, packagename: string) {
  const res = await http.request({
    url: `/ocr/ocrPicture/getPackagePicture`,
    method: 'get',
    params: { ...params, packagename },
  })

  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 pid = packageid || 0

  const res = await http.request({
    url: `/backstage/jifen/ocrtaskchildpicture/listbypackageid/${pid}`,
    method: 'get',
    params,
  })

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

/**
 * 任务包列表主图-新 
 * @param checkDuplicateId  查重id
 * @returns
 */
export async function getTaskDetailInfo(checkDuplicateId: string) {

  const res = await http.request({
    url: `/ocr/ocrPicture/getPackagePicture?checkDuplicateId=${checkDuplicateId}`,
    method: 'get',
  })

  return res.data
}

/**
 *  获取某个任务详情(相似图片列表)
 * @param packageid 任务包id
 * @param taskchildpictureid 任务详情的id
 * @param params
 * @returns
 */
export async function getTaskDetailPictureList(params: PageParam & PictureSortParam & SimilarityPictureSortParam) {

  const res = await http.request({
    url: `/backstage/jifen/ocrtaskchildpicture/getPictureSimilarityList`,
    method: 'get',
    params,
  })

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

  // 精简一下数据
  const list = records.map((item,index) => {
    return {
      id: item.id,
      taskId: item.taskId,
      taskname: item.fromTaskName,
      assignee: item.assignee,
      pictureid: item.pictureid,
      imgurl: item.serverThumbnailUrl,
      maxSimilarity: item.maxSimilarity,
      thumburl: item.ocrPicture?.serverThumbnailUrl || item.ocrPicture?.imgurl,
      iztrueorfalse: item.iztrueorfalse,
      states: item.states,
      history: item.ocpictureid && item.picturecompare && hasHistory(item.ocpictureid, item.picturecompare),
      historyStates:item.historyStates,
      submitDateTimestamp:item.submitDateTimestamp,
      photoDateTimestamp:item.photoDateTimestamp,
    }
  })
  return {
    pageCount: pages,
    data: list,
    total,
  }
}

// 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 },
  })
}

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

/**
 * ai审批工具
 * @param id
 * @returns
 */
export async function aiApprovaltools(params): Promise<any> {
  return await http.request({
    url: `/ocr/aitools/aiApprovaltools`,
    method: 'get',
    params,
  })
}

/**
 * ai审批工具批量设置假
 * @param id
 * @returns
 */
export async function aiApprovaltoolsClearmark(params): Promise<any> {
  return await http.request({
    url: `/ocr/aitools/aiApprovaltoolsClearmark`,
    method: 'get',
    params,
  })
}
