<template>
	<view class="details" v-if="orderInfo.extend_store">
		<view class="status" v-if="orderstate">
			<view class="text">
				<view class="status-text">{{ s_object[orderstate].text }}</view>
				<view class="time" v-if="orderInfo.view_type == 1 || orderInfo.view_type == 9">距离结束：{{ countdown }}</view>
			</view>
			<image :src="s_object[orderstate].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" @click="viewStoreDetails(orderInfo.extend_store.store_id)">
						<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" @click="viewGoodsDetails(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>
		 <!-- '4', '8' -->
		<view class="btn" v-if="['1', '2', '6'].indexOf(orderstate) >= 0">
			<!-- orderstate == '4' ||  -->
			<view class="cancel" v-if="(orderstate == '1') && orderInfo.is_refund == 1" @click="toOtherPage('RefundOrder')">申请退款</view>
			<view class="logistics" v-if="orderstate == '1'" @click="confirmReceive">确认收货</view>
			<view class="comment" v-if="orderstate == '2'" @click="toOtherPage('Comment')">立即评价</view>
			<view class="cancel" v-if="orderstate == '6'" @click="cancelOrder">取消支付</view>
			<!-- <view class="cancel" v-if="orderstate == '8'" @click="cancelOrder">取消订单</view> -->
			<view class="cancel" v-if="orderstate == '1' && orderInfo.shipping_code" @click="toOtherPage('Logistics')">查看物流</view>
			<view class="phone" v-if="orderstate == '1' && orderInfo.takeawayer_phone" @click="makePhone(order.takeawayer_phone)">
				<u-icon name="phone-fill" color="#FF780F" size="28"></u-icon>
				<text>联系骑手</text>
			</view>
			 <!-- || orderstate == '8' -->
			<view class="payment" v-if="orderstate == '6'" @click="payNow">立即支付</view>
			<view class="service" v-if="orderstate == '7'">联系官方客服</view>
			<view class="submit" v-if="orderstate == '7'">提交官方审核</view>
		</view>
	</view>
</template>
<script>
export default {
	data() {
		return {
			orderstate: '',
			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',
				},
				'8': {
					text: '拼团中',
					image: '../static/mine/37.png',
				},
				'9': {
					text: '已取消',
					image: '../static/mine/33.png',
				},
				'10': {
					text: '已拒绝',
					image: '../static/mine/34.png',
				},
			},
			orderInfo: {},
			oid: '',
			timer: '',
			countdown: '00:00:00', // 倒计时
			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 5:
					state = '3';
					break;
				case 6:
					state = '7';
					break;
				case 7:
					state = '5';
					break;
				case 9:
					state = '8';
					break;
				case 10:
					state = '9';
					break;
				case 11:
					state = '10';
					break;
				default:
					break;
			}
			this.orderstate = state;
		},
		viewStoreDetails(id) {
			this.$u.route('pageC/merchant/index', {
				id: id
			});
		},
		viewGoodsDetails(id) {
			this.$u.route({
				url: 'pageB/sdetails/index',
				params: {
					id: id,
				}
			})
		},
		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.countdown = '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.viewState(this.orderInfo.view_type);
					this.setTitle(this.orderInfo.view_type);
					if(res.data.view_type == 1) this.setSpikeTime(res.data.end_time);
					else if(res.data.view_type == 9) this.setSpikeTime(res.data.pintuangroup_endtime);
				}
				uni.stopPullDownRefresh(); // 结束刷新
			})
		},
		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,
				order_id: this.orderInfo.order_id,
			});
		},
		toOtherPage(url) {
			this.$u.route('/pageE/order/' + url, {
				oid: this.orderInfo.order_id,
			});
		},
		makePhone(phone) {
			uni.makePhoneCall({
				phoneNumber: phone
			});
		}
	},
};
</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));
		}
		.phone {
			@include btn-class($width: 206rpx, $color: rgba(255,119,15,1));
			display: flex;
			align-items: center;
			justify-content: center;
			.u-icon {
				margin-right: 10rpx;
			}
		}
	}
}
</style>