<script setup lang="ts">
import type { Purchase, PurchaseInsert } from '~/types/purchase'

const props = defineProps<{
  open: boolean
  initial?: Purchase
}>()

const emit = defineEmits<{
  'update:open': [value: boolean]
  submit: [data: PurchaseInsert]
}>()

const title = computed(() => props.initial ? '장비 수정' : '장비 추가')

function handleSubmit(data: PurchaseInsert) {
  // 부모의 비동기 핸들러가 완료된 후 모달을 닫으므로 여기서 닫지 않음
  emit('submit', data)
}
</script>

<template>
  <UModal :open="open" :title="title" @update:open="emit('update:open', $event)">
    <template #body>
      <PurchasesPurchaseForm
        :initial="initial"
        @submit="handleSubmit"
        @cancel="emit('update:open', false)"
      />
    </template>
  </UModal>
</template>
