<script setup lang="ts">
import DateEmbedding from "@/components/dueDates/DateEmbedding.vue";
import RichText from "@/components/ui/RichText.vue";
import type { Assignment } from "@/types";
import { useRouteQuery } from "@vueuse/router";
import dayjs from "dayjs";
import log from "loglevel";

interface Props {
  assignment: Assignment;
  submissionDeadlineStart?: string | null;
}

const props = withDefaults(defineProps<Props>(), {
  submissionDeadlineStart: "",
});

log.debug("AssignmentIntroductionView created", props.assignment);

const step = useRouteQuery("step");
</script>

<template>
  <RichText
    v-if="props.assignment.intro_text"
    class="text-large"
    :content="props.assignment.intro_text"
    open-links-in-new-tab
  />

  <h3 class="mb-4 mt-8">{{ $t("assignment.taskDefinitionTitle") }}</h3>
  <p class="text-large">
    {{ $t("assignment.taskDefinition") }}
  </p>
  <ul>
    <li v-for="(task, index) in props.assignment.tasks" :key="task.id" class="py-1">
      <button class="text-large link" @click="step = (index + 1).toString()">
        - {{ task.value.title }}
      </button>
    </li>
  </ul>

  <div v-if="submissionDeadlineStart" class="text-large">
    <h3 class="mb-4 mt-8">{{ $t("assignment.dueDateSubmission") }}</h3>
    {{ $t("assignment.dueDateIntroduction") }}
    <p>
      <DateEmbedding :single-date="dayjs(submissionDeadlineStart)"></DateEmbedding>
    </p>
  </div>

  <div v-if="props.assignment.effort_required">
    <h3 class="mb-4 mt-8">{{ $t("assignment.effortTitle") }}</h3>
    <p class="text-large">{{ props.assignment.effort_required }}</p>
  </div>

  <div v-if="props.assignment.performance_objectives.length">
    <h3 class="mt-8 border-b border-gray-500 pb-4">
      {{ $t("assignment.performanceObjectivesTitle") }}
    </h3>
    <p
      v-for="performance_objective in props.assignment.performance_objectives"
      :key="performance_objective.id"
      class="text-large border-b border-gray-500 py-4"
    >
      {{ performance_objective.value.text }}
    </p>
  </div>

  <div
    v-if="
      props.assignment.evaluation_description ||
      props.assignment.evaluation_document_url
    "
  >
    <h3 class="mb-4 mt-8">{{ $t("a.Bewertung") }}</h3>
    <RichText
      v-if="props.assignment.evaluation_description"
      class="text-large"
      :content="props.assignment.evaluation_description"
      open-links-in-new-tab
    />
    <p v-if="props.assignment.evaluation_document_url">
      <a
        :href="props.assignment.evaluation_document_url"
        target="_blank"
        class="text-large link"
      >
        {{ $t("assignment.showAssessmentDocument") }}
      </a>
    </p>
  </div>

  <div v-if="assignment?.competence_certificate">
    <h3 class="mb-4 mt-8">{{ $t("a.Kompetenznachweis") }}</h3>
    <p class="text-large">
      {{
        $t("circlePage.Dieser Inhalt gehört zu x", {
          x: assignment?.competence_certificate?.title,
        })
      }}
      <br />
      <router-link
        v-if="assignment.competence_certificate?.frontend_url"
        :to="assignment.competence_certificate?.frontend_url ?? ''"
        class="link"
        data-cy="open-competence-certificate"
      >
        {{ $t("circlePage.Im KompetenzNavi anschauen") }}
      </router-link>
    </p>
  </div>
</template>
