<template>
  <MainTemplate>
    <template #headerInsert>
      <RouterLink to="../" class="text-primary">zurück zur Liste (abbrechen)</RouterLink>
    </template>
    <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">Nutzer {{ user?.username }} - Daten bearbeiten</h1>
      </div>
    </template>
    <template #main>
      <Spinner v-if="loadingSingle == 'loading'" class="mx-auto" />
      <p v-else-if="loadingSingle == 'failed'">laden fehlgeschlagen</p>
      <form v-else class="flex flex-col gap-4 py-2 w-full max-w-xl mx-auto" @submit.prevent="triggerUpdateUser">
        <div>
          <label for="username">Nutzername</label>
          <input type="text" id="username" required :value="user?.username" @change="detectChange" />
        </div>
        <div>
          <label for="firstname">Vorname</label>
          <input type="text" id="firstname" required :value="user?.firstname" @change="detectChange" />
        </div>
        <div>
          <label for="lastname">Nachname</label>
          <input type="text" id="lastname" required :value="user?.lastname" @change="detectChange" />
        </div>
        <div>
          <label for="mail">Mailadresse</label>
          <input type="email" id="mail" required :value="user?.mail" @change="detectChange" />
        </div>
        <div class="flex flex-row justify-end gap-2">
          <button primary-outline type="reset" class="!w-fit" :disabled="!change_detect" @click="change_detect = false">
            verwerfen
          </button>
          <button primary type="submit" class="!w-fit" :disabled="updateStatus == 'loading'">speichern</button>
          <Spinner v-if="updateStatus == 'loading'" class="my-auto" />
          <SuccessCheckmark v-else-if="updateStatus?.status == 'success'" />
          <FailureXMark v-else-if="updateStatus?.status == 'failed'" />
        </div>
      </form>
    </template>
  </MainTemplate>
</template>

<script setup lang="ts">
import { defineComponent } from "vue";
import { mapState, mapActions } from "pinia";
import MainTemplate from "@/templates/Main.vue";
import Spinner from "@/components/Spinner.vue";
import SuccessCheckmark from "@/components/SuccessCheckmark.vue";
import FailureXMark from "@/components/FailureXMark.vue";
import { RouterLink } from "vue-router";
import { useUserStore } from "@/stores/admin/user";
import type { CreateOrUpdateUserViewModel, UserViewModel } from "@/viewmodels/admin/user.models";
</script>

<script lang="ts">
export default defineComponent({
  props: {
    id: String,
  },
  data() {
    return {
      change_detect: false as boolean,
    };
  },
  computed: {
    ...mapState(useUserStore, ["user", "loadingSingle", "updateStatus"]),
  },
  mounted() {
    this.resetStatus();
    this.fetchUserById(parseInt(this.id ?? ""));
  },
  methods: {
    ...mapActions(useUserStore, ["fetchUserById", "updateActiveUser", "resetStatus"]),
    detectChange() {
      this.resetStatus();
      this.change_detect = true;
    },
    triggerUpdateUser(e: any) {
      let formData = e.target.elements;
      let user: CreateOrUpdateUserViewModel = {
        username: formData.username.value,
        firstname: formData.firstname.value,
        lastname: formData.lastname.value,
        mail: formData.mail.value,
      };
      this.updateActiveUser(user);
      this.change_detect = false;
    },
  },
});
</script>
