<template>
  <div class="browser">
    <child-view>
      <div class="sub-page">
        <div class="mx-4 py-4 border border-app-blue-3 border-solid rounded-lg bg-app-blue-4">
          <div class="px-4 pb-4 border-0 border-b border-app-blue-3 border-solid">
            <p class="text-sm mb-3">{{ message.name }}</p>
            <div class="flex items-center text-yellow-400 mb-0 text-xs justify-between">
              <span class="inline-block mr-2 tracking-tighter">ID:{{message.ID}}</span>
              <div
                v-clipboard:copy="message.ID"
                v-clipboard:success="onCopy"
                v-clipboard:error="onError">
                <app-icon customize size="16px" color="#FFBB00" type='icon-fuzhi'></app-icon>
              </div>
            </div>
          </div>
          <p class="mt-4 mb-0 px-4 opacity-60 text-xs flex items-center justify-between">
            <span>当前总票数</span>
            <span>{{ this.$route.query.optionId ? message.voteOptions && message.voteOptions[0].score : message.amount }}票</span>
          </p>
        </div>
        <div class="mx-4 mt-5">
          <p class="mb-3 text-sm font-medium">投票详情</p>
          <template>
            <div class="border border-app-blue-3 border-solid rounded-lg overflow-hidden">
              <div class="h-9 leading-9 flex items-center justify-between text-xs bg-app-blue-4 px-3">
                <div class="w-1/3">个人ID</div>
                <div class="w-1/3 text-center">票数</div>
                <div class="w-1/3 text-right">投票HASH</div>
              </div>
              <ul v-if="lists.length > 0" class="hash-list mb-0">
                <li
                  v-for="(item, index) in lists"
                  :key="index"
                  class="leading-9 px-3 text-xs flex items-center justify-between">
                  <div class="w-1/3 text-yellow-400 " @click="goPersonVote(item.addr)">{{ sliceStr(item.addr) }}</div>
                  <div class="w-1/3 text-center">{{ item.amount }}票</div>
                  <div class="w-1/3 text-yellow-400 text-right" @click="goHashVote(item)">{{ sliceStr(item.txHash) }}</div>
                </li>
              </ul>
              <div v-else class="w-full h-24 flex items-center justify-center text-base">
                暂无数据
              </div>
            </div>
            <div>
              <x-pagination :currentPage="currentPage" :totalPage="totalPage" @handleChange="handleChange" />
            </div>
          </template>
          
        </div>
      </div>
    </child-view>
  </div>
</template>

<script lang="ts">
import childView from '@/layouts/childView.vue'
import Vue from 'vue'
import AppIcon from '@/components/common/Icon.vue'
import { Toast } from 'vant';
import XPagination from '@/components/common/Pagination.vue'
import VueClipboard from 'vue-clipboard2'
import { service } from '@/service/index'
Vue.use(VueClipboard);
export default Vue.extend({
  components: { childView, AppIcon, XPagination },
  data() {
    return {
      currentPage: 1,
      all: [],
      lists: [],
      totalPage: 1,
      count: 10,
      message: {} as any
    }
  },
  computed: {
    id() {
      return this.$route.query.id
    }
  },
  created() {
    this.getMessage()
    this.getDetails()
  },
  watch: {
    $route(n, o) {
      if (n.query.voteID !== o.query.voteID) {
        this.getMessage()
        this.getDetails()
      }
    }
  },
  methods: {
    sliceStr(i: string) {
      if (!i) return ''
      return i.slice(0,9) + '...' + i.slice(-4)
    },
    goPersonVote(addr: string) {
      this.$router.replace({ path: '/browser/PersonVote', query: {...this.$route.query, addr} })
    },
    goHashVote(item: any) {
      this.$router.replace({ path: '/Browser/VoteHash', query: {...this.$route.query, item: JSON.stringify(item), addr: item.txHash} })
    },
    onCopy() {
      Toast('复制成功')
    },
    onError() {
      Toast('复制失败')
    },
    getpage(i: number) {
      this.lists = this.all.slice(10*(i-1), 10*(i-1) + this.count)
    },
    handleChange(v: number) {
      this.currentPage = v
      this.getpage(v)
    },
    getMessage() {
      service.tradeList({
        execer: 'user.p.szhtest.rankvote',
        funcName: 'GetCommitInfoList',
        payload: {
          id: this.$route.query.voteID as string,
          // optId: '',
          optId: this.$route.query.optionId as string,
          addr: '',
          count: -1,
          direction: 0,
          height: 0,
          index: 0
        }
      }).then(res => {
        this.all = res.data.result && res.data.result.list || []
        this.getpage(this.currentPage)
        this.totalPage = Math.ceil(this.all.length/this.count) <= 0 ? 1 : Math.ceil(this.all.length/this.count)
      })
    },
    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.message = res.data.result
      })
    }
  }
})
</script>
