<template>
  <div class="browser">
    <child-view>
      <div class="sub-page">
        <div class="mx-4 py-4 border-app-blue-3 border border-solid rounded-lg bg-app-blue-4">
          <div class="px-4 pb-4 border-b border-solid border-app-blue-3">
            <div class="flex items-center justify-between mb-0 text-xs">
              <span class="inline-block mr-2 tracking-tighter">ID:{{ $route.query.addr }}</span>
              <div
                v-clipboard:copy="$route.query.addr"
                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>{{ amount }}票</span>
          </p>
        </div>
        <div class="mx-4 mt-5">
          <p class="mb-3 text-sm font-medium">投票详情</p>
          <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 text-left">个人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">{{ sliceStr(item.addr) }}</div>
                <div class="w-1/3 text-center">{{ item.amount }}票</div>
                <div class="w-1/3 text-right text-yellow-400" @click="goHash(item)">{{ sliceStr(item.txHash) }}</div>
              </li>
            </ul>
            <div v-else class="w-full h-24 flex items-center justify-center">暂无数据</div>
          </div>
        </div>
        <div>
          <x-pagination :currentPage="currentPage" :totalPage="totalPage" @handleChange="handleChange" />
        </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 XPagination from '@/components/common/Pagination.vue'
import VueClipboard from 'vue-clipboard2'
import { Toast } from 'vant'
import { service } from '@/service/index'
Vue.use(VueClipboard)
export default Vue.extend({
  components: { childView, AppIcon, XPagination },
  data() {
    return {
      currentPage: 1,
      all: [],
      lists: [],
      totalPage: 1,
      height: '',
      count: 10,
      message: {},
      amount: 0
    }
  },
  methods: {
    goHash(item: any) {
      this.$router.replace({ path: '/Browser/VoteHash', query: {...this.$route.query, item: JSON.stringify(item), addr: item.txHash} })
    },
    handleChange(v: number) {
      this.currentPage = v
      this.getpage(this.currentPage)
    },
    onCopy() {
      Toast('复制成功')
    },
    onError() {
      Toast('复制失败')
    },
    sliceStr(i: string) {
      if (!i) return ''
      return i.slice(0,9) + '...' + i.slice(-4)
    },
    getpage(i: number) {
      this.lists = this.all.slice(10*(i-1), 10*(i-1) + this.count)
    },
    getList() {
      service.tradeList({
        execer: 'user.p.szhtest.rankvote',
        funcName: 'GetCommitInfoList',
        payload: {
          id: '',
          optId: '',
          addr: this.$route.query.addr as string,
          count: -1,
          direction: 1,
          height: 0,
          index: 0
        }
      }).then(res => {
        this.all = res.data.result && res.data.result.list || []
        this.all.forEach((item: {amount: string}) => {
          this.amount += +item.amount
        })
        this.getpage(this.currentPage)
        this.totalPage = Math.ceil(this.all.length/this.count) <= 0 ? 1 : Math.ceil(this.all.length/this.count)
      }).catch(() => {
        this.lists = []
      })
    },
    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
      })
    },
  },
  created() {
    this.getList()
    this.getDetails()
  },
  watch: {
    $route(n, o) {
      if (n.query.voteID !== o.query.voteID) {
        this.getList()
        this.getDetails()
      }
    }
  },
})
</script>
