<script lang="ts" setup="">
import {useMaterialsStore} from "~/stores/materials/useMaterialsStore";
import {Button} from "~/components/ui/Buttons";
import dayjs from "dayjs";
import type {Material} from "~/stores/materials/types.materials";
import sanitizeHtml from "sanitize-html";
import IconCalendar from "~/components/icons/iconCalendar.vue";
import ru from 'dayjs/locale/ru'

dayjs.locale(ru);

const route = useRoute()
const materialsStore = useMaterialsStore()

const currentItem = computed<Material | undefined>(() => materialsStore.getById(route.params.id))
const date = computed(() => dayjs(currentItem.value?.datetime).format('D MMMM'))
const safeHTML = computed(() => {
  if (!currentItem.value) return ''
  return sanitizeHtml(currentItem.value.description_html)
})


await useAsyncData(async () => {
  await materialsStore.fetchMaterials()
  return true
})
</script>

<template>
  <div class="material-container">
    <Button.ButtonBack/>

    <div class="material-content">
      <div class="date-container">
        <IconCalendar class="icon" />
        <span class="date">{{date}}</span>
      </div>

      <div class="content" v-html="safeHTML" />
    </div>

  </div>
</template>

<style scoped lang="scss">
.material-container {
  max-width: 1014px;
  margin: 0 auto;
}

.material-content {
  margin-top: 48px;
  padding: 64px;
  border-radius: 32px;
  background-color: #fff;
}

.content {
  :deep(h1), :deep(h2), :deep(h3), :deep(p), :deep(blockquote) {
    margin: 0;
    padding-top: 24px;
  }

  :deep(h1) {
    font-family: SourceSans3, monospace;
    font-weight: 600;
    font-size: 36px;
    line-height: 120%;
    color: var(--text-black);
  }
  :deep(p:first-of-type) {
    font-family: FuturaPT, monospace;
    font-weight: 500;
    font-size: 22px;
    line-height: 120%;
    color: var(--text-black);
  }
  :deep(p:not(:first-of-type)) {
    font-family: FuturaPT, monospace;
    font-weight: 400;
    font-size: 20px;
    line-height: 120%;
    color: var(--text-black);
  }
}
.date-container {
  display: flex;
  gap: 8px;
  align-items: center;
  color: var(--text-light-gray);
}
.icon {
  @include mixins.square(20px);
}

.date {
  font-family: FuturaPT;
  font-weight: 400;
  font-size: 16px;
  line-height: 100%;
}

@media screen and (max-width: 768px) {
  .material-container {
    padding-inline: 0;
  }
  .material-content {
    margin-top: 32px;
    padding: 24px 16px;
    border-radius: 24px;
    position: relative;
    left: -16px;
    width: calc(100dvw - (16px * 2));
  }

  .content {
    :deep(h1) {
      font-size: 24px;
    }
    :deep(p:first-of-type) {
      font-size: 18px;
    }
    :deep(p:not(:first-of-type)) {
      font-size: 16px;
    }
  }
}
</style>
