<template>
	<view class="container details clearfix" :style="{ paddingBottom: isIphoneX ? 130 + 68 + 'rpx' : '130rpx' }">
		<!-- 自定义返回 -->
		<header-back
			:systeminfo="systeminfo"
			:navbar-data="nvabarData"
			:headerBtnPosi="headerBtnPosi"
			:isShare="isOrderShare"
		></header-back>
		<tui-skeleton
			v-if="!skeletonShow"
			backgroundColor="#fafafa"
			borderRadius="10rpx"
			:isLoading="true"
			:loadingType="5"
		></tui-skeleton>
		<view v-else class="container-details" :style="{ paddingTop: navbarHeight + 'px' }">
			<!-- 地址选择 -->
			<order-address ref="orderAddress" v-if="skeletonShow" :addressData="addressData"></order-address>
			<!-- 商品 -->
			<goods-list
				ref="goods"
				v-if="skeletonShow"
				:shopOrderData="shopOrderData"
				:information="information"
			></goods-list>
			<!-- 订单信息 -->
			<order-information ref="information" v-if="skeletonShow" :information="information"></order-information>
			<!-- 发票信息 -->
			<invoice-tent ref="invoice" v-if="skeletonShow" :orderInvoice="orderInvoice"></invoice-tent>
			<!-- 支付记录 -->
			<payment-record
				ref="payment"
				v-if="skeletonShow"
				:discernReceiptList="discernReceiptList"
				:receiptAmount="receiptAmount"
			></payment-record>
			<!-- 退款记录 -->
			<refund-record
				ref="refund"
				v-if="skeletonShow"
				:returnedPurchaseList="returnedPurchaseList"
				:returnedPurchaseFee="returnedPurchaseFee"
			></refund-record>
			<!-- 底部button -->
			<order-button
				ref="orderButton"
				v-if="skeletonShow"
				:status="btnStatus"
				:shareCode="shareCode"
				:order="orderInfo"
				@buttonConfirm="handButtonConfirm"
			>
			</order-button>
		</view>
		<!-- 分享弹窗 -->
		<share-alert :orderId="orderId" v-if="isShareModal" @btnConfirm="onShareAppMessage"> </share-alert>
		<!-- 提示弹窗 -->
		<tui-modal
			:show="modal"
			@click="handleClick"
			:content="contentModalText"
			:button="modalButton"
			color="#333"
			:size="32"
			shape="circle"
			:maskClosable="false"
		>
		</tui-modal>
	</view>
</template>

<script>
import headerBack from '@/components/cm-module/headerNavbar/header-back' //自定义导航
import orderAddress from '@/components/cm-module/orderDetails/orderAddress' //地址信息
import goodsList from '@/components/cm-module/orderDetails/goodsList' //商品列表
import invoiceTent from '@/components/cm-module/orderDetails/invoiceTent' //发票信息
import orderInformation from '@/components/cm-module/orderDetails/orderInformation' //订单信息
import paymentRecord from '@/components/cm-module/orderDetails/paymentRecord' //支付记录
import refundRecord from '@/components/cm-module/orderDetails/refundRecord' //退款记录
import orderButton from '@/components/cm-module/orderDetails/orderButton' //底部按钮
import shareAlert from '@/components/cm-module/modelAlert/shareAlert.vue' //分享弹窗

export default {
	components: {
		headerBack,
		orderInformation,
		orderAddress,
		invoiceTent,
		goodsList,
		paymentRecord,
		refundRecord,
		orderButton,
		shareAlert
	},
	data() {
		return {
			skeletonShow:false,
			headerBtnPosi: this.setHeaderBtnPosi(), //获取设备顶部胶囊高度
			systeminfo: this.setSysteminfo(), //获取设备信息
			isIphoneX: this.$store.state.isIphoneX,
			CustomBar: this.CustomBar, // 顶部导航栏高度
			navbarHeight: '',
			nvabarData: {
				//顶部自定义导航
				showCapsule: 1, // 是否显示左上角图标   1表示显示    0表示不显示
				title: '订单详情' // 导航栏 中间的标题
			},
			state: 0,
			userId: '',
			orderId: '',
			shareCode: '', //分享码
			shareType: '', //分享登录页过来记录的状态
			cellPhone: '', //客服电话
			payStatus: 0,
			btnStatus: 0, //按钮组件状态
			onlinePayFlag: '',
			isOrderShare: false,
			isShareModal: false,
			modelType: 0,
			orderInfo: {},
			alertOrderInfo: {},
			addressData: {}, //地址信息初始化
			information: {}, //订单信息初始化
			shopOrderData: {}, //商品信息初始化
			orderInvoice: {}, //发票信息初始化
			returnedPurchaseList: {}, //退款信息初始化
			discernReceiptList: {}, //支付信息初始化
			receiptAmount: 0, //支付金额
			returnedPurchaseFee: 0, //退款金额
			handleModelEven: 0,
			modal: false,
			contentModalText: '订单查询失败,请稍候重试~', //操作文字提示语句
			modalButton: [
				{
					text: '取消',
					type: 'gray',
					plain: true //是否空心
				},
				{
					text: '确定',
					customStyle: {
						color: '#fff',
						bgColor: '#F3B574'
					},
					plain: false
				}
			]
		}
	},
	onLoad(option) {
		console.log(option)
		this.shareType = option.type
		this.orderId = option.orderId
		if (this.shareType === 'share') {
			this.state = 0
			this.isOrderShare = true
		} else if (option.type === 'confim' || option.type === 'search') {
			this.state = 0
		} else {
			this.state = option.state
		}
		this.getHeaderTopHeight()
		this.initOrderDetaileData()
	},
	filters: {
		TextFormat(status) {
			//处理金额
			let HtmlText,
				typeTextObject = {
					0: '待确认',
					4: '交易完成',
					5: '订单完成',
					6: '已关闭',
					7: '交易全退',
					77: '交易全退',
					11: '待付款待发货',
					12: '待付款部分发货',
					13: '待付款已发货',
					21: '部分付款待发货',
					22: '部分付款部分发货',
					23: '部分付款已发货',
					31: '已付款待发货',
					32: '已付款部分发货',
					33: '已付款已发货',
					111: '待付款待发货'
				}
			Object.keys(typeTextObject).forEach(key => {
				if (key == status) {
					HtmlText = typeTextObject[key]
				}
			})
			return HtmlText
		}
	},
	methods: {
		// 获取订单详情
		async initOrderDetaileData() {
			try {
				const userInfo = await this.$api.getStorage()
				this.userId = userInfo.userId ? userInfo.userId : 0
				const res = await this.OrderService.QueryOrderDetails({ orderId: this.orderId, userId: this.userId })
				const data = res.data
				this.skeletonShow = true
				this.orderInfo = data.order
				this.shareCode = data.shareCode
				this.addressData = data.userInfo
				this.information = data.order
				this.btnStatus = data.order.status
				this.payStatus = data.order.payStatus
				this.payableAmount = data.order.payableAmount
				this.shopOrderData = data.shopOrderList
				this.orderInvoice = data.orderInvoice
				this.onlinePayFlag = data.order.onlinePayFlag
				this.returnedPurchaseList = data.returnedPurchaseList
				this.discernReceiptList = data.discernReceiptList
				this.receiptAmount = data.order.receiptAmount
				this.returnedPurchaseFee = data.order.returnedPurchaseFee
			} catch (error) {
				//TODO handle the exception
				this.$util.msg(error.msg, 2000)
			}
		},
		handButtonConfirm(data) {
			//监听点击时间的按钮类型并执行...
			this.handShowAlert(data)
		},
		handShowAlert(data) {
			//判断点击的按钮类型并执行...
			switch (data.type) {
				case 'cancel':
					//取消订单
					this.modal = true
					this.contentModalText = '确认取消该订单吗？'
					this.handleModelEven = 1
					break
				case 'query':
					this.$api.navigateTo('/pages/user/order/order-logistics?orderId=' + this.orderId)
					break
				case 'confirm':
					//确认收货
					this.modal = true
					this.contentModalText = '确认收货吗？'
					this.handleModelEven = 2
					break
				case 'pay':
					this.getOrderPaymentValidation(data)
					break
			}
		},
		getOrderPaymentValidation(data) {
			//监听根据付款状态做操作
			this.OrderService.OrderPaymentValidation({ orderId: data.orderId })
				.then(response => {
					let data = response.data
					if (data.onlinePayFlag == 1) {
						// 只能线下
						this.$api.navigateTo(`/pages/user/pay/card-under?orderId=${data.orderId}`)
					} else {
						this.$api.navigateTo(`/pages/user/order/order-pay-list?orderId=${data.orderId}`)
					}
				})
				.catch(error => {
					this.$util.msg(error.msg, 2000)
				})
		},
		handleClick(e) {
			//确认操作
			if (e.index == 1) {
				switch (this.handleModelEven) {
					case 1: //取消订单
						this.cancelOrder()
						break
					case 2: //确认收货
						this.confirmReceipt()
						break
				}
			}
			this.modal = false
		},
		async confirmReceipt() {
			//确认收货
			try {
				const res = await this.OrderService.ConfirmReceipt({ orderId: this.orderId })
				this.$util.msg(res.msg, 2000, true, 'success')
				setTimeout(() => {
					this.initOrderDetaileData()
				}, 2000)
			} catch (error) {
				this.$util.msg(error.msg, 2000)
			}
		},
		async cancelOrder() {
			//取消订单
			try {
				const res = await this.OrderService.CancelOrder({ orderId: this.orderId, userIdentity: 0 })
				this.$util.msg(res.msg, 2000, true, 'success')
				setTimeout(() => {
					this.initOrderDetaileData()
				}, 2000)
			} catch (error) {
				this.$util.msg(error.msg, 2000)
			}
		},
		onShareAppMessage(res) {
			//分享转发
			this.isShareModal = false
			if (res.from === 'button') {
				// 来自页面内转发按钮
			}
			return {
				title: '您有新的分享订单，快来查看吧~',
				path: `/pages/user/order/order-sharelogin?orderId=${this.orderId}&userId=${this.userId}`,
				imageUrl: 'https://static.caimei365.com/app/mini-mcare/icon/icon_shareOrder@2x.png'
			}
		},
		getHeaderTopHeight() {
			let statusBarHeight = this.systeminfo.statusBarHeight // 状态栏高度
			let headerPosi = this.headerBtnPosi
			let btnPosi = {
				// 胶囊实际位置，坐标信息不是左上角原点
				height: headerPosi.height,
				width: headerPosi.width,
				// 胶囊top - 状态栏高度
				top: headerPosi.top - statusBarHeight,
				// 胶囊bottom - 胶囊height - 状态栏height （现胶囊bottom 为距离导航栏底部的长度）
				bottom: headerPosi.bottom - headerPosi.height - statusBarHeight,
				// 屏幕宽度 - 胶囊right
				right: this.systeminfo.screenWidth - headerPosi.right
			}
			this.navbarHeight = headerPosi.bottom + btnPosi.bottom // 原胶囊bottom + 现胶囊bottom
		},
		setHeaderBtnPosi() {
			// 获得胶囊按钮位置信息
			let headerBtnPosi = uni.getMenuButtonBoundingClientRect()
			return headerBtnPosi
		},
		setSysteminfo() {
			let systeminfo
			uni.getSystemInfo({
				// 获取设备信息
				success: res => {
					systeminfo = res
				}
			})
			return systeminfo
		}
	},
	onShow() {}
}
</script>

<style lang="scss">
page {
	height: auto;
	background: #f7f7f7;
}
.details {
	padding-bottom: 130rpx;
}
.return-instructions {
	font-size: 26rpx;
	color: #f3b574;
	line-height: 42rpx;
	padding: 32rpx 50rpx;
	background-color: #fffaf8;
	text-align: justify;
}
.btn-hover {
	background: #ffffff;
}
.animation {
	/* transition: transform 0.3s ease;*/
	transition-property: transform;
	transition-duration: 0.3s;
	transition-timing-function: ease;
}
.invoice-balance {
	width: 702rpx;
	height: auto;
	padding: 0 24rpx;
	background: #ffffff;
	float: left;
	margin-top: 24rpx;
	margin-bottom: 24rpx;
	.balabce-t {
		width: 100%;
		height: 86rpx;
		line-height: 86rpx;
		font-size: $font-size-28;
		color: $text-color;
		float: left;
		.balabce-t-le {
			float: left;
			font-weight: bold;
		}
		.balabce-t-ri {
			float: right;
			display: flex;
			align-items: center;
			.money {
				display: flex;
				float: left;
			}
			.checkbox-box {
				display: flex;
				width: 60rpx;
				float: left;
				height: 100%;
				font-size: $font-size-24;
				.checkbox {
					width: 40rpx;
					text-align: right;
					box-sizing: border-box;
					text-align: center;
					text-decoration: none;
					border-radius: 0;
					-webkit-tap-highlight-color: transparent;
					overflow: hidden;
				}
			}
		}
	}
	.balabce-b {
		width: 100%;
		float: left;
		overflow: hidden;
		.balabce-b-text {
			width: 100%;
			line-height: 58rpx;
			font-size: $font-size-24;
			color: #ff2a2a;
			text-align: right;
			float: right;
		}
		&.balabce-b--hide {
			padding: 0 0;
			height: 0px;
			line-height: 0px;
		}
	}
}
.clause {
	float: right;
	font-size: 24rpx;
	color: #999999;
	margin-top: 60rpx;
	margin-right: 24rpx;
	&.noclick {
		pointer-events: none;
	}
	.text {
		color: #1890f9;
		&.color-bg {
			color: #333333;
		}
	}
}
.status-text {
	overflow: hidden;
	padding: 24rpx;
	background: #fff;
	font-size: $font-size-26;
	.view-type {
		float: left;
		color: #ff2a2a;
	}
	.bage-buss {
		display: inline-block;
		width: 72rpx;
		height: 32rpx;
		background: radial-gradient(circle, rgba(255, 39, 180, 1) 0%, rgba(193, 77, 245, 1) 100%);
		border-radius: 6rpx;
		line-height: 32rpx;
		text-align: center;
		color: #ffffff;
		margin-top: 10rpx;
		float: right;
	}
	.bage-auto {
		display: inline-block;
		width: 72rpx;
		height: 32rpx;
		background: radial-gradient(circle, rgba(255, 180, 39, 1) 0%, rgba(245, 142, 77, 1) 100%);
		border-radius: 6rpx;
		line-height: 32rpx;
		text-align: center;
		color: #ffffff;
		margin-top: 10rpx;
		float: right;
	}
}
</style>
