<script setup>
import { ref, reactive } from "vue";
import { useRoute, useRouter } from "vue-router";
import { useMainStore } from "@/stores/store";
import axios from "axios";
import moment from "moment";
import bookList from "@/utils/useBookList";
import Navbar from "@/components/Navbar.vue";

const route = useRoute();
const router = useRouter();
const store = useMainStore();

// 網址參數
const articleId = route.params.id;
const articleCategory = route.params.category;

console.log("articleId", articleId);
console.log("articleCategory", articleCategory);

let list = reactive({
  data: {},
});

if (articleCategory === "book") {
  list.data = bookList[articleId];
} else if (articleCategory === "article") {
  getArticle();
}

let filePath = ref("");
let file = reactive({
  list: [],
});

async function getArticle() {
  try {
    const response = await axios.get(
      `https://cmm.ai:8088/api/get_article?article_id=${articleId}`
    );

    console.log("response", response.data.articles[0]);
    list.data = response.data.articles[0];

    const fileData = response.data.articles[0].files;
    // 字符串轉物件
    const fileObject = JSON.parse(fileData.replace(/'/g, '"'));

    // 遍歷物件後將值存進陣列
    file.list = Object.keys(fileObject).map((key) => ({
      name: fileObject[key].split("/").pop(),
      url: fileObject[key],
    }));

    console.log("fileList", file.list);
    console.log("data", list.data);
  } catch (error) {
    console.error(error);
  }
}
</script>

<template>
  <Navbar />
  <div class="position-relative mb-16">
    <img src="@/assets/img/news/news-01.png" alt="" class="material-img" />
    <v-container class="pa-0 pt-16 position-relative">
      <div class="content">
        <div class="article">
          <v-row>
            <v-col cols="12">
              <div class="d-flex align-center mb-5">
                <v-chip size="large" variant="elevated" class="px-8 me-3">
                  {{
                    articleCategory === "book"
                      ? list.data.category
                      : list.data.group_sort
                  }}
                </v-chip>
                <!-- <p>
                  {{ moment(`${list.data.create_time}`).format("YYYY.MM.DD") }}
                </p> -->
              </div>
              <h2 class="text-center">{{ list.data.title }}</h2>
              <v-img
                class="cover-img my-10"
                :lazy-src="
                  articleCategory === 'book'
                    ? list.data.img
                    : `https://ntcri.org/${list.data.cover_img}`
                "
                :src="
                  articleCategory === 'book'
                    ? list.data.img
                    : `https://ntcri.org/${list.data.cover_img}`
                "
              >
                <template v-slot:placeholder>
                  <div class="d-flex align-center justify-center fill-height">
                    <v-progress-circular
                      color="grey-lighten-4"
                      indeterminate
                    ></v-progress-circular>
                  </div>
                </template>
              </v-img>
            </v-col>
            <v-col cols="12">
              <section class="d-flex flex-column pa-0">
                <p v-html="list.data.content"></p>
              </section>
            </v-col>
            <v-col cols="12" class="mt-10" v-if="file.list.length">
              <p>附件下載：</p>
              <ul class="mt-7">
                <li
                  v-for="(item, index) in file.list"
                  :key="index"
                  class="d-flex align-center mb-2"
                >
                  <v-icon
                    icon="mdi-file-download"
                    color="brown"
                    size="large"
                    class="me-2"
                  ></v-icon>
                  <a :href="`https://ntcri.org/${item.url}`" download>{{
                    item.name
                  }}</a>
                </li>
              </ul>
              <!-- <a :href="`https://ntcri.org/${filePath}`" download class="download-link">附件下載</a> -->
            </v-col>
          </v-row>
        </div>
      </div>
    </v-container>
    <router-link
      v-if="articleCategory === 'book'"
      to="/crafts"
      class="mt-16 back-link"
      >＜ 返回知識文章</router-link
    >
    <a href="javascript:;" v-else @click="router.go(-1)" class="back-link"
      >＜ 回到上一頁</a
    >
    <img src="@/assets/img/news/news-01.png" alt="" class="material-img" />
  </div>
</template>

<style lang="scss" scoped>
ul {
  a {
    line-height: 24px;
    letter-spacing: 1px;
    transition: all 0.3s;
    &:hover {
      opacity: 0.8;
    }
  }
}
.btn-block {
  display: flex;
  justify-content: end;
}

.content {
  padding: 20px;
  background-image: url("@/assets/img/news/news-02.png");
  background-position: center;
  background-size: cover;
  border-radius: 50px;
  @media (max-width: 960px) {
    width: 90%;
    margin: auto;
  }
}
.article {
  margin: auto;
  padding: 80px 50px;
  display: flex;
  justify-content: center;
  border: 1px solid #d4d6d8;
  border-radius: 50px;

  // @media (max-width: 960px) {
  //   width: 80%;
  // }

  @media (max-width: 600px) {
    padding: 50px 0;
    border: none;
  }

  h2 {
    margin: 40px auto;
    font-size: 28px;
    font-weight: 500;
    line-height: 38px;
    letter-spacing: 2px;
  }
  section {
    height: 100%;
    padding: 30px;
    p {
      font-size: 18px;
      font-weight: 400;
      line-height: 32px;
      letter-spacing: 1px;
      white-space: pre-line;
    }
  }

  .cover-img {
    width: 100%;
    max-height: 500px;
    @media (max-width: 600px) {
      height: auto;
    }
  }
}
.bg-img {
  height: 1000px;
  position: relative;
  z-index: -1;
  @media (max-width: 600px) {
    height: 650px;
  }
}

.back-link {
  margin-top: 50px;
  display: block;
  text-align: center;
  transition: all 0.3s;
  letter-spacing: 1px;

  &:hover {
    opacity: 0.8;
  }
}
.material-img {
  position: absolute;

  &:first-child {
    top: -15vw;
  }

  &:last-child {
    bottom: -130px;
    z-index: -5;
    transform: scaleX(-1);

    @media (max-width: 600px) {
      bottom: -50px;
    }
  }
}
</style>
