<template>
  <child-view>
    <div ref="box" class="sub-page mx-4">
      <div class="bg-app-dark-2 border border-app-blue-3 border-solid rounded-lg">
        <div class="p-4 border-0 border-b border-app-blue-3 border-solid">
          <community-card :community="community" type="pro" :voteId="$route.query.voteID" />
        </div>
        <div class="p-4 border-0 border-b border-app-blue-3 border-solid" :class="{'mb-36': hide}">
          <div
            v-for="(item, index) in lists"
            :key="index"
            class="mb-3 py-3 flex flex-col border border-app-blue-3 border-solid rounded-lg"
            >
            <div ref="content" class="text-sm px-3">
              <span class="inline-block" v-if="item.type1 === 0">{{ item.name }}</span>
              <div v-else class="flex items-end" @click="triggle(index, item.type1)">
                <span class="inline-block transition-all duration-300 delay-100 ease-in-out overflow-hidden" :class="{ 'initial-height': item.type1 === 1, 'max-h-screen': item.type1 === 2 }">
                  {{ item.name }}
                </span>
                <div class="transition-all duration-300 ease-in-out" :class="{'transform origin-center -rotate-90 mb-0': item.type1 === 2, 'mb-1': item.type1 === 1 }">
                  <app-icon customize type='icon-arrow-right-copy-copy'></app-icon>
                </div>
              </div>
            </div>
            <div class="mt-4 px-4 pb-3 flex items-center justify-between text-xs border-0 border-b border-app-blue-3 border-solid">
              <div class="w-1/2 flex-grow-0">
                <van-progress pivot-text="红色" color="#FF7327" :percentage="item.val" :show-pivot="false" />
              </div>
              <div>{{ item.score || 0 }}票</div>
              <div>{{ item.val + '%' || '0%' }}</div>
            </div>
            <div class="mt-3 px-3 flex items-center justify-between text-xs">
              <div>ID</div>
              <div class="flex items-center" @click="goProject(item.id)">
                <span class="tracking-tighter">{{ item.id }}</span>
                <app-icon customize type='icon-arrow-right-copy-copy'></app-icon>
              </div>
            </div>
          </div>
          <button class="w-full bg-app-blue-3 h-10 leading-10 text-center text-sm rounded-lg opacity-30">{{ community.status === 3 ? "已结束" : "已关闭" }}</button>
        </div>
      </div>
    </div>
  </child-view>
</template>

<script lang="ts">
import CommunityCard from '@/components/home/CommunityCard.vue'
import childView from '@/layouts/childView.vue'
import AppIcon from '@/components/common/Icon.vue'
import { Progress } from 'vant'
import { service } from '@/service/index'
import Vue from 'vue'
Vue.use(Progress)
export default Vue.extend({
  components: {
    childView,
    CommunityCard,
    AppIcon
  },
  data() {
    return {
      lists: [],
      type1: true,
      voteNum: '',
      rest: 1111,
      hide: false,
      community: {},
      sumVote: 0
    }
  },
  methods: {
    getDetails() {
      service.voteDetails({
        execer: 'user.p.szhtest.rankvote',
        funcName: 'GetRankVoteInfo',
        payload: {
          voteID: this.$route.query.voteID as string
        }
      }).then(res => {
        this.community = res.data.result
        this.lists = res.data.result.voteOptions
        this.sumVote = +res.data.result.amount
        this.lists.forEach((item: any) => {
          const val = this.sumVote ? +parseInt((item.score / this.sumVote)*100 + '') : 0
          this.$set(item, 'val', val)
        })
        this.$nextTick(() => {
          // const content = this.$refs.content as any[]
          for (let i = 0; i < this.lists.length; i++) {
            if ((this.lists[i] as any).name.length > 50) {
              this.$set(this.lists[i], 'type1', 1)
            } else {
              this.$set(this.lists[i], 'type1', 0)
            }
          }
        })
      })
    },
    goProject(id: number) {
      this.$router.push({ path: '/Browser/ProjectDetails', query: {optionId: '' + id, voteID: this.$route.query.voteID}})
    },
    triggle(i: number, v: number) {
      if (v === 1) {
        this.$set(this.lists[i], 'type1', 2)
      } else {
        this.$set(this.lists[i], 'type1', 1)
      }
    }
  },
  mounted() {
    this.getDetails()
  },
  watch: {
    $route(n, o) {
      if (n.query.voteID !== o.query.voteID) {
        this.getDetails()
      }
    }
  }
})
</script>

<style scoped>
  .type-1 {
    display: -webkit-box;
    overflow: hidden;  
    -webkit-line-clamp: 3;
    -webkit-box-orient: vertical;
  }
  .initial-height {
    max-height: 60px;
  }
</style>