<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.slice(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" :class="{'loading': isLoading}" 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 || info.ext.redpack_status == 1" @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.slice(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>{{openInfo.tokenType}}</em>
        </div>
        <p class="state" v-if="quantity">已转账到{{openInfo.type.toUpperCase()}}账户</p>
        <p class="state-num">
          共 {{openInfo.num_total}} 个，已领取 {{openInfo.num_total - openInfo.num_left}} 个
        </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)}} {{openInfo.tokenType}}</span>
              </div>
              <span class="time">{{formatTime(item.update_time_int)}}</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'
import { Message } from 'element-ui'
import dayjs from 'dayjs'

export default {
  name: 'packetGet',
  data () {
    return {
      openInfo: null,
      isLoading: false
    }
  },
  computed: {
    ...mapState([
      'userId',
      'account'
    ]),
    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 () {
      this.isLoading = true
      API.redpack.grabPacket({
        trx_id: this.trxId
      }).then(({ data }) => {
        // 抢光
        if (data.code === -1001) {
          this.$store.commit('unpdatePacketItem', {
            type: 'redpack_status',
            trxId: this.trxId,
            data: 1
          })
        } else {
          this.openInfo = data.data
          this.$store.commit('unpdatePacketItem', {
            type: 'grabbed',
            trxId: this.trxId,
            data: 1
          })
        }
      }).finally(() => {
        this.isLoading = false
      })
    },
    showDetail () {
      API.redpack.grabDetail({
        trx_id: this.trxId
      }).then(({ data }) => {
        this.openInfo = data.data
      })
    },
    formatNum (num) {
      return NP.divide(Number(num), 10000)
    },
    formatTime (timestamp) {
      return dayjs(timestamp).format('HH:mm')
    }
  },
  created () {
    if (this.account) {
      API.redpack.grabDetail({
        trx_id: this.trxId
      }).then(({ data }) => {
      // 已经领取
        if (this.info.ext.grabbed) {
          this.openInfo = data.data
        } else if (data.data.num_left == 0) {
        // 没领取
          this.$store.commit('unpdatePacketItem', {
            type: 'redpack_status',
            trxId: this.trxId,
            data: data.data.redpack_status
          })
        }
        this.visible = true
      })
    } else {
      Message({
        message: '请先登录',
        type: 'warning'
      })
    }
  }
}
</script>

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