<script setup lang="ts">
import { useDashboardStore } from "@/stores/dashboard";
import ItDropdownSelect from "@/components/ui/ItDropdownSelect.vue";
import { computed } from "vue";
import type { CourseStatisticsType, PresenceRecordStatisticsType } from "@/gql/graphql";
import StatisticFilterList from "@/components/dashboard/StatisticFilterList.vue";
import ItProgress from "@/components/ui/ItProgress.vue";
import { useCourseStatistics } from "@/composables";
import { getDateString } from "@/components/dueDates/dueDatesUtils";
import dayjs from "dayjs";

const dashboardStore = useDashboardStore();

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

const { courseSessionName, circleMeta } = useCourseStatistics();

const attendanceStats = (present: number, total: number) => {
  return {
    SUCCESS: present,
    FAIL: total - present,
    UNKNOWN: 0,
  };
};
</script>

<template>
  <main v-if="statistics">
    <div class="mb-10 flex items-center justify-between">
      <h3>{{ $t("Anwesenheit") }}</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.attendance_day_presences.records" class="mt-8 bg-white">
      <StatisticFilterList
        :course-session-properties="statistics.course_session_properties"
        :items="statistics.attendance_day_presences.records"
      >
        <template #default="{ item }">
          <div class="flex justify-between">
            <div>
              <h4 class="font-bold">
                {{ $t("a.Präsenztag") }}: Circle «{{
                  circleMeta(item.circle_id)?.name
                }}»
              </h4>
              <div>
                {{ $t("a.Durchfuehrung") }} «{{
                  courseSessionName(item.course_session_id)
                }}»
              </div>
              <div class="mt-4">
                {{ $t("a.Termin") }}:
                {{
                  getDateString(dayjs((item as PresenceRecordStatisticsType).due_date))
                }}
              </div>
            </div>
            <div class="w-72">
              <div>
                {{
                  $t("a.present von total Teilnehmenden anwesend", {
                    present: (item as PresenceRecordStatisticsType)
                      .participants_present,
                    total: (item as PresenceRecordStatisticsType).participants_total,
                  })
                }}
              </div>
              <ItProgress
                :status-count="
                  attendanceStats((item as PresenceRecordStatisticsType).participants_present, (item as PresenceRecordStatisticsType).participants_total)
                "
              ></ItProgress>
              <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>
