<template>
<div>
    <img class="bg" :src="bg" alt="">
    <div class="top" v-if="submitState!==2">
        <div class="top-message">
            您已回答
            <div class="time">{{$utils.formatTime(paperDuration)}}</div>
            秒！
        </div>
        <div class="nav-list">
            <div :class="page===item?'active':''" v-for="item in 5" :key="item" @click="page=item">{{`第${(item-1)*10+1}-${item*10}题`}}</div>
        </div>
    </div>
    <div class="f">
        <div class="content">
            <div class="title" v-if="!submitState">2022年冬奥会知识竞赛</div>
            <div class="answer">
                <div class="answer-item" v-for="(item,index) in answerList" :key="item.id">
                    <div class="answer-item-title">
                        <div class="answer-item-icon">{{(page-1)*10+index+1}}</div>
                        <div class="answer-item-text">{{item.topic}}</div>
                    </div>
                    <div class="answer-item-choose">
                        <div :class="chooseList[(page-1)*10+index]===num?'active':''" v-for="(i,num) in item.options" :key="i.optionId" @click="clickChoose((page-1)*10+index,num)">{{['A','B','C','D'][num]}}. {{i.optionContent}}</div>
                    </div>
                </div>
            </div>
        </div>
        <div class="button-group" v-if="page===1">
            <div class="first-button" @click="page=2" v-if="!submitState">下一页</div>
        </div>
        <div class="button-group" v-else-if="!submitState">
            <div class="back-button" @click="page-=1">上一页</div>
            <div class="next-button" @click="page+=1" v-if="page!==5">下一页</div>
            <div class="confim-button" @click="submit(false)" v-else>提交</div>
        </div>
        <div class="bounced" :style="conTop" v-if="submitState">
            <i class="el-icon-error" @click="submitState = 0"></i>
            <div :style="{'height':'50px'}" v-if="!(submitState===1)"></div>
            <img :src="icon" alt="">
            <div class="bounced-message">{{submitState===1?'您未答完全部题，确认提交吗？':'提交已成功！'}}</div>
            <div class="bounced-button" @click="submit(true)" v-if="submitState===1">提交</div>
            <!-- <div class="bounced-button" v-else>返回</div> -->
        </div>
    </div>
</div>
</template>

<script>
export default {
  data () {
    return {
      bg: require('../../public/img/contest/bg.png'),
      conTop: {
        backgroundImage: 'url(' + require('../../public/img/contest/confirm-bg.png') + ')',
        backgroundRepeat: 'no-repeat',
        backgroundSize: 'cover'
      },
      icon: require('../../public/img/contest/success.png'),
      page: 1,
      questions: [],
      chooseList: new Array(50),
      paperId: null,
      timer: null,
      paperDuration: 0,
      submitState: 0, // 0 正常 1 警告 2 完成
      user: JSON.parse(localStorage.getItem('user')) || {}
    }
  },
  computed: {
    answerList () {
      let {
        questions,
        page
      } = this.$data
      return this.submitState ? [] : questions.filter((itx, num) => num >= (page - 1) * 10 && num < page * 10)
    }
  },
  watch: {
    page () {
      document.body.scrollIntoView()
    }
  },
  methods: {
    // 数组乱序
    shuffle (arr) {
      let length = arr.length
      let r = length
      let rand = 0

      while (r) {
        rand = Math.floor(Math.random() * r--);
        [arr[r], arr[rand]] = [arr[rand], arr[r]]
      }
      return arr
    },
    getAttendContest () {
      this.$api.post('/mCompetition/attendContest', {
        userId: this.user.id
      }).then(r => {
        this.questions = this.shuffle(r.object.questions)
          .map(item => {
            item.options = this.shuffle(item.options)
            return item
          })
        this.paperId = r.object.paperId
      })
    },
    // q 问题下标 a 选择答案下标
    clickChoose (q, a) {
      this.$set(this.chooseList, q, a)
    },
    // false 需确认 true 直接请求
    submit (type = false) {
      if (!type) {
        for (let i = 0; i < this.chooseList.length; i++) {
          if (this.chooseList[i] === undefined) return (this.submitState = 1)
        }
      }
      let paperScore = 0
      let questionWrong = []
      let {
        paperDuration,
        paperId,
        questions,
        chooseList
      } = this.$data
      questions.forEach((item, index) => {
        (chooseList[index] || chooseList[index] === 0) && item.options[chooseList[index]].optionAnswer
          ? paperScore += 2
          : questionWrong.push({
            [item.id]: (chooseList[index] || chooseList[index] === 0) ? item.options[chooseList[index]].optionId : ''
          })
      })
      this.$api.post('/mCompetition/submitUserAnswer', {
        reqdata: {
          paperId,
          paperDuration,
          paperScore,
          questionWrong: JSON.stringify(questionWrong),
          userId: this.user.id
        }
      }).then(r => {
        this.submitState = 2
        clearInterval(this.timer)
      }).catch(e => {
        clearInterval(this.timer)
      })
    }
  },
  mounted () {
    this.getAttendContest()
  },
  created () {
    this.timer = setInterval(() => {
      this.paperDuration += 1
    }, 1000)
  },
  beforeDestroy () {
    this.timer && clearInterval(this.timer)
  }
}
</script>

<style lang="scss" scoped>
.bg {
    margin: 0;
    padding: 0;
    float: left;
    position: absolute;
    z-index: 0;
    left: 50%;
    transform: translateX(-50%);
}

.top {
    font-size: 24px;
    font-weight: 400;
    color: #FFFFFF;
    position: relative;
    z-index: 9;

    &-message {
        padding-top: 175px;
        display: flex;
        font-size: 24px;
        font-weight: 400;
        color: #FFFFFF;
        justify-content: center;
        align-items: center;

        .time {
            min-width: 136px;
            height: 39px;
            background: #495AFC;
            margin: 0 5px;
            border-radius: 5px;
            font-size: 36px;
            line-height: 39px;
            text-align: center;
            font-weight: 500;
            color: #FFFFFF;
        }
    }

    .nav-list {
        display: flex;
        justify-content: center;
        margin-top: 40px;
        font-size: 16px;
        font-weight: 400;
        color: #FFFFFF;

        &>div {
            width: 85px;
            height: 30px;
            line-height: 30px;
            margin: 0 15px;
            text-align: center;
        }

        .active {
            background-color: #ffffff;
            border-radius: 30px;
            font-size: 16px;
            font-weight: 500;
            color: #59598D;
        }
    }
}

.f {
    position: absolute;
    z-index: 9;
    left: 50%;
    transform: translateX(-50%);
    top: 306px;
}

.content {
    width: 905px;
    min-height: 579px;
    background: #FFFFFF;
    box-shadow: 0px 2px 15px 0px rgba(228, 231, 246, 0.5);
    border-radius: 38px;
    padding-bottom: 27px;

    .title {
        font-size: 24px;
        font-weight: 500;
        color: #59598D;
        text-align: center;
        padding-top: 50px;
    }

    .answer {
        padding-top: 42px;
        display: flex;
        align-items: center;
        flex-direction: column;

        &-item {
            width: 450px;
            margin-bottom: 25px;

            &-title {
                display: flex;
                align-items: center;
            }

            &-icon {
                width: 20px;
                height: 20px;
                border-radius: 20px;
                background-image: linear-gradient(#FF7A09, #F4B65A);
                margin-right: 13px;
                font-weight: 600;
                color: #FFFFFF;
                text-align: center;
                font-size: 14px;
            }

            &-text {
                width: 350px;
                font-size: 14px;
                font-weight: 500;
                color: #59598D;
            }

            &-choose {
                >div {
                    width: 336px;
                    min-height: 34px;
                    background: #FFFFFF;
                    border-radius: 17px;
                    border: 1px solid #EFEFFF;
                    font-size: 14px;
                    font-weight: 400;
                    padding: 8px 0 9px 20px;
                    margin: 10px 0 0 20px;
                    box-sizing: border-box;
                    color: #59598D;
                }

                .active {
                    color: #FFFFFF;
                    background: #5679F0;
                }
            }
        }
    }
}

.button-group {
    display: flex;
    justify-content: center;
    margin: 20px 0 30px 0;
}

.first-button {
    background-image: linear-gradient(to right, #57A8EF, #6247FF);
    width: 330px;
    height: 65px;
    border-radius: 60px;
    font-size: 18px;
    font-weight: 600;
    color: #FFFFFF;
    line-height: 65px;
    text-align: center;
}

.back-button,
.next-button {
    width: 142px;
    height: 58px;
    text-align: center;
    border-radius: 29px;
    font-size: 18px;
    font-weight: 500;
    line-height: 58px;
    color: #59598D;
    border: 1px solid #C7CCF9;
    margin: 0 5px;
}

.confim-button {
    width: 142px;
    height: 58px;
    background-image: linear-gradient(to right, #57A8EF, #6247FF);
    border-radius: 29px;
    font-size: 18px;
    line-height: 58px;
    font-weight: 600;
    color: #FFFFFF;
    text-align: center;
    margin-left: 5px;
}

.bounced {
    width: 340px;
    height: 340px;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    display: flex;
    flex-direction: column;
    align-items: center;

    >i {
        position: absolute;
        top: 20px;
        right: 10px;
        font-size: 18px;
    }

    >img {
        padding: 63px 0 34px 0;
        width: 50px;
        height: 50px;
    }

    &-message {
        margin-bottom: 33px;
        font-size: 18px;
        font-weight: 500;
        color: #161616;
    }

    &-button {
        width: 142px;
        height: 48px;
        border-radius: 25px;
        border: 1px solid #C7CCF9;
        line-height: 48px;
        font-size: 16px;
        font-weight: 600;
        color: #59598D;
        text-align: center;
    }
}
</style>
