<template>
  <TaskList v-if="!task_id" :prj="prj" :account-list="accountList" :pDraft="draft" />
  <TaskDetail v-if="task_id" :task_id="task_id" :draft="draft" :account-list="accountList" />
</template>

<script lang="ts" setup>
import { PropType, ref } from 'vue'
import { useEmitt } from '@/logics/mitt/useEmitt'
import TaskList from './plan-task/task-list.vue'
import TaskDetail from './plan-task/task-detail.vue'
import { OverviewProjectResParams } from '@/types/project'

const task_id = ref()

const props = defineProps({
  prj: {
    type: Object as PropType<OverviewProjectResParams>,
    required: true
  },
  accountList: {
    type: Array as PropType<{ id: string; name: string }[]>,
  },
  navigateOptions: {
    type: Object as PropType<{ [key: string]: any }>,
  },
})
const draft = ref(props.navigateOptions?.draft ? JSON.parse(props.navigateOptions?.draft) : false)
// 监听子组件
useEmitt({
  name: 'redirectTaskDetailPage',
  callback: (list: { id: string; draft: boolean }) => {
    task_id.value = list.id
    draft.value = list.draft
  }
})

// 监听子组件
useEmitt({
  name: 'redirectTaskListPage',
  callback: () => {
    task_id.value = undefined
  }
})



// 监听子组件
useEmitt({
  name: 'handlerDraftChanged',
  callback: (isDraft: boolean) => {
    draft.value = isDraft
  }
})


onLoad((option) => {
  console.log('playout')
  console.log(option)
})
// 监听子组件增加，修改，删除事件，更新列表
// useEmitt({
//   name: 'get_task_list',
//   callback: () => {
//     console.log('task')
//   }
// })

//   listenerRouteChange(async (r: RouteLocationNormalized) => {
//     const id = r.query?.task_id // id
//     // const isDraft = r.query?.draft // id
//     if (id) {
//       taskId.value = id
//     }
//   })
onMounted(() => {
  if (props.navigateOptions?.draft) {
    draft.value = JSON.parse(props.navigateOptions?.draft as string)
  }
  if (props.navigateOptions?.task_id) {
    task_id.value = props.navigateOptions?.task_id
  }
})
</script>

<script lang="ts">
export default {
  name: 'ProjectPlan'
}
</script>