<template>
	<view class="flex-col justify-start relative page">
	  <view class="section"></view>
	  <view class="flex-row items-center section_2 pos">
	    <image
	      class="image"
	      :src="orderStatusObj.img"
	    />
	    <view class="flex-col items-start flex-1 ml-6">
	      <text class="font text">{{ orderStatusObj.msg }}</text>
	      <text class="font_2 text_2 mt-3">{{ orderStatusObj.tips }}</text>
	    </view>
	  </view>
	  <view class="flex-row items-center section_3 pos_2">
	    <image
	      class="image_2"
	      :src="myOrderUrl + '/myServiceOrderDetail/lxr.png'"
	    />
	    <text class="font_3 text_3 ml-9">{{ contactObj.name }} {{ contactObj.phone }}</text>
	  </view>
	  <view class="flex-col section_4 pos_3">
	    <view class="flex-col">
	      <view class="flex-row list-item mt-12" v-for="(item, index) in order.orderItemList" :key="index">
	        <image
	          class="shrink-0 image_3"
	          :src="item.goodSnapshot.goodImg"
	        />
	        <view class="flex-col items-start flex-1 group_2 ml-20">
	          <text class="font"> {{ item.goodSnapshot.name }}</text>
	          <text class="font_4 text_4">已选时间:{{ item.reservationDate }} {{ getWeekday(item.reservationDate) }}</text>
	          <text class="font_5 text_5">{{ item.timeSlot }}</text>
	          <text class="font_6 text_6">¥{{ item.goodSnapshot.price.toFixed(2) }}</text>
	        </view>
	        <text class="self-start font_6 text_7 ml-20">× {{ item.quantity }}</text>
	      </view>
	    </view>
	    <view class="flex-row justify-between items-baseline view_2">
	      <text class="font_3 text_8">商品金额</text>
	      <text class="font_6 text_1">¥{{ allAmount.toFixed(2) }}</text>
	    </view>
	    <view class="flex-row justify-between items-baseline view_3">
	      <text class="font_7 text_10">优惠券</text>
	      <text class="font_6 text_9">¥{{ couponAmount.toFixed(2) }}</text>
	    </view>
	    <view class="flex-row justify-end items-baseline group_3">
	      <text class="font text_11">应付款:</text>
	      <view class="group_4">
	        <text class="font_6 text_12">¥</text>
	        <text class="text_13">{{ order.totalAmount.toFixed(2) }}</text>
	      </view>
	    </view>
	    <view class="flex-col group_5">
	      <text class="self-start font_8 text_14">买家留言</text>
	      <view class="flex-col justify-start self-stretch text-wrapper mt-2">
	        <text class="font_2 text_15" style="word-break: break-all; word-wrap: break-word;">
	          {{ order.note }}
	        </text>
	      </view>
	    </view>
	    <view class="flex-row justify-between items-baseline group_6"> 
	      <text class="font_8 text_16">创建时间:</text>
	      <text class="font_9 text_17">{{ order.createTime }}</text>
	    </view>
	    <view class="flex-row justify-between group_7">
	      <text class="font_7 text_18">订单编号:</text>
	      <view class="flex-row items-center group_8">
	        <text class="font_9 text_19">{{ order.orderNumber }}</text>
	        <view class="flex-col justify-start items-center shrink-0 text-wrapper_2 ml-4" @click="copy(order.orderNumber)">
	          <text class="text_20">复制</text>
	        </view>
	      </view>
	    </view>
	    <view class="flex-row justify-center items-center group_9">
			<button style="display: flex; align-items: center; height: 60rpx;" open-type="contact" bindcontact="handleContact" session-from="sessionFrom">			 
			  <image
				class="image_4"
				:src="myOrderUrl + '/myServiceOrderDetail/zxkf.png'"
			  />
			  <text class="font text_21">在线客服</text>
			</button>
	    </view>
	  </view>
	  <view class="flex-row justify-between items-center section_5 pos_4">
	    <view class="flex-row items-baseline">
	      <text class="font text_22">应付款:</text>
	      <view class="group_10">
	        <text class="font_10 text_23">¥</text>
	        <text class="font_10 text_24">{{ order.totalAmount.toFixed(2) }}</text>
	      </view>
	    </view>
		
	    <view v-if="order.operationList[0]" class="flex-col justify-start items-center text-wrapper_3" @click="wxPayFd(order)">
			<text class="font text_25">去支付</text>
		</view>
		
		<view v-if="order.operationList[1]" class="flex-col justify-start items-center text-wrapper_3" @click="gotoDeleteOrder(order.id)">
			<text class="font text_25">删除订单</text>
		</view>
		
		<view v-if="order.operationList[2]" style="background-color: #fff;" class="flex-col justify-start items-center text-wrapper_3">
			<text class="font text_25"></text>
		</view>
		
	  </view>
	</view>
	
	<!-- 遮罩层 -->
	    <view v-if="isShow" class="overlay"></view>
</template>

<script setup>
	import {onMounted, ref} from 'vue'
	import { onLoad } from "@dcloudio/uni-app";
	import { baseUrl } from '../../../api/request';
	import { JudgeIsNullity } from '../../../common/globalFunction';
	import { serviceStateList } from '../../../common/global.js'
	import emitter from '../../../utils/emitter';
	import { getFonts } from '../../../common/globalFont';
	import { myOrderUrl } from '../../../common/globalImagesUrl';
	const cookie = wx.getStorageSync('cookie')
	let orderId = ''
	const order = ref({})
	const couponAmount = ref(0)
	const allAmount = ref(0)
	const contactObj = ref({})
	const orderStatusObj = ref({})
	let debounceTimer = null;
	let wxPayTimer = null;
	let loading = false;
	let isLoading = ref(false)
	let isShow = ref(false)
	
	
	onLoad((options) => {
		getFonts()
		orderId = options.id
	})
	onMounted(() => {
		getOrderById(orderId)
	})
	
	
	const getOrderById = async (val) => {
		const res = await uni.request({
			url: baseUrl + '/order/get/id',
			method: 'POST',
			header: {
				cookie
			},
			data: {
				id: val
			}
		})
		console.log(res.data.data)
		order.value = res.data.data
		contactObj.value = order.value.contactsSnapshot
		couponAmount.value = JudgeIsNullity(order.value.couponSnapshot) ? 0 : order.value.couponSnapshot.conditionAmount
		allAmount.value = order.value.totalAmount + couponAmount.value 
		if (order.value.orderStatus === '待支付') {
			order.value.operationList = [true, false, false]
			orderStatusObj.value = serviceStateList[0]
		} else if (order.value.orderStatus === '交易关闭') {
			order.value.operationList = [false, true, false]
			orderStatusObj.value = serviceStateList[4]
		} else if (order.value.orderStatus === '待发货') {
			order.value.operationList = [false, false, true] 
			orderStatusObj.value = serviceStateList[1]
		} else if (order.value.orderStatus === '已退款') {
			order.value.operationList = [false, false, true] 
			orderStatusObj.value = serviceStateList[2]
		} else if (order.value.orderStatus === '交易成功') {
			order.value.operationList = [false, false, true] 
			orderStatusObj.value = serviceStateList[3]
		} 
	}
	
	const copy = ( orderNumber ) => {
		console.log('订单编号为--->',orderNumber);
		uni.setClipboardData({
			data: orderNumber,
			success: () => {
				uni.showToast({
					title: '成功复制到剪贴板'
				})
			}
		})
	}
	
	
	const wxPayFd = (order) => {  //微信支付按钮防抖
		clearTimeout(wxPayTimer)
		showLoading()  
		isShow.value = true  //打开遮罩
		wxPayTimer = setTimeout(async () => {
			await wxPay(order.id)
		}, 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
	  }
	}
	
	
	
	
	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
					})
					getOrderById(oid)
					console.log('支付成功res--->',res);
				},
				fail(e) {
					
				}
			})
		}catch(error) {
			console.error('支付请求失败');
			uni.showModal({
				content: '支付请求失败,请重试。',
				showCancel: false
			})
		}
	}
	

	const gotoDeleteOrder = (val) => {
		uni.showModal({
			title: '提示',
			content: '您确定要删除订单吗?',
			success: async (res) => {
				if (res.confirm) {
					await deleteOrder(val)
					await emitter.emit('flushOrderList')
					routerJump('pages/my-order/myServiceOrderDetail/myServiceOrderDetail')
				}
			}
		})
	}
	
	
	const deleteOrder = async (val) => {
		const res = await uni.request({
			url: baseUrl + '/order/delete/id',
			method: 'POST',
			header: {
				cookie
			},
			data: {
				id: val
			}
		})
		console.log(res.data.data)
	}
	
	
	
	function getWeekday(dateStr) {
	    const date = new Date(dateStr);
	    const weekdays = ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"];
	    return weekdays[date.getDay()];
	}
	
	
	const routerJump = (val) => {
		let pages = getCurrentPages();
		var num = pages.length
		if (num == 1) return ;
		console.log(pages)
			//当前页面栈总数
		var backnum = num 
		for( var i = 0; i < num; i ++ ) {
			//循环找到指定页面路由所在的页数
			if(pages[i].route == val){
			//'pages/mypage/mypage'替换成A页面的路由地址
				backnum = num - i - 1
				//计算返回的层数,总数-指定页面页数-1
			}		
		}
		uni.navigateBack({
			delta:backnum
			//返回的页面数,如果 delta 大于现有页面数,则返回到首页。
		})
	}
	
</script>

<style scoped lang="scss">
.overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.4);  /* 半透明黑色背景 */
  z-index: 999;
}
button {
	padding: 0;
	margin: 0;
	background-color: #fff;
}
button::after{
	border: none; 
}
	
.mt-3 {
  margin-top: 5.63rpx;
}
.ml-9 {
  margin-left: 16.88rpx;
}
.page {
  background-color: #ffffff;
  background-image: url(https://carbon2.obs.cn-north-4.myhuaweicloud.com/feiyi/miniProgram/my-order/myServiceOrderDetail/bkg.png);
  background-size: 100% 100%;
  background-repeat: no-repeat;
  width: 100%;
  overflow-y: auto;
  overflow-x: hidden;
  height: 100vh;
}
// .section {
//   background-image: url('https://ide.code.fun/api/image?token=67cf80c84ae84d001228feb1&name=9df295c356c1b6849e20b6b4853b4d1c.png');
//   background-size: 100% 100%;
//   background-repeat: no-repeat;
//   width: 750rpx;
// }
.section_2 {
  padding: 16.88rpx;
  background-color: #ffffff;
  border-radius: 18.75rpx;
}
.pos {
  position: absolute;
  left: 16.88rpx;
  right: 15rpx;
  top: 26.25rpx;
}
.image {
  width: 75rpx;
  height: 75rpx;
}
.font {
  font-size: 30rpx;
  font-family: FangZhengFonts;
  line-height: 29.18rpx;
  color: #323232;
}
.text {
  color: #323233;
  font-size: 28.13rpx;
  line-height: 27.58rpx;
}
.font_2 {
  font-size: 24.38rpx;
  font-family: FangZhengFonts;
}
.text_2 {
  color: #6b6e72;
  line-height: 24rpx;
}
.section_3 {
  padding: 24.38rpx 24.38rpx 26.25rpx;
  background-color: #ffffff;
  border-radius: 18.75rpx;
}
.pos_2 {
  position: absolute;
  left: 16.88rpx;
  right: 15rpx;
  top: 159.38rpx;
}
.image_2 {
  width: 46.88rpx;
  height: 46.88rpx;
}
.font_3 {
  font-size: 30rpx;
  font-family: FangZhengFonts;
  line-height: 24.51rpx;
  color: #818181;
}
.text_3 {
  line-height: 25.89rpx;
}
.section_4 {
  padding: 20.63rpx 16.01rpx 24.38rpx 20.85rpx;
  background-color: #ffffff;
  border-radius: 18.75rpx;
}
.pos_3 {
  // position: absolute;
  // left: 15rpx;
  // right: 16.88rpx;
  // top: 281.25rpx;
  margin: 281.25rpx 16.88rpx 150rpx 15rpx;
}
.list-item {
  padding-left: 12.9rpx;
  padding-right: 10.74rpx;
}
.list-item:first-child {
  margin-top: 0;
}
.image_3 {
  border-radius: 9.38rpx;
  width: 142.5rpx;
  height: 166.88rpx;
}
.group_2 {
  margin-bottom: 3.34rpx;
}
.font_4 {
  font-size: 26.25rpx;
  font-family: FangZhengFonts;
  line-height: 24.51rpx;
  color: #818181;
}
.text_4 {
  margin-left: 3.92rpx;
  margin-top: 17.72rpx;
}
.font_5 {
  font-size: 26.25rpx;
  font-family: FangZhengFonts;
  line-height: 17.53rpx;
  color: #818181;
}
.text_5 {
  margin-left: 5.14rpx;
  margin-top: 20.1rpx;
}
.font_6 {
  font-size: 30rpx;
  font-family: FangZhengFonts;
  line-height: 21.41rpx;
  color: #323232;
}
.text_6 {
  margin-left: 3.36rpx;
  margin-top: 31.29rpx;
  line-height: 22.18rpx;
}
.text_7 {
  margin-top: 145.46rpx;
}
.view_2 {
  margin-top: 46.52rpx;
  padding-left: 4.97rpx;
}
.text_8 {
  font-size: 28.13rpx;
  line-height: 26.04rpx;
}
.text_1 {
  font-size: 28.13rpx;
  line-height: 20.79rpx;
}
.view_3 {
  margin-top: 18.08rpx;
  padding-left: 3.98rpx;
}
.font_7 {
  font-size: 30rpx;
  font-family: FangZhengFonts;
  line-height: 29.18rpx;
  color: #818181;
}
.text_10 {
  font-size: 28.13rpx;
  line-height: 27.36rpx;
}
.text_9 {
  font-size: 28.13rpx;
  line-height: 20.79rpx;
}
.group_3 {
  margin-top: 18.41rpx;
  padding: 0 5.91rpx;
}
.text_11 {
  font-size: 28.13rpx;
  line-height: 25.71rpx;
}
.group_4 {
  line-height: 20.76rpx;
  height: 20.76rpx;
}
.text_12 {
  color: #ffaaa5;
  font-size: 28.13rpx;
  line-height: 20.29rpx;
}
.text_13 {
  color: #ffaaa5;
  font-size: 28.13rpx;
  font-family: FangZhengFonts;
  font-weight: 700;
  line-height: 20.76rpx;
}
.group_5 {
  margin-top: 27.88rpx;
  padding: 21.41rpx 3.52rpx 14.31rpx;
  border-top: solid 1.88rpx #dfdfdf;
  border-bottom: solid 1.88rpx #dfdfdf;
}
.font_8 {
  font-size: 30rpx;
  font-family: FangZhengFonts;
  line-height: 26.47rpx;
  color: #818181;
}
.text_14 {
  margin-left: 2.53rpx;
  font-size: 28.13rpx;
}
.text-wrapper {
  margin-right: 6.71rpx;
  padding: 6.6rpx 0 2.96rpx;
  border-radius: 9.38rpx;
  border: solid 1.88rpx #ffffff;
}
.text_15 {
  margin-right: 8.74rpx;
  color: #818181;
  line-height: 30rpx;
  text-shadow: 0 1.88rpx #ffffff, 0 -1.88rpx #ffffff, 1.88rpx 0 #ffffff, -1.88rpx 0 #ffffff;
}
.group_6 {
  margin-top: 28.14rpx;
}
.text_16 {
  font-size: 28.13rpx;
}
.font_9 {
  font-size: 26.25rpx;
  font-family: FangZhengFonts;
  color: #323232;
}
.text_17 {
  line-height: 19.41rpx;
}
.group_7 {
  margin-top: 19.2rpx;
}
.text_18 {
  font-size: 28.13rpx;
  line-height: 27.69rpx;
}
.group_8 {
  margin-right: 4.59rpx;
}
.text_19 {
  line-height: 19.29rpx;
}
.text-wrapper_2 {
  padding: 3.43rpx 0 3.86rpx;
  background-color: #ffffff;
  width: 65.63rpx;
  height: 30rpx;
  border: solid 0.94rpx #d1d1d1;
}
.text_20 {
  color: #323232;
  font-size: 22.5rpx;
  font-family: FangZhengFonts;
  line-height: 20.83rpx;
}
.group_9 {
  margin-top: 24.38rpx;
}
.image_4 {
  width: 45rpx;
  height: 45rpx;
}
.text_21 {
  line-height: 28.48rpx;
}
.section_5 {
  padding: 16.88rpx 15rpx 15rpx 21.56rpx;
  background-color: #ffffff;
}
.pos_4 {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
}
.text_22 {
  color: #000000;
  line-height: 27.43rpx;
}
.group_10 {
  line-height: 24.92rpx;
  height: 24.92rpx;
}
.font_10 {
  font-size: 33.75rpx;
  font-family: FangZhengFonts;
  line-height: 24.51rpx;
  color: #ffaaa5;
}
.text_23 {
  line-height: 24.34rpx;
}
.text_24 {
  font-weight: 700;
  line-height: 24.92rpx;
}
.text-wrapper_3 {
  padding: 20.63rpx 0 22.86rpx;
  background-color: #ffaaa5;
  border-radius: 75rpx;
  width: 204.38rpx;
  height: 71.25rpx;
}
.text_25 {
  color: #ffffff;
  line-height: 27.77rpx;
}
@import url(../../../common/css/global.css);
</style>