<!-- 积分商城：商品列表  -->
<template>
	<s-layout title="兑换详情" navbar="normal" :leftWidth="0" :rightWidth="0">
		<view class="main">
			<!-- 收货地址 -->
			<view class="t-address">
				<view class="t">
					<text class="l">{{ state.orderInfo.receiverName }}</text>
					<text class="r">{{ state.orderInfo.receiverMobile }}</text>
				</view>
				<view class="b">
					{{ state.orderInfo.receiverAreaName }} {{ state.orderInfo.receiverDetailAddress }}
				</view>
				<image
					src="https://zysc.fjptzykj.com:3000/shangcheng/0f01dda22fb349c40c4659d73cb346a1423fee44c9d53eb07bdae1a1e11b5299.png"
					class="img"></image>
			</view>

			<view class="c-goods">
				<view class="t">
					共{{state.orderInfo.productCount}}件商品
				</view>

				<view class="b-detail" v-for="(item,index) in state.orderInfo.items" :key="item.goods_id">
					<image :src="item.picUrl" class="img" mode=""></image>
					<view class="right">
						<view class="t">
							<view class="ss-line-2" style="    width: 88%;">
								{{item.spuName}}
							</view>
							<text class="r">x{{item.count}}</text>
						</view>
						<view class="c">
							{{item.properties.map((property) => property.valueName).join(' ')}}
						</view>
						<view class="b">
							{{state.orderInfo.usePoint}}积分
						</view>
					</view>
				</view>
			</view>

			<view class="b-goodsDetail">
				<view class="ddxx">
					<view class="l">
						订单编号:
					</view>
					<view class="r">
						{{ state.orderInfo.no }}
						<view class="copy-btn" @click="onCopy">
							复制
						</view>
					</view>

				</view>
				<view class="ddxx">
					<view class="l">
						订单状态:
					</view>
					<view class="r">
						{{ formatOrderStatus(state.orderInfo) }}
					</view>
				</view>
				<view class="ddxx">
					<view class="l">
						下单时间:
					</view>
					<view class="r">
						{{ sheep.$helper.timeFormat(state.orderInfo.createTime, 'yyyy-mm-dd hh:MM:ss') }}
					</view>
				</view>
				<view class="ddxx">
					<view class="l">
						支付积分
					</view>
					<view class="r">
						{{state.orderInfo.usePoint}}
					</view>
				</view>
			</view>
		</view>
	</s-layout>
</template>
<script setup>
	import sheep from '@/sheep';
	import {
		onLoad
	} from '@dcloudio/uni-app';
	import {
		reactive,
		ref
	} from 'vue';
	import {
		isEmpty
	} from 'lodash-es';
	import {
		fen2yuan,
		formatOrderStatus,
		formatOrderStatusDescription,
		handleOrderButtons,
	} from '@/sheep/hooks/useGoods';
	import OrderApi from '@/sheep/api/trade/order';
	import DeliveryApi from '@/sheep/api/trade/delivery';
	import PickUpVerify from '@/pages/order/pickUpVerify.vue';

	const statusBarHeight = sheep.$platform.device.statusBarHeight * 2;
	const headerBg = sheep.$url.css('/static/img/shop/order/order_bg.png');

	const state = reactive({
		orderInfo: {},
		merchantTradeNo: '', // 商户订单号
		comeinType: '', // 进入订单详情的来源类型
	});

	// ========== 门店自提（核销） ==========
	const systemStore = ref({}); // 门店信息

	// 复制
	const onCopy = () => {
		sheep.$helper.copyText(state.orderInfo.no);
	};

	// 去支付
	function onPay(payOrderId) {
		sheep.$router.go('/pages/pay/index', {
			id: payOrderId,
		});
	}

	// 查看商品
	function onGoodsDetail(id) {
		sheep.$router.go('/pages/goods/index', {
			id,
		});
	}

	// 取消订单
	async function onCancel(orderId) {
		uni.showModal({
			title: '提示',
			content: '确定要取消订单吗?',
			success: async function(res) {
				if (!res.confirm) {
					return;
				}
				const {
					code
				} = await OrderApi.cancelOrder(orderId);
				if (code === 0) {
					await getOrderDetail(orderId);
				}
			},
		});
	}

	// 查看物流
	async function onExpress(id) {
		sheep.$router.go('/pages/order/express/log', {
			id,
		});
	}

	// 确认收货 TODO 芋艿：待测试
	async function onConfirm(orderId, ignore = false) {
		// 需开启确认收货组件
		// todo: 芋艿：待接入微信
		// 1.怎么检测是否开启了发货组件功能？如果没有开启的话就不能在这里return出去
		// 2.如果开启了走mpConfirm方法,需要在App.vue的show方法中拿到确认收货结果
		let isOpenBusinessView = true;
		if (
			sheep.$platform.name === 'WechatMiniProgram' &&
			!isEmpty(state.orderInfo.wechat_extra_data) &&
			isOpenBusinessView &&
			!ignore
		) {
			mpConfirm(orderId);
			return;
		}

		// 正常的确认收货流程
		const {
			code
		} = await OrderApi.receiveOrder(orderId);
		if (code === 0) {
			await getOrderDetail(orderId);
		}
	}

	// #ifdef MP-WEIXIN
	// 小程序确认收货组件
	function mpConfirm(orderId) {
		if (!wx.openBusinessView) {
			sheep.$helper.toast(`请升级微信版本`);
			return;
		}
		wx.openBusinessView({
			businessType: 'weappOrderConfirm',
			extraData: {
				merchant_trade_no: state.orderInfo.wechat_extra_data.merchant_trade_no,
				transaction_id: state.orderInfo.wechat_extra_data.transaction_id,
			},
			success(response) {
				console.log('success:', response);
				if (response.errMsg === 'openBusinessView:ok') {
					if (response.extraData.status === 'success') {
						onConfirm(orderId, true);
					}
				}
			},
			fail(error) {
				console.log('error:', error);
			},
			complete(result) {
				console.log('result:', result);
			},
		});
	}

	// #endif

	// 评价
	function onComment(id) {
		sheep.$router.go('/pages/goods/comment/add', {
			id,
		});
	}

	const pickUpVerifyRef = ref();

	async function getOrderDetail(id) {
		// 对详情数据进行适配
		let res;
		if (state.comeinType === 'wechat') {
			// TODO 芋艿：微信场景下
			res = await OrderApi.getOrder(id, {
				merchant_trade_no: state.merchantTradeNo,
			});
		} else {
			res = await OrderApi.getOrder(id);
		}
		if (res.code === 0) {
			state.orderInfo = res.data;
			handleOrderButtons(state.orderInfo);
			// 配送方式：门店自提
			if (res.data.pickUpStoreId) {
				const {
					data
				} = await DeliveryApi.getDeliveryPickUpStore(res.data.pickUpStoreId);
				systemStore.value = data || {};
			}
			if (state.orderInfo.deliveryType === 2 && state.orderInfo.payStatus) {
				pickUpVerifyRef.value && pickUpVerifyRef.value.markCode(res.data.pickUpVerifyCode);
			}
		} else {
			sheep.$router.back();
		}
	}

	onLoad(async (options) => {
		let id = 0;
		if (options.id) {
			id = options.id;
		}
		// TODO 芋艿：下面两个变量，后续接入
		state.comeinType = options.comein_type;
		if (state.comeinType === 'wechat') {
			state.merchantTradeNo = options.merchant_trade_no;
		}
		await getOrderDetail(id);
	});
</script>

<style lang="scss" scope>
	.main {
		.t-address {
			margin: 9px 0;
			padding: 15px 10px;
			background: white;
			padding-bottom: 0;

			.t {
				font-size: 30rpx;
				color: black;
				display: flex;
				font-weight: 700;
				margin-bottom: 5px;

				.l {
					margin-right: 10px;
				}
			}

			.b {
				font-size: 26rpx;
				color: rgba(156, 143, 114);
			}

			.img {
				width: 100%;
				height: 2px;
			}
		}

		.c-goods {
			margin: 9px 0;
			padding: 15px 10px;
			background: white;

			&>.t {
				font-size: 30rpx;
				border-bottom: 1px solid rgba(240, 240, 240);
				padding-bottom: 15px;
			}

			.b-detail {
				display: flex;
				margin-top: 15px;

				.img {
					width: 78px;
					height: 78px;
					margin-right: 14px;
				}

				.right {
					flex: 1;

					.t {
						font-size: 26rpx;
						font-weight: 500;
						display: flex;
						justify-content: space-between;

						.r {
							color: rgba(134, 139, 151);
						}
					}

					.c {
						margin: 10px 0;
						color: rgba(134, 139, 151);
					}

					.b {
						color: rgba(254, 94, 51);
					}
				}
			}
		}

		.b-goodsDetail {
			margin: 9px 0;
			padding: 7px 10px;
			background: white;
		}

		.ddxx {
			display: flex;
			justify-content: space-between;
			margin: 8px 0;

			.l {
				font-size: 15px;

				.r {}
			}

			.r {
				font-size: 14px;
				color: rgba(102, 102, 102);
				display: flex;
				align-items: center;

				.copy-btn {
					width: 3.125rem;
					line-height: 1.5625rem;
					border-radius: 0.78125rem;
					padding: 0;
					background: #eeeeee;
					font-size: 0.6875rem;
					font-weight: 400;
					color: #333333;
					position: relative;
					text-align: center;
					margin-left: 10px;
					&:after {
						content: ' ';
						width: 200%;
						height: 200%;
						position: absolute;
						top: 0;
						left: 0;
						// border: 1px solid rgba(0, 0, 0, 0.2);
						transform: scale(0.5);
						transform-origin: 0 0;
						box-sizing: border-box;
						border-radius: 10px;
					}
				}
			}

		}
	}
</style>