import order from '../../../utils/controls/order';
import notice from '@/utils/notice/index';
export default {

	data:function () {
		return {
			payStatus:{
				msg:''
			}
		}
	},

	methods:{

		installPayEnd(item){
			if (item.have_paid === 0 && item.time && item.duetime) {
				let msg = this.setCountDown();
				if (msg) {
					this.payStatus.msg = msg;
				} else {
					this.setStatusCancel();
				}
			}
		},

		/* 设置倒计时 */
		setCountDown:function () {

			if (this.endTime === undefined) {
				// this.data.order.createdtime +
				this.endTime = this.item.orderInfo.duetime;
				this.startTime = parseInt(new Date().getTime()/1000);
			}
			let diff = this.getTimeDiff();

			if (diff > 0) {
				this.runCountDown();
				return this.getCountDownText(diff);
			} else {
				return false;
			}

		},

		getTimeDiff(){
			let nowTime = parseInt(new Date().getTime()/1000);

			let diff = nowTime - this.startTime;

			diff = this.endTime - diff - this.item.orderInfo.time;


			return diff;

		},

		/* 开启倒计时 */
		runCountDown:function(){

			// 一分钟触发一次
			this.time = setTimeout(()=>{

				let diff = this.getTimeDiff();
				if (diff <= 0) {
					clearTimeout(this.time);
					return this.setStatusCancel();
				} else {
					this.payStatus.msg = this.getCountDownText(diff);
					return this.runCountDown();
				}

			},1000);

		},

		// 设置状态已取消
		setStatusCancel:function(){

			this.changeStatus(order.createdUpdateObj('cancel',this.item.orderInfo,'','已取消'));

			// 执行调用取消接口
			return this.$request({
				url:'order/apiCancelOrder',
				data:{
					order_id: this.item.orderInfo.id
				},
				token:true,
				login:false
			}).then(()=>{
				// 设置视图状态为已取消
				notice.trigger('order',order.createdUpdateObj('cancel',this.item.orderInfo,'','已取消'));
			});
		},

		getCountDownText:function (diff) {

			let h = parseInt(diff / 60 / 60 % 60);
			h = h <10?'0'+h:h;

			let m = parseInt(diff / 60 % 60);
			m = m <10?'0'+m:m;

			let s = parseInt(diff % 60);
			s = s <10?'0'+s:s;

			return '剩'+h+'小时'+m+'分'+s+'秒自动关闭';
		},
	},

	beforeDestroy:function () {
		clearTimeout(this.time);
	}



}
