<script setup lang="ts">
import { computed, reactive } from 'vue'
import { Link, usePage } from '@inertiajs/vue3'

const { props } = usePage<{
  component: any
  types: any[]
  origin: string
  errors?: any[]
}>()
const { component, types, origin } = props
const errors = computed(() => props.errors || [])

const form = reactive({
  id: component.id,
  name: component.name,
  price: component.price,
  type_id: component.type.id,
  purchase_date: component.purchaseDate,
  origin: origin,
})

function getErrorMessage(field: string): string | null {
  const error = errors.value.find((error) => error.field === field)
  return error ? error.message : null
}

function formatDateForMySQL(date: string) {
  const d = new Date(date)
  const year = d.getFullYear()
  const month = String(d.getMonth() + 1).padStart(2, '0')
  const day = String(d.getDate()).padStart(2, '0')
  form.purchase_date = `${year}-${month}-${day}`
}
</script>

<template>
  <div class="max-w-2xl mx-auto p-8 bg-white shadow-lg rounded-lg">
    <h1 class="text-2xl font-semibold mb-8 text-gray-800">Edit Component</h1>
    <form class="space-y-6">
      <div>
        <label class="block text-gray-700 text-sm font-bold mb-2" for="name">Name</label>
        <input
          name="name"
          v-model="form.name"
          class="w-full mt-2 border-gray-200 focus:border-indigo-600 focus:ring focus:ring-indigo-500/40 px-3 py-2 border rounded text-gray-700"
          type="text"
        />
      </div>

      <div>
        <label class="block text-gray-700 text-sm font-bold mb-2" for="price">Price</label>
        <input
          name="price"
          v-model="form.price"
          class="w-full mt-2 border-gray-200 focus:border-indigo-600 focus:ring focus:ring-indigo-500/40 px-3 py-2 border rounded text-gray-700"
          type="number"
          step="0.01"
        />
      </div>

      <div>
        <label class="block text-gray-700 text-sm font-bold mb-2" for="type">Type</label>
        <select
          v-model="form.type_id"
          id="type"
          class="w-full mt-2 border-gray-200 focus:border-indigo-600 focus:ring-indigo-500 px-3 py-2 focus:ring-1 border rounded text-gray-700 bg-white"
        >
          <option disabled :value="null">Choose a type</option>
          <option v-for="type in types" :key="type.id" :value="type.id">
            {{ type.name }}
          </option>
        </select>
      </div>

      <div>
        <label class="block text-gray-700 text-sm font-bold mb-2" for="purchaseDate"
          >Purchase Date</label
        >
        <input
          name="purchase_date"
          v-model="form.purchase_date"
          class="w-full mt-2 border-gray-200 focus:border-indigo-600 focus:ring focus:ring-indigo-500/40 border rounded text-gray-700 px-3 py-2"
          type="date"
        />
      </div>

      <Link
        :href="`/components/${form.id}`"
        method="put"
        :data="form"
        as="button"
        class="w-full md:w-auto px-4 py-2 bg-indigo-600 text-white rounded hover:bg-blue-700 transition text-center"
      >
        Save
      </Link>
    </form>
  </div>
</template>

<style scoped></style>
