<template>
  <view class="card">
    <image class="card-bg" :src="data.url" mode="scaleToFill"></image>

    <view class="flex card-bar">
      <view class="flex countdown" v-if="countdown">
        距开始<text class="count">{{ countdown.day }}</text>天
        <text class="count">{{ countdown.hour }}</text>:<text class="count">{{ countdown.minute }}</text>:<text class="count">{{ countdown.second }}</text>
      </view>
      <button class="flex btn">进入直播间</button>
    </view>
  </view>
</template>

<script>
  import dayjs from "dayjs";
  import isSameOrBefore from 'dayjs/plugin/isSameOrBefore'
  
  dayjs.extend(isSameOrBefore);

  export default {
    props: {
      data: {
        type: Object,
        default() {
          return {}
        }
      }
    },
    data() {
      return {
        timer: null,
        countdown: null,
      }
    },
    watch: {
      data: {
        handler() {
          this.updateCountdown()
        },
        immediate: true,
        deep: true,
      }
    },
    methods: {
      updateCountdown() {
        this.timer && clearTimeout(this.timer)

        let current = dayjs()
        let startTime = dayjs(this.data.startTime)

        if (startTime.isSameOrBefore(current)) {
          this.countdown = null
          return
        }

        let countdown = {
          day: 0,
          hour: 0,
          minute: 0,
          second: 0,
        }

        let day = Math.floor(startTime.diff(current, 'day', true))
        countdown.day = day
        let hour = Math.floor(startTime.diff(current, 'hour', true))
        countdown.hour = hour - day * 24
        countdown.hour = countdown.hour < 10 ? `0${countdown.hour}` : countdown.hour
        let minute = Math.floor(startTime.diff(current, 'minute', true))
        countdown.minute = minute - hour * 60
        countdown.minute = countdown.minute < 10 ? `0${countdown.minute}` : countdown.minute
        let second = startTime.diff(current, 'second')
        countdown.second = second - minute * 60
        countdown.second = countdown.second < 10 ? `0${countdown.second}` : countdown.second

        this.countdown = countdown

        this.timer = setTimeout(() => {
          this.updateCountdown()
        }, 1000)
      },
    },
  }
</script>

<style scoped lang="scss">
.card {
  position: relative;
  width: 100%;
  height: 316rpx;
  border-radius: 40rpx;
  overflow: hidden;
  box-shadow: -5rpx -5rpx 10rpx 0 #FFFFFF,
              10rpx 10rpx 20rpx 0 #AAAACC80,
              4rpx 4rpx 10rpx 0 #AAAACC40,
              -2rpx -2rpx 5rpx 0 #FFFFFF;

  &-bg {
    width: 100%;
    height: 100%;
  }

  &-bar {
    justify-content: space-between;
    position: absolute;
    left: 0;
    bottom: 0;
    width: 100%;
    padding: 13rpx 32rpx;
    background: #FFFFFF69;
    box-sizing: border-box;

    .countdown {
      font-family: PingFang SC;
      font-weight: 400;
      font-size: 28rpx;
      line-height: 1.4;
      color: #252545;

      .count {
        margin: 0 8rpx;
        display: inline-flex;
        align-items: center;
        justify-content: center;
        min-width: 40rpx;
        height: 40rpx;
        background: #0000002B;
        border-radius: 8rpx;
      }
    }

    .btn {
      font-family: PingFang SC;
      font-weight: 600;
      font-size: 24rpx;
      line-height: 1.5;
      color: #252545;
      padding: 7rpx 16rpx;
      border-radius: 24rpx;
      background: #FFFFFF;
    }
  }
}
</style>