<template>
	<view class="flex-col page">
	  <view class="flex-row items-center section_2">
	    <image
	      class="image"
	      :src="stateObject.img"
	    />
	    <view class="flex-col items-start flex-1 ml-6">
	      <text class="font text">{{ stateObject.msg }}</text>
	      <text class="text_2 mt-4">{{ stateObject.tips }}</text>
	    </view>
	  </view>
	  <view class="flex-row items-center section_3">
	    <image
	      class="image_2"
	      src="https://carbon2.obs.cn-north-4.myhuaweicloud.com:443/feiyi%2Ftest%2F0%2FIwOnPxNs-contact.png"
	    />
	    <text class="text_3 ml-10">{{ contactInfo.name }} {{ contactInfo.phone }}</text>
	  </view>
	  <view class="flex-col section_4">
	    <view class="flex-col">
	      <view class="flex-row items-end list-item group mt-17" v-for="(item, index) in productList" :key="index">
	        <image
	          class="shrink-0 image_3"
	          :src="item.goodSnapshot.goodImg"
	        />
	        <view class="flex-col flex-1 group_5">
	          <view class="flex-row self-stretch group_3">
	            <text class="font_2">{{ item.goodSnapshot.name }}</text>
	            <text class="font_2 text_8">【{{ item.goodSnapshot.type }}】</text>
	          </view>
	          <text class="self-start font text_9">已选时间：{{ item.reservationDate }}星期二</text>
	          <text class="self-start font_3 text_10">{{ item.timeSlot }}</text>
	          <view class="flex-row items-center self-stretch group_23">
	            <!-- <image
	              class="image_4"
	              src="https://ide.code.fun/api/image?token=67c001c94ae84d0012277299&name=4a3c5c3a5c83be7a7b0551c598ed6037.png"
	            /> -->
	            <text class="font_4 ml-3">￥{{ item.goodSnapshot.price }}</text>
	            <!-- <text class="font_5 ml-3">00</text> -->
	          </view>
	        </view>
	        <text class="font_6 text_11 text_1">×{{ item.quantity }}</text>
	      </view>
	    </view>
	    <view class="flex-row justify-between items-center group_8">
	      <text class="font text_16">商品金额</text>
	      <view class="flex-row items-center">
	        <!-- <image
	          class="shrink-0 image_5"
	          src="https://ide.code.fun/api/image?token=67c001c94ae84d0012277299&name=4223cdafed72fca52c442dae885cee9c.png"
	        /> -->
	        <text class="font_7 text_17">￥{{ orderObject.totalAmount }}</text>
	      </view>
	    </view>
	    <view class="flex-row justify-between items-center group_9">
	      <text class="font">优惠券</text>
	      <view class="flex-row items-center">
	        <!-- <image
	          class="shrink-0 image_6"
	          src="https://ide.code.fun/api/image?token=67c001c94ae84d0012277299&name=shortmoney.png"
	        /> -->
	        <text class="font_7 text_18">-￥0</text>
	      </view>
	    </view>
	    <view class="flex-row justify-end items-center group_10">
	      <view class="flex-row items-center group_11">
	        <!-- <image
	          class="image_5 image_7"
	          src="https://ide.code.fun/api/image?token=67c001c94ae84d0012277299&name=15c19bceb4f8bbcc0ed72958e3788b20.png"
	        /> -->
	        <text class="font_2 text_19">合计：</text>
	      </view>
	      <text class="font_7 text_20">￥{{ orderObject.totalAmount }}</text>
	    </view>
	    <view class="flex-col group_12">
	      <text class="self-start font text_21">买家留言</text>
	      <view class="flex-col justify-start self-stretch relative group_13 mt-5">
	        <view class="group_14" v-if="orderObject.note != ''">
	        <text class="font text_22 pos_4">
	          {{ orderObject.note }}
	        </text>
					</view>
	      </view>
	    </view>
	    <view class="flex-row justify-between items-center group_15">
	      <text class="font text_23">订单编号：</text>
	      <view class="flex-row items-center group_16">
	        <view class="flex-col justify-start items-center text-wrapper" @click="copy(orderObject.orderNumber)"><text class="text_25">复制</text></view>
	        <text class="font_5 text_24">{{ orderObject.orderNumber }}</text>
	      </view>
	    </view>
	    <view class="flex-row justify-between items-center group_17">
	      <text class="font text_26">创建时间：</text>
	      <text class="font_5 text_27">{{ orderObject.createTime }}</text>
	    </view>
	    <view class="flex-col group_18">
	      <view class="flex-row justify-between items-baseline group_21"
					v-if="stateObject.state === '已退款'"
				>
	        <text class="font text_28">退款时间：</text>
	        <text class="font_5 text_29">{{ orderObject.updateTime }}</text>
	      </view>
	      <view class="flex-row justify-between items-baseline mt-3"
					v-if="stateObject.state === '已完成'"
				>
	        <text class="font text_30">完成时间：</text>
	        <text class="font_5 text_31">{{ orderObject.updateTime }}</text>
	      </view>
	    </view>
	  </view>
	  <view class="flex-row items-center section_5">
	    <view class="flex-row items-center shrink-0">
	      <text class="font_8 text_32">合计：</text>
	      <!-- <image
	        class="shrink-0 image_8"
	        src="https://ide.code.fun/api/image?token=67c001c94ae84d0012277299&name=549b29459342a9a09d24834be40eb53b.png"
	      /> -->
	      <text class="text_33">￥{{ orderObject.totalAmount }}</text>
	      <!-- <text class="font_6 text_36">00</text> -->
	    </view>
	    <view class="flex-row flex-1 ml-46">
				<button class="text-wrapper_9"  open-type="contact" bindcontact="handleContact" session-from="sessionFrom">
					<text class="font_9 text_64">联系客服</text>
				</button>
	      <view class="flex-col justify-start items-center text-wrapper_3 ml-6" @click="stateSubmit(orderObject)" v-if="orderObject.orderStatus == '待支付' ">
	        <text class="font_8 text_35">{{ bottomBtn }}</text>
	      </view>
	    </view>
	  </view>
	</view>
	<!-- 遮罩层 -->
	    <view v-if="isShow" class="overlay"></view>
</template>

<script setup>
	import { onMounted, ref } from 'vue';
	import { onLoad , onShow } from "@dcloudio/uni-app"; 
	import { serviceStateMap } from '../../../common/global';
	import { baseUrl } from '../../../api/request';
	const orderObject = ref({})  //后端传来的订单对象
	const contactInfo = ref({}) //联系人
	const productList = ref([])  //订单涵盖的商品
	const stateObject = ref({})  //当前订单状态
	const bottomBtn = ref('去支付')  //底下按钮
	const oid = ref(0) 
	onMounted(()=>{
		getOrderItem()
	})
	onLoad((options)=>{
		oid.value = JSON.parse(options.oid)  //获取从上一个页面传来的值
		console.log('当前订单ID为',oid.value);
	})
	onShow( async ()=>{
		await getOrderItem()
		await setBottomText()
	})
	const getOrderItem = async ()=>{  //获取当前订单详情
		const res = await uni.request({
			url: baseUrl + '/order/get/id',
			method: 'POST',
			header: {
				'cookie': wx.getStorageSync("cookie")
			},
			data: { id: oid.value } 
		})
		if(res.data.code === 1) {  
			orderObject.value = res.data.data,
			contactInfo.value = res.data.data.contactsSnapshot,
			productList.value = res.data.data.orderItemList
			stateObject.value = serviceStateMap.get(res.data.data.orderStatus)
		}
		console.log('联系人--->',contactInfo.value);
		console.log('商品信息为--->',productList.value);
		console.log('此订单信息为',res.data);
		console.log('订单状态---->',stateObject.value);
	}
	const setBottomText =()=>{  //更改按钮的字体
		switch(stateObject.value.state) {
			case '待支付': bottomBtn.value = '立即支付';break;
			case '已退款': bottomBtn.value = '删除订单';break;
			case '交易成功': bottomBtn.value = '删除订单';break;
		}
	}
	const stateSubmit = (obj) => {  //按钮状态改变的方法
		// console.log('商品obj--->',obj);
		if(bottomBtn.value === '立即支付') {   //重新支付
			wxPayFd(oid.value)
		} else if(bottomBtn.value === '删除订单') {
			console.log('订单被删除');
		}
	}
	const confirm =()=> {  //确认收货方法
		console.log('确认收货方法');
	}
	const wxPay = async( oid )=> {  //传入订单id
		try {
			const res = await uni.request({
				url: baseUrl + '/wechat/payment/create',
				method: 'POST',
				header: {
					'cookie': wx.getStorageSync("cookie")
				},
				data: { id: oid }
			})
			const paymentData = res.data.data
			wx.requestPayment({
				appid: paymentData.appId,
				nonceStr: paymentData.nonceStr,
				package: paymentData.packageVal,
				paySign: paymentData.paySign,
				timeStamp: paymentData.timeStamp,
				signType: paymentData.signType,
				success(res) {
					uni.showModal({
						content: '支付成功',
						showCancel: false
					})
					buffer()
				},
				fail(e) {
					// uni.showModal({
					// 	content: '支付失败，原因为:' + e.errMsg,
					// 	showCancel: false
					// })
				}
			})
		}catch(error) {
			console.error('支付请求失败',error);
			uni.showModal({
				content: '支付请求失败,请重试。',
				showCancel: false
			})
		}
	}
	const buffer = async ()=>{  //缓冲函数
		await getOrderItem();
		await setBottomText();
	}
	//复制到剪切板方法
	const copy = ( orderNumber ) => {
		console.log('订单编号为--->',orderNumber);
		uni.setClipboardData({
			data: orderNumber,
			success: () => {
				uni.showToast({
					title: '成功复制到剪贴板'
				})
			}
		})
	}
	//防抖相关变量
	let debounceTimer = null;
	let wxPayTimer = null;
	let loading = false;
	let isLoading = ref(false)
	let isShow = ref(false)
	const wxPayFd = (oid) => {  //微信支付按钮防抖
		clearTimeout(wxPayTimer)
		showLoading()  
		isShow.value = true  //打开遮罩
		wxPayTimer = setTimeout(async () => {
			await wxPay(oid)
		}, 1000)
		setTimeout(()=>{
			hideLoading()
			isShow.value = false  //关闭遮罩
		},2000)
	}
	function showLoading() {  //加载弹窗
	  if (!loading) {
	    wx.showLoading({
	      title: '加载中...',
	    });
	    loading = true;
		isLoading.value = true
	  }
	}
	function hideLoading() {  //关闭弹窗
	  if (loading) {
	    wx.hideLoading();
	    loading = false;
		isLoading.value = false
	  }
	}
</script>

<style lang="scss" scoped>
	.overlay {
	  position: fixed;
	  top: 0;
	  left: 0;
	  width: 100%;
	  height: 100%;
	  background-color: rgba(0, 0, 0, 0.4);  /* 半透明黑色背景 */
	  z-index: 999;
	}
.ml-3 {
  margin-left: 5.63rpx;
}
.mt-17 {
  margin-top: 31.88rpx;
}
.mt-5 {
  margin-top: 9.38rpx;
}
.mt-3 {
  margin-top: 5.63rpx;
}
.page {
  padding-top: 26.25rpx;
  background-color: #f8e8c1;
  background-image: url('https://carbon2.obs.cn-north-4.myhuaweicloud.com:443/feiyi%2Ftest%2F0%2FQfLHXSAU-feiyigongfangbeijin.png');
  background-size: 100% 100%;
  background-repeat: no-repeat;
  width: 100%;
  overflow-y: auto;
  overflow-x: hidden;
  height: 100vh;
}
.section_2 {
  margin-left: 16.88rpx;
  margin-right: 15rpx;
  padding: 16.88rpx;
  background-color: #ffffff;
  border-radius: 18.75rpx;
}
.image {
  width: 75rpx;
  height: 75rpx;
}
.font {
  font-size: 26.25rpx;
  font-family: FZSongKeBenXiuKaiS-R-GB;
  line-height: 25.54rpx;
  color: #818181;
}
.text {
  color: #323233;
  font-size: 28.13rpx;
  line-height: 27.58rpx;
}
.text_2 {
  color: #6b6e72;
  font-size: 22.5rpx;
  font-family: FZSongKeBenXiuKaiS-R-GB;
  line-height: 22.14rpx;
}
.section_3 {
  margin: 26.25rpx 15rpx 0 16.88rpx;
  padding: 20.63rpx 24.38rpx 24.98rpx;
  background-color: #ffffff;
  border-radius: 18.75rpx;
}
.image_2 {
  width: 65.63rpx;
  height: 67.5rpx;
}
.text_3 {
  color: #818181;
  font-size: 37.5rpx;
  font-family: FZSongKeBenXiuKaiS-R-GB;
  line-height: 32.38rpx;
}
.section_4 {
  margin: 25.65rpx 15rpx 70px 16.88rpx;
  padding: 20.63rpx 22.63rpx 9.79rpx 24.38rpx;
  background-color: #ffffff;
  border-radius: 18.75rpx;
}
.list-item {
  padding: 0 11.25rpx;
}
.list-item:first-child {
  margin-top: 0;
}
.group {
  padding: 0 7.5rpx;
}
.image_3 {
  border-radius: 9.38rpx;
  width: 142.5rpx;
  height: 166.88rpx;
}
.group_5 {
  margin-left: 30.04rpx;
  margin-top: 4.2rpx;
}
.group_3 {
  padding-left: 3.83rpx;
}
.font_2 {
  font-size: 26.25rpx;
  font-family: FZSongKeBenXiuKaiS-R-GB;
  line-height: 25.54rpx;
  color: #323232;
}
.text_8 {
  line-height: 25.01rpx;
}
.text_9 {
  margin-left: 8.01rpx;
  margin-top: 18.21rpx;
  line-height: 24.62rpx;
}
.font_3 {
  font-size: 26.25rpx;
  font-family: FZSongKeBenXiuKaiS-R-GB;
  line-height: 17.53rpx;
  color: #818181;
}
.text_10 {
  margin-left: 9.21rpx;
  margin-top: 19.99rpx;
}
.group_23 {
  margin-top: 27.58rpx;
}
.image_4 {
  width: 35.63rpx;
  height: 30rpx;
}
.font_4 {
  font-size: 33.75rpx;
  font-family: FZSongKeBenXiuKaiS-R-GB;
  line-height: 22.54rpx;
  color: #323232;
}
.font_5 {
  font-size: 26.25rpx;
  font-family: FZSongKeBenXiuKaiS-R-GB;
  line-height: 17.53rpx;
  color: #323232;
}
.font_6 {
  font-size: 30rpx;
  font-family: Open Sans;
  line-height: 22.54rpx;
  color: #323232;
}
.text_11 {
  margin-left: 39.99rpx;
  margin-bottom: 2.66rpx;
  line-height: 21.41rpx;
}
.text_1 {
  margin: 0 7.88rpx 2.66rpx 39.99rpx;
}
.group_8 {
  margin-top: 21.71rpx;
}
.text_16 {
  line-height: 24.3rpx;
}
.image_5 {
  width: 30rpx;
  height: 30rpx;
}
.font_7 {
  font-size: 26.25rpx;
  font-family: Open Sans;
  line-height: 17.53rpx;
  color: #323232;
}
.text_17 {
  line-height: 19.41rpx;
}
.group_9 {
  margin-top: 13.13rpx;
}
.image_6 {
  width: 41.25rpx;
  height: 30rpx;
}
.text_18 {
  line-height: 19.41rpx;
}
.group_10 {
  margin-top: 16.88rpx;
}
.group_11 {
  width: 102.3rpx;
}
.image_7 {
  margin-left: 72.3rpx;
}
.text_19 {
  // margin-left: -102.3rpx;
  line-height: 24.3rpx;
}
.text_20 {
  color: #ffaaa5;
  line-height: 19.41rpx;
}
.group_12 {
  margin-top: 15rpx;
  padding: 29.19rpx 0 16.88rpx;
  border-top: solid 1.88rpx #dfdfdf;
  border-bottom: solid 1.88rpx #dfdfdf;
}
.text_21 {
  margin-left: 2.36rpx;
  line-height: 24.71rpx;
}
.group_13 {
  margin-right: 3.62rpx;
  padding-top: 9.86rpx;
}
.group_14 {
  border-radius: 9.38rpx;
  height: 84.38rpx;
  border: solid 1.88rpx #ffffff;
}
.text_22 {
  line-height: 31.88rpx;
  text-shadow: 0 1.88rpx #ffffff, 0 -1.88rpx #ffffff, 1.88rpx 0 #ffffff, -1.88rpx 0 #ffffff;
  text-indent: 54.45rpx;
}
.pos_4 {
  position: absolute;
  left: 12.17rpx;
  right: 25.33rpx;
  top: 50%;
  transform: translateY(-50%);
}
.group_15 {
  margin-top: 15.36rpx;
}
.text_23 {
  line-height: 25.84rpx;
}
.group_16 {
  margin-right: 3.62rpx;
  width: 368.34rpx;
}
.text-wrapper {
  margin-left: 317.72rpx;
  padding-bottom: 2.31rpx;
  background-color: #ffffff;
  width: 50.63rpx;
  height: 22.5rpx;
  border: solid 0.94rpx #d1d1d1;
}
.text_25 {
  color: #323232;
  font-size: 18.75rpx;
  font-family: Open Sans;
  line-height: 17.36rpx;
}
.text_24 {
  margin-left: -404.34rpx;
}
.group_17 {
  margin-top: 10.24rpx;
}
.text_26 {
  line-height: 24.71rpx;
}
.text_27 {
  margin-right: 132.38rpx;
}
.group_18 {
  margin-top: 11.12rpx;
}
.group_21 {
  padding: 0 2.38rpx;
}
.text_28 {
  line-height: 24.51rpx;
}
.text_29 {
  margin-right: 137.49rpx;
}
.text_30 {
  line-height: 24.51rpx;
}
.text_31 {
  margin-right: 139.88rpx;
}
.section_5 {
	position: fixed;
	left: 0;
	right: 0;
	bottom: 0;
  // margin-top: 131.25rpx;
  padding: 16.88rpx 15rpx 15rpx 25.31rpx;
  background-color: #ffffff;
}
.font_8 {
  font-size: 30rpx;
  font-family: FZSongKeBenXiuKaiS-R-GB;
  line-height: 27.77rpx;
}
.text_32 {
  color: #000000;
}
.image_8 {
  width: 33.75rpx;
  height: 33.75rpx;
}
.text_33 {
  color: #ffaaa5;
  font-size: 37.5rpx;
  font-family: Open Sans;
  font-weight: 700;
  line-height: 27.62rpx;
}
.text_36 {
  color: #ffaaa5;
  font-weight: 700;
  line-height: 22.05rpx;
}
.text-wrapper_2 {
  padding: 17.81rpx 0 20.51rpx;
  flex: 1 1 204.38rpx;
  background-color: #ffffff;
  border-radius: 75rpx;
  height: 71.25rpx;
  border: solid 1.88rpx #e1e1e1;
}
.text_34 {
  color: #ffaaa5;
  line-height: 29.18rpx;
}
.text-wrapper_3 {
  padding: 20.63rpx 0 22.86rpx;
  flex: 1 1 204.38rpx;
  background-color: #ffaaa5;
  border-radius: 75rpx;
  height: 71.25rpx;
}
.text_35 {
  color: #ffffff;
}
.text-wrapper_9 {
	// position: relative;
	// left: 326rpx;
  background-color: #ffffff;
  border-radius: 75rpx;
  width: 214rpx;
  // height: 71rpx;
	font-size: unset; 
}
.ml-46 {
	margin-left: 141.25rpx;
}
.text_64 {
	color: #ffaaa5;
}
@import url(../../../common/css/global.css);
</style>
