<script setup lang="ts">
const config = useRuntimeConfig()
const apiBase = config.public.apiBase

import { marked } from 'marked';

import k_logo from '@/assets/icons/logo.svg'
import '@/assets/main.scss'

const { data: seoData } = await useFetch<ApiKpType>(`${apiBase}/kp/1`)
useSeoMeta({
  title: seoData.value?.title,
  ogTitle: seoData.value?.title,
  description: seoData.value?.content,
  ogDescription: seoData.value?.content,
  // ogImage: 'https://example.com/image.png',
  // twitterCard: 'summary_large_image',
})

const { data: pagesData } = await useFetch<ApiPagesType[]>(`${apiBase}/pages/?ordering=order`)

const { data: reviewsData } = await useFetch<ApiReviewsType[]>(`${apiBase}/review/`)
const { data: calculatorData } = await useFetch(`${apiBase}/calculator/5/`)

const about = (pagesData.value as ApiPagesType[]).find(el => el.slug == 'about')
const reviews = (pagesData.value as ApiPagesType[]).find(el => el.slug == 'clients')
const delivery = (pagesData.value as ApiPagesType[]).find(el => el.slug == 'delivery')
const advantages = (pagesData.value as ApiPagesType[]).find(el => el.slug == 'advantages')

const roubleSign = new Intl.NumberFormat('ru-RU', {
  style: 'currency',
  currency: 'RUB',
});

const aboutText = computed(() => marked.parse(about?.content || ''))
const deliveryText = computed(() => delivery?.content.split('[col]').map(el => marked.parse(el || '')))
const advantagesText = computed(() =>
  advantages?.content.split('[col]').map(el => {
    let c = el || ''
    Object.entries(calculatorData.value || {}).map(item => {
      c = c.replaceAll(`[${item[0]}]`, roubleSign.format(item[1]))
    })
    return marked.parse(c)
  }
  )
)

const { data: footerData } = await useFetch<ApiFooterType[]>(`${apiBase}/footer/?ordering=small_text`)
const { data: social_networkData } = await useFetch<ApiSocial_networkType[]>(`${apiBase}/social_network/`)
const { data: advData } = await useFetch<ApiAdvantageType[]>(`${apiBase}/advantage/`)

const { scrollToAnchor, scrollToTop } = useAnchorScroll({
  toTop: {
    scrollOptions: {
      behavior: 'smooth',
      offsetTop: 0,
    }
  },
})
</script>
<template>
  <div>
    <div class="header">
      <div class="container">
        <div class="logo">
          Купи забор
        </div>
        <div class="menu">
          <template v-for="item in pagesData">
            <NuxtLink :to="item.external_link || `#${item.slug}`" :target="item.external_link ? '_blank' : '_self'"
              @click="scrollToAnchor(item.slug)">
              {{ item.menu_title }}
            </NuxtLink>
          </template>
        </div>
      </div>
    </div>
    <div class="siteblock bg-white" :id="about?.slug" v-if="about">
      <div class="container">
        <h1 class="siteblock-title">{{ about?.title }}</h1>
        <div class="siteblock-content">
          <div class="siteblock-text">
            <span v-html="aboutText"></span>
          </div>
        </div>
        <div class="siteblock-image">
          <NuxtImg :src="[apiBase, about?.image].join('/')" />
        </div>
        <div class="feature">
          <template v-for="item in advData">
            <div class="feature-item">
              <div class="feature-name">{{ item.title }}</div> {{ item.content }}
            </div>
          </template>
        </div>
      </div>
    </div>
    <div class="siteblock siteblock_imgbg bg-slate-500"
      :style="[{ backgroundImage: `url(${[apiBase, reviews?.image].join('/')})` }]" v-if="reviews">
      <NuxtImg :src="[apiBase, reviews?.image].join('/')" class="invisible" />
    </div>
    <div class="siteblock bg-white" :id="reviews?.slug" v-if="reviews">
      <div class="container">
        <template v-for="item in reviewsData?.slice(0, 3)">
          <div class="review">
            <div class="review-image">
              <NuxtImg :src="[apiBase, item.image].join('/')" />
            </div>
            <div class="review-content">
              {{ item.comment }}
            </div>
            <div class="review-title">
              {{ item.text }}
            </div>
          </div>
        </template>
      </div>
    </div>
    <div class="siteblock bg-white">
      <ExpDiagram />
    </div>
    <div class="siteblock bg-white" :id="advantages?.slug" v-if="advantagesText">
      <div class="container">
        <div class="prose col-span-12 xl:col-span-6">
          <span v-html="advantagesText[0]"></span>
        </div>
        <div class="prose col-span-12 xl:col-span-6">
          <span v-html="advantagesText[1]"></span>
        </div>
      </div>
    </div>
    <div class="siteblock siteblock_imgbg bg-slate-500"
      :style="[{ backgroundImage: `url(${[apiBase, delivery?.image].join('/')})` }]">
      <NuxtImg :src="[apiBase, delivery?.image].join('/')" class="invisible" v-if="delivery" />
    </div>
    <div class="siteblock siteblock_calc bg-white">
      <CalcValues />
      <CalcModels />
    </div>
    <div class="siteblock bg-white" :id="delivery?.slug" v-if="deliveryText">
      <div class="container">
        <div class="prose col-span-12 xl:col-span-6">
          <span v-html="deliveryText[0]"></span>
        </div>
        <div class="prose col-span-12 xl:col-span-6">
          <span v-html="deliveryText[1]"></span>
        </div>
      </div>
    </div>
    <div class="footer" id="contacts">
      <div class="container">
        <div class="k-logo">
          <k_logo />
        </div>
        <template v-for="item in footerData">
          <div class="footer-text" :class="[{ 'footer-text-small': item.small_text }]">{{ item.text }}</div>
        </template>
        <div class="footer-text footer-text-social" v-if="social_networkData">
          <template v-for="item in social_networkData">
            <a :href="item.link" target="_blank">
              <Icon :name="item.icon" /> {{ item.name }}
            </a>
          </template>
        </div>
      </div>
    </div>
    <Modal />
  </div>
</template>
