<template>
  <van-popup v-model="visible" class="records-popup" swipeable>
    <div class="records-popup__close" @click="closePopup"></div>
    <div class="records-popup__title">
      <div class="records-popup__title-personal" :style="personalBg" @click="changeActive('personal')"></div>
      <div class="records-popup__title-winners" :style="winnersBg" @click="changeActive('winners')"></div>
    </div>
    <div class="records-popup__main" :style="mainBg">
      <div v-show="active === 'personal'" class="records-popup__personal-record">
        <div class="records-popup__record-content">
          <div class="records-popup__phone" :style="normalTextColor">
            领奖手机号：
            <span v-if="isBindPhone" :style="normalTextColor">{{ userInfo.phone }}</span>
            <span v-else class="records-popup__bind-phone" :style="prizebindTextColor" @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" :style="normalTextColor">
                    第{{ NUMBER_LIST[index + 1] }}场次
                  </div>
                  <div class="records-popup__time" :style="normalTextColor">
                    <span>{{ (item.drawTime * 1000) | formatDate('MM-DD HH:mm:ss') }}</span>
                  </div>
                </div>
                <div class="records-popup__prize-name" :style="normalTextColor">{{ item.prizeName }}</div>
              </div>
            </li>
          </ul>
          <div v-if="Object.keys(recordList).length === 0" class="records-popup__no-data" :style="noDataColor">
            暂无记录
          </div>
        </div>
        <div class="records-popup__jump-detail">
          <span @click="jumpDetail">查看奖品详情</span>
        </div>
      </div>
      <div v-show="active === 'winners' && isShowWinnersList" 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" :style="normalTextColor">
              {{ item.userNick }} 获得 <span class="records-popup__prize-name">{{ item.prizeName }}</span>
            </div>
            <div v-if="winnersList.length === 0" class="records-popup__no-data" :style="noDataColor">暂无数据</div>
          </div>
        </div>
      </div>
    </div>
  </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'
    },
    recordsConfig: {
      type: Object,
      default: () => ({
        mode: 'old'
      })
    }
  },
  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('setIsShowRecordsPopup', val)
      }
    },
    mainBg() {
      return {
        backgroundImage: `url(${this.recordsConfig.bg})`
      }
    },
    personalBg() {
      return {
        backgroundImage:
          this.active === 'personal'
            ? `url(${this.recordsConfig.personalBg})`
            : `url(${this.recordsConfig.personalBgDisabled})`
      }
    },
    winnersBg() {
      return {
        backgroundImage:
          this.active === 'winners'
            ? `url(${this.recordsConfig.winnersBg})`
            : `url(${this.recordsConfig.winnersBgDisabled})`
      }
    },
    closeBg() {
      return {
        backgroundImage: `url(${this.recordsConfig.closeIcon})`
      }
    },
    noDataColor() {
      return {
        color: this.recordsConfig.noDataColor
      }
    },
    normalTextColor() {
      return {
        color: this.recordsConfig.normalTextColor
      }
    },
    prizebindTextColor() {
      return {
        color: this.recordsConfig.prizebindTextColor
      }
    }
  },
  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()
        }
      })
    },
    changeActive(val) {
      this.active = val
      this.onChange()
    },
    closePopup() {
      this.$emit('setIsShowRecordsPopup', false)
    },
    jumpDetail() {
      this.$router.push({
        path: '/records',
        query: {
          uin: this.uin
        }
      })
    }
  }
}
</script>

<style lang="less" scoped>
.records-popup {
  top: 48%;
  width: 258px;
  height: 291px;
  background-color: transparent;
  border-radius: 15px;
  overflow: visible;
  font-size: 12px;
  &__close {
    position: absolute;
    right: -8px;
    top: 15px;
    width: 25px;
    height: 25px;
    background-size: 100% 100%;
  }
  &__main {
    position: relative;
    height: 100%;
    background-size: 100% 100%;
    padding: 24px 16px;
    transition: all 0.2s;
  }
  &__title {
    color: #fff;
    display: flex;
    justify-content: center;
    &-personal,
    &-winners {
      width: 83px;
      height: 29px;
      background-size: 100% 100%;
      margin: 0 3px;
      transition: all 0.2s;
    }
  }
  &__record-content {
    height: 85%;
  }
  &__jump-detail {
    width: 100%;
    height: 18px;
    line-height: 18px;
    text-align: center;
    color: #ff0024;
    font-size: 12px;
    margin-top: 10px;
  }
  /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: #d92e32;
      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: #000;
    margin-bottom: 12px;
  }
  &__bind-phone {
    color: #d92e32;
  }
  &__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-right: 5px;
    font-size: 13px;
    color: #000;
  }
  &__time {
    color: #000;
  }
  &__prize-name {
    margin: 8px 0 15px;
    color: #000;
  }
  &__item {
    margin-bottom: 8px;
  }
  &__winners-record {
    width: 100%;
    height: 100%;
    overflow: hidden auto;
    padding: 0 15px;
    .records-popup__prize-name {
      color: #d92e32;
    }
  }
  &__no-data {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    color: #999;
    text-align: center;
    // padding-top: 40px;
  }
}
</style>
