<template>
  <van-popup v-model="visible" class="record-popup" closeable position="left">
    <div class="record-popup__container">
      <div class="record-popup__container__head">我的抽奖纪录</div>
      <div class="record-popup__container__content">
        <div class="record-popup__container__content__phone">
          领奖手机号：
          <span v-if="isBindPhone">{{ userInfo.phone }}</span>
          <span v-else class="bind" @click="bindMobile">绑定手机号</span>
        </div>
        <ul v-if="Object.keys(recordList).length !== 0" class="record-popup__container__content__list">
          <li v-for="(session, key, index) in recordList" :key="index">
            <p v-if="session.length > 1" class="item__session">第{{ NUMBER_LIST[index + 1] }}场次</p>
            <div v-for="(item, itemIndex) in session" :key="itemIndex" class="item__session-info">
              <span class="item__session-info__level">{{ item.prizeName }}</span>
              <div class="item__session-info__time">
                <span>{{ (item.drawTime * 1000) | formatDate('MM-DD HH:mm:ss') }}</span>
              </div>
            </div>
          </li>
        </ul>
        <div v-if="Object.keys(recordList).length === 0" class="record-popup__container__content__no-data">
          暂无记录
        </div>
      </div>
    </div>
  </van-popup>
</template>

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

export default {
  name: 'RecordPopup',
  props: {
    value: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      NUMBER_LIST,
      recordList: {}
    }
  },
  computed: {
    ...mapGetters({
      uin: 'users/uin',
      userInfo: 'users/userInfo',
      isBindPhone: 'users/isBindPhone'
    }),
    visible: {
      get() {
        return this.value
      },
      set(val) {
        this.$emit('input', val)
      }
    }
  },
  watch: {
    visible(nVal) {
      if (nVal) {
        this.dataInit()
      }
    }
  },
  methods: {
    ...mapActions({ jumpToBinding: 'users/jumpToBinding' }),
    dataInit() {
      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)
        }
      })
    },
    bindMobile() {
      this.$toast({
        message: '正在为您转跳，请完成绑定手机操作',
        duration: 1500,
        onClose: () => {
          this.jumpToBinding()
        }
      })
    }
  }
}
</script>

<style lang="less" scoped>
@titleHeight: 46px;
.record-popup {
  height: 100%;
  width: 300px;
  box-shadow: 0 -1px 1px 0px #fff;
  /deep/ .van-popup__close-icon {
    color: #fc5147;
  }
  &__container {
    background-color: #ff8e5e;
    width: 100%;
    height: 100%;
    padding: 6px;
    &__head {
      height: @titleHeight;
      line-height: @titleHeight;
      text-align: center;
      font-size: 16px;
      font-weight: 600;
      color: #fc5147;
      background-color: #fcf7d5;
      border-radius: 6px 6px 0 0;
    }
    &__content {
      height: calc(100% - @titleHeight);
      background-color: #fcf7d5;
      border-radius: 0 0 6px 6px;
      overflow: hidden;
      overflow-y: auto;
      &__phone {
        color: #eda431;
        font-size: 16px;
        font-weight: 600;
        text-align: center;
        height: 36px;
        line-height: 36px;
        position: relative;
        margin-bottom: 10px;
        &:after {
          position: absolute;
          width: 70%;
          height: 1px;
          content: '';
          left: 50%;
          bottom: -10px;
          transform: translateX(-50%);
          background-color: #eda431;
        }
        > span {
          color: #f76b5f;
          &.bind {
            cursor: pointer;
          }
        }
      }
      &__list {
        color: #eda431;
        font-size: 14px;
        padding: 10px 0;
        width: 85%;
        margin: 0 auto;
        .item {
          &__session {
            text-align: center;
            height: 18px;
            line-height: 18px;
          }
          &__session-info {
            display: flex;
            justify-content: space-between;
            flex-wrap: nowrap;
            line-height: 16px;
            margin-top: 10px;
            &:first-child {
              margin-top: 0;
            }
            &__level {
              flex: 1;
            }
            &__time {
              width: 40%;
              margin-left: 10px;
              display: flex;
              align-items: flex-end;
            }
          }
        }
      }
      &__no-data {
        color: #aaa;
        text-align: center;
        font-size: 14px;
        height: 50px;
        line-height: 50px;
      }
    }
  }
}
</style>
