<template>
  <transition name="msgbox-fade">
    <div class="pub-modal-mask pub-scroll-box" v-if="visible">
      <div class="packet-bg" v-if="!openInfo">
        <i class="el-icon-close" @click="visible = false"></i>
        <div class="user-info"  :class="{'small': info.ext.redpack_status == 1}">
          <img class="avatar" v-if="info.avatar" :src="info.avatar" alt="">
          <div v-else class="avatar no-avatar" :class="'avatar_bg' + userId % 9">{{info.name.splice(0,2).toUpperCase()}}</div>
          <span class="name">{{info.name}}</span>
          <div class="text"  v-if="info.ext.redpack_status == 0">{{info.content.title}}</div>
        </div>
        <!-- redpack_status: 0 => 进行中, 1 => 已抢完, 2 => 已过期 -->
        <div class="open-btn" v-if="info.ext.redpack_status == 0" @click="openPacket">開</div>
        <div class="redpack-tips" v-else-if="info.ext.redpack_status == 1">
          手慢了，红包派完了
        </div>
        <div class="redpack-tips" v-else-if="info.ext.redpack_status == 2">
          该红包已经过期。
        </div>
        <div class="detail" v-if="userId == info.userId" @click="showDetail">查看领取详情</div>
      </div>
      <div class="detail-bg" v-else>
        <i class="el-icon-close" @click="visible = false"></i>
        <img class="avatar" v-if="info.avatar" :src="info.avatar" alt="">
        <div v-else class="avatar no-avatar" :class="'avatar_bg' + userId % 9">{{info.name.splice(0,2).toUpperCase()}}</div>
        <span class="name">{{info.name}}</span>
        <div class="text">{{info.content.title}}</div>
        <div class="money" v-if="quantity">
          {{formatNum(quantity)}} <em>EOS</em>
        </div>
        <p class="state" v-if="quantity">已转账到EOS账户</p>
        <p class="state-num">
          已领取{{openInfo.num_total - openInfo.num_left}}/{{openInfo.num_total}}.
          共{{formatNum(openInfo.quantity_total - openInfo.quantity_left)}}/{{formatNum(openInfo.quantity_total)}}元
        </p>
        <ul class="packet-list pub-scroll-box">
          <li v-for="(item, key) in openInfo.logs" :key="key">
            <img class="avatar" v-if="item.cover_photo" :src="item.cover_photo" alt="">
            <div v-else class="avatar no-avatar" :class="'avatar_bg' + item.user_id % 9">
              {{item.nick_name.slice(0,2).toUpperCase()}}
            </div>
            <div class="content">
              <div class="top">
                <span class="user-name">{{item.nick_name}}</span>
                <span class="amount">{{formatNum(item.quantity_int)}} EOS</span>
              </div>
              <span class="time">{{item.create_time}}</span>
            </div>
          </li>
        </ul>
        <p class="tips">未领取的红包，将于24小时后发起退款</p>
      </div>
    </div>
  </transition>
</template>

<script>
import API from '@/api'
import NP from 'number-precision'
import { mapState } from 'vuex'

export default {
  name: 'packetGet',
  data () {
    return {
      openInfo: null
    }
  },
  computed: {
    ...mapState([
      'userId'
    ]),
    trxId () {
      return this.info.content.trxId
    },
    quantity () {
      if (this.openInfo && this.openInfo.logs.length) {
        let arr = this.openInfo.logs
        let item = arr.find(v => {
          return v.user_id == this.userId
        })
        if (item) {
          return item.quantity_int
        } else {
          return 0
        }
      } else {
        return 0
      }
    }
  },
  methods: {
    openPacket () {
      API.redpack.grabPacket({
        trx_id: this.trxId
      }).then(({ data }) => {
        this.openInfo = data.data
        this.$store.commit('unpdatePacketItem', {
          type: 'grabbed',
          trxId: this.trxId,
          data: 1
        })
      })
    },
    showDetail () {
      API.redpack.grabDetail({
        trx_id: this.trxId
      }).then(({ data }) => {
        this.openInfo = data.data
      })
    },
    formatNum (num) {
      return NP.divide(Number(num), 10000)
    }
  },
  created () {
    API.redpack.grabDetail({
      trx_id: this.trxId
    }).then(({ data }) => {
      // 已经领取 并且 红包未过期 打开显示详情页面
      if (this.info.ext.grabbed && this.info.ext.status !== 2) {
        this.openInfo = data.data
      } else if (data.data.quantity_left == 0) {
        // 没领取
        this.$store.commit('unpdatePacketItem', {
          type: 'redpack_status',
          trxId: this.trxId,
          data: data.data.redpack_status
        })
      }
      this.visible = true
    })
  }
}
</script>

<style lang="scss" scoped>
@import './style.scss';
</style>
