<template>
  <div class="browser">
    <child-view>
      <div class="sub-page">
        <div class="mx-4 py-4 border-app-blue-1 border border-solid rounded-lg bg-app-blue-4">
          <div class="">
            <h4 class="px-4 text-xl text-white break-all">{{ message.name }}</h4>
            <p class="mb-0 px-4 pb-3 text-xs leading-normal border-0 border-b border-app-blue-1 border-solid break-all">{{ message.desc }}</p>
          </div>
          <div class="px-4 py-4">
            <div class="flex items-center justify-between mb-0 text-xs text-app-white">
              <div class="flex-shrink-0">
                地址
              </div>
              <div class="flex items-center">
                <span class="inline-block mr-2 tracking-tighter">{{ addr.slice(0, 25) + '...' + addr.slice(-4) }}</span>
                <div
                  v-clipboard:copy="addr"
                  v-clipboard:success="onCopy"
                  v-clipboard:error="onError">
                  <app-icon customize size="16px" color="#FFBB00" type='icon-fuzhi'></app-icon>
                </div>
              </div>
              
            </div>
          </div>
          <p class="mb-0 px-4 text-app-white text-xs flex items-center justify-between">
            <span>当前总票数</span>
            <span>{{ message.score }}票</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>个人ID</div>
              <div>票数</div>
              <div>投票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="text-yellow-400" @click="goAddr(item)">{{ sliceStr(item.addr) }}</div>
                <div>{{ item.amount }}票</div>
                <div class="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>
            <x-pagination :currentPage="currentPage" :totalPage="totalPage" @handleChange="handleChange" />
          </div>
        </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,
      lists: [],
      totalPage: 1,
      all: [],
      count: 10,
      message: {},
      addr: ''
    }
  },
  methods: {
    goAddr(item?: any) {
      this.$router.replace({ path: '/browser/PersonVote', query: { addr: item.addr, optionId: this.$route.query.optionId }})
    },
    goHash(item?: any) {
      this.$router.replace({ path: '/Browser/VoteHash', query: {...this.$route.query, item: JSON.stringify(item), addr: item.txHash }})
    },
    sliceStr(i: string) {
      if (!i) return ''
      return i.slice(0,9) + '...' + i.slice(-4)
    },
    handleChange(v: number) {
      this.currentPage = v
      this.getpage(this.currentPage)
    },
    onCopy() {
      Toast('复制成功')
    },
    onError() {
      Toast('复制失败')
    },
    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.voteOptions[0]
        this.addr = res.data.result.creator
      })
    },
    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: 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.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)
      }).catch(() => {
        this.all = []
        this.lists = []
      })
    }
  },
  created() {
    this.getList()
    this.getDetails()
  },
  watch: {
    $route(n, o) {
      if (n.path.indexOf('/ProjectDetails') <= -1) return
      if (n.query.optionId !== o.query.optionId) {
        this.getList()
        this.getDetails()
      }
    }
  },
})
</script>
