<template>
  <div>
    <child-view>
      <div class="sub-page">
        <div class="mx-4 mb-5 py-3 px-2 text-xs border-app-blue-3 border border-solid rounded-lg bg-app-blue-4 text-app-white">
          <div class="flex items-center justify-between leading-9">
            <div class="flex-shrink-0">哈希</div>
            <div class="flex items-center justify-between text-xs">
              <span class="inline-block mr-2 tracking-tighter">{{ message.txHash ? message.txHash.slice(0, 25) + '...' + message.txHash.slice(-4) : '' }}</span>
              <div
                v-clipboard:copy="message.txHash"
                v-clipboard:success="onCopy"
                v-clipboard:error="onError">
                <app-icon customize size="16px" color="#FFBB00" type='icon-fuzhi'></app-icon>
              </div>
            </div>
          </div>
          <div class="flex items-center justify-between leading-9">
            <div class="">区块高度</div>
            <div>{{ zHeight }}</div>
          </div>
          <div class="flex items-center justify-between leading-9">
            <div class="">投票数</div>
            <div>{{ message.amount }}票</div>
          </div>
          <div class="flex items-center justify-between leading-9">
            <div class="">投票时间</div>
            <div>{{ timer() }}</div>
          </div>
          <div class="flex items-center justify-between leading-9">
            <div class="">手续费</div>
            <div>{{ 0.002 }}BTY</div>
          </div>
        </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">投票HASH</div>
              <div class="w-1/3 text-center">票数</div>
              <div class="w-1/3 text-right">参与项目ID</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.txHash) }}</div>
                <div class="w-1/3 text-center">{{ item.amount }}票</div>
                <div class="w-1/3 text-right text-yellow-400" @click="goPro(item)">{{ item.voteID.slice(0,3) + '...' + item.voteID.slice(-10) }}</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 {
      lists: [] as any,
      all: [] as any,
      currentPage: 1,
      totalPage: 10,
      count: 10,
      message: {
        blockTime: ''
      } as any,
      zHeight: 0
    }
  },
  computed: {
    
  },
  created() {
    this.getList()
    this.message = JSON.parse(this.$route.query.item as any)
  },
  watch: {
    $route(n) {
      if (n.path.indexOf('VoteHash') <= -1) return
      this.getList()
      this.message = JSON.parse(this.$route.query.item as any)
    }
  },
  methods: {
    goPro(item: any) {
      this.$router.replace({ path: '/Browser/index', query: { voteID: item.voteID, optionId: item.optionId }})
      // this.$router.replace({ path: '/Browser/index', query: { voteID: item.voteID, optionId: this.$route.query.optionId }})
    },
    timer() {
      const timeStamp = +(this.message as any).blockTime
      return (new Date(timeStamp*1000).getFullYear()) + '/' + (new Date(timeStamp*1000).getMonth() + 1) + '/' + (new Date(timeStamp*1000).getDate())
    },
    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.hashList({
        execer: 'user.p.szhtest.rankvote',
        funcName: 'GetCommitInfo',
        payload: {
          data: this.$route.query.addr as string
        }
      }).then(res => {
        this.all = [res.data.result] || []
        this.zHeight = this.all[0].height
        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 = []
      })
    }
  }
})
</script>
