<template>
  <div class="turn-table">
    <div class="pick-target" v-show="isShowPickTarget == true">
      <img v-if="this.pickTarget.img != ''" :src="this.pickTarget.img" alt="" />
      <div v-else class="pick-default-img"></div>
      <div class="pick-info">
        <div class="pick-sort">{{ this.pickTarget.sort }}</div>
        <span v-show="this.pickTarget.name != ''" class="pick-text">{{ this.pickTarget.name }}</span>
      </div>
    </div>
    <div class="container">
      <section class="turntable">
        <div class="list" id="list">
          <ul :style="{ transform: `rotate(${this.rotatedeg}deg)` }" :class="{ go: isRotating }">
            <li v-for="item in prize_list" :key="item.index">
              <p class="list-name">{{ item.name }}</p>
              <p>{{ item.sort }}</p>
            </li>
          </ul>
        </div>
        <button class="turntable_btn" type="button" @click="pickOne()">pick</button>
        <svg viewBox="-50 -50 100 100">
          <g class="polyline" :style="{ transform: `rotate(${this.rotatedeg}deg)` }" :class="{ go: isRotating }">
            <polyline points="-16,-38 16,-38 0,0"></polyline>
            <polyline points="16,-38 38,-16 0,0"></polyline>
            <polyline points="38,-16 38,16 0,0"></polyline>
            <polyline points="0,0 38,16 16,38"></polyline>
            <polyline points="0,0 16,38 -16,38"></polyline>
            <polyline points="0,0 -16,38 -38,16"></polyline>
            <polyline points="-38,-16 -38,16 0,0"></polyline>
            <polyline points="-16,-38 -38,-16 0,0"></polyline>
          </g>
          <circle cx="0" cy="0" r="40" :class="{ go: isRotating }"></circle>

          <g class="mark">
            <!--circle cx="0" cy="-43" r="4"></circle-->
            <polyline points="-5.5,-45 5.5,-45 0,-35"></polyline>
            <!--circle class="round" cx="0" cy="-43" r="1.5"></circle-->
          </g>
        </svg>
      </section>
    </div>
  </div>
</template>

<script>
export default {
  name: 'TurnTable',
  data() {
    return {
      prize_list: [],
      nameList: this.$store.state.students,
      iEnd: -1,
      targetPrizeElement: '',
      rotatedeg: 0,
      isRotating: false,
      isShowPickTarget: false,
      pickTarget: {
        name: '',
        sort: '',
        img: ''
      }
    }
  },
  created() {
    for (let i=1 ;i<= 8;i++) {
      this.prize_list.push({
        name: '',
        sort: i,
        img: ''
      })
    }
    console.log(this.prize_list)
  },
  methods: {
    randomPrizelist() {
      if (this.nameList.length == 0) {
        this.$Message.warning('暂无成员参与挑人。')
      } else {
        this.prize_list.forEach(element => {
          let ramdomIndex = Math.floor(Math.random() * this.nameList.length)
          //console.log(this.nameList[ramdomIndex])
          element.img = this.nameList[ramdomIndex].headImg
          element.sort = this.nameList[ramdomIndex].sort
          element.name = this.nameList[ramdomIndex].studentName.length>6?this.nameList[ramdomIndex].studentName.slice(0,5)+'..':this.nameList[ramdomIndex].studentName
        })
      }
    },
    async pickOne() {
      this.pickTaget = ''
      this.isShowPickTarget = false
      await this.randomPrizelist()
      if (this.isRotating == false) {
        this.iEnd = Math.floor(Math.random() * 8)
        this.isRotating = true

        setTimeout(() => {
          this.isRotating = false
          this.isShowPickTarget = true
          this.pickTarget.img = this.prize_list[this.iEnd].img
          this.pickTarget.sort = this.prize_list[this.iEnd].sort
          this.pickTarget.name = this.prize_list[this.iEnd].name
          //   alert('選中' + this.prize_list[this.iEnd].sort + '' + this.prize_list[this.iEnd].name)
        }, 4000)
        // console.log(this.iEnd);
        // let prizes = document.getElementById("list").getElementsByTagName('p')
        // for(let i=0; i<prizes.length;i++){
        //     if(parseInt(prizes[i].innerText)==this.iEnd){
        //         console.log(prizes[i])
        //     }
        // }
        this.rotatedeg = this.iEnd * 45
      }
    }
  }
}
</script>

<style scoped lang="less">
@charset "UTF-8";
@keyframes shiny {
  0% {
    fill: white;
  }
  50% {
    fill: #ffc242;
  }
  100% {
    fill: white;
  }
}
@keyframes go {
  0% {
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(360deg);
  }
}
* {
  padding: 0;
  margin: 0;
  font-family: '微軟正黑體';
}

body {
  background-color: #3dbfbb;
}

.container {
  max-width: 1000px;
  min-width: 400px;
  margin: 0 auto;
  overflow: hidden;
}

header {
  text-align: center;
  padding: 50px;
}

section.turntable {
  position: relative;
  min-width: 500px;
  min-height: 500px;
  button.turntable_btn {
    cursor: default;
    opacity: 1;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    box-shadow: none;
    border: none;
    outline: none;
    font-weight: 900;
    width: 30%;
    height: 30%;
    border-radius: 50%;
    font-size: 40px;
    color: #0080c0;
    background-color: #ffb929;
    cursor: pointer;
    &::after {
      position: absolute;
      left: 50%;
      top: 50%;
      transform: translate(-50%, -50%);
      content: '';
      width: 80%;
      height: 80%;
      border-radius: 50%;
      background-color: #ffe3a8;
      z-index: -1;
      box-shadow: 0px 2px 4px #754f00;
    }
    &:active::after {
      box-shadow: none;
      margin-top: 2px;
      background-color: #ffda8f;
      opacity: 1;
    }
  }
  .list {
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    border-radius: 50%;
    width: 70%;
    height: 70%;
    text-align: center;
    ul {
      list-style: none;
      width: 100%;
      height: 100%;
      transform: rotate(0deg);
      li {
        color: #fff;
        font-size: 24px;
        line-height: 40px;
        position: absolute;
        width: 100%;
        height: 100%;
       
       
      }
    }
    .go {
      animation: go 0.8s linear 4;
      -webkit-animation: go 0.8s linear 4;
    }
  }
  svg > circle {
    fill: none;
    stroke: blue;
    stroke-width: 4px;
  }
  svg g.polyline.go {
    animation: go 0.8s linear 4;
    -webkit-animation: go 0.8s linear 4;
  }
  svg g.polyline polyline {
    fill: #0056f5;
  }
  svg g.polyline polyline:nth-child(odd) {
    fill: rgb(65, 130, 252);
  }
  svg g.circle circle {
    fill: #fff;
    stroke: none;
    box-shadow: 0px 2px 5px #ccc;
  }
  svg g.circle circle:nth-child(odd) {
    animation: shiny 0.8s linear infinite;
    -webkit-animation: shiny 0.8s linear infinite;
  }
  svg g.circle circle:nth-child(odd).go {
    animation: go 0.8s linear 4, shiny 0.8s linear infinite;
    -webkit-animation: go 0.8s linear 4, shiny 0.8s linear infinite;
  }
  svg g.circle circle:nth-child(even) {
    animation: shiny 0.8s 0.5s linear infinite;
    -webkit-animation: shiny 0.8s 0.5s linear infinite;
  }
  svg g.circle circle:nth-child(even).go {
    animation: go 0.8s 0.8s linear 4, shiny 0.8s 0.5s linear infinite;
    -webkit-animation: go 0.8s linear 4, shiny 0.8s 0.5s linear infinite;
  }
  svg g.mark circle {
    fill: #ffb10f;
  }
  svg g.mark circle.round {
    fill: none;
    stroke: #ffebc2;
  }
  svg g.mark polyline {
    fill: #ffb10f;
    stroke: none;
  }
}

.list ul li:nth-child(2) {
  transform: rotate(315deg);
}
.list ul li:nth-child(3) {
  transform: rotate(270deg);
}
.list ul li:nth-child(4) {
  transform: rotate(225deg);
}
.list ul li:nth-child(5) {
  transform: rotate(180deg);
}
.list ul li:nth-child(6) {
  transform: rotate(135deg);
}
.list ul li:nth-child(7) {
  transform: rotate(90deg);
}
.list ul li:nth-child(8) {
  transform: rotate(45deg);
}
.pick-target {
  position: absolute;
  top: 210px;
  left: -60px;
  width: 150px;
  height: 150px;
  //background-color: #1c6bff;
  color: white;
  text-align: center;
  font-size: 20px;
  border: 6px solid blue;
  border-radius: 5px;
  z-index: 99;
  .pick-default-img {
    background: url('../../assets/img/head0.jpg') no-repeat;
    background-size: contain;
    z-index: 100;
    width: 100%;
    height: 100%;
  }
  .pick-info {
    position: absolute;
    width: 100%;
    bottom: 0%;
    line-height: 32px;
    font-weight: bolder;
    background-color: blue;

    .pick-sort {
      display: inline-block;
      position: relative;
      background-color: #ffb929;
      color: black;
      width: 30px;
      height: 30px;
      top: 4px;
      border-radius: 50%;
    }
    .pick-text {
      margin-left: 10px;
      display: inline-block;
      position: relative;
      top: 2px;
    }
  }
}
</style>
