<template>
  <div>
    <child-view rightText="参与投票" @rightHandle="goVote">
      <div class="sub-page">
        <div class="mt-0.5 mx-4 py-5 bg-app-dark-2 border border-app-blue-3 border-solid rounded-lg">
          <div class="border-0 border-b border-app-blue-3 border-solid px-4 pb-3">
            <community-card :community="community" type="community" :optionId="$route.query.optionId" :voteId="$route.query.voteID"/>
          </div>
          <div class="mt-7 px-4">
            <TicketBar :ticketNum="ticketNum" />
          </div>
        </div>
        <div class="mb-10">
          <h4 class="text-sm text-white text-center mt-7 mb-2 relative">投票人员详情</h4>
          <div class="mx-4 border border-app-blue-3 border-solid rounded-lg overflow-hidden">
            <div class="w-full h-10 bg-app-blue-4 flex items-center justify-around text-xs">
              <div class="w-1/3 flex-grow text-center">人员ID</div>
              <div class="w-1/3 flex-grow text-center">票数</div>
              <div class="w-1/3 flex-grow text-center">投票HASH</div>
            </div>
            <ul v-if="list.length > 0" class="mb-0">
              <li
                v-for="item in list"
                :key="item.id"
                class="list-content leading-8 flex justify-around bg-app-dark-2 items-center text-xs">
                <div class="text-yellow-500" @click="goAddr(item)">{{ sliceStr(item.addr) }}</div>
                <div class="">{{ item.amount }}票</div>
                <div class="text-yellow-500" @click="goHash(item)">{{ sliceStr(item.txHash) }}</div>
              </li>
            </ul>
            <div v-else class="w-full h-24 flex items-center justify-center test-xs">暂无数据</div>
          </div>
        </div>
      </div>
      
    </child-view>
  </div>
</template>

<script lang="ts">
import CommunityCard from '@/components/home/CommunityCard.vue'
import TicketBar from '@/components/home/TicketBar.vue'
import Vue from 'vue'
import ChildView from '@/layouts/childView.vue'
import { service } from '@/service/index'
export default Vue.extend({
  components: {
    CommunityCard,
    TicketBar,
    ChildView
  },
  data() {
    return {
      community: {} as any,
      ticketNum: 0,
      list: [],
    }
  },
  methods: {
    goAddr(item: any) {
      this.$router.push({ path: '/browser/PersonVote', query: { addr: item.addr, optionId: this.$route.query.optionId }})
    },
    goHash(item: any) {
      this.$router.push({ path: '/Browser/VoteHash', query: {...this.$route.query, item: JSON.stringify(item), addr: item.txHash} })
    },
    goVote() {
      this.$router.push({ path: '/Home/CommunityVote', query: { ...this.$route.query }})
    },
    sliceStr(i: string) {
      if (!i) return ''
      return i.slice(0,9) + '...' + i.slice(-4)
    },
    getDetails() {
      service.voteDetails({
        execer: 'user.p.szhtest.rankvote',
        funcName: 'GetRankVoteInfo',
        payload: {
          voteID: this.$route.query.voteID as string,
          optionId: this.$route.query.optionId as string
        }
      }).then(res => {
        this.community = res.data.result.voteOptions[0]
        this.community.description = this.community.desc
        this.ticketNum = +(this.community as any).score
      })
    },
    getList() {
      service.tradeList({
        execer: 'user.p.szhtest.rankvote',
        funcName: 'GetCommitInfoList',
        payload: {
          id: this.$route.query.voteID as string,
          optId: this.$route.query.optionId as string,
          addr: '',
          count: -1,
          direction: 0,
          height: 0,
          index: 0
        }
      }).then(res => {
        this.list = res.data.result && res.data.result.list || []
      })
    }
  },
  created() {
    this.getDetails()
    this.getList()
  },
  watch: {
    // $route(n, o) {
    //   if (n.query.voteID !== o.query.voteID) {
    //     this.getDetails()
    //     this.getList()
    //   }
    // }
  }
})
</script>
