<template>
  <child-view rightText="参与投票" @rightHandle="goVote">
    <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="community.type === 2 ? 'community' : 'pro'" :optionId="$route.query.optionId" :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">{{ item.content }}</span>
              <div v-else class="flex items-end" @click="item.type1 = false">
                <span class="inline-block type-1">
                  {{ item.content }}
                </span>
                <div class="mb-1">
                  <app-icon customize type='icon-arrow-right-copy-copy'></app-icon>
                </div>
              </div>
            </div> -->
            <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 }}票</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 tracking-tighter" @click="goProject(item.id)">
                <span>{{ item.id }}</span>
                <app-icon customize type='icon-arrow-right-copy-copy'></app-icon>
              </div>
            </div>
          </div>
          <!-- <button class="w-full bg-app-blue-2 h-10 leading-10 text-center text-sm rounded-lg opacity-30">已结束</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 Vue from 'vue'
import { service } from '@/service/index'
Vue.use(Progress)
export default Vue.extend({
  components: {
    childView,
    CommunityCard,
    AppIcon
  },
  data() {
    return {
      lists: [
      ],
      type1: true,
      voteNum: '',
      rest: 0,
      hide: false,
      sumVote: 0,
      community: {} as any
    }
  },
  methods: {
    goVote() {
      if (this.community.status < 3) {
        if (this.community.type === 2) {
          return
        }
        this.$router.push({ path: '/Square/SquareVote', query: { ...this.$route.query }})
      } else {
        this.$router.push({ path: '/Square/SquareDetails', query: { ...this.$route.query }})
      }
    },
    goProject(id: string) {
      this.$router.push({path: '/Browser/ProjectDetails', query: {...this.$route.query, optionId: id}})
    },
    triggle(i: number, v: number) {
      if (v === 1) {
        this.$set(this.lists[i], 'type1', 2)
      } else {
        this.$set(this.lists[i], 'type1', 1)
      }
    },
    getDetails() {
      service.voteDetails({
        execer: 'user.p.szhtest.rankvote',
        funcName: 'GetRankVoteInfo',
        payload: {
          voteID: this.$route.query.voteID as string,
          optionId: ''
        }
      }).then(res => {
        this.community = res.data.result
        this.lists = res.data.result.voteOptions
        this.sumVote = +res.data.result.amount
        this.lists.forEach((item: any) => {
          if (this.sumVote === 0) {
            item.val = 0
          } else {
            item.val = +Math.round((item.score / this.sumVote)*100)
          }
        })
        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)
            }
          }
        })
      })
    }
  },
  mounted() {
    this.getDetails()
  },
  watch: {
    $route(n, o) {
      if (n.path.indexOf('/MyProject') <= -1) return
      if (n.query.optionId !== o.query.optionId) {
        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>