<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 Navbar from "@/components/Navbar.vue";

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

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

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

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

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

async function getArticle() {
  try {
    const response = await axios.get(
      `${store.apiUrl}/api/get_article?article_id=${articleId}`
    );
    console.log("response", response.data.articles[0]);
    list.data = response.data.articles[0];
    // 附件
    const fileData = list.data.files;
    console.log("fileData", fileData);
    if (fileData !== "undefined") {
      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);
    }
    // const fileObject = JSON.parse(fileData.replace(/'/g, '"')); // 字符串轉物件
    // 標籤
    tags.list = JSON.parse(list.data.tags);
    console.log("tags.list", tags.list);

    console.log("data", list.data);
  } catch (error) {
    console.error(error);
  }
}

getArticle();
</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-10">
                <v-chip size="large" variant="elevated" class="px-8 me-3">
                  {{ 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="`${store.imgUrl}/${list.data.cover_img}`"
                :src="`${store.imgUrl}/${list.data.cover_img}`"
                alt="臺灣工藝學習平台"
              >
                <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"
                v-html="list.data.content"
              ></section>
            </v-col>
            <v-col cols="12" class="mt-10">
              <ul class="d-flex mb-8 tag-list" v-if="tags.list">
                <li v-for="(item, index) in tags.list" :key="index">
                  <v-chip class="me-3" label variant="flat" color="blue">
                    <p class="text-white">{{ item }}</p>
                  </v-chip>
                </li>
              </ul>
              <div
                v-if="file.list.length"
                class="d-flex flex-column align-start"
              >
                <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="`${store.imgUrl}/${item.url}`"
                      download
                      target="_blank"
                      >{{ item.name }}</a
                    >
                  </li>
                </ul>
              </div>
              <!-- <a :href="`${store.imgUrl}/${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>
p {
  text-align: center;
}

ul {
  a {
    line-height: 1.5em;
    letter-spacing: 0.0625em;
    transition: all 0.3s;
    &:hover {
      opacity: 0.8;
    }
  }
}

section {
  line-height: 2;
  letter-spacing: 1px;
}

.content {
  padding: 1.25em;
  background-image: url("@/assets/img/news/news-bg.webp");
  background-position: center;
  background-size: cover;
  border-radius: 3.125em;
  @media (max-width: 960px) {
    width: 90%;
    margin: auto;
  }
}

.bg-img {
  height: 62.5em;
  position: relative;
  z-index: -1;
  @media (max-width: 600px) {
    height: 40.625em;
  }
}

.back-link {
  margin-top: 3.125em;
}

.material-img {
  position: absolute;

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

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

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