<!--
 * @Author: ch
 * @Date: 2022-04-12 17:33:42
 * @LastEditors: ch
 * @LastEditTime: 2022-04-13 13:47:07
 * @Description: file content
-->
<template>
	<view class="seckill">
		<view class="seckill--price-box">
			<text class="seckill--price-icon">￥</text>
			<text class="seckill--price">{{data.activityPrice}}</text>
			<text class="seckill--price-org">￥{{data.originalPrice}}</text>
		</view>
		<view class="seckill--timer">
			<view>{{hours}}</view>
			<text>:</text>
			<view>{{minute}}</view>
			<text>:</text>
			<view>{{second}}</view>
		</view>
	</view>
</template>
<script>
export default {
	props : {
		data : {
			type : Object,
			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}`;
		}
	},
	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: 100rpx;
	width: 690rpx;
	margin: 20rpx auto;
	background:linear-gradient(180deg, #FFF3EF 0%, #FFFFFF 100%);
	border-radius: 16rpx;
	position: relative;
	display: flex;
	justify-content: space-between;
	padding: 0 20rpx 0 234rpx;
	&::after{
		position: absolute;
		left: 0;
		top: 0;
		display: block;
		content: '';
		width: 266rpx;
		height: 100rpx;
		background: url('@/static/goods/seckill.png') no-repeat -8rpx -16rpx;
		background-size: 290rpx;
	}
	&--price-box{
		padding-top: 30rpx;
	}
	&--price-icon{
		color: $color-yellow4;
	}
	&--price{
		color: $color-yellow4;
		font-size: 52rpx;
	}
	&--price-org{
		color: $color-grey4;
		text-decoration: line-through;
	}

	&--timer{
		display: flex;
		align-items: center;
		view{
			background: #000;
			width: 48rpx;
			height: 48rpx;
			line-height: 48rpx;
			color: #fff;
			text-align: center;
			border-radius: 8rpx;
			font-size: 24rpx;
		}
		text{
			margin: 0 5rpx;
		}
	}
}
</style>