<template>
  <MainTemplate title="Ausrüstung erfassen">
    <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">
          <MemberSearchSelectSingle title="Träger" useScanner v-model="selectedRespiratoryWearer" />
          <div class="flex flex-row justify-end gap-2">
            <RouterLink
              :to="{ name: 'admin-unit-respiratory_wearer' }"
              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 { useRespiratoryWearerStore } from "@/stores/admin/unit/respiratoryWearer/respiratoryWearer";
import type { CreateRespiratoryWearerViewModel } from "@/viewmodels/admin/unit/respiratory/respiratoryWearer.models";
import Spinner from "@/components/Spinner.vue";
import SuccessCheckmark from "@/components/SuccessCheckmark.vue";
import FailureXMark from "@/components/FailureXMark.vue";
import MemberSearchSelectSingle from "@/components/search/MemberSearchSelectSingle.vue";
</script>

<script lang="ts">
export default defineComponent({
  data() {
    return {
      status: null as null | "loading" | { status: "success" | "failed"; reason?: string },
      timeout: null as any,
      selectedRespiratoryWearer: "" as string,
    };
  },
  beforeUnmount() {
    try {
      clearTimeout(this.timeout);
    } catch (error) {}
  },
  methods: {
    ...mapActions(useRespiratoryWearerStore, ["createRespiratoryWearer"]),
    triggerCreate(e: any) {
      if (this.selectedRespiratoryWearer == null) return;
      let formData = e.target.elements;
      let createRespiratoryWearer: CreateRespiratoryWearerViewModel = {
        memberId: this.selectedRespiratoryWearer,
      };
      this.status = "loading";
      this.createRespiratoryWearer(createRespiratoryWearer)
        .then((res) => {
          this.status = { status: "success" };

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