<!--
 * @Author: ch
 * @Date: 2022-04-12 17:33:42
 * @LastEditors: ch
 * @LastEditTime: 2022-05-07 16:37:00
 * @Description: file content
-->
<template>
	<view>
		<view class="seckill" :class="activityStatus === 'startActivity' && 'seckill__start'">
			<view class="seckill--price-box" v-if="activityStatus === 'noStartActivity'">
				<text class="seckill--price-icon">￥</text>
				<text class="seckill--price">{{data.originalPrice}}</text>
				<text class="seckill--price-icon">起</text>
			</view>
			<view class="seckill--price-box" v-else>
				<text class="seckill--price-icon">￥</text>
				<text class="seckill--price">{{data.activityPrice}}</text>
				<text class="seckill--price-org">￥{{data.originalPrice}}</text>
			</view>
			<view>
				<view class="seckill--timer-title">
					{{activityStatus === 'startActivity' ? '距结束仅剩' : '即将开始秒杀' }}
				</view>
				<view v-if="!isDay" class="seckill--date">{{startDate}}开始</view>
				<view v-else class="seckill--timer">
					<view>{{hours}}</view>
					<text>:</text>
					<view>{{minute}}</view>
					<text>:</text>
					<view>{{second}}</view>
				</view>
			</view>
		</view>
		<view class="title">{{title}}</view>
	</view>
</template>
<script>
import {formatDate} from '@/common/utils';
export default {
	props : {
		data : {
			type : Object,
			default : () => ({})
		},
		activityStatus : {
			type : String,
			default : 'noStartActivity'
		},
		title : {
			type : String,
			default : ''
		}
	},
	data(){
		return {
			 secondNum : 0,
			 timerStop : null
		}
	},
	watch:{
		data(){
			this.calcStartSecond();
		}
	},
	mounted(){
		this.calcStartSecond();
	},
	computed:{
		hours(){
			let hours = parseInt(this.secondNum / 3600);
			return hours > 9 ? hours : `0${hours}`;
		},
		minute(){
			let minute = parseInt((this.secondNum - this.hours * 3600) / 60);
			return minute > 9 ? minute : `0${minute}`;
		},
		second(){
			let second = parseInt(this.secondNum % 60);
			return second > 9 ? second : `0${second}`;
		},
		isDay(){
			const currentTime = new Date(this.data.currentTime);
			const  activityTime = new Date(this.data.activityStartTime);
			return currentTime.getDate() === activityTime.getDate();
		},
		startDate(){
			return formatDate(this.data.activityStartTime, 'm月d日hh:ii')
		}
	},
	methods:{
		/**
		 * 计算倒计时开始秒数
		 */
		calcStartSecond(){
			const {isStartActivity, activityStartTime, activityEndTime , currentTime} = this.data;
			let curTime = (new Date(currentTime)).getTime(),
				expireTime = 0;
			if(isStartActivity){
				// 已开始秒杀 显示距结束倒计时
				expireTime = (new Date(activityEndTime)).getTime();
			}else{
				// 即将开抢 显示距开始倒计时
				expireTime = (new Date(activityStartTime)).getTime();
			}
			const second = Math.floor((expireTime - curTime) / 1000);
			this.secondNum = second > 0 ? second : 0;
			this.timer();
		},
		/**
		 * 倒计时
		 */
		timer(){
			if(this.secondNum < 2){
				clearTimeout(this.timerStop);
				this.$emit('change');
				// 倒计时到0 后如果是已开始则触发已结束秒杀事件
				// if(this.data.isStartActivity){
				// 	this.$emit('change');
				// 	// this.$emit('update:data',{...this.data, isActivity : false});
				// }else{
				// 	this.$emit('change');
				// 	// 未开始则变为已开始，并重新开始倒计时
				// 	// this.$emit('update:data',{...this.data, isStartActivity : true});
				// 	// this.calcStartSecond();
				// }
				return false;
			}
			this.secondNum--;
			this.timerStop = setTimeout(this.timer, 1000)

		}
	}
}
</script>
<style lang="scss" scoped>

.seckill{
	height: 110rpx;
	width: 690rpx;
	margin: 20rpx auto 0;
	position: relative;
	display: flex;
	justify-content: space-between;
	padding: 0 20rpx 0 27rpx;
	background-image: url('@/static/goods/seckill.png');
	background-repeat : no-repeat;
	background-size: 100%;
	&__start{
		padding: 0 20rpx 0 108rpx;
		background-image: url('@/static/goods/seckill_start.png');
		.seckill--price-icon,.seckill--price{
			color: $color-grey0;
		}

	}
	&--price-box{
		padding-top: 30rpx;
	}
	&--price-icon{
		color: $color-yellow4;
		font-weight: bold;
		font-size: 32rpx;
	}
	&--price{
		color: $color-yellow4;
		font-weight: bold;
		font-size: 50rpx;
		margin-right: 10rpx;

	}
	&--price-org{
		color: $color-grey0;
		text-decoration: line-through;
		margin-left: 6rpx;
	}
	&--timer-title{
		margin: 20rpx 0 10rpx 0;
		color: $color-grey0;
		text-align: right;
		font-size: $font-size-sm;
	}
	&--date{
		color: $color-grey0;
		text-align: right;
		font-size: $font-size-sm;
		padding-top: 6rpx;
	}
	&--timer{
		display: flex;
		align-items: center;
		view{
			background: #E83710;
			width: 40rpx;
			height: 40rpx;
			line-height: 40rpx;
			color: #fff;
			text-align: center;
			border-radius: 8rpx;
			font-size: $font-size-sm;
		}
		text{
			margin: 0 5rpx;
			color: #fff;
		}
	}
}
.title{
	margin: 0 30rpx ;
	background: $color-grey0;
	border-radius: 16rpx;
	padding: 30rpx;
	font-size: $font-size-lg;
	line-height: 48rpx;
}
</style>