<template>
  <MainTemplate :showBack="false">
    <template #headerInsert>
      <RouterLink :to="{ name: 'admin-operation-mission' }" class="text-primary !w-fit">zurück zur Liste</RouterLink>
    </template>
    <template #diffMain>
      <div class="flex flex-col gap-2 grow px-2 overflow-hidden">
        <div class="flex flex-col grow gap-2 overflow-hidden">
          <div class="w-full flex flex-row justify-center">
            <RouterLink
              v-for="tab in tabs"
              :key="tab.hash"
              :to="{ hash: tab.hash }"
              replace
              class="w-full p-0.5 first:pl-0 last:pr-0"
            >
              <p
                :class="[
                  'w-full rounded-lg py-2.5 text-sm text-center font-medium leading-5 focus:ring-0 outline-none',
                  routeHash == tab.hash
                    ? 'bg-red-200 shadow border-b-2 border-primary rounded-b-none'
                    : ' hover:bg-red-200',
                ]"
              >
                {{ tab.title }}
              </p>
            </RouterLink>
          </div>

          <MissionDetail v-show="routeHash == '#edit'" />
          <MissionPresence v-show="routeHash == '#presence'" />
        </div>
      </div>
    </template>
  </MainTemplate>
</template>

<script setup lang="ts">
import { defineComponent } from "vue";
import { mapActions, mapState } from "pinia";
import MainTemplate from "@/templates/Main.vue";
import { useConnectionStore } from "@/stores/admin/operation/connection";
import MissionDetail from "./MissionDetail.vue";
import MissionPresence from "./MissionPresence.vue";
import { useForceStore } from "@/stores/admin/configuration/force";
import type { ForceViewModel } from "@/viewmodels/admin/configuration/force.models";
import { useMissionDetailStore } from "../../../../stores/admin/operation/missionDetail";
</script>

<script lang="ts">
export default defineComponent({
  props: {
    id: {
      type: String,
      default: "",
    },
  },
  data() {
    return {
      tabs: [
        { hash: "#edit", title: "Einträge" },
        { hash: "#presence", title: "Anwesenheit" },
        { hash: "#protocol", title: "Protokoll" },
      ],
      forces: [] as Array<ForceViewModel>,
    };
  },
  watch: {
    "$route.hash"() {
      this.manageHash();
    },
    connectionStatus() {
      if (this.connectionStatus) {
        this.init(this.id);
      }
    },
  },
  computed: {
    ...mapState(useConnectionStore, ["connectionStatus"]),
    routeHash() {
      return this.$route.hash;
    },
  },
  mounted() {
    this.manageHash();
    this.connectClient();
    this.init(this.id);
    this.getAvailableForces();
  },
  beforeUnmount() {
    this.destroy();
  },
  methods: {
    ...mapActions(useConnectionStore, ["connectClient"]),
    ...mapActions(useMissionDetailStore, ["init", "destroy"]),
    ...mapActions(useForceStore, ["getAvailableForces"]),
    manageHash() {
      if (!this.$route.hash || !this.tabs.map((t) => t.hash).includes(this.$route.hash)) {
        this.$router.replace({ hash: this.tabs[0].hash });
      }
    },
  },
});
</script>
