import type { FinalParam, ResetParam } from '/#/api'
import { http } from '@/utils/http/axios'
import { notEmpty } from '@/utils'
import { pickBy } from 'lodash-es'
import { formatToDate2 } from '@/utils/dateUtil'

/**
 * 获取审核列表
 * @returns
 */
export async function getFinalList(params: FinalParam) {
  const notEmptyParams = pickBy(params, notEmpty)

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

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

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

  const res = await http.request({
    url: `/flow/task/listfinal`,
    method: 'get',
    params: notEmptyParams,
  })

  const { data: { list, totalPage } } = res
  return {
    pageCount: totalPage,
    data: list,
  }
}

/**
 * 重置审批
 * @param params
 * @returns
 */
export async function resetApproval(params: ResetParam) {
  return http.request({
    url: `/flow/task/removeTask`,
    method: 'get',
    params,
  })
}
