import request from '@/utils/request';
import type { ApiResult, PageResult } from '@/api';
import type { Task, TaskParam } from './model/index';
import { TabsParam } from '@/api/tabs';
import { MODULES_API_URL } from '@/config/setting';

/**
 * 分页查询任务
 */
export async function pageTask(params: TaskParam) {
  const res = await request.get<ApiResult<PageResult<Task>>>(
    MODULES_API_URL + '/oa/task/page',
    {
      params
    }
  );
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 查询任务列表
 */
export async function listTask(params?: TaskParam) {
  const res = await request.get<ApiResult<Task[]>>(
    MODULES_API_URL + '/oa/task',
    {
      params
    }
  );
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 根据id查询任务
 */
export async function getTask(id: number) {
  const res = await request.get<ApiResult<Task>>(
    MODULES_API_URL + '/oa/task/' + id
  );
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 添加任务
 */
export async function addTask(data: Task) {
  const res = await request.post<ApiResult<Task>>(
    MODULES_API_URL + '/oa/task',
    data
  );
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 修改任务
 */
export async function updateTask(data: Task) {
  const res = await request.put<ApiResult<unknown>>(
    MODULES_API_URL + '/oa/task',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 删除任务
 */
export async function removeTask(id?: number) {
  const res = await request.delete<ApiResult<unknown>>(
    MODULES_API_URL + '/oa/task/' + id
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 批量删除任务
 */
export async function removeBatchTask(data: (number | undefined)[]) {
  const res = await request.delete<ApiResult<unknown>>(
    MODULES_API_URL + '/oa/task/batch',
    {
      data
    }
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

export async function getCount(params?: TabsParam) {
  const res = await request.get<ApiResult<TabsParam>>(
    MODULES_API_URL + '/oa/task/count',
    {
      params
    }
  );
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 获取工单二维码
 * @param params
 */
export async function taskQRCode(params: TaskParam) {
  const res = await request.get<ApiResult<PageResult<Task>>>(
    MODULES_API_URL + '/oa/task/taskQRCode',
    {
      params
    }
  );
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 获取邀请加入二维码
 * @param params
 */
export async function taskJoinQRCode(params: TaskParam) {
  const res = await request.get<ApiResult<PageResult<Task>>>(
    MODULES_API_URL + '/oa/task/taskJoinQRCode',
    {
      params
    }
  );
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}
