<script setup lang="ts">
import Task from './Task.vue'
import TaskTemplate from './TaskTemplate/index.vue'
import { nextTick, ref } from 'vue'

const taskRef = ref<{ currentTaskID: string; restoreFromHistory: (plan: any) => void }>()
const taskTemplateRef = ref<{
  changeTask: () => void
  clear: () => void
  openHistoryDialog: () => void
}>()

function handleSearch() {
  nextTick(() => {
    taskTemplateRef.value?.changeTask()
  })
}

// 获取当前任务的 TaskID
function getTaskID(): string {
  return taskRef.value?.currentTaskID || ''
}

// 打开历史记录弹窗
function openHistoryDialog() {
  taskTemplateRef.value?.openHistoryDialog()
}

// 处理历史任务恢复
function handleRestorePlan(plan: any) {
  taskRef.value?.restoreFromHistory(plan)
}

defineExpose({
  getTaskID,
  openHistoryDialog
})
</script>

<template>
  <div class="p-[24px] h-[calc(100%-60px)]">
    <Task
      ref="taskRef"
      @search="handleSearch"
      @search-start="taskTemplateRef?.clear"
      @open-history="openHistoryDialog"
    />
    <TaskTemplate
      ref="taskTemplateRef"
      :TaskID="taskRef?.currentTaskID"
      @restore="handleRestorePlan"
    />
  </div>
</template>
