<template>
  <div class="vote">
    <h1 class="vote-title">您的投票名单</h1>
    <div class="vote-box">
      <div class="vote-project" v-for="(item, index) in list" :key="index">
        <h2 class="vote-project-title">{{ item.project }}:</h2>
        <p class="vote-project-text">
          <template v-for="(user, index) in item.checked">
            <span v-if="user.checked" :key="user.id"><template v-if="index">、</template>{{ user.name }}</span>
          </template>
        </p>
      </div>
    </div>
    <div class="vote-button" @click="handSubmit">确认投票</div>
    <van-dialog v-model="show" title="您已投票成功" :showConfirmButton="false">
      <img src="../../assets/img/vote_success.png" class="vote-success-icon" />
      <div class="vote-button-success" @click="goHome">
        回到首页
        <van-count-down ref="countDown" :time="3000" :auto-start="false" @finish="finish">
          <template #default="timeData"> ({{ timeData.seconds }}s) </template>
        </van-count-down>
      </div>
    </van-dialog>
  </div>
</template>

<script>
import * as api from '@/api/vote'
export default {
  metaInfo: { title: '投票' },
  data() {
    return {
      show: false
    }
  },
  computed: {
    city() {
      return this.$route.query.city
    },
    list() {
      return this.$store.state.voteList.map(item => {
        item.checked = item.candidate.filter(item => item.checked)
        return item
      })
    }
  },
  methods: {
    async handSubmit() {
      const ids = this.list.reduce((results, item) => {
        item.candidate.forEach(user => {
          user.checked && results.push(user.id)
        })
        return results
      }, [])
      const hasVote = await this.checkVote()
      if (hasVote) {
        this.$toast('你已经投过票了')
      } else {
        this.handleRequest(ids)
      }
    },
    // 检测是否投票
    checkVote() {
      return api.checkVote().then(response => {
        return response.flag
      })
    },
    handleRequest(ids) {
      api.addVote(ids).then(response => {
        if (response.success) {
          this.show = true
          this.$nextTick(() => {
            this.$refs.countDown.start()
          })
        } else {
          this.$toast(response.message)
        }
      })
    },
    finish() {
      this.goHome()
    },
    goHome() {
      this.$router.replace('/')
    }
  },
  beforeMount() {
    !this.list.length && this.$router.back()
  }
}
</script>

<style scoped src="../../assets/css/vote.css"></style>
<style lang="scss" scoped>
.vote-project {
  margin-bottom: 0.2rem;
}
.vote-project-title {
  font-size: 0.2rem;
  font-weight: 600;
  color: #222;
}
.vote-project-text {
  margin-top: 0.05rem;
  font-size: 0.14rem;
  color: #666;
}
.vote-success-icon {
  display: block;
  width: 158px;
  margin: 30px auto;
}
.vote-button-success {
  margin: 20px auto;
  width: 225px;
  height: 35px;
  background: #c62245;
  border-radius: 6px;
  font-size: 15px;
  font-weight: 600;
  color: #fff;
  line-height: 35px;
  text-align: center;
  cursor: pointer;
}
::v-deep .van-count-down {
  display: inline;
  color: #fff;
}
::v-deep .van-dialog {
  width: 295px;
  border-radius: 6px;
}
::v-deep .van-overlay {
  background-color: rgba(0, 0, 0, 0.3);
}
</style>