<template>
  <task-block
    v-model="taskMod"
    title="task_edit__title_card"
    btnText="task_edit__btn"
    @update=updateTask()
  />
</template>

<script setup lang="ts">
  import { ref } from 'vue'
  import { useRouter, useRoute } from 'vue-router'
  import taskBlock from 'components/taskBlock.vue'
  import { useTasksStore } from 'stores/tasks'
  import type { TaskParams } from 'types/Task'
  import { parseIntString } from 'src/helpers/helpers'

  const router = useRouter()
  const route = useRoute()
  const tasksStore = useTasksStore()
  const taskId = parseIntString(route.params.taskId)
  const initialTask = taskId && tasksStore.taskById(taskId)

  const taskMod = ref({
    ...initialTask 
  } as TaskParams)
  
  async function updateTask () {
    if (!taskId || !initialTask) return
    await tasksStore.update(taskId, taskMod.value)
    router.go(-1)
  }

</script>
