<template>
	<view class="detail">
		<u-navbar title="订单详情" :border="true" :placeholder="true" :autoBack="true" bgColor="#fff" />
		<!-- 订单状态 -->
		<view class="status">
			<image src="../../packageGoods/static/ordr/bg.png" mode="scaleToFill"></image>
			<view class="status-info">
				<image src="../../packageGoods/static/ordr/car.png" mode="scaleToFill"></image>
				<view class="status-info-contet">
					<view class="status-info-contet-txt" :class="{'status-info-contet-one': status.value != 2 }">{{ status.desc }}</view>
					<view class="status-info-contet-time" v-if="status.value == 2">还剩 {{ autoConfirmTime || "--" }} 自动确认</view>
				</view>
			</view>
		</view>
		
		<!-- 地址 -->
		<view class="address">
			<view class="address-content">
				<image src="../static/ordr/addr.png" mode="scaleToFill"></image>
				<view class="address-content-txt">
					<view class="name">{{ info.receiver }} <text> {{ info.tel }}</text></view>
					<view class="city ells">{{ info.province }} {{ info.city }} {{ info.area }} {{ info.address }}</view>
				</view>
			</view>
		</view>
		
		<!-- 商品 -->
		<view class="goods">
			<view class="goods-title">商品信息</view>
			<view class="goods-item" v-for="(item, index) in list" :key="index">
				<image :src="item.picUrl" mode="aspectFit"></image>
				<view class="info">
					<view class="info-title ells">{{ item.title }}</view>
					<view class="info-num flex">
						<view class="info-num-sku ells-one">规格：{{ item.properties || '-' }}</view>
						<view class="info-num-goods">共{{ item.goodsNum }}件</view>
					</view>
				</view>
			</view>
			<!-- 运费 -->
			<view class="goods-freight">
				<view class="freight-title">运费</view>
				<view class="freight-price">￥{{ $numberFormat(info.freightAmt) }}</view>
			</view>
			<!-- 总价 -->
			<view class="goods-total flex">
				<view class="goods-total-price">应付：<text>￥{{ $numberFormat(info.payAmt) }}</text></view>
			</view>
		</view>
		
		<!-- 订单信息 -->
		<view class="order">
			<view class="order-title">订单信息</view>
			<view class="order-item flex">
				<view class="order-item-title">订单编号</view>
				<view class="order-item-content flex" @click="copyOrderId" v-if="status.value == 2 || status.value == 4">
					<text>{{ info.orderId }}</text>
					<view class="copy flex">
						<text>复制</text>
					</view>
				</view>
				<view class="order-item-content flex" v-else>{{ info.orderId }}</view>
			</view>
			<view class="order-item flex">
				<view class="order-item-title">下单时间</view>
				<view class="order-item-content">{{ $parseTime(info.createdTime) }}</view>
			</view>
		</view>
		
		<!-- 发货信息 -->
		<!-- <view class="order">
			<view class="order-title">发货信息</view>
			<view class="order-item flex">
				<view class="order-item-title">配送方式</view>
				<view class="order-item-content">无需物流</view>
			</view>
			<view class="order-item flex">
				<view class="order-item-title">发货时间</view>
				<view class="order-item-content">{{ $parseTime(info.createdTime) }}</view>
			</view>
		</view> -->
		
		<!-- 留言 -->
		<view class="order" v-if="info.memo != ''">
			<view class="order-title">留言</view>
			<view class="order-item flex">
				<view class="order-item-title tip">{{ info.memo }}</view>
			</view>
		</view>

		<!-- 操作按钮 -->
		<view class="footer-fixed" v-if="status.value == 2 || status.value == 3 || status.value == 0">
			<view class="btn flex">
				<view class="btn-item flex logistics" v-if="status.value == 2 || status.value == 3"  @click="getLogistics"><text>查看物流</text></view>
				<view class="btn-item flex pay" v-if="status.value == 2"  @click="confirmOne"><text>确认收货</text></view>
				<view class="btn-item flex cancel" v-if="status.value == 0" @click="cancelOrder"><text>取消订单</text></view>
				<view class="btn-item flex pay"v-if="status.value == 0"  @click="payOrder"><text>去支付</text></view>
			</view>
		</view>
		
		<auth :auth-show="authShow" @close="authShow = false" />
	</view>
</template>

<script>
	import env from '../../config/env.js'
	import $http from '@/utils/request.js'
	import Auth from '../../components/auth/auth.vue'
	export default {
		components: {
			Auth
		},
		data() {
			return {
				orderId: '',
				status: {},
				info: {},
				addr: {},
				list: [],
				deliverList: [],
				autoConfirmTime: '',
				authShow: false,
			};
		},
		onLoad(opthios) {
			this.orderId = opthios.id
			this.getDetail()
		},
		methods: {
			getDetail() {
				uni.showLoading({
					title: '加载中'
				});
				$http.post('/api/v1/mp/user/deliver/order/detail', {
					orderId: this.orderId
				}).then(res => {
					uni.hideLoading();
					this.info = res.data
					this.status = JSON.parse(res.data.status)
					this.autoConfirmTime = res.data.autoConfirmTime
					res.data.items.forEach(item => {
						let picUrlArr = item.picUrl.split(',')
						item.picUrl = env.filePublic + picUrlArr[0] + '?imageView2/2/w/170'
					})
					res.data.deliverList && res.data.deliverList.forEach((item) => {
						item.items.forEach((ele) => {
							let picUrlArr = ele.picUrl.split(",");
							ele.picUrl = env.filePublic + picUrlArr[0] + '?imageView2/2/w/170';
						});
					});
					this.deliverList = res.data.deliverList;
					this.list = res.data.items
				}).catch(() => {
					uni.hideLoading();
				})
			},

			cancelOrder() {
				let orderId = this.orderId
				uni.showModal({
					title: '提示',
					content: '您确认要取消订单吗？',
					success(res) {
						if (res.confirm) {
							$http.post('/api/v1/mp/user/deliver/order/cancel', {
								orderId
							}).then(res => {
								if (res.code == 0) {
									uni.$u.toast('订单取消成功');
									setTimeout(() => {
										uni.navigateBack({
											delta: 1
										})
									}, 500)
								}
							})
						}
					}
				})
			},

			payOrder() {
				let _this = this
				let payIng = false
				if (payIng) return
				uni.showLoading({
					title: '加载中'
				});
				$http.post('/api/v1/mp/user/deliver/order/pay', {
					orderId: _this.info.orderId,
					payType: 2
				}).then(ele => {
					payIng = true
					uni.hideLoading();
					if (ele.code == 0) {
						uni.requestPayment({
							timeStamp: ele.data.timeStamp,
							nonceStr: ele.data.nonceStr,
							package: ele.data.package,
							signType: ele.data.signType,
							paySign: ele.data.paySign,
							success() {
								uni.showToast({
									title: '支付成功',
									icon: 'success',
									duration: 2000
								})
								setTimeout(() => {
									uni.navigateBack({
										delta: 1
									})
								}, 500)
							},
							fail() {
								payIng = false
							}
						})
					} else if (ele.code == 1005) {
						_this.authShow = true
					} else {
						payIng = false
						uni.$u.toast('支付失败!');
					}
				}).catch(() => {
					payIng = false
					uni.$u.toast('支付失败!');
					uni.hideLoading();
				})
			},

			copyOrderId() {
				uni.setClipboardData({
					data: this.info.orderId,
				});
			},

			copyDeliveryFlowId(id) {
				uni.setClipboardData({
					data: id,
				});
			},

			// 查看物流
			getLogistics() {
				uni.navigateTo({
					url: `/packageGoods/order/logistics?id=${ this.orderId }`
					// url:`/pages/order/logistics?id=96666733329055749370`
				});
			},

			// 确认收货
			confirmOne() {
				let orderId = this.orderId
				uni.showModal({
					title: '提示',
					content: '确定已经收到货了吗？',
					success(res) {
						if (res.confirm) {
							$http.post('/api/v1/mp/user/deliver/order/confirm', {
								orderId
							}).then(res => {
								if (res.code == 0) {
									uni.$u.toast('确认收货成功');
									setTimeout(() => {
										uni.navigateBack({
											delta: 1
										})
									}, 500)
								}
							})
						}
					}
				})
			}
		}
	}
</script>
<style lang="scss" scoped>
	.detail {
		padding-bottom: 200rpx;
	}
	
	// 订单状态
	.status {
		position: relative;
		height: 216rpx;
		
		image {
			width: 100%;
			height: 100%;
		}
		
		&-info {
			position: absolute;
			top: 0;
			display: flex;
			width: 100%;
			height: 100%;
			padding: 56rpx 0 0 34rpx;
			
			image {
				width: 74rpx;
				height: 62rpx;
				margin: 18rpx 22rpx 0 0;
			}
			
			&-contet {
				&-txt {
					font-size: 38rpx;
					font-weight: 800;
					color: #FFFFFF;
					line-height: 60rpx;
				}
				
				&-one {
					margin-top: 18rpx;
					height: 62rpx;
					line-height: 62rpx;
				}
				
				&-time {
					font-size: 30rpx;
					font-weight: 500;
					color: #FFFFFF;
					line-height: 44rpx;
				}
			}
		}
	}
	
	// 地址
	.address {
		position: relative;
		margin-top: -34rpx;
		background-color: #fff;
		border-radius: 34rpx 34rpx 0 0;
		z-index: 10;
		padding-top: 34rpx;
		margin-bottom: 22rpx;
		
		&-content {
			display: flex;
			align-items: center;
			// border-top: 1px dashed rgb(254, 143, 62);
			padding: 18rpx 0 36rpx 34rpx;
			
			image {
				width: 30rpx;
				height: 38rpx;
				margin-right: 34rpx;
			}
			
			&-txt {
				flex: 1;
				
				.name {
					font-size: 30rpx;
					line-height: 30rpx;
					margin-bottom: 20rpx;
					
					text {
						padding: 0 8rpx;
						font-size: 26rpx;
						color: #414141;
					}
				}
				
				.city {
					color: #999999;
					line-height: 28rpx;
				}
			}
		}
	}
	
	// 商品
	.goods {
		padding: 34rpx;
		background-color: #fff;
		margin-bottom: 22rpx;
		
		&-title {
			font-size: 30rpx;
			line-height: 30rpx;
			margin-bottom: 22rpx;
		}
		
		// 商品列表
		&-item{
			display: flex;
			padding: 34rpx 32rpx 34rpx 22rpx;
			box-shadow: 0px 0px 8px 0px rgba(26, 35, 113, 0.08);
			border-radius: 4rpx;
			margin-bottom: 20rpx;
			
			image {
				width: 176rpx;
				height: 176rpx;
				border-radius: 12rpx;
				margin-right: 22rpx;
			}
			
			.info {
				display: flex;
				flex-direction: column;
				justify-content: space-between;
				flex: 1;
				padding: 8rpx 0;
				
				&-title {
					font-weight: bold;
					line-height: 40rpx;
				}
				
				&-num {
					justify-content: space-between;
					
					&-sku {
						flex: 1;
						color: #666666;
						font-size: 26rpx;
					}
					
					&-goods {
						color: #666666;
						font-size: 26rpx;
					}
				}
			}
		}
		
		&-item:last-child {
			margin-bottom: 0;
		}
		
		// 运费
		&-freight {
			display: flex;
			align-items: center;
			justify-content: space-between;
			padding-bottom: 18rpx;
			border-bottom: 1px solid #eee;
			margin-bottom: 20rpx;
			
			&-title {
				line-height: 28rpx;
				color: #999;
			}
			
			&-price {
				line-height: 28rpx;
			}
		}
		
		// 总价
		&-total {
			justify-content: flex-end;
			
			&-price {
				font-size: 30rpx;
				color: #F24E4E;
			}
		}
	}
	
	// 订单信息
	.order {
		padding: 34rpx;
		background-color: #fff;
		margin-bottom: 22rpx;
		
		&-title {
			font-size: 30rpx;
			line-height: 30rpx;
			margin-bottom: 22rpx;
		}
		
		&-item {
			justify-content: space-between;
			margin-bottom: 28rpx;
			
			&-title {
				color: #999999;
				line-height: 28rpx;
			}
			
			.tip {
				line-height: 40rpx;
			}
			
			&-content {
				.copy {
					height: 100%;
					margin-left: 8rpx;
					text-align: center;
					border-radius: 12rpx;
					font-size: 24rpx;
					line-height: 24rpx;
					padding: 0 6rpx;
					background-color: rgba(153, 153, 153, .1);
					
					text {
						display: inline-block;
						transform: scale(0.8);
					}
				}
			}
		}
		
		&-item:last-child {
			margin-bottom: 0;
		}
	}
	
	// 操作按钮
	.footer-fixed {
		position: fixed;
		bottom: var(--window-bottom);
		left: 0;
		right: 0;
		z-index: 11;
		box-shadow: 0 -4rpx 40rpx 0 rgba(151, 151, 151, 0.24);
		background: #fff;
		// 设置ios刘海屏底部横线安全区域
		padding-bottom: constant(safe-area-inset-bottom);
		padding-bottom: env(safe-area-inset-bottom);
	
		.btn {
			justify-content: flex-end;
			padding: 20rpx 34rpx;
			
			&-item {
				width: 200rpx;
				height: 66rpx;
				font-size: 30rpx;
				border-radius: 33rpx;
				margin-left: 44rpx;
					
				text {
					line-height: 30rpx;
				}
			}
			
			.logistics {
				border: 1px solid #F9822C;
				color: #F9822C;
			}
			
			.detail {
				background-color: rgb(249, 130, 44);
				color: #FFFFFF;
			}
			
			.cancel {
				border: 1px solid #c4c6c9;
				color: #c4c6c9;
			}
			
			.pay {
				background-color: #5ac725;
				color: #fff;
			}
			
			&-item:first-child {
				margin-left: 0;
			}
		}
	}
</style>
