<script setup>
import AppLayout from '@/Layouts/AppLayout.vue';
import { Link, useForm } from '@inertiajs/vue3';
import DialogModal from '@/Components/DialogModal.vue';
import PrimaryButton from '@/Components/PrimaryButton.vue';
import InputLabel from '@/Components/InputLabel.vue';
import InputError from '@/Components/InputError.vue';
import { ref, onMounted } from 'vue';
import Multiselect from 'vue-multiselect';

const props = defineProps({
  settings: Array,
  segments: Array,
  decisions: Array,
});

const showCreate = ref(false);
const segmentOptions = ref([]);
const decisionOptions = ref([]);

onMounted(() => {
  segmentOptions.value = (props.segments || []).map(s => ({ id: s.id, name: s.name }));
  decisionOptions.value = (props.decisions || []).map(d => ({ id: d.id, name: d.name }));
});

const form = useForm({
  segment_id: null,
  initial_decision_id: null,
  asign_decision_id: null,
  complete_decision_id: null,
});

const openCreate = () => {
  form.reset();
  showCreate.value = true;
};

const closeCreate = () => {
  showCreate.value = false;
  form.reset();
};

const store = () => {
  form.post(route('settings.fieldjob.store'), {
    preserveScroll: true,
    onSuccess: () => closeCreate(),
  });
};
</script>

<template>
  <AppLayout title="Field Job Settings">
    <template #header></template>
    <div class="pt-12">
      <div class="max-w-7xl mx-auto sm:px-6 lg:px-8">
        <div class="bg-white overflow-hidden shadow-xl sm:rounded-lg p-6">
          <div class="flex items-center justify-between mb-4">
            <h2 class="text-xl font-semibold">Field Job Settings</h2>
            <PrimaryButton @click="openCreate">+ New</PrimaryButton>
          </div>

          <DialogModal :show="showCreate" @close="closeCreate">
            <template #title>
              Create Field Job Setting
            </template>
            <template #content>
              <form @submit.prevent="store">
                <div class="grid grid-cols-1 gap-4">
                  <div>
                    <InputLabel for="segment" value="Segment" />
                    <multiselect
                      id="segment"
                      v-model="form.segment_id"
                      :options="segmentOptions.map(o=>o.id)"
                      :multiple="false"
                      :searchable="true"
                      placeholder="Select segment"
                      :append-to-body="true"
                      :custom-label="(opt) => (segmentOptions.find(o=>o.id===opt)?.name || '')"
                    />
                    <InputError :message="form.errors.segment_id" class="mt-1" />
                  </div>

                  <div>
                    <InputLabel for="initialDecision" value="Initial Decision" />
                    <multiselect
                      id="initialDecision"
                      v-model="form.initial_decision_id"
                      :options="decisionOptions.map(o=>o.id)"
                      :multiple="false"
                      :searchable="true"
                      placeholder="Select initial decision"
                      :append-to-body="true"
                      :custom-label="(opt) => (decisionOptions.find(o=>o.id===opt)?.name || '')"
                    />
                    <InputError :message="form.errors.initial_decision_id" class="mt-1" />
                  </div>

                  <div>
                    <InputLabel for="assignDecision" value="Assign Decision" />
                    <multiselect
                      id="assignDecision"
                      v-model="form.asign_decision_id"
                      :options="decisionOptions.map(o=>o.id)"
                      :multiple="false"
                      :searchable="true"
                      placeholder="Select assign decision"
                      :append-to-body="true"
                      :custom-label="(opt) => (decisionOptions.find(o=>o.id===opt)?.name || '')"
                    />
                    <InputError :message="form.errors.asign_decision_id" class="mt-1" />
                  </div>

                  <div class="mt-2">
                    <InputLabel for="completeDecision" value="Complete Decision" />
                    <multiselect
                      id="completeDecision"
                      v-model="form.complete_decision_id"
                      :options="decisionOptions.map(o=>o.id)"
                      :multiple="false"
                      :searchable="true"
                      placeholder="Select complete decision"
                      :append-to-body="true"
                      :custom-label="(opt) => (decisionOptions.find(o=>o.id===opt)?.name || '')"
                    />
                    <InputError :message="form.errors.complete_decision_id" class="mt-1" />
                  </div>
                </div>

                <div class="flex justify-end gap-2 mt-6">
                  <button type="button" @click="closeCreate" class="px-4 py-2 rounded bg-gray-200 hover:bg-gray-300">Cancel</button>
                  <PrimaryButton :disabled="form.processing">Create</PrimaryButton>
                </div>
              </form>
            </template>
          </DialogModal>
          <table class="min-w-full text-left text-sm">
            <thead>
              <tr class="border-b">
                <th class="py-2 pr-4">ID</th>
                <th class="py-2 pr-4">Segment</th>
                <th class="py-2 pr-4">Initial Decision</th>
                <th class="py-2 pr-4">Assign Decision</th>
                <th class="py-2 pr-4">Complete Decision</th>
              </tr>
            </thead>
            <tbody>
              <tr v-for="row in settings" :key="row.id" class="border-b last:border-0">
                <td class="py-2 pr-4">{{ row.id }}</td>
                <td class="py-2 pr-4">{{ row.segment?.name }}</td>
                <td class="py-2 pr-4">{{ row.initial_decision?.name || row.initialDecision?.name }}</td>
                <td class="py-2 pr-4">{{ row.asign_decision?.name || row.asignDecision?.name }}</td>
                <td class="py-2 pr-4">{{ row.complete_decision?.name || row.completeDecision?.name }}</td>
              </tr>
            </tbody>
          </table>
        </div>
      </div>
    </div>
  </AppLayout>
</template>
