<script setup lang="ts">
import AudioPlayer from "~/organisms/audioPlayer/AudioPlayer.vue";
const { currentArticle } = storeToRefs(useArticleStore());

const getSrc = (htmlString: string) => {
  const srcRegex = /src="([^"]+)"/;
  return htmlString?.match(srcRegex);
};
// const getArticleById = async (articleId: number) => {
//   try {
//     const { apiUrl } = useRuntimeConfig().public;
//     const { item }: any = await $fetch(`${apiUrl}/cms/digital-article/${articleId}`, {
//       headers: {
//         Site: "1",
//       },
//     });
//     return item;
//   } catch (error) {
//     handleError(error);
//   }
// };
const listArticle = ref([]);
const audioPlay = ref({});
const defaultClass = {
  article: ["group", "max-w-full", "grid", "gap-3", "overflow-hidden", "p-4"],
  thumbnail: ["rounded-3xl", "shadow-md", "max-w-full", "w-full", "aspect-5/3", "group-hover:scale-[1.05]", "duration-500", "ease-in-out", "object-cover"],
  title: ["font-bold", "px-4", "md:px-0", "xl:text-xl", "text-base"],
  brief: ["text-sm", "sm:text-base", "mx-4", "pb-4", "md:pb-0", "md:mx-0", "border-b", "border-stone-400", "md:border-none"],
};
// const getListArticle = async () => {
//   if (currentArticle && currentArticle.audioIds) {
//     const audioIds = currentArticle.audioIds.split(",").map(Number);
//     const articles = await Promise.all(audioIds.map(async (audioId) => await getArticleById(audioId)));
//     if (articles.length > 0) {
//       listArticle.value = articles;
//       audioPlay.value = articles[0];
//     }
//   }
//   // const test = "8,9";
// };
// getListArticle();
</script>

<template>
  <div class="w-full grid grid-cols-12 gap-4" v-if="currentArticle">
    <div class="col-span-12 h-60 md:h-100 relative bg-center" :style="'background-image: url(' + currentArticle?.thumbnail + '); background-size: cover;'">
      <div class="absolute inset-0 bg-black opacity-80 z-1"></div>
      <div class="w-full mx-auto px-4 max-w-6xl relative flex items-center justify-center">
        <div class="w-full h-40 md:h-80 absolute inset-0 z-2">
          <div class="grid grid-cols-10 w-full">
            <div class="col-span-3 flex justify-center items-center h-60 md:h-80 mx-2 md:mx-8">
              <div
                class="h-40 md:h-60 w-full rounded-tl-3xl rounded-br-3xl border-double px-2 overflow-x-hidden relative overflow-y-hidden bg-cover z-1 after:z-2 after:content-[''] after:w-full after:h-full after:top-0 after:left-0 after:bg-#000 after:opacity-30 after:absolute"
                :style="{ backgroundImage: `url(${currentArticle?.thumbnail})` }"
              >
                <img :src="currentArticle?.thumbnail" alt="" class="relative z-3 h-40 md:h-60 w-full object-contain" />
              </div>
            </div>
            <div class="col-span-7 grid grid-cols-12 relative">
              <div class="col-span-12 w-full grid grid-cols-12 mt-8 md:mb-4">
                <div class="col-span-11">
                  <h1 class="text-md md:text-3xl text-[#fff] font-bold font-['SFD']" v-html="currentArticle?.title"></h1>
                  <time class="xs:mt-0.5 text-[10px] md:text-sm text-[#fff]">
                    {{ utils.dateFormat(currentArticle?.createdOn, "dddd, DD/MM/YYYY - HH:mm") }}
                  </time>
                </div>
              </div>
              <div class="col-span-12 w-full mb-4 hidden md:block">
                <div v-html="currentArticle?.intro" class="text-left text-xl text-[#fff] font-['SFD']"></div>
              </div>
              <div class="col-span-11">
                <AudioPlayer :src="getSrc(currentArticle?.detail)?.[1]" />
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
    <!-- <div class="md:col-span-3 col-span-12" v-if="listArticle?.length > 1">
      <div class="flex items-center mb-5">
        <ul class="bg-red-500 text-white text-sm font-semibold hover:bg-red-400 font-medium inline-block rounded-tl-lg rounded-br-lg">
          <li class="inline-block uppercase rounded-l-lg border-radius border-red-500 border-r-0 px-2 py-1 text-center block transition-transform duration-300 transform hover:scale-105">Podcast Hôm nay</li>
        </ul>
        <div class="border border-slate-7 flex-grow ml-4"></div>
      </div>
      <div class="grid w-full">
        <div class="" v-for="(item, index) in listArticle.filter((item) => item.id !== audioPlay.id)" :key="index">
          <article mode="basic" :class="defaultClass.article" @click="audioPlay = { ...item }">
            <div class="rounded-sm overflow-hidden relative">
              <NuxtImg :src="item?.thumbnail || '/images/default-thumbnail.jpg'" placeholder fit="cover" :class="defaultClass.thumbnail" loading="lazy" />
              <div class="absolute bottom-2 left-0 bg-stone-200/[.56] h-10 flex justify-center items-center w-20 rounded-full">
                <span class="icon">
                  <svg width="30" height="30" viewBox="0 0 25 25" fill="none" xmlns="http://www.w3.org/2000/svg">
                    <path
                      d="M12.0131 0.5C5.38869 0.5 0 5.88331 0 12.5C0 19.1167 5.38869 24.5 12.0131 24.5C18.6376 24.5 24.0263 19.1167 24.0263 12.5C24.0263 5.88331 18.6376 0.5 12.0131 0.5ZM16.7889 12.9204L9.78122 17.4204C9.6991 17.4736 9.60426 17.5 9.51041 17.5C9.42829 17.5 9.34518 17.4795 9.2709 17.439C9.10957 17.3511 9.00985 17.1831 9.00985 17V8C9.00985 7.81691 9.10957 7.64891 9.2709 7.56102C9.42927 7.47411 9.62773 7.47945 9.78122 7.57958L16.7889 12.0796C16.9316 12.1714 17.0186 12.3301 17.0186 12.5C17.0186 12.6699 16.9316 12.8286 16.7889 12.9204Z"
                      fill="#FF0000"
                    ></path>
                  </svg>
                </span>
              </div>
            </div>
            <p class="xs:mt-0.5 xs:text-sm text-sm">{{ utils.dateFormat(item?.createdOn) }}</p>
            <h3 :class="defaultClass.title" v-html="item?.title"></h3>
          </article>
        </div>
      </div>
    </div> -->
  </div>
</template>

<style lang="scss" scoped>
.name {
  text-align: center;
  line-height: 100px;
}
</style>
