<template>
	<view class="container details clearfix"  :style="{ paddingBottom: isIphoneX ? 130 + 68 + 'rpx' : '130rpx' }">
<!-- 		自定义返回
		<header-back
			:systeminfo="systeminfo"
			:navbar-data="nvabarData"
			:headerBtnPosi="headerBtnPosi"
			:isShare="isOrderShare"
		/> -->
		<tui-skeleton
			v-if="!skeletonShow"
			backgroundColor="#fafafa"
			borderRadius="10rpx"
			:isLoading="true"
			:loadingType="5"
		/>
		<view class="container-details">
			<view class="status-text">
				<view class="view-type">{{ orderInfo.status | statusFilters }}</view>
				<text class="bage-buss btn" v-if="orderInfo.orderSubmitType == 3 || orderInfo.orderSubmitType == 4"
					>采购员</text
				>
				<text
					class="bage-auto btn"
					v-if="
						orderInfo.orderSubmitType == 0 ||
							orderInfo.orderSubmitType == 1 ||
							orderInfo.orderSubmitType == 2
					"
					>自主</text
				>
			</view>
			<!-- 地址选择 -->
			<cm-address-temp ref="orderAddress" v-if="skeletonShow && !rechargeGoods" :addressData="addressData" />
			<!-- 商品 -->
			<cm-goods-temp
				ref="goods"
				v-if="skeletonShow"
				:list="orderInfo.orderProductList"
				:orderInfo="orderInfo"
				@popupClick="hanldePopupFn"
			/>
			<!-- 订单信息 -->
			<cm-order-temp ref="orderInfo" v-if="skeletonShow" :orderInfo="orderInfo" />
			<!-- 发票信息 -->
			<cm-invice-temp ref="invoice" v-if="skeletonShow" :orderInvoice="orderInvoice" />
			<!-- 支付记录 -->
			<cm-payment-temp
				ref="payment"
				v-if="skeletonShow"
				:discernReceiptList="discernReceiptList"
				:receiptAmount="receiptAmount"
			/>
			<!-- 退款记录 -->
			<cm-refund-temp
				ref="refund"
				v-if="skeletonShow"
				:returnedPurchaseList="returnedPurchaseList"
				:returnedPurchaseFee="returnedPurchaseFee"
			/>
			<!-- 支付凭证记录 -->
			<cm-voucher-temp ref="voucher" v-if="skeletonShow && voucherList.length > 0" :list="voucherList" />
			<!-- 底部button -->
			<cm-details-button
				ref="orderButton"
				v-if="skeletonShow"
				:status="orderInfo.status"
				:order="orderInfo"
				:rechargeGoods="rechargeGoods"
				:ableUserMoney="ableUserMoney"
				:shareCode="shareCode"
				:secondHandOrderFlag="orderInfo.secondHandOrderFlag"
				@buttonConfirm="handButtonConfirm"
			/>
		</view>
		<!-- 分享弹窗 -->
		<cm-share-popup
			:shopOrderId="shopOrderId"
			:shareType="isShareType"
			v-if="isShareModal"
			@shareConfirm="onShareAppMessage"
		/>
		<!-- 提示弹窗 -->
		<tui-modal
			:show="modal"
			@click="handleClick"
			:content="contentModalText"
			:button="modalButton"
			color="#333"
			:size="32"
			shape="circle"
			:maskClosable="false"
		/>
	</view>
</template>

<script>
import headerBack from '@/components/cm-module/headerNavbar/header-back' //自定义导航
import cmAddressTemp from './components/details/cm-address-temp' //地址信息
import cmGoodsTemp from './components/details/cm-goods-temp' //商品列表
import cmOrderTemp from './components/details/cm-order-temp' //订单信息
import cmInviceTemp from './components/details/cm-invice-temp' //发票信息
import cmPaymentTemp from './components/details/cm-payment-temp' //支付记录
import cmRefundTemp from './components/details/cm-refund-temp' //退款记录
import cmVoucherTemp from './components/details/cm-voucher-temp' //退款记录
import cmDetailsButton from './components/cm-details-button' //底部按钮
import cmSharePopup from './components/cm-share-popup' //分享弹窗

export default {
	components: {
		headerBack,
		cmAddressTemp,
		cmGoodsTemp,
		cmOrderTemp,
		cmInviceTemp,
		cmPaymentTemp,
		cmRefundTemp,
		cmVoucherTemp,
		cmDetailsButton,
		cmSharePopup
	},
	data() {
		return {
			nvabarData: {
				//顶部自定义导航
				showCapsule: 1, // 是否显示左上角图标   1表示显示    0表示不显示
				title: '订单详情' // 导航栏 中间的标题
			},
			state: 0,
			userId: '',
			orderId: 0,
			shopOrderId: 0,
			clubUserId: 0,
			serviceProviderId: '', //协销ID
			shareCode: '', //分享码
			shareType: '', //分享登录页过来记录的状态
			cellPhone: '', //客服电话
			skeletonShow: false, //是否加载完成渲染子组件
			isOrderShare: false,
			isShareModal: false,
			isShareType: 2,
			isPayModel: false,
			modelType: 0,
			ableUserMoney: '', //机构余额
			addressData: {}, //地址信息初始化
			orderInfo: {}, //订单信息初始化
			orderInvoice: {}, //发票信息初始化
			returnedPurchaseList: {}, //退款信息初始化
			discernReceiptList: {}, //支付信息初始化
			voucherList:[], // 线下支付凭证
			receiptAmount: 0, //支付金额
			returnedPurchaseFee: 0, //退款金额
			navbarHeight: '',
			rechargeGoods: false,
			headerBtnPosi: this.setHeaderBtnPosi(), //获取设备顶部胶囊高度
			systeminfo: this.setSysteminfo(), //获取设备信息
			isIphoneX: this.$store.state.isIphoneX,
			CustomBar: this.CustomBar, // 顶部导航栏高度
			popupShow: false,
			handlerPros: {},
			clauseData: {},
			showAgan: false,
			failList: [], // 再来一单可购买商品
			promptitle: '',
			returnGoodsStutas: false,
			helpContent: '',
			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.shopOrderId = option.shopOrderId
		this.clubUserId = option.userId
		if (this.shareType === 'share') {
			this.listType = 0
			this.isOrderShare = true
		} else if (option.type === 'confim' || option.type === 'search' || option.type === 'cash') {
			this.listType = 0
		} else {
			this.listType = option.listType
		}
		this.getHeaderTopHeight()
		this.initOrderDetaileData()
	},
	filters: {
		NumFormat(value) {
			//处理金额
			return Number(value).toFixed(2)
		},
		statusFilters(value) {
			//处理订单状态显示
			const map = {
				0: '待确认',
				2: '交易完成',
				4: '已关闭',
				5: '交易全退',
				6: '交易全退',
				11: '待付款待发货',
				12: '待付款部分发货',
				13: '待付款已发货',
				21: '部分付款待发货',
				22: '部分付款部分发货',
				23: '部分付款已发货',
				31: '已付款待发货',
				32: '已付款部分发货',
				33: '已付款已发货'
			}
			return map[value]
		}
	},
	methods: {
		async initOrderDetaileData() {
			try {
				const res = await this.OrderService.QueryOrderDetails({
					shopOrderId: this.shopOrderId,
					userId: this.clubUserId
				})
				const data = res.data
				this.orderInfo = data.shopOrder
				this.userId = this.orderInfo.userId
				this.rechargeGoods = this.orderInfo.rechargeGoods
				// this.shareCode = data.shareCode
				this.addressData = data.userInfo
				this.receiptAmount = this.orderInfo.receiptAmount
				this.returnedPurchaseFee = this.orderInfo.returnedPurchaseFee
				this.orderInvoice = data.orderInvoice
				this.returnedPurchaseList = data.returnedPurchaseList
				this.discernReceiptList = data.discernReceiptList
				this.voucherList = data.voucher
				this.ableUserMoney = data.ableUserMoney
				this.clauseData = data.clause
				if (this.clauseData && this.clauseData.name.length > 10) {
					this.clauseData.name = this.clauseData.name.substr(0, 10) + '...'
				}
				// 特殊商品退货须知
				this.returnGoodsStutas = data.returnGoodsStutas && data.returnGoodsStutas === 2 // 1:可以 2:不可以
				this.helpContent = data.helpContent
				this.skeletonShow = true
			} catch (error) {
				this.$util.msg(error.msg, 2000)
			}
		},
		handButtonConfirm(data) {
			//判断点击的按钮类型并执行...
			switch (data.type) {
				case 'cancel': //取消订单
					this.modal = true
					this.contentModalText = '确认取消该订单吗？'
					this.handleModelEven = 1
					break
				case 'delete': //删除订单
					this.modal = true
					this.contentModalText = '确认删除该订单吗？'
					this.handleModelEven = 2
					break
				case 'confirm': // 确认订单
					this.modal = true
					this.contentModalText = '确认此订单吗？'
					this.handleModelEven = 3
					break
				case 'balance':
					this.getOrderPaymentValidation(data)
					break
				case 'again':
					this.handOrderAgain(data.shopOrderId)
					break
				case 'query':
					this.isModalLayer = true
					this.$api.navigateTo('/pages/user/order/order-logistics?shopOrderId=' + data.shopOrderId)
					break
			}
		},
		handleClick(e) {
			//确认操作
			if (e.index == 1) {
				switch (this.handleModelEven) {
					case 1: //取消订单
						this.cancelOrder(this.shopOrderId)
						break
					case 2: //删除订单
						this.deleteOrder(this.shopOrderId)
						break
					case 3: //确认订单
						this.affirmOrder(this.shopOrderId)
						break
				}
			}
			this.modal = false
		},
		//取消订单
		async cancelOrder(shopOrderId) {
			try {
				const res = await this.OrderService.CancelOrder({ shopOrderId: shopOrderId, userIdentity: 1 })
				this.$util.msg(res.msg, 2000, true, 'success')
				setTimeout(() => {
					this.initOrderDetaileData()
				}, 2000)
			} catch (error) {
				this.$util.msg(error.msg, 2000)
			}
		},
		//删除订单
		async deleteOrder(shopOrderId) {
			try {
				const res = await this.OrderService.DeleteOrder({ shopOrderId: shopOrderId })
				this.$util.msg(res.msg, 2000, true, 'success')
				setTimeout(() => {
					if (this.shareType === 'share') {
						this.$api.switchTabTo('/pages/tabBar/home/index')
					} else {
						this.$api.redirectTo(`/pages/user/order/order-list?type=detele&state=${this.state}`)
					}
				}, 2000)
			} catch (error) {
				this.$util.msg(error.msg, 2000)
			}
		},
		//确认订单
		async affirmOrder(shopOrderId) {
			try {
				const res = await this.OrderService.AffirmOrder({ shopOrderId: shopOrderId })
				this.$util.msg(res.msg, 2000, true, 'success')
				setTimeout(() => {
					this.initOrderDetaileData()
				}, 2000)
			} catch (error) {
				this.$util.msg(error.msg, 2000)
			}
		},
		// 再来一单
		async handOrderAgain(shopOrderId) {
			try{
				const res = await this.SellerService.SellerCreateOrderAgain({
					confirmFlag: 0,
					shopOrderId: shopOrderId,
					serviceProviderId: this.serviceProviderId
				})
				const data = res.data
				this.$api.setStorage('orderUserInfo', {
					clubId: data.clubId,
					againBuyProductIds: data.productIds,
					userId: data.userId
				})
				this.$api.navigateTo('/pages/seller/cart/cart')
			}catch(error){
				if (error.code == -3) {
					this.showAgan = true
					this.promptitle = error.msg
					this.failList = error.data
				} else if (error.code == -2) {
					this.$util.modal('', error.msg, '确定', '', false, () => {})
				} else {
					this.$util.msg(error.msg, 2000)
				}
			}
		},
		async handleAddAgian() {
			// 再来一单弹窗
			try{
				await this.SellerService.SellerCreateOrderAgain({
					confirmFlag: 1,
					shopOrderId: this.shopOrderId,
					serviceProviderId: this.serviceProviderId,
				})
				this.$api.navigateTo('/pages/seller/cart/cart')
				this.showAgan = false
			}catch(error){
				this.$util.msg(error.msg, 2000)
			}
		},
		//监听余额抵扣按钮
		async hanldPaymentConfirm(data) {
			try{
				await this.OrderService.OrderBalanceDeduction({ shopOrderId: this.shopOrderId })
				this.$util.msg('余额抵扣成功', 2000)
				this.initOrderDetaileData()
			}catch(error){
				this.$util.msg(error.msg, 2000)
			}
		},
		//监听根据付款状态做操作
		async getOrderPaymentValidation(order) {
			try{
				const res = await this.OrderService.OrderPaymentValidation({ shopOrderId: order.shopOrderId })
				const data = res.data
				this.payModelData = data
				if (data.balanceFlag == 0) {
					// 0可以走余额抵扣，1不能走余额抵扣
					if (data.code == -1) {
						this.$util.modal('', '订单已申请全部退款，无需再付款！', '确定', '', false, () => {})
					} else {
						console.log('payModelData',this.payModelData)
						this.isPayModel = true
						this.modelType = 3
						console.log('isPayModel',this.isPayModel)
					}
				} else {
					this.$util.modal('', '该订单已走线上支付流程,不能进行余额抵扣。', '确定', '', false, () => {})
				}
			}catch(error){
				this.$util.msg(error.msg, 2000)
			}
		},
		// 条款
		openclauseConten(id) {
			this.$api.navigateTo(`/pages/service/sellconten?clauseId=${id}`)
		},
		hanldePopupFn(data) {
			//监听活动内容
			this.popupShow = true
			this.handlerPros = data
		},
		onShareAppMessage(res) {
			//分享转发
			this.isShareModal = false
			if (res.from === 'button') {
				// 来自页面内转发按钮
			}
			return {
				title: '您有新的订单，请点击查看~',
				path: `/pages/user/order/order-sharelogin?shopOrderId=${this.shopOrderId}&userId=${
					this.clubUserId
				}&serviceProviderId=${this.serviceProviderId}`,
				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() {
		this.$api.getStorage().then(resolve => {
			this.serviceProviderId = resolve.serviceProviderId ? resolve.serviceProviderId : 0
		})
	}
}
</script>

<style lang="scss">
page {
	height: auto;
	background: #f7f7f7;
}
.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;
		font-weight: bold;
	}
	.status-visible {
		float: left;
		color: #999999;
		margin-left: 20rpx;
	}
	.bage-buss {
		display: inline-block;
		width: 100rpx;
		height: 36rpx;
		background: radial-gradient(circle, rgba(255, 39, 180, 1) 0%, rgba(193, 77, 245, 1) 100%);
		border-radius: 6rpx;
		line-height: 36rpx;
		text-align: center;
		color: #ffffff;
		float: right;
		font-size: $font-size-24;
	}
	.bage-auto {
		display: inline-block;
		width: 100rpx;
		height: 36rpx;
		background: radial-gradient(circle, rgba(255, 180, 39, 1) 0%, rgba(245, 142, 77, 1) 100%);
		border-radius: 6rpx;
		line-height: 36rpx;
		text-align: center;
		color: #ffffff;
		float: right;
		font-size: $font-size-24;
	}
	.bage-name {
		line-height: 32rpx;
		display: inline-block;
		color: #999999;
		float: right;
		font-size: $font-size-24;
	}
	.Rebate {
		display: inline-block;
		width: 72rpx;
		height: 32rpx;
		background: #ff7a51;
		border-radius: 6rpx;
		line-height: 32rpx;
		text-align: center;
		color: #ffffff;
		float: right;
		font-size: $font-size-24;
		margin-left: 20rpx;
	}
}
.aganBj {
	position: fixed;
	left: 0;
	top: 0;
	bottom: 0;
	width: 100%;
	height: 100%;
	background-color: rgba(0, 0, 0, 0.5);
	z-index: 999999;
	.alertAgan {
		position: absolute;
		top: 50%;
		left: 50%;
		transform: translate(-50%, -50%);
		width: 580rpx;
		background-color: #fff;
		border-radius: 16rpx;
		.title {
			font-size: 30rpx;
			color: #333333;
			line-height: 42rpx;
			padding: 30rpx;
			display: block;
		}
		.goods {
			padding: 0 30rpx;
			.list {
				padding: 10px 0;
				border-bottom: 1rpx solid #e1e1e1;
				margin: 10rpx 0;
				.image-left {
					width: 86rpx;
					height: 86rpx;
					border: 2rpx solid #e1e1e1;
					border-radius: 6rpx;
					display: inline-block;
					vertical-align: middle;
				}
				.name-right {
					display: inline-block;
					width: 416rpx;
					margin-left: 15rpx;
					font-size: 26rpx;
					color: #666666;
					vertical-align: middle;
					word-break: break-all;
					overflow: hidden;
					text-overflow: ellipsis;
					display: -webkit-inline-box;
					-webkit-line-clamp: 2;
					-webkit-box-orient: vertical;
				}
			}
		}
		.BtnAll {
			margin-top: 30rpx;
			.btn {
				display: inline-block;
				width: 290rpx;
				height: 90rpx;
				line-height: 90rpx;
				text-align: center;
				&.closebtn {
					border-radius: 0px 0px 0px 10px;
					color: #999999;
					background: #efefef;
				}
				&.cancel {
					border-radius: 0px 0px 8px 0px;
					background: $btn-confirm;
					color: #fff;
				}
			}
		}
	}
}
.return-instructions {
	font-size: 26rpx;
	color: #ff5b00;
	line-height: 42rpx;
	padding: 32rpx 50rpx;
	background-color: #fffaf8;
	text-align: justify;
}
</style>
