<template>
  <div class="max-w-4xl mx-auto p-6">
    <div class="bg-white rounded-lg shadow-lg p-8">
      <h2 class="text-3xl font-bold text-gray-800 mb-2">
        Undangan Ulang Tahun Premium
      </h2>
      <p class="text-gray-600 mb-6">Harga: Rp 200.000</p>

      <form @submit.prevent="submitForm" class="space-y-6">
        <!-- Data Pemesan -->
        <div class="border-b pb-6">
          <h3 class="text-xl font-semibold text-gray-700 mb-4">Data Pemesan</h3>
          
          <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
            <div>
              <label class="block text-sm font-medium text-gray-700 mb-2">
                Nama Pemesan <span class="text-red-500">*</span>
              </label>
              <input
                v-model="formData.nama_pemesan"
                type="text"
                required
                class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent"
                placeholder="Masukkan nama pemesan"
              />
            </div>

            <div>
              <label class="block text-sm font-medium text-gray-700 mb-2">
                Email <span class="text-red-500">*</span>
              </label>
              <input
                v-model="formData.email"
                type="email"
                required
                class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent"
                placeholder="contoh@email.com"
              />
            </div>

            <div>
              <label class="block text-sm font-medium text-gray-700 mb-2">
                No Telepon
              </label>
              <input
                v-model="formData.no_telepon"
                type="text"
                class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent"
                placeholder="08xxxxxxxxxx"
              />
            </div>
          </div>
        </div>

        <!-- Data Yang Berulang Tahun -->
        <div class="border-b pb-6">
          <h3 class="text-xl font-semibold text-gray-700 mb-4">Data Yang Berulang Tahun</h3>
          
          <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
            <div>
              <label class="block text-sm font-medium text-gray-700 mb-2">
                Nama Lengkap
              </label>
              <input
                v-model="formData.nama_lengkap"
                type="text"
                class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent"
                placeholder="Nama lengkap"
              />
            </div>

            <div>
              <label class="block text-sm font-medium text-gray-700 mb-2">
                Nama Panggilan
              </label>
              <input
                v-model="formData.nama_panggilan"
                type="text"
                class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent"
                placeholder="Nama panggilan"
              />
            </div>

            <div>
              <label class="block text-sm font-medium text-gray-700 mb-2">
                Nama Bapak
              </label>
              <input
                v-model="formData.nama_bapak"
                type="text"
                class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent"
                placeholder="Nama bapak"
              />
            </div>

            <div>
              <label class="block text-sm font-medium text-gray-700 mb-2">
                Nama Ibu
              </label>
              <input
                v-model="formData.nama_ibu"
                type="text"
                class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent"
                placeholder="Nama ibu"
              />
            </div>

            <div>
              <label class="block text-sm font-medium text-gray-700 mb-2">
                Umur Yang Dirayakan
              </label>
              <input
                v-model.number="formData.umur_yang_dirayakan"
                type="number"
                class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent"
                placeholder="Contoh: 7"
              />
            </div>

            <div>
              <label class="block text-sm font-medium text-gray-700 mb-2">
                Anak Ke
              </label>
              <input
                v-model.number="formData.anak_ke"
                type="number"
                class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent"
                placeholder="Contoh: 1"
              />
            </div>
          </div>
        </div>

        <!-- Media Sosial -->
        <div class="border-b pb-6">
          <h3 class="text-xl font-semibold text-gray-700 mb-4">Media Sosial</h3>
          
          <div class="grid grid-cols-1 gap-4">
            <div>
              <label class="block text-sm font-medium text-gray-700 mb-2">
                Link Instagram
              </label>
              <input
                v-model="formData.instagram"
                type="text"
                class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent"
                placeholder="https://instagram.com/username"
              />
            </div>

            <div>
              <label class="block text-sm font-medium text-gray-700 mb-2">
                Link Facebook
              </label>
              <input
                v-model="formData.facebook"
                type="text"
                class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent"
                placeholder="https://facebook.com/username"
              />
            </div>

            <div>
              <label class="block text-sm font-medium text-gray-700 mb-2">
                Link Twitter
              </label>
              <input
                v-model="formData.twitter"
                type="text"
                class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent"
                placeholder="https://twitter.com/username"
              />
            </div>
          </div>
        </div>

        <!-- Detail Acara -->
        <div class="border-b pb-6">
          <h3 class="text-xl font-semibold text-gray-700 mb-4">Detail Acara</h3>
          
          <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
            <div>
              <label class="block text-sm font-medium text-gray-700 mb-2">
                Hari & Tanggal Acara
              </label>
              <input
                v-model="formData.hari_tanggal_acara"
                type="date"
                class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent"
              />
            </div>

            <div>
              <label class="block text-sm font-medium text-gray-700 mb-2">
                Waktu
              </label>
              <input
                v-model="formData.waktu"
                type="text"
                class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent"
                placeholder="Contoh: 14.00 - 16.00 WIB"
              />
            </div>

            <div class="md:col-span-2">
              <label class="block text-sm font-medium text-gray-700 mb-2">
                Alamat
              </label>
              <input
                v-model="formData.alamat"
                type="text"
                class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent"
                placeholder="Alamat lengkap acara"
              />
            </div>

            <div class="md:col-span-2">
              <label class="block text-sm font-medium text-gray-700 mb-2">
                Link Google Maps
              </label>
              <input
                v-model="formData.link_gmaps"
                type="text"
                class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent"
                placeholder="https://maps.google.com/..."
              />
            </div>

            <div class="md:col-span-2">
              <label class="block text-sm font-medium text-gray-700 mb-2">
                Say Something
              </label>
              <textarea
                v-model="formData.say_something"
                rows="4"
                class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent"
                placeholder="Pesan atau kata-kata untuk undangan"
              ></textarea>
            </div>
          </div>
        </div>

        <!-- Rekening -->
        <div class="border-b pb-6">
          <h3 class="text-xl font-semibold text-gray-700 mb-4">Rekening (Opsional)</h3>
          
          <div class="grid grid-cols-1 gap-4">
            <div>
              <label class="block text-sm font-medium text-gray-700 mb-2">
                Rekening 1
              </label>
              <input
                v-model="formData.rekening_1"
                type="text"
                class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent"
                placeholder="Contoh: BCA - 1234567890 - Nama Pemilik"
              />
            </div>

            <div>
              <label class="block text-sm font-medium text-gray-700 mb-2">
                Rekening 2
              </label>
              <input
                v-model="formData.rekening_2"
                type="text"
                class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent"
                placeholder="Contoh: Mandiri - 9876543210 - Nama Pemilik"
              />
            </div>

            <div>
              <label class="block text-sm font-medium text-gray-700 mb-2">
                Rekening 3
              </label>
              <input
                v-model="formData.rekening_3"
                type="text"
                class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent"
                placeholder="Contoh: BNI - 5555555555 - Nama Pemilik"
              />
            </div>
          </div>
        </div>

        <!-- Upload Foto -->
        <div class="border-b pb-6">
          <h3 class="text-xl font-semibold text-gray-700 mb-4">Upload Foto</h3>
          
          <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
            <div v-for="i in 5" :key="i">
              <label class="block text-sm font-medium text-gray-700 mb-2">
                Foto {{ i }}
              </label>
              <input
                type="file"
                accept="image/*"
                @change="handleFileUpload($event, `foto_${i}`)"
                class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent"
              />
              <p v-if="formData[`foto_${i}`]" class="text-sm text-green-600 mt-1">
                ✓ File dipilih: {{ formData[`foto_${i}`].name }}
              </p>
            </div>
          </div>
        </div>

        <!-- Link Music -->
        <div class="pb-6">
          <h3 class="text-xl font-semibold text-gray-700 mb-4">Background Music</h3>
          
          <div>
            <label class="block text-sm font-medium text-gray-700 mb-2">
              Link Music
            </label>
            <input
              v-model="formData.link_music"
              type="text"
              class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent"
              placeholder="Link YouTube atau file musik"
            />
          </div>
        </div>

        <!-- Submit Button -->
        <div class="flex gap-4">
          <button
            type="submit"
            :disabled="loading"
            class="flex-1 bg-blue-600 hover:bg-blue-700 text-white font-semibold py-3 px-6 rounded-lg transition duration-200 disabled:bg-gray-400 disabled:cursor-not-allowed"
          >
            {{ loading ? 'Mengirim...' : 'Kirim Pesanan' }}
          </button>
          <button
            type="button"
            @click="resetForm"
            class="px-6 py-3 border border-gray-300 text-gray-700 font-semibold rounded-lg hover:bg-gray-50 transition duration-200"
          >
            Reset
          </button>
        </div>
      </form>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'

const formData = ref({
  nama_pemesan: '',
  email: '',
  no_telepon: '',
  nama_lengkap: '',
  nama_panggilan: '',
  nama_bapak: '',
  nama_ibu: '',
  umur_yang_dirayakan: null,
  anak_ke: null,
  instagram: '',
  facebook: '',
  twitter: '',
  hari_tanggal_acara: '',
  waktu: '',
  alamat: '',
  link_gmaps: '',
  say_something: '',
  rekening_1: '',
  rekening_2: '',
  rekening_3: '',
  foto_1: null,
  foto_2: null,
  foto_3: null,
  foto_4: null,
  foto_5: null,
  link_music: ''
})

const loading = ref(false)

const handleFileUpload = (event, fieldName) => {
  const file = event.target.files[0]
  if (file) {
    formData.value[fieldName] = file
  }
}

const submitForm = async () => {
  loading.value = true
  
  try {
    const formDataToSend = new FormData()
    
    // Append semua data ke FormData
    Object.keys(formData.value).forEach(key => {
      if (formData.value[key] !== null && formData.value[key] !== '') {
        formDataToSend.append(key, formData.value[key])
      }
    })
    
    // Kirim ke API Laravel
    const response = await $fetch('/api/orders/ulang-tahun-premium', {
      method: 'POST',
      body: formDataToSend
    })
    
    alert('Pesanan berhasil dikirim!')
    resetForm()
    
    // Redirect atau tindakan lainnya
    // navigateTo('/success')
    
  } catch (error) {
    console.error('Error:', error)
    alert('Terjadi kesalahan saat mengirim pesanan')
  } finally {
    loading.value = false
  }
}

const resetForm = () => {
  formData.value = {
    nama_pemesan: '',
    email: '',
    no_telepon: '',
    nama_lengkap: '',
    nama_panggilan: '',
    nama_bapak: '',
    nama_ibu: '',
    umur_yang_dirayakan: null,
    anak_ke: null,
    instagram: '',
    facebook: '',
    twitter: '',
    hari_tanggal_acara: '',
    waktu: '',
    alamat: '',
    link_gmaps: '',
    say_something: '',
    rekening_1: '',
    rekening_2: '',
    rekening_3: '',
    foto_1: null,
    foto_2: null,
    foto_3: null,
    foto_4: null,
    foto_5: null,
    link_music: ''
  }
  
  // Reset file inputs
  const fileInputs = document.querySelectorAll('input[type="file"]')
  fileInputs.forEach(input => {
    input.value = ''
  })
}
</script>

<style scoped>
input:focus,
textarea:focus {
  outline: none;
}
</style>