<template>
  <div class="flex flex-col gap-2 h-full w-full overflow-y-auto">
    <Spinner v-if="loading == 'loading'" class="mx-auto" />
    <p v-else-if="loading == 'failed'" @click="fetchProtocolVoting" class="cursor-pointer">
      &#8634; laden fehlgeschlagen
    </p>

    <div class="flex flex-col gap-2 h-full overflow-y-scroll">
      <details
        v-for="(item, index) in sortedVoting"
        class="flex flex-col rounded-lg w-full justify-between border border-primary overflow-hidden min-h-fit"
      >
        <summary class="flex flex-row gap-2 bg-primary p-2 w-full justify-between items-center cursor-pointer">
          <svg
            indicator
            class="fill-white stroke-white opacity-75 w-4 h-4"
            xmlns="http://www.w3.org/2000/svg"
            viewBox="0 0 20 20"
          >
            <path d="M12.95 10.707l.707-.707L8 4.343 6.586 5.757 10.828 10l-4.242 4.243L8 15.657l4.95-4.95z" />
          </svg>

          <input
            type="text"
            name="title"
            id="title"
            placeholder="Abstimmung"
            autocomplete="off"
            v-model="item.topic"
            @keyup.prevent
            :disabled="!can('create', 'club', 'protocol')"
          />

          <DoubleConfirmClick
            v-if="can('create', 'club', 'protocol')"
            light
            v-slot="{ isSensitive }"
            @click:submit="removeFromArray(item.id)"
          >
            <TrashIcon v-if="!isSensitive" class="h-5 w-5" />
            <TrashIconSolid v-else class="h-5 w-5" />
          </DoubleConfirmClick>

          <div class="flex flex-col">
            <ChevronUpIcon
              v-if="index != 0"
              class="text-white w-4 h-4 stroke-2"
              @click.prevent="changeSort('up', item.id, index)"
            />
            <ChevronDownIcon
              v-if="index != voting.length - 1"
              class="text-white w-4 h-4 stroke-2"
              @click.prevent="changeSort('down', item.id, index)"
            />
          </div>
        </summary>
        <QuillEditor
          id="top"
          theme="snow"
          placeholder="Entscheidung Inhalt..."
          style="height: 100px; max-height: 100px; min-height: 100px"
          contentType="html"
          :toolbar="toolbarOptions"
          v-model:content="item.context"
          :enable="can('create', 'club', 'protocol')"
          :style="!can('create', 'club', 'protocol') ? 'opacity: 75%; background: rgb(243 244 246)' : ''"
        />
        <div class="px-2 pb-2">
          <p>Ergebnis:</p>
          <div class="flex flex-row gap-2">
            <div class="w-full">
              <p>dafür</p>
              <input type="number" v-model="item.favour" />
            </div>
            <div class="w-full">
              <p>dagegen</p>
              <input type="number" v-model="item.against" />
            </div>
            <div class="w-full">
              <p>enthalten</p>
              <input type="number" v-model="item.abstain" />
            </div>
          </div>
        </div>
      </details>
    </div>

    <button v-if="can('create', 'club', 'protocol')" primary class="w-fit!" @click="createProtocolVoting">
      Abstimmung hinzufügen
    </button>
  </div>
</template>

<script setup lang="ts">
import { defineComponent } from "vue";
import { mapActions, mapState, mapWritableState } from "pinia";
import Spinner from "@/components/Spinner.vue";
import { QuillEditor } from "@vueup/vue-quill";
import "@vueup/vue-quill/dist/vue-quill.snow.css";
import { toolbarOptions } from "@/helpers/quillConfig";
import { useProtocolVotingStore } from "@/stores/admin/club/protocol/protocolVoting";
import { useAbilityStore } from "@/stores/ability";
import { ChevronDownIcon, ChevronUpIcon, TrashIcon } from "@heroicons/vue/24/outline";
import { TrashIcon as TrashIconSolid } from "@heroicons/vue/24/solid";
import DoubleConfirmClick from "@/components/DoubleConfirmClick.vue";
</script>

<script lang="ts">
export default defineComponent({
  props: {
    protocolId: String,
  },
  computed: {
    ...mapWritableState(useProtocolVotingStore, ["voting", "loading"]),
    ...mapState(useAbilityStore, ["can"]),
    sortedVoting() {
      return this.voting.slice().sort((a, b) => a.sort - b.sort);
    },
  },
  mounted() {
    this.normalizeSort();
  },
  methods: {
    ...mapActions(useProtocolVotingStore, ["fetchProtocolVoting", "createProtocolVoting"]),
    changeSort(dir: "up" | "down", thisId: number, index: number) {
      let affected = this.sortedVoting[dir == "up" ? index - 1 : index + 1]?.id;
      if (affected) {
        this.voting.find((a) => a.id == thisId)!.sort = dir == "up" ? index - 1 : index + 1;
        this.voting.find((a) => a.id == affected)!.sort = dir == "up" ? index + 1 : index - 1;
      }
      this.normalizeSort();
    },
    normalizeSort() {
      let rightSort = this.voting.every((val, index) => val.sort == index);
      if (!rightSort) {
        this.sortedVoting.forEach((e, index) => {
          e.sort = index;
        });
      }
    },
    removeFromArray(thisId: number) {
      this.voting = this.voting.filter((item) => item.id !== thisId);
    },
  },
});
</script>
