<template>
  <div class="content-wrap ">
    <div class="flex justify-center">
      <div>
        <div class="frame title-box">
          <div class="photo">
            <img :src="imageSrc" alt="">
          </div>
          <p class="title mt20" style="color: #fff;">2024.07.01 1단원 완료!</p>

        </div>
        <div class="btn-wrap flex justify-center mt40" style="gap: 40px;">
          <button class="login-btn" @click="goToPage('PhotoDesign')">
            <img src="../../../resources/img/btn07_s.png" alt="">
            <p>수정하기</p>
          </button>

          <button class="login-btn" type="submit" @click="insertPhoto()"><img src="../../../resources/img/btn07_s.png"
              alt="">
            <p>완료</p>
          </button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import axios from 'axios';
export default {
  data() {
    return {
      items: [
        { imgSrc1: 'client/resources/img/btn20_75s_normal.png', imgSrc2: 'client/resources/img/btn20_75s_click.png', },
        { imgSrc1: 'client/resources/img/btn21_75s_normal.png', imgSrc2: 'client/resources/img/btn21_75s_click.png', },
      ],
      timer: '00',
      selectedIndex: 0,

      imageSrc: '',
      file: '',
      fileId: '',
    }
  },
  methods: {
    async fetchImageAsFile(imageUrl, filename = "image.jpg") {
      try {
        const response = await fetch(imageUrl);
        const blob = await response.blob();

        const file = new File([blob], filename, { type: blob.type });

        return file;
      } catch (error) {
        console.error("이미지 변환 오류:", error);
        throw new Error("이미지 변환에 실패했습니다.");
      }
    },
    async fetchPhoto() {
      const image = this.$route.query.image;
      if (image) {
        this.imageSrc = decodeURIComponent(image);
        try {
          this.file = await this.fetchImageAsFile(this.imageSrc, "image.jpg");
        } catch (error) {
          console.error("파일 변환 오류:", error);
        }
      }
    }, async uploadFiles(file) {
      if (!file) {
        console.error("파일이 없습니다.");
        throw new Error("파일이 없습니다.");
      }
      const formData = new FormData();
      formData.append("files", file);

      try {
        const response = await axios.post("/file/upload.json", formData, {
          headers: {
            "Content-Type": "multipart/form-data",
          },
        });

        return response.data.fileMngId; // 서버에서 반환된 파일 매니지 ID
      } catch (error) {
        console.error("파일 업로드 오류:", error);
        throw new Error("파일 업로드에 실패했습니다.");
      }
    }, async insertPhoto() {
      try {
        this.fileId = await this.uploadFiles(this.file);

        const response = await axios({
          url: "/photo/insertPhoto.json",
          method: "post",
          headers: {
            "Content-Type": "application/json; charset=UTF-8",
          },
          data: {
            "photoMngId": "PHOTO_MNG_000000000000001", // 사진첩아이디 교체
            "likeData": "0",
            "unitId": "UNIT_000000000000001", // 유닛아이디 교체
            "stdId": "1", //학생아이디 교체
            "fileMngId": this.fileId
          }
        });
        this.$router.push({ name: 'AIDashboard' });
      } catch (error) {
        console.error("사진 삽입 오류:", error);
      }
    },
    updateContent(index) {
      this.selectedIndex = index;
    },
    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);
    }
  },
  watch: {

  },
  computed: {

  },
  components: {
  },
  mounted() {
    this.fetchPhoto();
  }
}
</script>
<style scoped>
.imgGroup {
  width: fit-content;
}

.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 button {
  position: relative;
  margin-right: 30px;
}

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

.pickGroup article img {
  object-fit: contain;
  width: -webkit-fill-available;
}

.pickGroup article>div>p {
  font-size: 64px;
}

.popTxt {
  width: 101px;
}
</style>