<script setup>
import AppLayout from '@/Layouts/AppLayout.vue'
import { useForm } from '@inertiajs/vue3'
import { computed, watch } from 'vue'

const props = defineProps({
  setting: Object,
  decisions: Array,
  actions: Array,
})

const form = useForm({
  default_currency: props.setting?.default_currency ?? 'EUR',
  create_activity_on_payment: !!props.setting?.create_activity_on_payment,
  default_action_id: props.setting?.default_action_id ?? null,
  default_decision_id: props.setting?.default_decision_id ?? null,
  activity_note_template: props.setting?.activity_note_template ?? 'Prejeto plačilo: {amount} {currency}',
})

const filteredDecisions = computed(() => {
  const actionId = form.default_action_id
  if (!actionId) return []
  const action = props.actions?.find(a => a.id === actionId)
  if (!action || !action.decision_ids) return []
  const ids = new Set(action.decision_ids)
  return (props.decisions || []).filter(d => ids.has(d.id))
})

watch(() => form.default_action_id, (newVal) => {
  if (!newVal) {
    form.default_decision_id = null
  } else {
    // If current decision not in filtered list, clear it
    const ids = new Set((filteredDecisions.value || []).map(d => d.id))
    if (!ids.has(form.default_decision_id)) {
      form.default_decision_id = null
    }
  }
})

const submit = () => {
  form.put(route('settings.payment.update'), {
    preserveScroll: true,
  })
}
</script>

<template>
  <AppLayout title="Nastavitve plačil">
    <template #header></template>
    <div class="max-w-3xl mx-auto p-6">
      <div class="bg-white rounded-lg shadow-sm border border-gray-200 p-6">
        <h1 class="text-xl font-semibold text-gray-900">Nastavitve plačil</h1>

        <div class="mt-6 grid gap-6">
        <div>
          <label class="block text-sm text-gray-700 mb-1">Privzeta valuta</label>
          <input type="text" maxlength="3" v-model="form.default_currency" class="w-40 rounded border-gray-300" />
          <div v-if="form.errors.default_currency" class="text-sm text-red-600 mt-1">{{ form.errors.default_currency }}</div>
        </div>

        <div>
          <label class="inline-flex items-center gap-2">
            <input type="checkbox" v-model="form.create_activity_on_payment" />
            <span class="text-sm text-gray-700">Ustvari aktivnost ob dodanem plačilu</span>
          </label>
        </div>

        <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
          <div>
            <label class="block text-sm text-gray-700 mb-1">Privzeto dejanje</label>
            <select v-model="form.default_action_id" class="w-full rounded border-gray-300">
              <option :value="null">— Brez —</option>
              <option v-for="a in actions" :key="a.id" :value="a.id">{{ a.name }}</option>
            </select>
            <div v-if="form.errors.default_action_id" class="text-sm text-red-600 mt-1">{{ form.errors.default_action_id }}</div>
          </div>
          <div>
            <label class="block text-sm text-gray-700 mb-1">Privzeta odločitev</label>
            <select v-model="form.default_decision_id" class="w-full rounded border-gray-300" :disabled="!form.default_action_id">
              <option :value="null">— Najprej izberite dejanje —</option>
              <option v-for="d in filteredDecisions" :key="d.id" :value="d.id">{{ d.name }}</option>
            </select>
            <div v-if="form.errors.default_decision_id" class="text-sm text-red-600 mt-1">{{ form.errors.default_decision_id }}</div>
          </div>
        </div>

        <div>
          <label class="block text-sm text-gray-700 mb-1">Predloga opombe aktivnosti</label>
          <input type="text" v-model="form.activity_note_template" class="w-full rounded border-gray-300" />
          <p class="text-xs text-gray-500 mt-1">Podprti žetoni: {amount}, {currency}</p>
          <div v-if="form.errors.activity_note_template" class="text-sm text-red-600 mt-1">{{ form.errors.activity_note_template }}</div>
        </div>

          <div class="flex justify-end gap-2">
            <button type="button" class="px-4 py-2 rounded bg-gray-200 hover:bg-gray-300" @click="form.reset()">Ponastavi</button>
            <button type="button" class="px-4 py-2 rounded bg-indigo-600 text-white hover:bg-indigo-700" :disabled="form.processing" @click="submit">Shrani</button>
          </div>
        </div>
      </div>
    </div>
  </AppLayout>
</template>
