<template>
  <company-block
    v-if="company"
    :company
    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 company = ref<CompanyParams | null>(null)
  
  async function updateMyCompany (company: CompanyParams) {
    if (company) {
      await authStore.updateMyCompany(company)
      router.go(-1)
    }
  }

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

</script>
