<script setup lang="ts">
import { useDashboardStore } from "@/stores/dashboard";
import ItDropdownSelect from "@/components/ui/ItDropdownSelect.vue";
import { computed } from "vue";
import type {
  CourseStatisticsType,
  FeedbackStatisticsRecordType,
  PresenceRecordStatisticsType,
} from "@/gql/graphql";
import StatisticFilterList from "@/components/dashboard/StatisticFilterList.vue";
import { useCourseStatistics } from "@/composables";
import { getBlendedColorForRating } from "@/utils/ratingToColor";

const dashboardStore = useDashboardStore();

const statistics = computed(() => {
  return dashboardStore.currentDashBoardData as CourseStatisticsType;
});

const { courseSessionName, circleMeta } = useCourseStatistics();
</script>

<template>
  <main v-if="statistics">
    <div class="mb-10 flex items-center justify-between">
      <h3>{{ $t("a.Feedback Teilnehmer") }}</h3>
      <ItDropdownSelect
        :model-value="dashboardStore.currentDashboardConfig"
        class="mt-4 w-full lg:mt-0 lg:w-96"
        :items="dashboardStore.dashboardConfigs"
        @update:model-value="dashboardStore.switchAndLoadDashboardConfig"
      ></ItDropdownSelect>
    </div>
    <div v-if="statistics.feedback_responses.records" class="mt-8 bg-white">
      <StatisticFilterList
        :course-session-properties="statistics.course_session_properties"
        :items="statistics.feedback_responses.records"
      >
        <template #default="{ item }">
          <div class="flex justify-between">
            <div>
              <h4 class="font-bold">
                Feedback: Circle «{{ circleMeta(item.circle_id)?.name }}»
              </h4>
              <div>
                {{ $t("a.Durchfuehrung") }} «{{
                  courseSessionName(item.course_session_id)
                }}» - Trainer: {{ (item as FeedbackStatisticsRecordType).experts }}
              </div>
            </div>
            <div class="w-80">
              <div class="mb-4 flex items-center space-x-2">
                <div
                  class="rounded px-2 py-1"
                  :style="{ backgroundColor: getBlendedColorForRating((item as FeedbackStatisticsRecordType).satisfaction_average) }"
                >
                  <i18next :translation="$t('a.{AVG} von {MAX}')">
                    <template #AVG>
                      <span class="font-bold">
                        {{
                          (
                            item as FeedbackStatisticsRecordType
                          ).satisfaction_average.toFixed(1)
                        }}
                      </span>
                    </template>
                    <template #MAX>
                      <span>
                        {{ (item as FeedbackStatisticsRecordType).satisfaction_max }}
                      </span>
                    </template>
                  </i18next>
                </div>
                <span>
                  {{ $t("a.Allgemeine Zufriedenheit") }}
                </span>
              </div>
              <router-link
                class="underline"
                target="_blank"
                :to="(item as PresenceRecordStatisticsType).details_url"
              >
                {{ $t("a.Details anschauen") }}
              </router-link>
            </div>
          </div>
        </template>
      </StatisticFilterList>
    </div>
  </main>
</template>
