<template>
	<view>
		<u-navbar title="订单详情" :border="true" :placeholder="true" :autoBack="true" bgColor="#fff" />
		<view class="detail">
			<view class="detail-state">
				<view class="detail-state-title">
					<text>订单状态：</text>
					<text>{{ status.desc }}</text>
				</view>
				<view class="detail-state-time" v-if="status.value == 2">
					还剩 {{ autoConfirmTime || "--" }} 自动确认
				</view>
			</view>
			<view class="detail-info">
				<view class="detail-info-title">商品信息</view>
				<view class="detail-info-content">
					<view class="detail-info-content-goods">
						<view class="flex detail-info-content-goods__detail" v-for="(item, index) in list" :key="index">
							<view class="detail-info-content-goods__detail__left">
								<view class="img">
									<image class="img" :src="item.picUrl" mode="aspectFill">
									</image>
								</view>
							</view>
							<view class="detail-info-content-goods__detail__right">
								<view class="ells title">{{ item.title }}</view>
								<view class="sku" v-if="item.properties">规格：{{ item.properties }}</view>
								<view class="num">数量：{{ item.goodsNum }}</view>
							</view>
						</view>
					</view>

					<view class="detail-info-content-money">
						<view class="detail-info-content-money__item">
							<text>运费</text>
							<text>￥{{ $numberFormat(info.freightAmt) }}</text>
						</view>
					</view>

					<view class="detail-info-content-toatl">
						<text v-if="status.value == 0">应付：</text>
						<text v-else>实付：</text>
						<text>￥{{ $numberFormat(info.payAmt) }}</text>
					</view>
				</view>
			</view>

			<view class="detail-info">
				<view class="detail-info-title">订单信息</view>
				<view class="detail-info-content">
					<view class="detail-info-content-desc">
						<view>收货信息：</view>
						<view>
							{{ info.receiver }},{{ info.tel }},{{ info.province }}{{ info.city }}{{ info.area }}{{ info.address }}
						</view>
					</view>
					<view class="detail-info-content-desc">
						<view>订单编号：</view>
						<view class="copy" @click="copyOrderId" v-if="status.value == 2 || status.value == 4">复制</view>
						<view>{{ info.orderId }}</view>
					</view>
					<view class="detail-info-content-desc">
						<view>下单时间：</view>
						<view>{{ $parseTime(info.createdTime) }}</view>
					</view>
				</view>
			</view>
			<view class="freight" v-if="info.memo != ''">
				<view style="width: 150rpx;display: inline-block;">留言:</view>
				<view style="display: inline-block;width: 524rpx;vertical-align: top;">{{info.memo}}</view>
			</view>

			<!-- <view class="detail-info" v-if="(status.value == 2 || status.value == 4) && deliverList.length > 1">
				<view class="detail-info-title">发货信息</view>
				<view class="detail-info-content border" v-for="(item, index) in deliverList" :key="index">
					<view class="detail-info-content-desc">
						<view>配送方式：</view>
						<view>{{ item.companyName ? "快递发货" : "无需物流" }}</view>
					</view>
					<view class="detail-info-content-desc" v-if="item.companyName">
						<view>快递公司：</view>
						<view>{{ item.companyName || '-' }}</view>
					</view>
					<view class="detail-info-content-desc" v-if="item.deliveryFlowId">
						<view>物流单号：</view>
						<view class="copy" @click="copyDeliveryFlowId(item.deliveryFlowId)">复制</view>
						<view>{{ item.deliveryFlowId || '-' }}</view>
					</view>
					<view class="detail-info-content-desc">
						<view>发货时间：</view>
						<view>{{ $parseTime(item.deliveryTime) }}</view>
					</view>
					<view class="detail-info-content-deliver">
						<view class="flex detail-info-content-deliver__detail" v-for="(items, indexs) in item.items"
							:key="indexs">
							<view class="detail-info-content-deliver__detail__left">
								<view class="img">
									<image class="img" :src="items.picUrl" mode="aspectFill">
									</image>
								</view>
							</view>
							<view class="detail-info-content-deliver__detail__right">
								<view class="title">{{ items.title }}</view>
								<view class="flex num">
									<view class="sku" v-if="!items.properties"></view>
									<view class="sku" v-if="items.properties">规格：{{ items.properties }}</view>
									<view class="">数量：{{ items.goodsNum }}</view>
								</view>
							</view>
						</view>
					</view>
				</view>
			</view>
			 -->
			<!-- <view class="detail-info" v-if="(status.value == 2 || status.value == 4) && deliverList.length == 1">
				<view class="detail-info-title">发货信息</view>
				<view class="detail-info-content" v-for="(item, index) in deliverList" :key="index">
					<view class="detail-info-content-desc">
						<view>配送方式：</view>
						<view>{{ item.companyName ? "快递发货" : "无需物流" }}</view>
					</view>
					<view class="detail-info-content-desc" v-if="item.companyName">
						<view>快递公司：</view>
						<view>{{ item.companyName || '-' }}</view>
					</view>
					<view class="detail-info-content-desc" v-if="item.deliveryFlowId">
						<view>物流单号：</view>
						<view class="copy" @click="copyDeliveryFlowId(item.deliveryFlowId)">复制</view>
						<view>{{ item.deliveryFlowId || '-' }}</view>
					</view>
					<view class="detail-info-content-desc">
						<view>发货时间：</view>
						<view>{{ $parseTime(item.deliveryTime) }}</view>
					</view>
				</view>
			</view> -->
		</view>

		<view class="footer-fixed" v-if="status.value == 2 || status.value == 3">
			<view class="flex btn">
				<view class="btn-right">
					<text class="logistics" @click="getLogistics">查看物流</text>
				</view>
				<view class="btn-right">
					<text class="confirm" @click="confirmOne" v-if="status.value == 2">确认收货</text>
				</view>
			</view>
		</view>

		<view class="footer-fixed" v-if="status.value == 0">
			<view class="flex btn">
				<view class="btn-right">
					<text class="logisticsCancel" @click="cancelOrder">取消订单</text>
				</view>
				<view class="btn-right">
					<text class="confirmPay" @click="payOrder">去支付</text>
				</view>
			</view>
		</view>

	</view>
</template>

<script>
	import env from '../../config/env.js'
	import $http from '@/utils/request.js'
	export default {
		data() {
			return {
				orderId: '',
				status: {},
				info: {},
				addr: {},
				list: [],
				deliverList: [],
				autoConfirmTime: '',
			};
		},
		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 {
						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>
	.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 40rpx;

			&-right {
				display: flex;
				align-items: center;
				justify-content: right;

				.value {
					color: $uni-text-color;
				}

				@mixin btn {
					width: 160rpx;
					height: 60rpx;
					line-height: 60rpx;
					border-radius: 8rpx;
					text-align: center;
					margin-left: 20rpx;
				}

				.logistics {
					@include btn border: 1px solid #bbbbbb;
					color: #414141;
				}

				.logisticsCancel {
					@include btn border: 2rpx solid #666;
				}

				.confirm {
					@include btn border: 2rpx solid rgba(236, 112, 9, 100);
					color: rgba(236, 112, 9, 100);
				}

				.confirmPay {
					@include btn background-color: $uni-bg-color;
					border: 2rpx solid $uni-bg-color;
					color: #FFFFFF;
				}
			}
		}
	}
</style>

<style lang="scss" scoped>
	.freight {
		margin-bottom: 20rpx;
		background-color: #fff;
		// justify-content: space-between;
		padding: 20rpx 28rpx;
	}

	.detail {
		margin: 10rpx;
		padding-bottom: 160rpx;

		&-state {
			padding: 28rpx;
			margin-bottom: 20rpx;
			background-color: #FFFFFF;

			&-title {
				line-height: 40rpx;
				font-weight: bold;
				display: inline-block;
			}

			&-time {
				line-height: 40rpx;
				color: #b1b1b1;
				float: right;
			}
		}

		&-info {
			margin-bottom: 20rpx;
			padding: 20rpx 28rpx;
			background-color: #FFFFFF;

			&-title {
				line-height: 40rpx;
				font-weight: bold;
			}

			&-content {

				&-goods {

					&__detail {
						padding: 20rpx 0;
						justify-content: space-between;
						border-bottom: 1px solid rgba(236, 236, 236, 100);

						&__left {
							display: flex;
							height: 170rpx;

							.img {

								image {
									width: 170rpx;
									height: 170rpx;
									margin-right: 20rpx;
								}
							}
						}

						&__right {
							flex: 1;
							display: flex;
							height: 170rpx;
							flex-direction: column;
							justify-content: space-between;
							align-items: flex-start;
							font-size: 30rpx;

							.title {
								font-weight: bold;
							}

							.sku {
								color: #8C8C8C;
							}

						}

					}

					&__detail:last-child {
						border: none;
					}
				}

				&-money {
					margin-top: 40rpx;

					&__item {
						display: flex;
						align-items: center;
						justify-content: space-between;
						line-height: 40rpx;
						margin-bottom: 24rpx;
					}
				}

				&-toatl {
					padding-top: 24rpx;
					display: flex;
					align-items: center;
					justify-content: flex-end;
					font-weight: bold;
				}

				&-desc {
					position: relative;
					display: flex;
					line-height: 40rpx;
					margin-top: 20rpx;
					margin-bottom: 24rpx;

					view:first-child {
						width: 150rpx;
					}

					view:last-child {
						width: calc(100% - 150rpx)
					}

					.copy {
						position: absolute;
						right: 0;
						top: -6rpx;
						width: 136rpx;
						height: 52rpx;
						line-height: 52rpx;
						text-align: center;
						color: rgba(149, 149, 149, 100);
						border: 1px solid rgba(187, 187, 187, 100);
						border-radius: 10rpx;
					}
				}

				&-desc:last-child {
					margin-bottom: 0;
				}

				&-deliver {
					&__detail {
						padding: 20rpx 0;
						justify-content: space-between;
						border-bottom: 1px solid rgba(236, 236, 236, 100);

						&__left {
							display: flex;
							height: 170rpx;

							.img {

								image {
									width: 170rpx;
									height: 170rpx;
									margin-right: 20rpx;
								}
							}
						}

						&__right {
							flex: 1;
							display: flex;
							height: 170rpx;
							flex-direction: column;
							justify-content: space-between;
							align-items: flex-end;

							.num {
								width: 100%;
								justify-content: space-between;

								.sku {
									color: #8C8C8C;
								}
							}
						}

					}

					&__detail:last-child {
						border: none;
					}
				}
			}

			.border {
				border-bottom: 1px solid rgba(236, 236, 236, 100);
			}

			.border:last-child {
				border: none;
			}
		}


	}

	.detail-btn {
		display: flex;
		align-items: center;
		justify-content: flex-end;
		position: fixed;
		bottom: var(--window-bottom);
		left: 0;
		right: 0;
		// min-height: 120rpx;
		z-index: 11;
		box-shadow: 0 -4rpx 40rpx 0 rgba(151, 151, 151, 0.24);
		background: #fff;
		// margin-bottom: 40rpx;

		// 设置ios刘海屏底部横线安全区域
		padding-bottom: constant(safe-area-inset-bottom);
		padding-bottom: env(safe-area-inset-bottom);
		box-shadow: 0 -5rpx 5rpx #ececec;
		padding: 20rpx 20rpx;

		text {

			display: block;
			box-sizing: border-box;
			margin: 0 0 0 40rpx;
			width: 160rpx;
			height: 60rpx;
			line-height: 60rpx;
			text-align: center;
			font-size: 24rpx;
			border-radius: 8rpx;
			border: none;
		}

		text:first-child {
			background-color: #fff;
			line-height: 56rpx;
			border: 2rpx solid $uni-bg-color;
		}

		text:last-child {
			background-color: $uni-bg-color;
			color: #FFFFFF;
		}
	}
</style>
