<template>
  <project-block
    :project
    title="project_edit__title_card"
    btnText="project_edit__btn"
    @update="updateProject"
  />
</template>

<script setup lang="ts">
  import { computed } from 'vue'
  import { useRouter } from 'vue-router'
  import projectBlock from 'components/projectBlock.vue'
  import { useProjectsStore } from 'stores/projects'
  import type { ProjectParams } from 'types/Project'

  const router = useRouter()
  const projectsStore = useProjectsStore()

  const projectId = computed(() => projectsStore.currentProjectId)
  
  const project = projectId.value && projectsStore.projectById(projectId.value) || {} as ProjectParams

  async function updateProject (projectData: ProjectParams) {
    if (!projectId .value || !project) return
    await projectsStore.update(projectId.value, projectData)
    router.go(-1)
  }

</script>
