import { useMain as useTrainMain } from '@/views/modelTraining/index/hooks/index.ts'
import { ref, h } from 'vue'
import { Modal, message } from 'ant-design-vue';
import type { ModalProps } from 'ant-design-vue'

export const useMain = () => {
  const { listRequest, createTaskRequest, onInterruptRequest, statusRequest, logsRequest, deleteRequest } = useTrainMain()

  const listData = ref([])
  // 获取任务结果
  const getTaskList = async () => {
    const res = await listRequest.request()
    if (res?.code == 200) {
      listData.value = res.data
    }
    return res
  }

  // 创建任务
  const createTask = async (params: Record<string, any>) => {
    const res = await createTaskRequest.request(params)
    return res
  }

  // 中断任务
  const onInterrupt = async (id: string) => {
    const res = await onInterruptRequest.request(id)
    return res
  }

  // 获取任务状态
  const getStatus = async (id: string) => {
    const res = await statusRequest.request(id)
    return res
  }

  // 获取任务日志
  const getLogs = async (id: string) => {
    const res = await logsRequest.request(id)
    return res
  }

  // 构造删除删除弹出层
  const createDeleteProps = () => {
    return {
      centered: true,
      title: h("div", { style: "font-size: 16px" }, "删除数据"),
      icon: h("span"),
      content: h(
        "div",
        { style: "font-size: 16px; margin-bottom: 20px" },
        "确定要删除当前数据吗?本次操作无法撤回",
      ),
      okText: "确定",
      cancelText: "取消",
      onOk: async () => {},
      onCancel() {},
    }


};

  // 删除任务
  const onShowTaskDelete = async (taskId: string) => {
    return new Promise((resolve, reject) => {
      const modalProps = createDeleteProps()
      modalProps.onOk = async () => {
        const res = await onDelete(taskId)
        if (res?.code == 200) {
          message.success('删除成功')
          resolve(res)
        } else {
          reject(res)
        }
      }
      Modal.confirm(modalProps);
    })
  }

  const onDelete = async (info: any) => {
    const res = await deleteRequest.request(info)
    return res
  }


  const state = {
    listData: listData
  }

  return {
    state: state,
    api: {
      getTaskList,
      createTask,
      onInterrupt,
      getStatus,
      getLogs,
      onShowTaskDelete,
      onDelete
    },
    requestMap: {
      logsRequest,
      statusRequest,
      createTaskRequest,
      onInterruptRequest,
      listRequest,
      deleteRequest
    }
  }
}
