<template>
	<view class="flex-col justify-start relative page" :style="{ backgroundImage: 'url(' + bkgUrl + ')'}">
	  <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="text_2 mt-3">{{ orderStatusObj.tips }}</text>
	    </view>
	  </view>
	  <view class="flex-row items-center section_3 pos_2">
	    <image
	      class="image_2"
	      :src="bookUrl + '/myPhotoProductsOrderDetail/lxr.png'"
	    />
	    <text class="font_2 text_3 ml-9">{{ order.contactsSnapshot.name }} {{ order.contactsSnapshot.phone }}</text>
	  </view>
	  <view class="flex-row section_4 pos_3">
	    <view class="flex-row items-center group pos_13">
	      <image
	        class="image_3"
			:src="publicPath + order.photoProductsSnapshot.introImg"
	      />
	      <view class="flex-col items-start flex-1 ml-12">
	        <text class="font_3 text_4">{{ order.photoProductsSnapshot.name }}</text>
	        <text class="font_3 text_5 mt-59">¥{{ order.photoProductsSnapshot.price.toFixed(2) }}</text>
	      </view>
	    </view>
	    <view class="flex-col section_5 pos_1">
	      <text class="self-start font text_6">预约门店:哈尔滨师范大学</text>
	      <view class="flex-row justify-between self-stretch mt-8">
	        <view class="flex-col items-start">
	          <text class="font text_7">到店时间:{{ order.specificDate }} {{ order.timePoint }}</text>
	          <text class="font mt-9">妆造服务:{{ order.isMakeup === 1 ? '本店上妆' : '无需妆造' }}</text>
	        </view>
	        <view @click="nav" class="flex-col items-center group_2">
	          <image
	            class="image_4" 
	            :src="bookUrl + '/myPhotoProductsOrderDetail/nav.png'"
	          />
	          <text class="text_8 mt-3">导航</text>
	        </view>
	      </view>
	      <text class="self-start font text_9 mt-8">拍摄场地:{{ order.isIndoors === 1 ? '室内' : '室外' }}</text>
	    </view>
	    <view class="flex-row justify-end items-center group_3 pos_6">
	      <text class="font_3 text_10">实付款:</text>
	      <text class="text_11">¥{{ order.photoProductsSnapshot.price.toFixed(2) }}</text>
	    </view>
	    <view class="flex-row justify-between items-baseline view pos_7">
	      <text class="font_2 text_12">创建时间:</text>
	      <text class="font_4 text_13">{{ order.createTime }}</text>
	    </view>
	    <text class="font_2 text_14 pos_8">订单编号:</text>
	    <view class="flex-row items-center pos_9">
	      <text class="font_4 text_15">{{ order.orderNumber }}</text>
	      <view class="flex-col justify-start items-center shrink-0 text-wrapper ml-4" @click="copy(order.orderNumber)">
	        <text class="text_16">复制</text>
	      </view>
	    </view>
		
		<button class="button-pos" 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="bookUrl + '/myPhotoProductsOrderDetail/lxkf.png'"
			/>
			<text class="font_3 text_17">在线客服</text>
		</button>
			
	  </view>
	  <view class="flex-row justify-between items-center section_6 pos_5">
	    <view class="flex-row">
	      <text class="font text_18">应付款:</text>
	      <text class="text_19">¥{{ order.photoProductsSnapshot.price.toFixed(2) }}</text>
	    </view>
		
	    <view v-if="order.operationList[0]" class="flex-col justify-start items-center text-wrapper_2" @click="wxPayFd(order)">
			<text class="font text_20">去支付</text>
		</view>
		
		<view v-if="order.operationList[1]" class="flex-col justify-start items-center text-wrapper_2" @click="gotoDeleteOrder(order.id)">
			<text class="font text_20">删除订单</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_24"></text>
		</view>
		
	  </view>
	</view>
	
	<!-- 遮罩层 -->
	    <view v-if="isShow" class="overlay"></view>
</template>

<script setup>
	import {onMounted, ref} from 'vue'
	import { bookUrl } from '../../../common/globalImagesUrl';
	import { onLoad } from "@dcloudio/uni-app";
	import { baseUrl } from '../../../api/request';
	import { publicPath } from '../../../common/globalImagesUrl';
	import { getFonts } from '../../../common/globalFont';
	import { onPullDownRefresh } from '@dcloudio/uni-app';
	import { photoOrderMap } from '../../../common/global';
	import emitter from '../../../utils/emitter';
	
	const bkgUrl = ref(bookUrl + '/myPhotoProductsOrderDetail/bkg.png')
	const id = ref(0)
	const cookie = wx.getStorageSync('cookie')
	const order = ref({})
	const orderStatusObj = ref({})   //根据全局变量获取状态、图片 --- photoOrderMap
	
	let wxPayTimer = null;
	let loading = false;
	let isLoading = ref(false)
	let isShow = ref(false)

	const flushOrderListHandler = () => {
		getMyOrder()
	}
	
	onLoad((options) => {
		getFonts()
		id.value = options.id
	})
	
	onMounted(() => {
		getAdvanceOrderById(id.value)
	})
	
	onPullDownRefresh( async ()=>{  //下拉刷新
		await getAdvanceOrderById(id.value)
		setTimeout(()=>{
			uni.stopPullDownRefresh()   //停止下拉刷新
		},1000)
	}) 
	
	
	
	const getAdvanceOrderById = async (val) => {
		const res = await uni.request({
			url: baseUrl + '/advanceOrder/get/id',
			method: 'POST',
			header: {
				cookie
			},
			data: {
				id: val
			}
		})
		order.value = res.data.data
		orderStatusObj.value = photoOrderMap.get(res.data.data.orderStatus)
		console.log('orderStatusObj--->',orderStatusObj.value);
		if (order.value.orderStatus === '待支付') {
			order.value.operationList = [true, false, false]
		} else if (order.value.orderStatus === '待发货') {
			order.value.operationList = [false, false, true]
		} else if (order.value.orderStatus === '已退款') {
			order.value.operationList = [false, false, true] 
		} else if (order.value.orderStatus === '交易成功') {
			order.value.operationList = [false, false, true] 
		} else if (order.value.orderStatus === '交易关闭') {
			order.value.operationList = [false, true, false]
		}
	}
	
	
	
	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/photo/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
					})
					getAdvanceOrderById(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('flushAdvanceOrderList')
					routerJump('pages/book/photoProductsOrderDetail/photoProductsOrderDetail')
				}
			}
		})
	}
	
	
	const deleteOrder = async (val) => {
		const res = await uni.request({
			url: baseUrl + '/advanceOrder/delete',
			method: 'POST',
			header: {
				cookie
			},
			data: {
				id: val
			}
		})
		console.log(res.data.data)
	}
	
	
	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 {
	padding: 0;
	margin: 0;
	background-color: #fff;
}
button::after{
	border: none; 
}

.button-pos {
	position: absolute;
	left: 0;
	right: 0;
	bottom: 20rpx;
	margin: 0 auto;
	
}
.ml-9 {
  margin-left: 16.88rpx;
}
.mt-59 {
  margin-top: 110.63rpx;
}
.mt-9 {
  margin-top: 16.88rpx;
}
.mt-3 {
  margin-top: 5.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.92rpx;
  color: #000000;
}
.text {
  color: #323233;
  line-height: 27.58rpx;
}
.text_2 {
  color: #6b6e72;
  font-size: 24.38rpx;
  font-family: FangZhengFonts;
  line-height: 24rpx;
}
.section_3 {
  padding: 24.38rpx 33.75rpx 26.25rpx;
  background-color: #ffffff;
  border-radius: 18.75rpx;
}
.pos_2 {
  margin: 165rpx 24.38rpx 0 22.5rpx;
}
.image_2 {
  width: 46.88rpx;
  height: 46.88rpx;
}
.font_2 {
  font-size: 28.13rpx;
  font-family: FangZhengFonts;
  line-height: 26.92rpx;
  color: #818181;
}
.text_3 {
  font-size: 30rpx;
  line-height: 25.89rpx;
}
.section_4 {
  padding: 0 11.25rpx 31.88rpx 16.88rpx;
  background-color: #ffffff;
  border-radius: 18.75rpx;
  height: 703.13rpx;
}
.pos_3 {
  position: relative;
  margin: 30rpx 24.38rpx 0 22.5rpx;
}
.group {
  padding: 30rpx 15rpx 24.38rpx;
}
.pos_13 {
  position: absolute;
  left: 16.88rpx;
  right: 11.27rpx;
  top: 0;
}
.image_3 {
  border-radius: 9.38rpx;
  width: 148.13rpx;
  height: 165rpx;
}
.font_3 {
  font-size: 28.13rpx;
  font-family: FangZhengFonts;
  line-height: 26.92rpx;
  color: #323232;
}
.text_4 {
  font-size: 30rpx;
  line-height: 29.18rpx;
}
.text_5 {
  margin-left: 9.73rpx;
  font-size: 30rpx;
  line-height: 20.04rpx;
}
.section_5 {
  padding: 17.85rpx 19.95rpx 21.53rpx 19.97rpx;
  background-color: #f4f4f4;
  border-radius: 9.38rpx;
}
.pos_1 {
  position: absolute;
  left: 30rpx;
  right: 30rpx;
  top: 219.38rpx;
}
.text_6 {
  line-height: 27.13rpx;
}
.text_7 {
  line-height: 26.25rpx;
}
.group_2 {
  margin-right: 7.44rpx;
}
.image_4 {
  width: 45rpx;
  height: 45rpx;
}
.text_8 {
  color: #000000;
  font-size: 22.5rpx;
  font-family: FangZhengFonts;
  line-height: 21.09rpx;
}
.text_9 {
  line-height: 25.93rpx;
}
.group_3 {
  padding: 25.58rpx 0 23.66rpx;
  border-bottom: solid 2.81rpx #dbdbdb;
}
.pos_6 {
  position: absolute;
  left: 16.88rpx;
  right: 23.27rpx;
  top: 414.38rpx;
}
.text_10 {
  line-height: 26.7rpx;
}
.text_11 {
  color: #c35c5d;
  font-size: 33.75rpx;
  font-family: FangZhengFonts;
  line-height: 26.7rpx;
}
.view {
  padding-left: 15.22rpx;
  padding-right: 13.73rpx;
}
.pos_7 {
  position: absolute;
  left: 16.88rpx;
  right: 11.27rpx;
  top: 526.2rpx;
}
.text_12 {
  line-height: 26.47rpx;
}
.font_4 {
  font-size: 28.13rpx;
  font-family: Open Sans;
  line-height: 20.83rpx;
  color: #323232;
}
.text_13 {
  font-size: 26.25rpx;
  line-height: 19.41rpx;
}
.text_14 {
  line-height: 27.69rpx;
}
.pos_8 {
  position: absolute;
  left: 33.41rpx;
  bottom: 104.79rpx;
}
.pos_9 {
  position: absolute;
  right: 23rpx;
  top: 570rpx;
}
.text_15 {
  font-size: 26.25rpx;
  line-height: 19.29rpx;
}
.text-wrapper {
  padding: 3.43rpx 0 3.86rpx;
  background-color: #ffffff;
  width: 65.63rpx;
  height: 30rpx;
  border: solid 0.94rpx #d1d1d1;
}
.text_16 {
  color: #323232;
  font-size: 22.5rpx;
  font-family: Open Sans;
  line-height: 20.83rpx;
}
.text_17 {
  font-size: 30rpx;
  line-height: 28.48rpx;
}
.pos_11 {
  position: absolute;
  right: 269.76rpx;
  bottom: 31.88rpx;
}
.pos_4 {
  position: absolute;
  left: 266.25rpx;
  bottom: 22.5rpx;
}
.section_6 {
  padding: 16.88rpx 15rpx 15rpx 21.56rpx;
  background-color: #ffffff;
}
.pos_5 {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
}
.text_18 {
  font-size: 30rpx;
  line-height: 27.43rpx;
}
.text_19 {
  color: #c35c5d;
  font-size: 33.75rpx;
  font-family: Open Sans;
  line-height: 24.96rpx;
}
.text-wrapper_2 {
  padding: 20.63rpx 0 22.86rpx;
  background-color: #e79ea1;
  border-radius: 75rpx;
  width: 204.38rpx;
  height: 71.25rpx;
}
.text_20 {
  color: #ffffff;
  font-size: 30rpx;
  line-height: 27.77rpx;
}
@import url(../../../common/css/global.css);
</style>