<template>
  <company-block
    v-if="companyMod"
    v-model="companyMod"
    title="account_company__title_card"
    btnText="account_company__btn"
    @update="updateMyCompany"
  />
</template>

<script setup lang="ts">
  import { ref, onMounted } from 'vue'
  import { useRouter } from 'vue-router'
  import companyBlock from 'components/companyBlock.vue'
  import { useAuthStore } from 'stores/auth'
  import type { CompanyParams } from 'types/Company'

  const router = useRouter()
  const authStore = useAuthStore()

  const companyMod = ref<CompanyParams | null>(null)
  
  async function updateMyCompany () {
    if (companyMod.value) {
      await authStore.updateMyCompany(companyMod.value)
      router.go(-1)
    }
  }

  onMounted(() => {
    if (authStore.customer?.company) {
      companyMod.value = authStore.customer?.company as CompanyParams
    } else {
      companyMod.value = {
        name: '',
        logo: '',
        description: '',
        site: '',
        address: '',
        phone: '',
        email: ''
      }
    }
  })

</script>
