<template>
	<view class="details" v-if="orderInfo.extend_store">
		<view class="status" v-if="state">
			<view class="text">
				<view class="status-text">{{ s_object[state].text }}</view>
				<view class="time" v-if="orderInfo.view_type == 1">距离结束：{{ countdown }}</view>
			</view>
			<image :src="s_object[state].image"></image>
		</view>
		<view class="info">
			<view class="info-address">
				<image src="../static/mine/28.png"></image>
				<view class="address-right">
					<view class="user-info">
						<view>{{ orderInfo.extend_order_common.reciver_name }}</view>
						<view>{{ orderInfo.extend_order_common.reciver_info.mob_phone }}</view>
					</view>
					<view class="address-text u-line-2">{{ orderInfo.extend_order_common.reciver_info.address }}</view>
				</view>
			</view>
			<view class="info-goods">
				<view class="goods-store">
					<view class="stoer-title">
						<image :src="orderInfo.extend_store.store_avatar" class="store-image"></image>
						<view class="store-name">{{ orderInfo.extend_store.store_name }}</view>
						<image src="../static/mine/21.png" class="right-icon"></image>
					</view>
					<view v-for="goods in orderInfo.extend_order_goods" :key="goods.goods_id">
						<view class="store-goods">
							<image :src="goods.goods_image"></image>
							<view class="goods-info">
								<view class="goods-name u-line-2">{{ goods.goods_name }}</view>
								<view class="goods-order">
									<view class="price">￥{{ goods.goods_pay_price }}</view>
									<view class="num">x{{ goods.goods_num }}</view>
								</view>
							</view>
						</view>
					</view>
				</view>
				<view class="goods-others">
					<view>
						<view class="title">运费(快递/骑手)</view>
						<view class="price">￥{{ orderInfo.shipping_fee }}</view>
					</view>
					<view>
						<view class="title">店铺优惠</view>
						<view class="price">￥{{ orderInfo.extend_order_common.voucher_price || '0.00' }}</view>
					</view>
					<view>
						<view class="title">订单总价</view>
						<view class="price">￥{{ orderInfo.goods_amount }}</view>
					</view>
					<view>
						<view class="title">实付费(含运费)</view>
						<view class="price">￥{{ orderInfo.order_amount }}</view>
					</view>
					<view>
						<view class="title">支付方式</view>
						<view class="price">{{ orderInfo.payment_name }}</view>
					</view>
				</view>
			</view>
			<view class="info-order">
				<view>订单编号：{{ orderInfo.order_sn }}</view>
				<view>支付单号：{{ orderInfo.pay_sn }}</view>
				<view>创建时间：{{ orderInfo.add_time | date}}</view>
			</view>
		</view>
		<view class="btn" v-if="['1', '2', '4', '6'].indexOf(state) >= 0">
			<view class="cancel" v-if="state == '4' || state == '1'" @click="applyRefund">申请退款</view>
			<view class="cancel" v-if="state == '1'" @click="toOtherPage('Logistics')">查看物流</view>
			<view class="logistics" v-if="state == '1'" @click="confirmReceive">确认收货</view>
			<view class="comment" v-if="state == '2'" @click="toOtherPage('Comment')">立即评价</view>
			<view class="cancel" v-if="state == '6'" @click="cancelOrder">取消支付</view>
			<view class="payment" v-if="state == '6'" @click="payNow">立即支付</view>
			<view class="service" v-if="state == '7'">联系官方客服</view>
			<view class="submit" v-if="state == '7'">提交官方审核</view>
		</view>
	</view>
</template>
<script>
export default {
	data() {
		return {
			state: '', // 1: 待收货 2: 待评价 3: 交易成功 4: 已取消 5: 已退款 6: 待支付 7: 待退款 8: 已退款
			s_object: {
				'1': {
					text: '待收货',
					image: '../static/mine/32.png',
				},
				'2': {
					text: '待评价',
					image: '../static/mine/35.png',
				},
				'3': {
					text: '交易成功',
					image: '../static/mine/31.png',
				},
				'4': {
					text: '待发货',
					image: '../static/mine/36.png',
				},
				'5': {
					text: '已退款',
					image: '../static/mine/34.png',
				},
				'6': {
					text: '待支付',
					image: '../static/mine/31.png',
				},
				'7': {
					text: '待退款',
					image: '../static/mine/34.png',
				},
			},
			orderInfo: {},
			oid: '',
			timer: '',
			countdown: '', // 倒计时
			isSpike: false, // 倒计时是否结束
		}
	},
	onLoad(option) {
		this.setTitle();
		this.oid = option.oid;
		this.getOrderInfo(option.oid);
	},
	// 下拉刷新
	onPullDownRefresh() {
        this.getOrderInfo(this.oid);
	},
	beforeDestroy() {
		clearInterval(this.timer);
	},
	methods: {
		viewState(value) {
			let state;
			switch (value) {
				case 1:
					state = '6';
					break;
				case 2:
					state = '4';
					break;
				case 3:
					state = '1';
					break;
				case 4:
					state = '2';
					break;
				case 6:
					state = '7';
					break;
				case 7:
					state = '5';
					break;
				default:
					break;
			}
			this.state = state;
		},
		setSpikeTime(time) {
			// console.log(time);
			this.timer = setInterval(() => {
				let spikeTime = time * 1000 - new Date().getTime();
				// console.log(spikeTime);
				if(spikeTime <= 0) {
					this.isSpike = true;
					this.spikeTime = '00:00:00'
					clearInterval(this.timer);
					return false;
				};
				// 计算天数
				const days = Math.floor(spikeTime/(24*3600*1000));
				//计算出小时数
				const leave1 = spikeTime % (24*3600*1000) // 计算天数后剩余的毫秒数
				let hours = Math.floor(leave1/(3600*1000));
				//计算相差分钟数
				const leave2 = leave1 % (3600*1000) // 计算小时数后剩余的毫秒数
				let minutes = Math.floor(leave2 / (60*1000))
				//计算相差秒数
				const leave3 = leave2 % (60*1000) // 计算分钟数后剩余的毫秒数
				let seconds = Math.round(leave3 / 1000)
				// 把天数算在小时里
				hours = days * 24 + hours;
				if(hours < 10) hours = '0' + hours;
				if(minutes < 10) minutes = '0' + minutes;
				if(seconds < 10) seconds = '0' + seconds;
				this.countdown = hours + ':' + minutes + ':' + seconds;
			}, 1000)
		},
		getOrderInfo(id) {
			this.$u.api.getOrderInfo({
				order_id: id,
			}).then(res => {
				if(res.errCode == 0) {
					this.orderInfo = res.data;
					this.setSpikeTime(res.data.end_time);
					this.viewState(this.orderInfo.view_type);
					this.setTitle(this.orderInfo.view_type);
				}
				uni.stopPullDownRefresh(); // 结束刷新
			})
		},
		applyRefund() {

		},
		cancelOrder() {
			this.$u.api.cancelOrder({
				order_id: this.orderInfo.order_id,
			}).then(res => {
				if(res.errCode == 0) {
					uni.navigateBack();
				}
			})
		},
		confirmReceive() {
			this.$u.api.confirmReceive({
				order_id: this.order.order_id,
			}).then(res => {
				if(res.errCode == 0) {
					this.getOrderInfo(this.oid);
				}
				this.$u.toast(res.message);
			})
		},
		setTitle(value){
			let title = '';
			switch (value) {
				case 1:
					title = "支付成功"
					break;
				default:
					title = "订单详情"
					break;
			}
			// uni.setNavigationBarTitle({
			// 	title: title
			// });
		},
		payNow() {
			this.$u.route('/pageC/cart/cashier', {
				pay_sn: this.orderInfo.pay_sn,
				price: this.orderInfo.order_amount,
			});
		},
		toOtherPage(url) {
			this.$u.route('/pageE/order/' + url, {
				oid: this.orderInfo.order_id,
			});
		}
	},
};
</script>
<style lang="scss" scoped>
.details {
	min-height: calc(100vh - var(--window-top));
	background-color: #ECECEC;
	padding-bottom: 98rpx;
	.status {
		display: flex;
		height: 180rpx;
		// height: calc(180rpx + var(--window-top));
		width: 100%;
		background: #ff780f;
		.text {
			margin: 74rpx auto 0 73rpx;
			color: rgba(255,255,255,1);
			.status-text {
				font-size: 36rpx;
				font-weight: 500;
				margin-bottom: 17rpx;
			}
			.time {
				// white-space: nowrap;
				font-size: 26rpx;
				color: rgba(255,255,255,1);
			}
		}
		> image {
			margin: 36rpx 70rpx 0 0;
			width: 126rpx;
			height: 109rpx;
			flex-shrink: 0;
		}
	}
	.info {
		margin-bottom: 10rpx;
		.info-address {
			padding: 30rpx;
			display: flex;
			align-items: center;
			margin-bottom: 10rpx;
			background:rgba(255,255,255,1);
			> image {
				width: 28rpx;
				height: 34rpx;
				margin-right: 31rpx;
				flex-shrink: 0;
			}
			.address-right {
				.user-info {
					display: flex;
					align-items: center;
					font-size: 28rpx;
					color: rgba(51,51,51,1);
					margin-bottom: 20rpx;
					> view:first-child {
						margin-right: 13rpx;
					}
				}
				.address-text {
					font-size: 24rpx;
					color: rgba(102,102,102,1);
					line-height: 42rpx;
				}
			}
		}
		.info-goods {
			margin-bottom: 10rpx;
			.goods-store {
				padding: 30rpx;
				margin-bottom: 2rpx;
				background:rgba(255,255,255,1);
				.stoer-title {
					display: flex;
					align-items: center;
					margin-bottom: 20rpx;
					.store-image {
						width: 60rpx;
						height: 60rpx;
						border-radius: 50%;
						margin-right: 16rpx;
					}
					.store-name {
						font-size: 28rpx;
						color: rgba(51,51,51,1);
						margin-right: 14rpx;
					}
					.right-icon {
						width: 11rpx;
						height: 22rpx;
					}
				}
				.store-goods {
					display: flex;
					margin-bottom: 20rpx;
					> image {
						width: 180rpx;
						height: 160rpx;
						border-radius: 10rpx;
						margin-right: 30rpx;
						flex-shrink: 0;
					}
					.goods-info {
						flex: 1;
						display: flex;
						flex-direction: column;
						justify-content: space-between;
						.goods-name {
							font-size: 30rpx;
							color: rgba(51,51,51,1);
							line-height: 44rpx;
						}
						.goods-order {
							display: flex;
							align-items: center;
							justify-content: space-between;
							.price {
								font-size: 30rpx;
								font-weight: 500;
								color: rgba(51,51,51,1);
							}
							.num {
								font-size: 28rpx;
								color: rgba(102,102,102,1);
							}
						}
					}
				}
			}
			.goods-others {
				background:rgba(255,255,255,1);
				padding: 30rpx;
				> view {
					display: flex;
					align-items: center;
					justify-content: space-between;
					&:not(:last-child) {
						margin-bottom: 30rpx;
					}
					.title {
						font-size: 28rpx;
						color: rgba(102,102,102,1);
					}
					.price {
						font-size: 30rpx;
						color: rgba(51,51,51,1);
					}
				}
			}
		}
		.info-order {
			background:rgba(255,255,255,1);
			padding: 30rpx;
			font-size: 28rpx;
			color: rgba(102,102,102,1);
			> view:not(:last-child) {
				margin-bottom: 30rpx;
			}
		}
	}
	.btn {
		position: fixed;
		bottom: 0;
		left: 0;
		width: 100%;
		height: 98rpx;
		background: rgba(255,255,255,1);
		display: flex;
		justify-content: flex-end;
		padding: 19rpx 30rpx;
		@mixin btn-class($width, $color) {
			width: $width;
			height: 60rpx;
			border: 2rpx solid $color;
			color: $color;
			border-radius: 30rpx;
			padding: 15rpx 23rpx;
			font-size: 28rpx;
			&:not(:last-child) {
				margin-right: 30rpx;
			}
		}
		.logistics, .comment, .payment {
			@include btn-class($width: 160rpx, $color: rgba(255,119,15,1));
		}
		.cancel {
			@include btn-class($width: 160rpx, $color: rgba(155,153,153,1));
		}
		.service {
			@include btn-class($width: 216rpx, $color: rgba(155,153,153,1));
		}
		.submit {
			@include btn-class($width: 216rpx, $color: rgba(255,119,15,1));
		}
	}
}
</style>