<template>
<view class="flex-col justify-start relative page" :style="{ backgroundImage: 'url(' + bkgUrl + ')' }">
  <view class="flex-col group_1">
		<!-- 地址信息 -->
	<view @click="loadPop" :style="{ pointerEvents: isLoading ? 'none' : 'auto' }">
		<addressSelectedVue></addressSelectedVue>
	</view>
		<!-- 商品信息 -->
    <view class="flex-col section_3">
      <view class="flex-col self-stretch">
        <view :style="{marginBottom : index === productArr.length - 1 ? '10rpx' : '20rpx'}" class="flex-row items-end group_18" v-for="(item, index) in productArr" :key="item.id">
          <view class="flex-row items-end flex-1 group_19">
            <image
              class="shrink-0 image_3 image_4" mode="aspectFill"
              :src="publicPath + item.cartGoodVO.goodImg.split(';')[0]"
            />
            <view class="flex-col flex-1 ml-17">
              <view class="flex-row">
                <text class="font">{{ item.cartGoodVO.name }}</text>
                <text class="font text_1">【{{ item.cartGoodVO.type }}】</text>
              </view>
              <view class="flex-row items-center mt-52">
           <!--     <image
                  class="image_7"
                  :src="orderUrl + '/product-waitpay/money.png'"
                /> -->
                <text class="font_4 ml-2">¥{{ item.cartGoodVO.price.toFixed(2) }}</text>
                <!-- <text class="font_6 text_27 ml-2">00</text> -->
              </view>
            </view>
          </view>
          <view class="flex-row shrink-0 ml-21">
            <image
              class="image_5"
              :src="orderUrl + '/product-waitpay/sub.png'"
							@click="shortNum(index)"
            />
            <view class="flex-col justify-start items-center text-wrapper ml-2"><text class="font_5">{{ item.quantity }}</text></view>
            <image
              class="image_6 ml-2"
              :src="orderUrl + '/product-waitpay/add.png'"
							@click="addNum(index)"
            />
          </view>
        </view>
      </view>

    </view>
    <view class="flex-col section_5">
      <view class="flex-row justify-between items-center group_14">
        <view class="flex-row">
          <text class="font_7 text_14">商品总价</text>
          <text class="font_8 ml-7">共{{ productArr.length }}件商品</text>
        </view>
        <view class="flex-row items-center">
       <!--   <image
            class="shrink-0 image_9"
            src="https://carbon2.obs.cn-north-4.myhuaweicloud.com:443/feiyi%2Ftest%2F0%2FjIVFaWXA-rmb.png"
          /> -->
          <text class="font_9">¥{{ sumprice.toFixed(2) }}</text>
        </view>
      </view>
      <view class="flex-col mt-25">
        <view class="flex-row justify-between group_10"  @click="openCoupon" :style="{ pointerEvents: isLoading ? 'none' : 'auto' }">
          <text class="font_7">优惠券</text>
          <view class="flex-row">
            <text class="font_8" :style="{color: textColor}"> {{ templateString }}</text>
            <image
              class="shrink-0 image_7 ml-3"
              :src="orderUrl + '/product-waitpay/yjt2.png'"
            />
          </view>
        </view>
        <view class="flex-col mt-9">
          <view class="flex-row justify-between items-center group_12">
            <text class="font_7 text_18">合计</text>
            <view class="flex-row items-center">
            <!--  <image
                class="shrink-0 image_9"
                src="https://carbon2.obs.cn-north-4.myhuaweicloud.com:443/feiyi%2Ftest%2F0%2FjIVFaWXA-rmb.png"
              /> -->
              <text class="font_9">¥{{ sfAmount.toFixed(2) }}</text>
            </view>
          </view>
        </view>
      </view>
    </view>
    <view class="flex-col section_6">
      <text class="self-start font_7 text_19">订单备注</text>
      <textarea maxlength="250" placeholder-style="font-size: 25rpx;" v-model="note" placeholder="备注建议提前协商(250字以内)" class="flex-col justify-start items-start self-stretch text-wrapper_2 mt-11">
      </textarea>
    </view>
  </view>
  <view class="flex-row justify-between items-center section_7 pos_8">
    <view class="flex-row items-center">
      <text class="text_21">应付:</text>
      <text class="text_22">¥{{ sfAmount.toFixed(2) }}</text>
    </view>
    <view class="flex-col justify-start items-center text-wrapper_3" @click="wxPayFd" :style="{ pointerEvents: isLoading ? 'none' : 'auto' }"><text class="font_7 text_23">微信支付</text></view>
  </view>
</view>
	<uni-popup ref="popup" background-color="#fff">
				<view class="popup-content">
					<addressComponentVue></addressComponentVue>
				</view>
	</uni-popup>
	
	<uni-popup ref="coupon">
		<view class="coupon-popup">
			<couponPopupVue></couponPopupVue>
		</view>
	</uni-popup>
	 
	<!-- 遮罩层 -->
	    <view v-if="isShow" class="overlay"></view>
</template>

<script setup>
import {nextTick, onMounted, onUnmounted, ref, toRaw} from 'vue'
import  emitter  from '../../../utils/emitter'
import { onLoad , onShow } from  "@dcloudio/uni-app"; 
import { getFonts } from '../../../common/globalFont';
import { baseUrl } from '../../../api/request';
import addressComponentVue from '../component/addressComponent.vue'; //导入组件
import couponPopupVue from '../../coupon/component/couponPopup.vue';
import { stateMap } from '../../../common/global';
import * as math from 'mathjs'
import { dealResult } from '../../../common/globalFunction';
import addressSelectedVue from '../../Shopping-cart/component/addressSelected.vue';
import { orderUrl } from '../../../common/globalImagesUrl';
import { publicPath } from '../../../common/globalImagesUrl';
const bkgUrl = ref(orderUrl + '/product-waitpay/bkg.png')
//一些暂时变量
const sumprice = ref(0) //总价格
//11.4开发 不应该删掉的
const popup = ref(null) //弹窗对象
const addressRealInfo = ref({})
const productArr = ref([])  //商品对象
const labelList = ref([])  //老套路,商品标签
const userInfo = wx.getStorageSync('userInfo')  //用户信息
const orderItemList = ref({})
const totalInfo = ref([]) //购物车传过来的批量商品
const note = ref('')
const postCartArr = ref([])
const useCouponId = ref(null)
const sfAmount = ref(0)
const templateString = ref(null)
const myCouponList = ref([])
const couponObj = ref({})
const cookie = wx.getStorageSync("cookie")  //请求头
const textColor = ref('')
const coupon = ref(null)
const conditionAmount = ref(0)


const closeAddressHandler = () => {
	nextTick(() => {
		if (popup.value) {
			popup.value.close()
		}
	})
}

const addressInfoHandler = (val) => {
	addressRealInfo.value = val 
}


const delMyAddressHandler = (val) => {
	if (addressRealInfo.value.id === val) {
		addressRealInfo.value = {}
	}
}



const closeCouponHandler = () => {
	nextTick(() => {
		if (coupon.value) {
			coupon.value.close()
		}
	})
}

const cancelCouponHandler = () => {
	templateString.value = myCouponList.value.length + '张优惠券可用'
	sfAmount.value = sumprice.value 
	useCouponId.value = null
	textColor.value = '#818181'
}

const getCouponObjHandler = (val) => {
	couponObj.value = val
	templateString.value = "-¥" + val.couponVO.conditionAmount.toFixed(2)
	conditionAmount.value = val.couponVO.conditionAmount
	sfAmount.value = sumprice.value - conditionAmount.value
	useCouponId.value = val.id
	textColor.value = '#FFB6B9'
	console.log(val)
}

const updateAddressHandler = (val) => {
	if (addressRealInfo.value.id === val.id) {
		addressRealInfo.value = val
	}
}


onMounted(() => {
	getDefaultAddress()
	//将关闭弹窗方法传入弹窗页面,绑定弹窗按钮可关闭弹窗
	emitter.on('closeAddress', closeAddressHandler)
	//获取地址信息
	emitter.on('addressInfo', addressInfoHandler)
	
	emitter.on('closeCoupon', closeCouponHandler)
	
	emitter.on('cancelCoupon', cancelCouponHandler)
	
	emitter.on('getCouponObj', getCouponObjHandler)
	
	emitter.on('delMyAddress', delMyAddressHandler)
	
	emitter.on('updateAddress', updateAddressHandler)
	
})

onUnmounted(() => {
	//将关闭弹窗方法传入弹窗页面,绑定弹窗按钮可关闭弹窗
	emitter.off('closeAddress', closeAddressHandler)
	//获取地址信息
	emitter.off('addressInfo', addressInfoHandler)
	
	emitter.off('closeCoupon', closeCouponHandler)
	
	emitter.off('cancelCoupon', cancelCouponHandler)
	
	emitter.off('getCouponObj', getCouponObjHandler)
	
	emitter.off('delMyAddress', delMyAddressHandler)
	
	emitter.off('updateAddress', updateAddressHandler)
})


onLoad( async (options)=>{
	getFonts()
	totalInfo.value = JSON.parse(options.cartInfo)
	await getProduct()
	await getMyCouponList() 
})


//加载弹窗。默认从底部弹出
const loadPop =() =>{
	popup.value.open('bottom')
}
//获取用户默认地址
const getDefaultAddress = async () =>{
	const res = await uni.request({
		url: baseUrl + '/address/list',
		method: 'POST',
		header: {
			cookie: wx.getStorageSync('cookie')
		}
	})
	//根据for循环遍历默认地址
	let addArr = res.data.data
	for(let key in addArr) {
		if(addArr[key].isDefault === 1) {
			addressRealInfo.value = addArr[key]
		}
	}
	emitter.emit('getAddressInfo', addressRealInfo.value)
}
const createOrder = async () => {  //购物车的购买方法
	console.log('地址信息-->',addressRealInfo.value);
	formatArr()
	console.log('postCartArr--->',postCartArr.value);
	const res = await uni.request({   //向后端发送生成订单请求
		url: baseUrl + '/order/add/cart',
		method: 'POST',
		header: {
			cookie: wx.getStorageSync('cookie')
		},
		data: {
			orderType: productArr.value[0].cartGoodVO.type === '服务类' ? 'service' : 'product',
			userName: userInfo.userName,
			addressId: addressRealInfo.value.id, //地址信息id
			couponId: useCouponId.value, //优惠卷id
			note: note.value,
			cartOrderItemAddRequestList: toRaw(postCartArr.value)
		}
	})
	if (!dealResult(res)) {
		hideLoading()
		isShow.value = false
		return 
	} 
	wxPay(res.data.data)
}
//根据商品id数组获取商品信息
const getProduct = async ()=> {
	const res = await uni.request({
		url: baseUrl + '/cart/submit/list',
		method: 'POST',
		header: {
			cookie: wx.getStorageSync('cookie')
		},
		data: {
			idList: totalInfo.value
		}
	})
	console.log('展示订单页面的商品信息-->',res.data);
	if( res.data.code === 1 ) {
		dealRes(res)
	}
}

const dealRes =(res)=> {  //处理来自后端的数据
	productArr.value = res.data.data
	console.log('productArr--->',productArr.value);
	productArr.value.forEach((item)=>{
		sumprice.value += item.cartGoodVO.price * item.quantity  //计算总金额
	})
	sfAmount.value = sumprice.value
}
//减少当前商品数量
const shortNum = (index)=>{
	if(productArr.value[index].quantity > 1) {
		productArr.value[index].quantity -= 1
		//计算商品价格
		sumprice.value -= productArr.value[index].cartGoodVO.price * 1
		sfAmount.value = sumprice.value 
		useCouponId.value = null
		textColor.value = '#818181'
		fd()
	}
}
//增加当前商品数量
const addNum = (index)=>{
	 if( productArr.value[index].quantity < productArr.value[index].cartGoodVO.inventory ) { //数量肯定不能大于库存
		 productArr.value[index].quantity += 1
		 sumprice.value += productArr.value[index].cartGoodVO.price * 1  //计算商品价格
		 sfAmount.value = sumprice.value
		 useCouponId.value = null
		 textColor.value = '#818181'
		 fd()
	 }
}
const formatArr = () =>{
	postCartArr.value.splice(0,postCartArr.value.length)
	productArr.value.forEach((item,index)=>{
		postCartArr.value.push({
			cartRecordId: totalInfo.value[index],
			quantity: item.quantity
		})
	})
}
const wxPay = async( oid )=> {  //传入订单id
	console.log('oid--->',oid);
	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
				})
				uni.redirectTo({
					url: '/pages/my-order/myGeneralOrderDetail/myGeneralOrderDetail?id=' + oid
				})
				console.log('支付成功res--->',res);
			},
			fail(e) {
				uni.redirectTo({
					url: '/pages/my-order/myGeneralOrderDetail/myGeneralOrderDetail?id=' + oid
				})
				console.log('e.errMsg--->',e.errMsg);
			}
		})
	}catch(error) {
		console.error('支付请求失败',error);
		uni.showModal({
			content: '支付请求失败,请重试。',
			showCancel: false
		})
	}
}

//防抖相关变量
	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 = () => {  //微信支付按钮防抖
		console.log(addressRealInfo.value)
		if (addressRealInfo.value.name === null || addressRealInfo.value.name === '' || addressRealInfo.value.name === undefined) {
			uni.showToast({
				title: '请选择地址',
				icon: 'error'
			})
			return ;
		}
		clearTimeout(wxPayTimer)
		showLoading()
		isShow.value = true
		wxPayTimer = setTimeout(async () => {
			await createOrder()
		}, 1000)
	}
	function showLoading() {  //加载弹窗
	  if (!loading) {
	    wx.showLoading({
	      title: '加载中...',
	    });
	    loading = true;
		isLoading.value = true
	  }
	}
	function hideLoading() {  //关闭弹窗
	  if (loading) {
	    wx.hideLoading();
	    loading = false;
		isLoading.value = false
	  }
	}
	
	
	
	
	
	
	
	
	
	
	
	const openCoupon = () => {
		emitter.emit('getTotalPrice', sumprice.value)
		coupon.value.open('bottom')
	}
	
	
	
	const getMyCouponList = async () => {
		const res = await uni.request({
			url: baseUrl + '/coupon/list/use',
			method: 'POST',
			header: {
				cookie
			},
			data: {
				currentAmount: sumprice.value,
				isAvailable: true
			}
		})
		myCouponList.value = res.data.data
		templateString.value = myCouponList.value.length + '张优惠券可用'
		console.log('============================>', myCouponList.value)
	}
	
	
	
	
	
	
	
	
	
	
	
</script>

<style lang="scss" scoped>
	.my-text-wrap {
		width:480rpx; ;display:inline-block;white-space: pre-wrap; word-wrap: break-word;height: auto;
	}
	
	.coupon-popup {
		height: 1200rpx; 
		justify-content: center;
		background-color: #fff;
		border-radius: 20rpx 20rpx 0 0;
	}
	
	.overlay {
	  position: fixed;
	  top: 0;
	  left: 0;
	  width: 100%;
	  height: 100%;
	  background-color: rgba(0, 0, 0, 0.4);  /* 半透明黑色背景 */
	  z-index: 999;
	}

	
.popup-content { 
	height: 400px;
	align-items: center;
	justify-content: center;
	// background-color: #fff;
	border-radius: 20rpx 20rpx 0 0;
}



.ml-17 {
  margin-left: 31.88rpx;
}
.ml-21 {
  margin-left: 39.38rpx;
}
.ml-7 {
  margin-left: 13.13rpx;
}
.mt-25 {
  margin-top: 46.88rpx;
}
.ml-3 {
  margin-left: 5.63rpx;
}
.mt-9 {
  margin-top: 16.88rpx;
}
.mt-11 {
  margin-top: 20.63rpx;
}
.page {
  background-color: #fff;
  background-size: 100% 100%;
  background-repeat: no-repeat;
  width: 100%;
  overflow-y: auto;
  overflow-x: hidden;
  height: 100vh;
}
.group_1 {
  padding: 18.75rpx 15rpx 247.5rpx 16.88rpx;
}
.section_2 {
  padding: 21.07rpx 18.75rpx 18.51rpx 20.63rpx;
  background-color: #ffffff;
  border-radius: 18.75rpx;
}
.font {
  font-size: 26.25rpx;
  font-family: FangZhengFonts;
  line-height: 25.54rpx;
  color: #323232;
}
.text {
	margin-left: 54rpx;
  line-height: 25.74rpx;
}
.image {
  width: 43.13rpx;
  height: 43.13rpx;
}
.font_2 {
  font-size: 22.5rpx;
  font-family: FangZhengFonts;
  line-height: 22.54rpx;
  color: #818181;
}
.text_2 {
  line-height: 22.07rpx;
}
.group {
  margin-top: 36.54rpx;
}
.font_3 {
  font-size: 26.25rpx;
  font-family: FangZhengFonts;
  line-height: 25.54rpx;
  color: #ffaaa5;
}
.text_3 {
  line-height: 24.41rpx;
}
.image_2 {
  width: 24.38rpx;
  height: 24.38rpx;
}
.group_2 {
  padding: 0 59.23rpx;
}
.text_4 {
  color: #5a5a5a;
  line-height: 19.42rpx;
}
.text_5 {
	margin-left: 24rpx;
  line-height: 15.04rpx;
}
.section_3 {
  margin-top: 16.88rpx;
  padding: 22.5rpx 0 12.63rpx;
  background-color: #ffffff;
  border-radius: 18.75rpx;
}
.group_18 {
  padding-left: 26.27rpx;
  padding-right: 24.36rpx;
	margin-bottom: 20rpx;
}
.group_19 {
  margin-bottom: 3.75rpx;
  display: flex;
  align-items: center;
}
.image_3 {
  border-radius: 9.38rpx;
  width: 146.25rpx;
  height: 165rpx;
}
.image_4 {
  margin-bottom: 9.38rpx;
}
.text_1 {
  line-height: 25.01rpx;
}
.image_7 {
  width: 30rpx;
  height: 30rpx;
}
.font_4 {
  font-size: 33.75rpx;
  font-family: FangZhengFonts;
  line-height: 22.54rpx;
  color: #323232;
}
.font_6 {
  font-size: 26.25rpx;
  font-family: FangZhengFonts;
  line-height: 17.23rpx;
  color: #323232;
}
.text_27 {
  line-height: 17.53rpx;
}
.image_5 {
  border-radius: 9.38rpx 0rpx 0rpx 9.38rpx;
  width: 45rpx;
  height: 45rpx;
}
.text-wrapper {
  padding: 16.31rpx 0 16.46rpx;
  background-color: #ffaaa5;
  width: 58.13rpx;
  height: 45rpx;
}
.font_5 {
  font-size: 26.25rpx;
  font-family: FangZhengFonts;
  line-height: 17.23rpx;
  color: #ffffff;
}
.image_6 {
  border-radius: 0rpx 9.38rpx 9.38rpx 0rpx;
  width: 45rpx;
  height: 45rpx;
}
.group_9 {
  line-height: 21.71rpx;
  height: 21.71rpx;
}
.text_12 {
  line-height: 21.71rpx;
}
.text_13 {
  color: #ed4845;
  line-height: 21rpx;
}
.image_8 {
  width: 26.25rpx;
  height: 26.25rpx;
}
.section_5 {
  margin-top: 16.88rpx;
  padding: 31.88rpx 13.13rpx 5.63rpx;
  background-color: #ffffff;
  border-radius: 18.75rpx;
}
.group_14 {
  padding: 0 24.02rpx;
}
.font_7 {
  font-size: 30rpx;
  font-family: FangZhengFonts;
  line-height: 29.18rpx;
  color: #323232;
}
.text_14 {
  line-height: 28.13rpx;
}
.font_8 {
  font-size: 30rpx;
  font-family: FangZhengFonts;
  line-height: 29.18rpx;
  color: #818181;
}
.image_9 {
  width: 33.75rpx;
  height: 33.75rpx;
}
.font_9 {
  font-size: 30rpx;
  font-family: FangZhengFonts;
  line-height: 20.04rpx;
  color: #323232;
}
.group_10 {
  padding: 25rpx 3.75rpx 25rpx 22.97rpx;
}
.group_11 {
  padding: 26.25rpx 10.42rpx 28.13rpx 24.62rpx;
  border-bottom: solid 1.88rpx #dfdfdf;
}
.text_15 {
  line-height: 29.42rpx;
}
.image_10 {
  margin-left: 21.6rpx;
}
.text_16 {
  margin-left: -55.35rpx;
  line-height: 25.74rpx;
}
.text_17 {
  margin-left: 33.68rpx;
  color: #ffaaa5;
}
.group_12 {
  padding: 26.25rpx 11.94rpx 28.13rpx 23.44rpx;
}
.text_18 {
  line-height: 27.77rpx;
}
.section_6 {
  margin-top: 16.88rpx;
  padding: 17.81rpx 24.13rpx 22.5rpx;
  background-color: #ffffff;
  border-radius: 18.75rpx;
}
.text_19 {
  color: #323232;
  line-height: 29.53rpx;
}
.text-wrapper_2 {
	 height: 200rpx;
	 width: 650rpx;
	 padding: 22.35rpx;
	 box-sizing: border-box;
	 background-color: #ffefef;
	 border-radius: 9.38rpx;
	 margin: 20rpx auto 0;
}
.text_20 {
  margin-left: 21.15rpx;
  line-height: 21.81rpx;
}
.section_7 {
  padding: 16.88rpx 25.31rpx 15rpx;
  background-color: #ffffff;
}
.pos_8 {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
}
.image_11 {
  margin-left: 85.31rpx;
}
.text_21 {
  // margin-left: -119.06rpx;
  color: #000000;
  font-size: 30rpx;
  font-family: FangZhengFonts;
  line-height: 26.49rpx;
}
.text_22 {
  color: #ffaaa5;
  font-size: 37.5rpx;
  font-family: FangZhengFonts;
  font-weight: 700;
  line-height: 27.62rpx;
}
.text_24 {
  color: #ffaaa5;
  font-size: 30rpx;
  font-family: FangZhengFonts;
  font-weight: 700;
  line-height: 22.05rpx;
}
.text-wrapper_3 {
  padding: 20.51rpx 0 22.97rpx;
  background-color: #ffaaa5;
  border-radius: 75rpx;
  width: 204.38rpx;
  height: 71.25rpx;
}
.text_23 {
  color: #ffffff;
  line-height: 27.77rpx;
}
@import url(../../../common/css/global.css);
</style>