<!-- 积分商城：商品列表  -->
<template>
  <s-layout title="兑换成功" navbar="normal" :leftWidth="0" :rightWidth="0">
		<view class="main">
			<image src="https://zysc.fjptzykj.com/admin-api/infra/file/25/get/4fd1540f5e7c4545140fe1278fd6db65f18a3cd86bc866b24470b888bf6664fe.png" class="img" mode=""></image>
			<view class="title">商品兑换成功</view>
			<view class="fgx"></view>
			<view class="ddxx">
				<view class="l">
					订单编号
				</view>
				<view class="r">
					{{ state.orderInfo.no }}
				</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">
					积分兑换
				</view>
			</view>
			<view class="ddxx">
				<view class="l">
					支付积分
				</view>
				<view class="r">
					{{state.orderInfo.usePoint}}
				</view>
			</view>
			<view class="fgx"></view>
			
			<view class="ck-detail" @click="sheep.$router.redirect('/pages/activity/point/exchange_detail',{id:state.id})">
				查看详情
			</view>
			<view class="bk-home" @click="sheep.$router.redirect('/pages/index/index');">
				返回首页
			</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: '', // 进入订单详情的来源类型
		id:0
	});

	// ========== 门店自提（核销） ==========
	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;
			state.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{
		background:white;
		width:85%;
		padding:15px;
		position:fixed;
		top:50%;
		left:50%;
		transform:translate(-50%, -50%);
		border-radius:12px;
		.img{
			width:80px;
			height:80px;
			display:block;
			margin:0 auto;
			margin-top: -52px;
		}
		.title{
			text-align: center;
			font-size:18px;
			margin:15px 0;
			font-weight: 700;
		}
		.ddxx{
			display: flex;
			justify-content: space-between;
			margin:10px 0;
			.l{
				font-size: 17px;
			}
			.r{
				font-size: 16px;
				color:rbga(102,102,102);
			}
		}
		.fgx{
			margin: 20px 0;
			height: 1px;
			width: 100%;
			background:rgba(239,239,239);
		}
		.ck-detail{
			// background: rgba(254,92,45);
			background: rgba(207,162,92);
			padding: 10px 0;
			color: white;
			font-size: 19px;
			text-align: center;
			width: 100%;
			margin-bottom: 15px;
			border-radius:31px;
		}
		.bk-home{
			border:1px solid rgba(207,162,92);
			padding: 10px 0;
			color: rgba(207,162,92);
			font-size: 19px;
			text-align: center;
			width: 100%;
			border-radius:31px;
		}
		
	}
</style>
