<template>
  <div id="Chapter1_1" class="content-wrap">
    <div style="margin: 30px 0px 50px; width: 20%">
      <router-link to="/MyPlan.page">
        <div class="logo mb25">
          <img src="../../../../resources/img/logo2.png" alt="" />
        </div>
      </router-link>
    </div>
    <div
      class="title-box mb25 flex align-center mt40"
      style="justify-content: space-between"
    >
      <div>
        <span class="title mr40">1. Hello WORLD</span>
        <span class="subtitle">my name is dd</span>
      </div>
      <button class="completeBtn" @click="complete">학습 종료</button>
    </div>
    <div class="flex justify-between align-center">
      <div class="pre-btn" @click="goToPage('Chapter1_3')">
        <img src="../../../../resources/img/left.png" alt="" />
      </div>
      <div class="content title-box">
        <p class="title mt25 title-bg">STEP 2 - 단어로 공부하는 영어</p>
        <div class="flex align-center mb30">
          <p class="subtitle2 mr20"></p>
        </div>

        <div class="flex justify-center">
          <div class="vocaGroup" style="display: flex; flex-wrap: wrap;">
            <div
              v-for="(word) in wordList"
              :key="word.wdId"
              class="flex justify-between mb80"
              style="width: 49%;"
            >
              <article class="flex align-center">
                <div class="imgGroup mr30">
                  <img
                    :src= word.fileApath
                    data-num="1"
                    style="width: 200px"
                  />
                </div>
                <div class="flex align-start">
                  <button
                    class="listen-btn mr30"
                    data-video="1"
                    tabindex="0"
                    aria-label="음성 재생"
                  >
                    <img
                      src="../../../../resources/img/btn10_s.png"
                      data-num="1"
                      @click="playAudio('audio-family')"
                    />
                  </button>
                  <audio
                    id="audio-family"
                    src="client/resources/audio/family.wav"
                    preload="auto"
                  ></audio>
                  <div>
                    <h3>{{ word.wdNm }}</h3>
                    <div class="flex align-center mt10">
                      <p class="yellow-box">명</p>
                      <span class="title1">{{ word.wdMnng }}</span>
                    </div>
                  </div>
                </div>
              </article>
            </div>

          </div>
        </div>
      </div>
      <div class="next-btn" @click="goToPage('Chapter2_3')">
        <img src="../../../../resources/img/right.png" alt="" />
      </div>
    </div>
  </div>
</template>

<script>
import axios from 'axios';

export default {
  data() {
    return {
      wdBookId: "WORD_BOOK_000000000000042",
      wordList: [],
      wordContentList: [],
      wdContentId: "",
    };
  },
  methods: {
    async fetchWordList() {
      try {
        const response = await axios.post("/word/getWordsByBookId.json", {
          wdBookId: this.wdBookId,
        });

        const wordList = response.data;

        // 각 word 객체에 대해 fileApath를 받아오는 요청 처리
        const requests = wordList.map(async (word) => {
          const fileResponse = await axios.post("/file/find.json", {
            file_mng_id: word.fileMngId,
          });

          const fileApath = fileResponse.data.list.length > 0 ? fileResponse.data.list[0].fileApath : null;
          console.log("각 단어의 fileApath: ", fileApath);

          word.fileApath = "http://localhost:9080/" + fileApath; // fileApath 값을 해당 객체에 추가
        });

        // 모든 요청이 완료될 때까지 대기
        await Promise.all(requests);

        // 최종적으로 wordList를 설정
        this.wordList = wordList;
        console.log("단어 리스트 -> ", this.wordList);
      } catch (error) {
        console.error("단어 목록을 불러오는 중 오류 발생:", error);
      }
    },

    goToPage(page) {
      this.$router.push({ name: page });
    },

    playAudio(audioId) {
      const audio = document.getElementById(audioId);
      if (audio) {
        audio.play();
      } else {
        console.error(`Audio element with ID ${audioId} not found.`);
      }
    },

  },
  mounted() {
    console.log("챕터2 단어장 마운트 완료");
    this.fetchWordList();
  },
};

</script>

<style scoped>
.grid-container {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 20px;
}
.completeBtn {
  margin-right: 100px;
  background-color: #ffba08;
  padding: 10px 30px;
  border-radius: 10px;
  font-size: 28px;
  font-family: "ONEMobilePOPOTF";
}
</style>
