<template>
  <MainTemplate>
    <template #topBar>
      <div class="flex flex-row items-center justify-between pt-5 pb-3 px-7">
        <h1 class="font-bold text-xl h-8">Fahrzeug erfassen</h1>
      </div>
    </template>
    <template #diffMain>
      <div class="flex flex-col gap-2 h-full w-full overflow-y-auto">
        <form class="flex flex-col gap-4 py-2 w-full max-w-xl mx-auto" @submit.prevent="triggerCreate">
          <VehicleTypeSearchSelect title="Typ" v-model="selectedType" />
          <div>
            <label for="name">Bezeichnung</label>
            <input type="text" id="name" required />
          </div>
          <ScanInput name="code" label="Code" :required="false" />
          <div>
            <label for="commissioned">In-Betrieb-Nahme</label>
            <input type="date" id="commissioned" required />
          </div>
          <div>
            <label for="location">Verortung (optional)</label>
            <input type="text" id="location" />
          </div>
          <div class="flex flex-row justify-end gap-2">
            <RouterLink
              :to="{ name: 'admin-unit-vehicle' }"
              primary-outline
              button
              class="w-fit!"
              :disabled="status == 'loading' || status?.status == 'success'"
            >
              abbrechen
            </RouterLink>
            <button primary type="submit" class="w-fit!" :disabled="status == 'loading'">speichern</button>
            <Spinner v-if="status == 'loading'" class="my-auto" />
            <SuccessCheckmark v-else-if="status?.status == 'success'" />
            <FailureXMark v-else-if="status?.status == 'failed'" />
          </div>
        </form>
      </div>
    </template>
  </MainTemplate>
</template>

<script setup lang="ts">
import { defineComponent } from "vue";
import { mapActions, mapState } from "pinia";
import MainTemplate from "@/templates/Main.vue";
import { useVehicleStore } from "@/stores/admin/unit/vehicle/vehicle";
import type { CreateVehicleViewModel } from "@/viewmodels/admin/unit/vehicle/vehicle.models";
import Spinner from "@/components/Spinner.vue";
import SuccessCheckmark from "@/components/SuccessCheckmark.vue";
import FailureXMark from "@/components/FailureXMark.vue";
import { useVehicleTypeStore } from "@/stores/admin/unit/vehicleType/vehicleType";
import VehicleTypeSearchSelect from "@/components/search/VehicleTypeSearchSelect.vue";
import ScanInput from "@/components/ScanInput.vue";
</script>

<script lang="ts">
export default defineComponent({
  data() {
    return {
      status: null as null | "loading" | { status: "success" | "failed"; reason?: string },
      timeout: null as any,
      selectedType: "" as string,
    };
  },
  computed: {
    ...mapState(useVehicleTypeStore, ["vehicleTypes"]),
  },
  beforeUnmount() {
    try {
      clearTimeout(this.timeout);
    } catch (error) {}
  },
  methods: {
    ...mapActions(useVehicleStore, ["createVehicle"]),
    triggerCreate(e: any) {
      if (this.selectedType == null) return;
      let formData = e.target.elements;
      let createVehicle: CreateVehicleViewModel = {
        name: formData.name.value,
        code: formData.code.value || null,
        location: formData.location.value,
        vehicleTypeId: this.selectedType,
        commissioned: formData.commissioned.value,
      };
      this.status = "loading";
      this.createVehicle(createVehicle)
        .then((res) => {
          this.status = { status: "success" };

          this.timeout = setTimeout(() => {
            this.$router.push({
              name: "admin-unit-vehicle-overview",
              params: {
                vehicleId: res.data,
              },
            });
          }, 1500);
        })
        .catch((err) => {
          this.status = { status: "failed" };
        });
    },
  },
});
</script>
