<template>
  <div class="maturity-game-page" v-loading="pageLoading">
    <head-component :headinfo=headinfo></head-component>
    <div class="page-content">
      <div class="content-left"></div>
      <div class="content-center">
        <div class="content-img">
          <img  src="@/assets/images/wakeup/maturity/shadow-bg.png" >
          <div v-for="(item,index) in maturityAppraisalData">
            <p v-show="index===0?true:false">{{item.questionTitle}}</p>
          </div>
        </div>
        <div class="group-btn">
          <img src="@/assets/images/wakeup/maturity/btn1.png" @click="selectBtn('1')">
          <img src="@/assets/images/wakeup/maturity/btn2.png" @click="selectBtn('2')">
          <img src="@/assets/images/wakeup/maturity/btn3.png" @click="selectBtn('3')">
          <img src="@/assets/images/wakeup/maturity/btn4.png" @click="selectBtn('4')">
          <img src="@/assets/images/wakeup/maturity/btn5.png" @click="selectBtn('5')">
        </div>
        <div class="progress-container">
          <div class="progress-bar" :style="{ width: progress + '%' }">
          </div>
          <div class="progress-info">
             {{ newData.length }} / {{ questionSize }}
          </div>
        </div>
      </div>
      <div class="content-right">
          <img class="btn-img" v-show="newData.length>0" @click="previousQuestion" src="@/assets/images/wakeup/maturity/pre-qustion.png" >
        <div class="time">
          <img src="@/assets/images/wakeup/maturity/time.png" >
          <p> {{ formattedTime(elapsedTime) }}</p>
        </div>
        <img class="btn-img" v-show="newData.length >= questionSize" src="@/assets/images/wakeup/maturity/submit.png" >
      </div>
    </div>
  </div>

</template>

<script setup>
import headComponent from '@/views/xjc-integratedmachine/components/head_component.vue'
import {exampleList, maturityAppraisalList} from "@/api/xjc-integratedmachine/wakeup/index.js";


const router = useRouter()
let pageLoading = ref(false)
const headinfo = ref({})
// const progress = ref(0)
const draggedItem = ref(null);
const maturityAppraisalData = ref([])
const itemScore = ref('')
const newData = ref([])
const appraisalId = ref(null)
const questionSize = ref(null);
function dragStart(event) {
  draggedItem.value = event.target;
}

function drop(event) {
  event.preventDefault(); // 防止默认处理（例如打开链接）
  if (event.target !== draggedItem.value) {
    const target = event.target; // 获取放置的目标元素
    const item = draggedItem.value; // 获取被拖拽的元素
    // 交换位置或进行其他操作
    item.parentNode.insertBefore(item, target); // 将被拖拽的元素插入到目标元素之前
  }
}

function setHeadinfo(){
  headinfo.value = {
    title: '生涯成熟度测评',
    user: {
      avatar: '头像路径',
      nickName: '张三'
    },
    backUrl : '/index'
  }
}


function jumpTo(path) {
  router.push({
    path: path,
    query: {name: 123}
  })
}

function getMaturityAppraisalList() {
  pageLoading = true;
  let appraisalCode = "3001"
  maturityAppraisalList({appraisalCode:appraisalCode}).then(res => {
    if(res.data.questionList.length > 0){
      maturityAppraisalData.value = res.data.questionList;

    }
    appraisalId.value = res.data.appraisalId;
    questionSize.value = res.data.questionSize;
    pageLoading = false;
  }).catch((err)=>{
    console.log("error")
    pageLoading = false;
  })
}
//计时器
const elapsedTime = ref(0);
const timerId = ref(null);
const isRunning = ref(false);

const startTimer = () => {
  if (!isRunning.value) {
    isRunning.value = true;
    timerId.value = setInterval(() => {
      elapsedTime.value += 1000;
    }, 1000);
  }
};

const stopTimer = () => {
  if (isRunning.value) {
    isRunning.value = false;
    clearInterval(timerId.value);
    timerId.value = null;
  }
};

const resetTimer = () => {
  stopTimer();
  elapsedTime.value = 0;
};

const formattedTime = (ms) => {
  const totalSeconds = Math.floor(ms / 1000);
  const hours = String(Math.floor(totalSeconds / 3600)).padStart(2, '0');
  const minutes = String(Math.floor((totalSeconds % 3600) / 60)).padStart(2, '0');
  const seconds = String(totalSeconds % 60).padStart(2, '0');
  return `${hours}:${minutes}:${seconds}`;
};

onMounted(() => {
  // 可以在这里自动开始计时器，如果需要的话
  startTimer();
  setHeadinfo()
  getMaturityAppraisalList()
});

// onUnmounted(() => {
  stopTimer();
// });
// 定义当前完成题目数量和总题目数量
const current = ref(34);
const total = ref(100); // 假设总共有10道题

// 计算进度
const progress = computed(() => {
  return Math.floor((current.value / total.value) * 100);
});

// 更新当前完成题目数量的函数（例如，当用户完成一道题时调用）
const updateCurrent = (newCurrent) => {
  if (newCurrent >= 0 && newCurrent <= total.value) {
    current.value = newCurrent;
  }
};
const selectBtn = (val) => {
  //将当前题目加入新数组，
  // 拼接选项数据
  if(newData.value.length != questionSize.value ){
    itemScore.value += val + '|'
    newData.value.push(maturityAppraisalData.value[0]);
  }
  //更换题卡；
  if(maturityAppraisalData.value.length >1 ){
    maturityAppraisalData.value.splice(0, 1);
  }
};
const previousQuestion = (val) => {
  //新数组中的最后一位从头插入旧数组
  //新数组删除最后一位
  maturityAppraisalData.value.unshift(newData.value.pop());
  // newData.value.push(maturityAppraisalData.value[0]);
  // maturityAppraisalData.value.splice(0, 1);
  //字符串删除后两位
  itemScore.value = itemScore.value.slice(0, -2);
  console.log("itemScore",itemScore.value,newData.value)
};
</script>


<style scoped lang="scss">
p{
  margin: 0;
  padding:0;
}
.maturity-game-page{
  background: url('@/assets/images/wakeup/maturity/maturity-game-bg.png') no-repeat;
  background-size: 1920px 1080px;
  z-index:10;
  width: 100%;
  height: 1080px;
  .page-content{
    width: 100%;
    position: absolute;
    top: 123px;
    bottom: 0;
    display: flex;
    justify-content: space-around;
    .content-left{
      width: 244px;
    }
    .content-center{
      height:910px;
      display: flex;
      flex-direction: column;
      .content-img{
        width: 1187px;
        height: 424px;
        //border: 1px solid;
        margin-top: 40px;
        position: relative;
        img{
          width: 1187px;
          height: 424px;
        }
        p{
          width: 930px;
          position: absolute;
          top: 150px;
          left: 130px;
          font-weight: 400;
          font-size: 30px;
          color: #000000;
          letter-spacing: 4px;
          text-align: center;
        }
      }
      .progress-container {
        width: 100%;
        //background-color: #f3f3f3;
        border-radius: 4px;
        margin-bottom: 10px;
        overflow: hidden;
        display: flex;
        justify-content: center;
        align-items: center;
        margin-top: 200px;
      }

      .progress-bar {
        height: 10px;
        background-color: #63FFA4;
        text-align: center;
        line-height: 20px;
        border-radius: 100px 100px 100px 100px;
        color: white;
        transition: width 0.3s ease;
      }

      .progress-info {
        font-weight: 400;
        font-size: 24px;
        color: #00DF5D;
        margin-left: 20px;
      }
    }
   .content-right{
      display: flex;
      flex-direction: column;
      align-items: center;
      .btn-img{
         width: 244px;
         height: 77px;
         margin-top: 20px;
       }
      .time{
        width:220px;
        height: 50px;
        //border: 1px solid;
        display: flex;
        justify-content: center;
        margin-top: 20px;
        img{
          width: 42px;
          height: 42px;
          //border: 1px solid;
        }
        p{
          width: 150px;
          font-weight: 300;
          font-size: 32px;
          color: #363a3d;
          margin-left: 10px;
        }
      }
    }
  }
  .group-btn{
    display: flex;
    justify-content: space-around;
    margin-top: 80px;
    img{
      width: 200px;
      height: 135px;
    }
  }
}


</style>


