<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">
      <span class="title mr40">{{ unitData.unit_nm || '(단원 이름 데이터 없음)' }}</span>
      <span class="subtitle">my name is dd</span>
    </div>
    <div class="flex justify-between align-center">
      <div class="pre-btn" @click="goToPage('Chapter3_2')"><img src="../../../../resources/img/left.png" alt=""></div>
      <div class="content title-box">
        <p class="title mt25 title-bg">step3.</p>
        <div class="flex align-center mb30">
          <p class="subtitle2 mr20">{{ dataList.prblmExpln }}</p>
        </div>

        <div class="text-ct">
          <div class="time-hint">
          <button class="hint-btn">HINT</button>
            <div class="time-bg mt20">
                <div>
                  <div class="time">
                    <p class="second">{{ timer }}</p>
                    <p class="text">sec</p>
                  </div>
                </div>
              </div>
         </div>
          <div class="pickGroup mt60 flex align-center justify-center" style="gap: 100px; margin-top: 7%;">

            <article v-for="(detail, index) in problemDetail" :key="index" style="gap: 60px;">
              <div class="flex align-center">
                <button><img src="../../../../resources/img/img136_71s.png" alt="">
                  <p>{{ index + 1 }}</p>
                </button>
                <p>{{ detail.prblmDtlExpln }}</p>
              </div>
            </article>

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

<script>
import axios from "axios";

export default {
  data() {
    return {
      timer: '00',
      prblmId: "",
      problemDetail: [],
      dataList: [],
      prblmExpln: null,
      unitData: {}, // 단원 정보 데이터
    }
  },
  methods: {

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

    startTimer() {
      if (this.intervalId) {
        clearInterval(this.intervalId);
      }
      this.timer = 5;
      this.intervalId = setInterval(() => {
        if (this.timer > 0) {
          this.timer--;
        } else {
          clearInterval(this.intervalId);
        }
      }, 1000);
    },

    // 단원 정보를 불러오는 API 호출
    fetchUnitDetail(unitId) {
        console.log("테스트", this.dataList);
        axios.post('/unit/unitDetail.json', {
            unitId: unitId
        })
        .then(response => {
            console.log(response.data[0]);
            if (response.data.length > 0) {
                this.unitData = response.data[0];
            }
        })
        .catch(error => {
            console.error('단원 정보를 불러오는 중 오류 발생:', error);
        })
    },

    problemSearch() {
        const vm = this;
        vm.prblmId = JSON.parse(sessionStorage.getItem("prblmId"));
        axios({
            url: "problem/problemInfo.json",
            method: "post",
            headers: {
                "Content-Type": "application/json; charset=UTF-8",
            },
            data: {
                prblmId: vm.prblmId,
            },
        })
            .then(function (res) {
                console.log("problem - response : ", res.data);
                vm.dataList = res.data.problem;
                vm.problemDetail = res.data.problemDetail;

                // 문제 정보가 성공적으로 받아와진 후, unitId를 이용해 단원 정보를 가져옴
                if (vm.dataList.unitId) {
                  vm.fetchUnitDetail(vm.dataList.unitId);
                } else {
                  console.error("단원 ID가 없습니다.");
                }
            })
            .catch(function (error) {
                console.log("problem - error : ", error);
            });
    },

  },
  watch: {

  },
  computed: {

  },
  components: {
  },
  mounted() {
    this.problemSearch();
  }
}
</script>
<style scoped>
.inputbox {
  font-size: 38px;
  font-family: 'Pretendard-ExtraBold';
}

.imgGroup button {
  position: relative;
}

.imgGroup button p,
.textbox p {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: fit-content;
  height: fit-content;
  background: #ffffffb8;
  border-radius: 5px;
  padding: 10px;
  font-size: 48px;
  font-family: 'ONEMobilePOP';
}

.pickGroup p {
  font-size: 34px;
  font-weight: bold;
}

.pickGroup button {
  position: relative;
  margin-right: 30px;
}

.pickGroup button p {
  font-size: 34px;
  color: #c6c6c6;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}
</style>