<template>
  <div class="page-wrapper">
    <main class="create-object-page">
      <div class="container max-w-4xl">
        <div class="page-header">
          <div class="header-content">
            <div class="header-text">
              <h1 class="page-title">Добавить новый объект</h1>
              <p class="page-subtitle">Заполните информацию о вашем объекте</p>
            </div>
            <NuxtLink to="/objects/my-objects" class="btn btn-outline btn-with-icon">
              ← Мои объекты
            </NuxtLink>
          </div>
        </div>

        <ObjectForm :loading="loading" @submit="handleSubmit" @cancel="handleCancel" />
      </div>
    </main>

  </div>
</template>

<script setup lang="ts">
const { create } = useObjects()
const loading = ref(false)

const handleSubmit = async (formData: Record<string, unknown>) => {
  loading.value = true
  try {
    await create(formData as Parameters<typeof create>[0])
    navigateTo('/objects/my-objects')
  } catch (error) {
    console.error('Error creating object:', error)
    alert('Ошибка при создании объекта')
  } finally {
    loading.value = false
  }
}

const handleCancel = () => {
  navigateTo('/objects/my-objects')
}
</script>

<style scoped>
.create-object-page {
  padding: var(--space-xl) 0;
  min-height: calc(100vh - 160px);
  background: var(--bg-secondary);
}

.page-header {
  background: var(--bg-primary);
  border-radius: var(--radius-lg);
  padding: var(--space-xl);
  margin-bottom: var(--space-lg);
  box-shadow: var(--shadow-sm);
  border: 1px solid var(--border-light);
}

.header-content {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: var(--space-xl);
}

.header-text {
  display: flex;
  flex-direction: column;
  gap: var(--space-xs);
}

.page-title {
  font-family: var(--font-heading);
  font-size: var(--text-3xl);
  font-weight: var(--font-bold);
  color: var(--text-primary);
  margin: 0;
}

.page-subtitle {
  font-size: var(--text-lg);
  color: var(--text-secondary);
  margin: 0;
}

@media (max-width: 768px) {
  .header-content {
    flex-direction: column;
  }
}
</style>
