<template>
  <van-popup v-model="visible" class="records-popup" closeable swipeable close-icon="close">
    <van-tabs v-model="active" class="records-popup__tabs" :line-width="16" @change="onChange">
      <van-tab title="抽奖记录" name="personal">
        <div class="records-popup__personal-record">
          <div class="records-popup__record-content">
            <div class="records-popup__phone">
              领奖手机号：
              <span v-if="isBindPhone">{{ userInfo.phone }}</span>
              <span v-else class="records-popup__bind-phone" @click="bindMobile">绑定手机号</span>
            </div>
            <ul v-if="Object.keys(recordList).length !== 0" class="records-popup__record-list">
              <li v-for="(session, key, index) in recordList" :key="index">
                <div v-for="(item, itemIndex) in session" :key="itemIndex" class="records-popup__session-info">
                  <div class="records-popup__info-wrap">
                    <div v-if="session.length > 1" class="records-popup__session">
                      第{{ NUMBER_LIST[index + 1] }}场次
                    </div>
                    <div class="records-popup__time">
                      <span>{{ (item.drawTime * 1000) | formatDate('MM-DD HH:mm:ss') }}</span>
                    </div>
                  </div>
                  <div class="records-popup__prize-name">{{ item.prizeName }}</div>
                </div>
              </li>
            </ul>
            <div v-if="Object.keys(recordList).length === 0" class="records-popup__no-data">暂无记录</div>
          </div>
        </div>
      </van-tab>
      <van-tab v-if="isShowWinnersList" title="中奖名单" name="winners">
        <div class="records-popup__winners-record">
          <div class="records-popup__record-content">
            <div class="records-popup__winners-list">
              <div v-for="(item, index) in winnersList" :key="index" class="records-popup__item">
                {{ item.userNick }} 获得 <span class="records-popup__prize-name">{{ item.prizeName }}</span>
              </div>
              <div v-if="winnersList.length === 0" class="records-popup__no-data">暂无数据</div>
            </div>
          </div>
        </div>
      </van-tab>
    </van-tabs>
  </van-popup>
</template>

<script>
import { mapGetters, mapActions } from 'vuex'
import { getLotteryRecordList, getWinnersList } from '@/api/modules/lottery'
import { NUMBER_LIST } from '@/utils/constant'

export default {
  name: 'RecordsPupup',
  props: {
    value: {
      type: Boolean,
      default: false
    },
    isShowWinnersList: {
      type: Boolean,
      default: false
    },
    activeTab: {
      type: String,
      default: 'personal'
    }
  },
  data() {
    return {
      NUMBER_LIST,
      recordList: {},
      winnersList: [],
      active: ''
    }
  },
  computed: {
    ...mapGetters({
      uin: 'users/uin',
      userInfo: 'users/userInfo',
      isBindPhone: 'users/isBindPhone'
    }),
    visible: {
      get() {
        return this.value
      },
      set(val) {
        this.$emit('input', val)
      }
    }
  },
  watch: {
    visible: {
      handler(val) {
        if (val) {
          this.active = this.activeTab
          this.dataInit()
        }
      },
      immediate: true
    }
  },
  methods: {
    ...mapActions({ jumpToBinding: 'users/jumpToBinding' }),

    dataInit() {
      if (this.active === 'personal') {
        getLotteryRecordList({
          id: this.$route.query.id,
          uin: this.uin,
          playId: this.$route.query.sessionId
        }).then(res => {
          const { code, errorCode, errorMessage, data } = res
          if (code === 200 && errorCode === 0) {
            this.recordList = data
          } else {
            this.$toast(errorMessage)
          }
        })
      }
      if (this.active === 'winners') {
        getWinnersList({
          id: this.$route.query.id,
          uin: this.$route.query.uin
        }).then(res => {
          const { code, errorCode, errorMessage, data } = res
          if (code === 200 && errorCode === 0) {
            this.winnersList = data
          } else {
            this.$toast(errorMessage)
          }
        })
      }
    },
    onChange() {
      if (!Object.keys(this.recordList).length || !this.winnersList.length) {
        this.dataInit()
      }
    },
    bindMobile() {
      this.$toast({
        message: '正在为您转跳，请完成绑定手机操作',
        duration: 1500,
        onClose: () => {
          this.jumpToBinding()
        }
      })
    }
  }
}
</script>

<style lang='less' scoped>
.records-popup {
  width: 258px;
  height: 291px;
  background: #ffe9d8;
  border-radius: 15px;
  overflow: visible;
  font-size: 12px;
  /deep/ .van-popup__close-icon {
    right: -22px;
    top: -20px;
    font-size: 22px;
    color: #fff;
  }
  &__tabs {
    padding: 8px 0 0;
    height: 100%;
    overflow: hidden;
    display: flex;
    flex-direction: column;
    ::v-deep .van-tabs__wrap {
      display: flex;
      justify-content: center;
      height: 34px;
    }
    ::v-deep .van-tabs__nav {
      background-color: transparent;
      justify-content: center;
      width: 160px;
      .van-tab--active {
        .van-tab__text {
          color: #333;
          font-weight: 500;
          font-size: 14px;
        }
      }
    }
    ::v-deep .van-tabs__line {
      background-color: #f57641;
      border-radius: 1px;
    }
    ::v-deep .van-tabs__content {
      flex: 1;
      overflow: hidden;
      .van-tab__pane {
        width: 100%;
        height: 100%;
        padding: 15px 0;
      }
    }
  }
  &__personal-record {
    width: 100%;
    height: 100%;
    overflow: hidden auto;
    padding: 0 15px;
  }
  &__phone {
    color: #666;
    margin-bottom: 12px;
  }
  &__bind-phone {
    color: #f57641;
  }
  &__record-list {
    li {
      margin-bottom: 15px;
      &:last-child {
        margin-bottom: 0;
      }
    }
  }
  &__session-info {
    display: flex;
    flex-direction: column;
    &:last-child {
      .records-popup__prize-name {
        margin-bottom: 0;
      }
    }
  }
  &__info-wrap {
    display: flex;
    align-items: center;
  }
  &__session {
    margin-left: 5px;
    font-size: 13px;
    color: #333;
  }
  &__time {
    color: #666;
  }
  &__prize-name {
    margin: 8px 0 15px;
    color: #666;
  }
  &__item {
    margin-bottom: 8px;
  }
  &__winners-record {
    width: 100%;
    height: 100%;
    overflow: hidden auto;
    padding: 0 15px;
    .records-popup__prize-name {
      color: #f57641;
    }
  }
  &__no-data {
    color: #999;
    text-align: center;
    padding-top: 40px;
  }
}
</style>
