<template>
	<view class="flex-col justify-start relative page" :style="{ backgroundImage: 'url(' + bkgPubilcPath + ')'}">
	  <view class="flex-row items-center section_2 pos">
	    <image
	      class="image"
	      :src="orderStatusObj.img"
	    />
	    <view class="ml-6 flex-col items-start flex-1">
	      <text class="font text">{{ orderStatusObj.msg }}</text>
	      <text class="text_2 mt-3">{{ orderStatusObj.tips }}</text>
	    </view>
	  </view>
	  <view class="flex-row items-center section_3 pos_2">
	    <image
	      class="image_2"
	      :src="clothesRentUrl + '/clothesRentOrderDetail/lxr.png'"
	    />
	    <text class="font text_3 ml-9">{{ contactsSnapshot.name }} {{ contactsSnapshot.phone }}</text>
	  </view>
	  <view class="flex-col section_4 pos_3">
	    <view class="flex-row justify-between group">
	      <view class="flex-row">
	        <image
	          class="image_3" mode="aspectFill"
	          :src="publicPath + clothesSnapshot.image"
	        />
	        <view class="ml-12 flex-col items-start group_2">
	          <text class="font_2 text_4">{{ clothesSnapshot.name }}</text>
	          <text class="font_3 text_6 mt-59">￥{{ clothesSnapshot.price.toFixed(2) }}</text>
	        </view>
	      </view>
	      <view class="flex-col items-center self-start" @click="nav">
	        <image
	          class="image_2"
	          :src="clothesRentUrl + '/clothesRentOrderDetail/nav.png'"
	        />
	        <text class="font_2 text_5 mt-11">提货地址</text>
	      </view>
	    </view>
	    <view class="mt-18 flex-col">
	      <view class="flex-col group_3">
	        <view class="flex-row justify-between items-baseline">
	          <text class="font text_7">租赁天数</text>
	          <text class="font_3 text_8">X{{ clothesSnapshot.period }}</text>
	        </view>
	        <view class="mt-14 flex-row justify-end items-baseline">
	          <text class="font_2 text_9">实付款：</text>
	          <text class="text_10">￥{{ rentOrderDetail.totalAmount.toFixed(2) }}</text>
	        </view>
	      </view>
	      <view class="flex-col group_4">
	        <view class="flex-col" style="position: relative;">
	          <view class="flex-row justify-between items-baseline">
	            <text class="font">创建时间：</text>
	            <text class="font_4 text_11">{{ rentOrderDetail.createTime }}</text>
	          </view>
	          <view class="flex-row mt-9">
	            <text class="font text_12">订单编号：</text>
	            <view class="flex-row items-center flex-1 group_5" style="position: absolute; right: 0;">
	              <text class="font_4 text_13">{{ rentOrderDetail.orderNumber }}</text>
	              <view class="ml-4 flex-col justify-start items-center shrink-0 text-wrapper" @click="copy(rentOrderDetail.orderNumber)">
	                <text class="text_14">复制</text>
	              </view>
	            </view>
	          </view>
	        </view>
					
					<button  class="flex-row justify-center items-center mt-19" style="display: flex; align-items: center; justify-content: space-around; height: 60rpx; width: 175rpx;" open-type="contact" bindcontact="handleContact" session-from="sessionFrom">
						<image
						  class="image_4"
						  :src="clothesRentUrl + '/clothesRentOrderDetail/lxkf.png'"
						/>
						<text class="font_2 text_15">在线客服</text>
					</button>
	      </view>
	    </view>
	  </view>
	  <view class="flex-row justify-between items-center section_5 pos_4">
	    <view class="flex-row items-baseline">
	      <text class="font text_16">应付款：</text>
	      <text class="text_17">￥{{ rentOrderDetail.totalAmount.toFixed(2) }}</text>
	    </view>
	    <view class="flex-col justify-start items-center text-wrapper_2" v-if="rentOrderDetail.operationList[0]" @click="wxPayFd(rentOrderDetail)">
				<text class="font text_18">去支付</text>
			</view>
			<view class="flex-col justify-start items-center text-wrapper_2" v-if="rentOrderDetail.operationList[1]" @click="gotoDeleteOrder(rentOrderDetail.id)">
				<text class="font text_18">删除订单</text>
			</view>
			<view class="flex-col justify-start items-center text-wrapper_2" style="background-color: #fff;" v-if="rentOrderDetail.operationList[2]">
				<text class="font text_18"></text>
			</view>
	  </view>
	</view>

	<!-- 遮罩层 -->
	    <view v-if="isShow" class="overlay"></view>

</template>

<script setup>
	import {ref,onMounted, onUnmounted, onBeforeUnmount} from 'vue'
	import { onLoad } from "@dcloudio/uni-app";
	import { clothesRentUrl,bkgPubilcPath } from '../../../common/globalImagesUrl';
	import { clothesRentOrderMap } from '../../../common/global';
	import { baseUrl } from '../../../api/request';
	import { publicPath } from '../../../common/globalImagesUrl';
	import { onPullDownRefresh } from '@dcloudio/uni-app';
	import { getFonts } from '../../../common/globalFont';

	import emitter from '../../../utils/emitter';
	import { showTips } from '../../../common/globalFunction';
	const cookie = wx.getStorageSync('cookie')
	const rentOrderDetail = ref({})    //租赁订单详情
	const contactsSnapshot = ref({})   //联系人快照
	const clothesSnapshot = ref({})    //服装快照
	const orderStatusObj = ref({})   //订单状态----对应全局变量clothesRentOrderMap
	
	
	let wxPayTimer = null;
	let loading = false;
	let isLoading = ref(false)
	let isShow = ref(false)
	
	onMounted( async ()=>{
		getOrderDetail()
	})
	
	onPullDownRefresh( async ()=>{  //下拉刷新
		await getOrderDetail()
		setTimeout(()=>{
			uni.stopPullDownRefresh()   //停止下拉刷新
		},1000)
	}) 
	
	onLoad((options)=>{
		getOrderDetail(options.id)
		getFonts()
	})
	
	const getOrderDetail = async (val) => {
		const res = await uni.request({
			url: baseUrl + '/clothesRent/get/id',
			method: 'POST',
			header: {
				cookie
			},
			data: {
				id: val
			}
		})
		rentOrderDetail.value = res.data.data
		contactsSnapshot.value = res.data.data.contactsSnapshot
		clothesSnapshot.value = res.data.data.clothesSnapshot
		orderStatusObj.value = clothesRentOrderMap.get(res.data.data.orderStatus)
		if (rentOrderDetail.value.orderStatus === '待支付') {
			rentOrderDetail.value.operationList = [true, false, false]
		} else if (rentOrderDetail.value.orderStatus === '待发货' || rentOrderDetail.value.orderStatus === '待收货') {
			rentOrderDetail.value.operationList = [false, false, true]
		} else if (rentOrderDetail.value.orderStatus === '已退款') {
			rentOrderDetail.value.operationList = [false, false, true]
		} else if (rentOrderDetail.value.orderStatus === '交易成功') {
			rentOrderDetail.value.operationList = [false, false, true]
		} else if (rentOrderDetail.value.orderStatus === '交易关闭') {
			rentOrderDetail.value.operationList = [false, true, false]
		}
	}
	
	const gotoDeleteOrder = (val) => {   //删除订单跳转服装租赁订单列表
		uni.showModal({
			title: '提示',
			content: '您确定要删除订单吗？',
			success: async (res) => {
				if (res.confirm) {
					await deleteOrder(val)
					await emitter.emit('flushClothesOrderList')
					routerJump('pages/clothesRent/clotherRentOrderList/clotherRentOrderList')
					showTips('删除成功')
				}
			}
		})
	}
	
	
	
	const deleteOrder = async (val) => {
		const res = await uni.request({
			url: baseUrl + '/clothesRent/delete',
			method: 'POST',
			header: {
				cookie
			},
			data: {
				id: val
			}
		})
		// console.log(res.data.data)
	}
	
	const wxPayFd = (order) => {  //微信支付按钮防抖
		showLoading()  
		isShow.value = true  //打开遮罩
		wxPay(order.id)
	}
	
	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/clothesRent/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({
						title: '提示',
						content: '支付成功',
						showCancel: false
					})
					getOrderDetail(oid)
				},
				fail(e) {
					
				},
				complete () {
					hideLoading()
					isShow.value = false  //关闭遮罩
				}
			})
		}catch(error) {
			hideLoading()
			isShow.value = false  //关闭遮罩
			uni.showModal({
				title: '提示',
				content: '支付失败，请刷新后重试',
				showCancel: false
			})
		}
	}
	
	const copy = ( orderNumber ) => {   //复制到粘贴板
		// console.log('订单编号为--->',orderNumber);
		uni.setClipboardData({
			data: orderNumber,
			success: () => {
				uni.showToast({
					title: '成功复制到剪贴板'
				})
			}
		})
	}
	
	const nav = () => {  //导航
		wx.openLocation({
			latitude: 45.867741,
			longitude: 126.560037,
			name: '哈尔滨师范大学(松北校区)',
			address: '黑龙江省哈尔滨市呼兰区利民经济开发区师大路1号',
			success: (res) => {
			console.log(res)
			}
		})
	}
	
	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-pos {
	position: absolute;
	left: 0;
	right: 0;
	bottom: 20rpx;
	margin: 0 auto;
	
}

button {
	padding: 0;
	// margin: 0;
	background-color: #fff;
}
button::after{
	border: none; 
}

.mt-3 {
  margin-top: 5.63rpx;
}
.ml-9 {
  margin-left: 16.88rpx;
}
.mt-59 {
  margin-top: 110.63rpx;
}
.mt-11 {
  margin-top: 20.63rpx;
}
.mt-9 {
  margin-top: 16.88rpx;
}
.mt-19 {
  margin-top: 35.63rpx;
}
.page {
  background-color: #ffffff;
  overflow: hidden;
  background-size: 100% 100%;
  background-repeat: no-repeat;
  width: 100%;
  overflow-y: auto;
  overflow-x: hidden;
  height: 100vh;
}
.section_2 {
  padding: 16.88rpx 20.63rpx;
  background-color: #ffffff;
  border-radius: 18.75rpx;
}
.pos {
  position: absolute;
  left: 22.5rpx;
  right: 24.38rpx;
  top: 31.88rpx;
}
.image {
  width: 75rpx;
  height: 75rpx;
}
.font {
  font-size: 28.13rpx;
  font-family: FangZhengFonts;
  line-height: 26.47rpx;
  color: #818181;
}
.text {
  color: #323233;
  line-height: 27.58rpx;
}
.text_2 {
  color: #6b6e72;
  font-size: 24.38rpx;
  font-family: FangZhengFonts;
  line-height: 24rpx;
}
.section_3 {
  padding: 26.25rpx 30rpx;
  background-color: #ffffff;
  border-radius: 18.75rpx;
}
.pos_2 {
  position: absolute;
  left: 22.5rpx;
  right: 24.38rpx;
  top: 165rpx;
}
.image_2 {
  width: 46.88rpx;
  height: 46.88rpx;
}
.text_3 {
  font-size: 30rpx;
  line-height: 25.89rpx;
}
.section_4 {
  padding: 30rpx 11.25rpx 30rpx 16.88rpx;
  background-color: #ffffff;
  border-radius: 18.75rpx;
}
.pos_3 {
  position: absolute;
  left: 24.38rpx;
  right: 24.38rpx;
  top: 285rpx;
}
.group {
  padding: 0 15rpx;
}
.image_3 {
  border-radius: 9.38rpx;
  width: 148.13rpx;
  height: 165rpx;
}
.group_2 {
  margin-bottom: 3.75rpx;
}
.font_2 {
  font-size: 28.13rpx;
  font-family: FangZhengFonts;
  line-height: 26.47rpx;
  color: #323232;
}
.text_4 {
  font-size: 30rpx;
  line-height: 29.18rpx;
}
.font_3 {
  font-size: 28.13rpx;
  font-family: FangZhengFonts;
  line-height: 19.41rpx;
  color: #323232;
}
.text_6 {
  margin-left: 11.25rpx;
  font-size: 30rpx;
  line-height: 20.04rpx;
}
.text_5 {
  font-size: 26.25rpx;
  line-height: 25.22rpx;
}
.group_3 {
  padding-left: 15rpx;
  padding-bottom: 26.25rpx;
  border-bottom: solid 2.81rpx #dbdbdb;
}
.text_7 {
  line-height: 27.02rpx;
}
.text_8 {
  margin-right: 22.5rpx;
  line-height: 18.79rpx;
}
.text_9 {
  line-height: 26.7rpx;
}
.text_10 {
  color: #c35c5d;
  font-size: 33.75rpx;
  font-family: FangZhengFonts;
  line-height: 22.54rpx;
}
.group_4 {
  padding: 33.75rpx 15rpx 0;
}
.font_4 {
  font-size: 28.13rpx;
  font-family: Open Sans;
  line-height: 19.41rpx;
  color: #323232;
}
.text_11 {
  font-size: 26.25rpx;
}
.text_12 {
  line-height: 27.69rpx;
}
.group_5 {
  margin-right: 7.5rpx;
}
.text_13 {
  font-size: 26.25rpx;
  line-height: 19.29rpx;
}
.text-wrapper {
  background-color: #ffffff;
  width: 65.63rpx;
  height: 30rpx;
  border: solid 0.94rpx #d1d1dd;
}
.text_14 {
  color: #323232;
  font-size: 22.5rpx;
  font-family: Open Sans;
  line-height: 20.83rpx;
}
.image_4 {
  width: 45rpx;
  height: 45rpx;
}
.text_15 {
  font-size: 30rpx;
  line-height: 28.48rpx;
}
.section_5 {
  padding: 15rpx 15rpx 15rpx 22.5rpx;
  background-color: #ffffff;
}
.pos_4 {
	position: fixed;
	left: 0;
	right: 0;
	bottom: 0;
}
.text_16 {
  color: #323232;
  font-size: 30rpx;
  line-height: 27.43rpx;
}
.text_17 {
  color: #c35c5d;
  font-size: 33.75rpx;
  font-family: Open Sans;
  line-height: 24.96rpx;
}
.text-wrapper_2 {
  padding: 22.5rpx 0;
  background-color: #e79ea1;
  border-radius: 75rpx;
  width: 204.38rpx;
  height: 71.25rpx;
}
.text_18 {
  color: #ffffff;
  font-size: 30rpx;
  line-height: 27.77rpx;
}
@import url(../../../common/css/global.css);
</style>
 