<template>
  <div class="w-full md:max-w-md">
    <div class="flex flex-col items-center">
      <p class="text-xl font-medium">Rolle erstellen</p>
    </div>
    <br />
    <form class="flex flex-col gap-4 py-2" @submit.prevent="triggerCreateRole">
      <div>
        <label for="role">Rollenbezeichnung</label>
        <input type="text" id="role" required />
      </div>
      <div class="flex flex-row gap-2">
        <button primary type="submit" :disabled="createStatus == 'loading' || createStatus?.status == 'success'">
          erstellen
        </button>
        <Spinner v-if="createStatus == 'loading'" class="my-auto" />
        <SuccessCheckmark v-else-if="createStatus?.status == 'success'" />
        <FailureXMark v-else-if="createStatus?.status == 'failed'" />
      </div>
    </form>

    <div class="flex flex-row justify-end">
      <div class="flex flex-row gap-4 py-2">
        <button primary-outline @click="closeModal">schließen</button>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { defineComponent } from "vue";
import { mapState, mapActions } from "pinia";
import { useModalStore } from "@/stores/modal";
import Spinner from "@/components/Spinner.vue";
import SuccessCheckmark from "@/components/SuccessCheckmark.vue";
import FailureXMark from "@/components/FailureXMark.vue";
import { useRoleStore } from "@/stores/admin/role";
</script>

<script lang="ts">
export default defineComponent({
  mounted() {
    this.resetStatus();
  },
  computed: {
    ...mapState(useRoleStore, ["createStatus"]),
  },
  methods: {
    ...mapActions(useModalStore, ["closeModal"]),
    ...mapActions(useRoleStore, ["createRole", "resetStatus"]),
    triggerCreateRole(e: any) {
      let formData = e.target.elements;
      this.createRole(formData.role.value);
    },
  },
});
</script>
