<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 justify-between items-center section_3">
	    <view class="flex-col group">
	      <text class="self-start font text_3">{{ addressInfo.name }} {{ addressInfo.phone }}</text>
	      <view class="flex-row items-center self-stretch">
	        <image
	          class="image_2"
	          src="https://carbon2.obs.cn-north-4.myhuaweicloud.com:443/feiyi%2Ftest%2F0%2FXZHjzkRH-dingwei.png"
	        />
	        <text class="font_2 text_4 ml-9">{{ addressInfo.region }}</text>
	      </view>
	      <text class="self-end font_2 text_6">{{ addressInfo.detailAddress }}</text>
	    </view>
	  </view>
	  <view class="flex-col section_4">
	    <view class="flex-col">
	      <view class="flex-row justify-between goodsBox" v-for="(item, index) in productList" :key="index">
	        <view class="flex-row">
	          <image
	            class="image_4"
	            :src="item.goodSnapshot.goodImg.split(';')[0]"
	          />
	          <view class="flex-col group_2 ml-17">
	            <view class="flex-row">
	              <text class="font">{{ item.goodSnapshot.name }}</text>
	              <text class="font text_11">【{{ item.goodSnapshot.type }}】</text>
	            </view>
	            <view class="flex-row items-center mt-53">
	              <image
	                class="image_5"
	                src="https://ide.code.fun/api/image?token=6764d712797f850011f38d7b&name=2a1f46d015a316fa98edfbdad0f9921f.png"
	              />
	              <text class="font_3">{{ item.goodSnapshot.price }}</text>
	              <!-- <text class="font_4">00</text> -->
	            </view>
	          </view>
	        </view>
	        <text class="self-start font_5 text_8 text_1">×{{ item.quantity }}</text>
	      </view>
	    </view>
	    <view class="flex-col group_7 mt-6">
	      <text class="self-start font_6 text_18">买家留言</text>
	      <view class="flex-col justify-start self-stretch relative group_8 mt-5">
	        <view class="group_9"></view>
	        <text class="font_6 text_19 pos_4">
	          {{ orderObject.note }}
	        </text>
	      </view>
	    </view>
	    <view class="flex-row justify-between items-center mt-6">
	      <text class="font_6 text_20">订单编号:</text>
	      <view class="flex-row items-center group_11">
	        <view class="flex-col justify-start items-center text-wrapper view" @click="copy(orderObject.orderNumber)">
	          <text class="font_7 text_22">复制</text>
	        </view>
	        <text class="font_4 text_21">{{ orderObject.orderNumber }}</text>
	      </view>
	    </view>
	    <view class="flex-row justify-between items-center mt-6">
	      <text class="font_6 text_23">创建时间:</text>
	      <text class="font_4 text_24">{{ orderObject.createTime }}</text>
	    </view>
	    <view class="flex-row justify-between items-baseline group_1 mt-6" 
				v-if="stateObject.state === '已退款'">
	      <text class="font_6 text_25">退款时间:</text>
	      <text class="font_4 text_26">{{ orderObject.updateTime }}</text>
	    </view>
	    <view class="flex-row justify-between group_16 mt-6" 
				v-if="stateObject.state === '待收货'">
	      <text class="font_6 text_27">物流单号:</text>
	      <view class="flex-row items-center group_14">
	        <text class="font_4">{{ orderObject.trackingNumber }}</text>
	        <!-- <view class="flex-col justify-start items-center shrink-0 text-wrapper ml-5">
	          <text class="font_7 text_22">复制</text>
	        </view> -->
	      </view>
	    </view>
	    <view class="flex-row justify-between items-baseline mt-6" 
				v-if="stateObject.state === '交易成功'" >
	      <text class="font_6 text_28">成交时间:</text>
	      <text class="font_4 text_29">{{ orderObject.updateTime }}</text>
	    </view>
	  </view>
	  <view class="flex-row justify-between items-center section_5"
			v-if="stateObject.state != '交易成功'">
	    <view class="flex-row items-center">
	      <text class="font_8 text_30">合计:</text>
	      <image
	        class="shrink-0 image_8"
	        src="https://ide.code.fun/api/image?token=6764d712797f850011f38d7b&name=549b29459342a9a09d24834be40eb53b.png"
	      />
	      <text class="text_31">¥{{ orderObject.totalAmount }}</text>
	      <!-- <text class="text_33">00</text> -->
	    </view>
	    <view class="flex-col justify-start items-center text-wrapper_2" @click="stateSubmit(orderObject)" v-if="bottomBtn != '联系客服'">
	    	<text class="font_8 text_32" >{{ bottomBtn }}</text>
	    </view>
			<view class="flex-col justify-start items-center text-wrapper_2" @click="confirm()" v-if="bottomBtn === '查看物流'">
				<text class="font_8 text_32" >确认收货</text>
			</view>
			<button class="text-wrapper_3" v-if="bottomBtn === '联系客服'" open-type="contact" bindcontact="handleContact" session-from="sessionFrom">
				<text class="font_8 text_32">联系客服</text>
			</button>
	  </view>
	</view>
	<!-- 遮罩层 -->
	    <view v-if="isShow" class="overlay"></view>
</template>

<script setup>
import { ref } from 'vue';
import { onLoad , onShow } from "@dcloudio/uni-app"; 
import { stateMap } from '../../../common/global';
import { baseUrl } from '../../../api/request';
const orderObject = ref({})  //后端传来的订单对象
const addressInfo = ref({}) //地址信息
const productList = ref([])  //订单涵盖的商品
const stateObject = ref({})  //当前订单状态
const bottomBtn = ref('去支付')  //底下按钮
const oid = ref(0) 
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,
		addressInfo.value = res.data.data.addressSnapshot,
		productList.value = res.data.data.orderItemList
		stateObject.value = stateMap.get(res.data.data.orderStatus)
	}
	console.log('地址信息--->',addressInfo.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;
		case '已退款': bottomBtn.value = '删除订单';break;
		case '交易成功': bottomBtn.value = '删除订单';break;
		default: bottomBtn.value = '删除订单'
	}
}
const stateSubmit = (obj) => {  //按钮状态改变的方法
	// console.log('商品obj--->',obj);
	if(bottomBtn.value === '立即支付') {   //重新支付
		wxPayFd(oid.value)
	} else if(bottomBtn.value === '删除订单') {
		console.log('订单被删除');
	} else if(bottomBtn.value === '查看物流') {
		viewLogistics(obj.id)
	}
}
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 viewLogistics = ( oid )=>{ 
	// console.log('订单编号--->',oid);
	uni.request({
		url: baseUrl + '/logistics/get/info',
		method: 'POST',
		data: {
			id: oid
		},
		header: {
			'cookie': wx.getStorageSync("cookie")
		},
		success: (res) => {
			console.log(res.data.data)
			waybillToken = res.data.data.waybill_token
			plugin.openWaybillTracking({
				waybillToken: waybillToken
			})
		}
	})
}
//复制到剪切板方法
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 fd = () => {  //防抖
		clearTimeout(debounceTimer)
		showLoading()
		debounceTimer = setTimeout(async () => {
			await getMyCouponList()
			hideLoading()
		}, 1000)
	}
	const wxPayFd = (oid) => {  //微信支付按钮防抖
		clearTimeout(wxPayTimer)
		isShow.value = true
		showLoading()
		wxPayTimer = setTimeout(async () => {
			await wxPay(oid)
			hideLoading()
		}, 1000)
	}
	function showLoading() {  //加载弹窗
	  if (!loading) {
	    wx.showLoading({
	      title: '加载中...',
	    });
	    loading = true;
		isLoading.value = true
	  }
	}
	function hideLoading() {  //关闭弹窗
	  if (loading) {
	    wx.hideLoading();
	    loading = false;
		isLoading.value = false
		isShow.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;
	}
.goodsBox {
	margin-bottom: 20rpx;
}
.ml-9 {
  margin-left: 16.88rpx;
}
.mt-53 {
  margin-top: 99.38rpx;
}
.ml-17 {
  margin-left: 31.88rpx;
}
.mt-5 {
  margin-top: 9.38rpx;
}
.ml-5 {
  margin-left: 9.38rpx;
}
.page {
  padding-top: 17.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: #323232;
}
.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: 15rpx 15rpx 0 16.88rpx;
  padding: 24.81rpx 11.25rpx 25.26rpx 20.68rpx;
  background-color: #ffffff;
  border-radius: 18.75rpx;
}
.group {
  width: 410.31rpx;
  height: 84.94rpx;
}
.text_3 {
  margin-left: 59.89rpx;
  line-height: 22.67rpx;
}
.image_2 {
  width: 43.13rpx;
  height: 43.13rpx;
}
.font_2 {
  font-size: 26.25rpx;
  font-family: FZSongKeBenXiuKaiS-R-GB;
  line-height: 30rpx;
  color: #818181;
}
.text_4 {
  font-size: 24.38rpx;
}
.text_6 {
  margin-top: -10.26rpx;
  font-size: 24.38rpx;
}
.text_5 {
  color: #ffaaa5;
  line-height: 24.41rpx;
}
.image_3 {
  width: 24.38rpx;
  height: 24.38rpx;
}
.section_4 {
  margin: 15rpx 15rpx 110rpx 16.88rpx;
  padding: 22.5rpx 22.63rpx 24.79rpx 24.38rpx;
  background-color: #ffffff;
  border-radius: 18.75rpx;
}
.image_4 {
  border-radius: 9.38rpx;
  width: 146.25rpx;
  height: 165rpx;
}
.group_2 {
  margin-top: 4.2rpx;
  margin-bottom: 5.63rpx;
}
.text_11 {
  line-height: 25.01rpx;
}
.image_5 {
  width: 30rpx;
  height: 30rpx;
}
.font_3 {
  font-size: 33.75rpx;
  font-family: FZSongKeBenXiuKaiS-R-GB;
  line-height: 22.54rpx;
  color: #323232;
}
.font_4 {
  font-size: 26.25rpx;
  font-family: FZSongKeBenXiuKaiS-R-GB;
  line-height: 17.53rpx;
  color: #323232;
}
.font_5 {
  font-size: 26.25rpx;
  font-family: Open Sans;
  line-height: 17.53rpx;
  color: #323232;
}
.text_8 {
  line-height: 18.73rpx;
}
.text_1 {
  margin-right: 7.58rpx;
  margin-top: 133.14rpx;
}
.font_6 {
  font-size: 26.25rpx;
  font-family: FZSongKeBenXiuKaiS-R-GB;
  line-height: 25.54rpx;
  color: #818181;
}
.group_7 {
  padding: 29.19rpx 0 16.88rpx;
  border-top: solid 1.88rpx #dfdfdf;
  border-bottom: solid 1.88rpx #dfdfdf;
}
.text_18 {
  margin-left: 2.36rpx;
  line-height: 24.71rpx;
}
.group_8 {
  margin-right: 3.62rpx;
  padding-top: 9.86rpx;
}
.group_9 {
  border-radius: 9.38rpx;
  height: 84.38rpx;
  border: solid 1.88rpx #ffffff;
}
.text_19 {
  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%);
}
.text_20 {
  line-height: 25.84rpx;
}
.group_11 {
  margin-right: 3.62rpx;
  width: 368.34rpx;
}
.text-wrapper {
  padding-bottom: 2.29rpx;
  background-color: #ffffff;
  width: 50.63rpx;
  height: 22.5rpx;
  border: solid 0.94rpx #d1d1d1;
}
.view {
  margin-left: 317.72rpx;
}
.font_7 {
  font-size: 18.75rpx;
  font-family: Open Sans;
  line-height: 17.53rpx;
  color: #323232;
}
.text_22 {
  line-height: 17.38rpx;
}
.text_21 {
  margin-left: -368.34rpx;
}
.text_23 {
  line-height: 24.71rpx;
}
.text_24 {
  margin-right: 26.38rpx;
}
.group_1 {
  padding: 0 2.38rpx;
}
.text_25 {
  line-height: 24.51rpx;
}
.text_26 {
  margin-right: 137.49rpx;
}
.group_16 {
  padding: 0 3.32rpx;
}
.text_27 {
  line-height: 25.84rpx;
}
.group_14 {
  margin-right: 28.43rpx;
}
.text_28 {
  line-height: 24.51rpx;
}
.text_29 {
  margin-right: 139.88rpx;
}
.section_5 {
	position: fixed;
	left: 0;
	right: 0;
	bottom: 0;
  // margin-top: 78.75rpx;
  padding: 16.88rpx 25.31rpx 15rpx;
  background-color: #ffffff;
}
.font_8 {
  font-size: 30rpx;
  font-family: FZSongKeBenXiuKaiS-R-GB;
  line-height: 27.77rpx;
}
.text_30 {
  color: #000000;
}
.image_8 {
  width: 33.75rpx;
  height: 33.75rpx;
}
.text_31 {
  color: #ffaaa5;
  font-size: 37.5rpx;
  font-family: Open Sans;
  font-weight: 700;
  line-height: 27.62rpx;
}
.text_33 {
  color: #ffaaa5;
  font-size: 30rpx;
  font-family: Open Sans;
  font-weight: 700;
  line-height: 22.05rpx;
}
.text-wrapper_2 {
  padding: 20.63rpx 0 22.86rpx;
  background-color: #ffaaa5;
  border-radius: 75rpx;
  width: 204.38rpx;
  height: 71.25rpx;
}
.text_32 {
  color: #ffffff;
}
.text-wrapper_3 {
	position: relative;
	left: 140rpx;
  background-color: #ffaaa5;
  border-radius: 75rpx;
  width: 204rpx;
  // height: 71rpx;
	font-size: unset; 
}
@import url(../../../common/css/global.css);
</style>