<template>
	<view class="container">
		<view class="card" v-if="listinfo.tableName">
			<view class="head">
				<text class="title">
					<text>{{listinfo.tableName || '空'}}</text>
				</text>
				<text class="status" v-if="listinfo.status == 'unpaid'">
					<text>待支付</text>
				</text>
				<text class="status" v-if="listinfo.status == 'unsend'">
					<text>待发货</text>
				</text>
				<text class="status" v-if="listinfo.status == 'closed'">
					<text>订单完成</text>
				</text>
				<text class="status" v-if="listinfo.status == 'send'">
					<text>已发</text>
				</text>
				<text class="status" v-if="listinfo.status == 'refunding'">
					<text>申请退单</text>
				</text>
				<text class="status" v-if="listinfo.status == 'refund'">
					<text>退单</text>
				</text>
				<text class="status" v-if="listinfo.status == 'cancelled'">
					<text>取消订单</text>
				</text>
				<text class="status" v-if="listinfo.status == 'merge'">
					<text>合台</text>
				</text>
			</view>
			<view class="tag-wrap">
				<text class="tag" v-if="listinfo.sendType == 'post'">快递</text>
				<text class="tag" v-if="listinfo.sendType == 'takeaway'">外卖</text>
				<text class="tag" v-if="listinfo.sendType == 'takeself'">自提</text>
				<text class="tag" v-if="listinfo.sendType == 'table'">堂食</text>
			</view>
			<view class="number-wrap" v-if="listinfo.status != 'unpaid'">
				<text class="t">取餐号</text>
				<text class="number">{{listinfo.tableName || '空'}}</text>
			</view>
		</view>
		<view class="card">
			<view class="head border">
				<text class="title">
					<text>点单详情</text>
				</text>
			</view>
			<view class="shop-info">
				<view class="item" v-for="(item,index) in listinfo.detailList" :key="index">
					<view class="cover">
						<c-image width="120" height="120" radius="16" :src='item.coverImg'></c-image>
					</view>
					<view class="info">
						<text>{{item.name}}</text>
						<text class="n">x{{item.number}}</text>
					</view>
					<view class="price">
						<text class="i">￥</text>
						<text class="num">{{item.salePrice}}</text>
					</view>
				</view>
			</view>
			<view class="total-wrap">
				<view class="price">
					<text>合计：</text>
					<text class="i">￥</text>
					<text class="num">{{listinfo.amounts}}</text>
				</view>
			</view>
		</view>
		<view class="sixcontent">
			<view class="sixcontentone flex-between" style="padding: 0 0;" @click="orderfoodindex">
				<view class="sixcontent_one" style="padding: 0 0;">
					优惠卷
				</view>
				<text style="color: red;">{{emitorderfoodform.couponsPrice}}</text>
			</view>
		</view>
		<view class="sixcontent" v-if="listinfo.status == 'unpaid'">
			<view class="sixcontentone flex-between">
				<view class="sixcontent_one">
					支付方式
				</view>
			</view>
			<view class="sixcontenttow">
				<view class="sixcontenttowitem flex-between" @click="clickselect(1)">
					<view class="sixcontenttowitemone flex-start">
						<u-icon name="weixin-fill" color="#999999" size="32"></u-icon>
						<text class="sixcontenttowitemonetext">微信支付</text>
					</view>
					<view class="flex-start">
						<u-icon v-if="pay_type == 1" name="checkmark-circle-fill" color="#333333" size="26"></u-icon>
						<u-icon v-else name="checkmark-circle-fill" color="#999999" size="26"></u-icon>

					</view>
				</view>
				<!-- <view class="sixcontenttowitem flex-between" @click="clickselect(2)">
					<view class="sixcontenttowitemone flex-start">
						<u-icon name="zhifubao" color="#999999" size="26"></u-icon>
						<text class="sixcontenttowitemonetext">余额支付</text>
					</view>
					<view class="flex-start">
						<u-icon v-if="pay_type == 2" name="checkmark-circle-fill" color="#333333" size="26"></u-icon>
						<u-icon v-else name="checkmark-circle-fill" color="#999999" size="26"></u-icon>
					</view>
				</view> -->
			</view>
		</view>
		<view :style="{height:height}">

		</view>
		<view class="fixedview">
			<view class="flex-between">
				<view class="fixedview_one flex-start">
					<view class="fixedview_oneone">
						实付金额：
					</view>
					<view class="fixedview_onetow">
						<text>¥</text>{{listinfo.amount}}
					</view>
					<view class="fixedview_onethere" v-if="emitorderfoodform.couponsPrice">
						以优惠¥{{emitorderfoodform.couponsPrice}}
					</view>
				</view>
				<view class="fixedview_tow" @click="showpopupclick">
					立即付款
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import webSocketUtils from '@/common/js/websocket.js'
	export default {
		data() {
			return {
				height: '',
				pay_type: 1,
				info: {},
				listinfo: {
					detailList: [],
					amount: '',
					amounts: ''
				},
				emitorderfoodform: {
					clickiconone: ''
				}
			};
		},

		onUnload() {
			uni.$off('createOrdermessage')
		},
		onLoad(e) {
			console.log(e, 140)
			let res = JSON.parse(e.tableId)
			// .toFixed(2)
			this.listinfo.detailList = res
			this.listinfo.amount = e.amount
			this.listinfo.amounts = e.amount
			this.handlemessage()
			// uni.$on('createOrdermessage', this.creategetMessage)
		},
		onShow() {

		},
		created() {
			uni.$on('emitclickorderfood', (num) => {
				console.log(num)
				// 总金额  couponsAmount 优惠卷购买金额   couponsPrice优惠金额
				if (num.clickiconone == 1) {
					this.listinfo.amount = (Number(this.listinfo.amounts) - Number(num.couponsPrice)).toFixed(2)
				} else {
					this.listinfo.amount = (Number(this.listinfo.amounts) + Number(num.couponsAmount) - Number(num
						.couponsPrice)).toFixed(2)
				}
				this.emitorderfoodform = num
				// this.num += num
			})
		},
		methods: {
			creategetMessage(msg) {
				if (msg.status != 'success') {
					uni.showToast({
						title: msg.msg,
						icon: "none",
					})
					this.socketTicket.Close()
					uni.$off('createOrdermessage')
					return false;
				} else {
					switch (msg.type) {
						case 'createOrder': //去结算
							this.showpopupclickdd()
							// let item = JSON.stringify(msg.data)
							// uni.redirectTo({
							// 	url: '/pages/order_detail/order_detail?tableId=' + item
							// });
							break;
					}
				}
			},
			handlemessage() {
				this.socketTicket ? this.socketTicket.Close() : null //调用前先判断是否有socket正在进行 先关闭后链接
				this.socketTicket = new webSocketUtils(
					`${uni.conf.baseUrlwws}/websocket/table/${this.tableCode}/${uni.cache.get('shopUser')}/${uni.cache.get('userInfo').id}`,
					5000)

			},
			// 数据处理
			socketSendMsg(data) {
				if (this.socketTicket) {
					this.socketTicket.send(data);
				}
			},
			orderfoodindex() {
				uni.pro.navigateTo('index/coupons/index', {
					orderfood: 0,
					amount: this.listinfo.amounts

				})
			},
			async ordercreatOrder() {
				let res = await this.api.ordercreatOrder({
					tableId: this.info.tableId,
					shopId: this.info.shopId,
					userId: this.info.userId
				})
				this.listinfo = res.data
			},
			copyHandle(e) {
				uni.setClipboardData({
					data: e,
					success() {
						uni.showToast({
							title: '复制成功',
							icon: 'none'
						});
					}
				});
			},
			async paymodfiyOrderInfo() {
				let res = await this.api.paymodfiyOrderInfo({
					orderId: this.listinfo.id
				})
			},
			// 去结算
			showpopupclick() {

				let data = { //定义socket数据传参
					"skuId": '',
					"num": '', //数量
					"type": "createOrder", //“addcart:添加购物车，create0rder:生成订单，clearCart:庆康购物车”，
					"couponsId": this.emitorderfoodform.id ? this.emitorderfoodform.id : '', //优惠券ID,
					"isYhq": this.emitorderfoodform.id ? 1 : 0, // 是否使用优惠券（ 1： 使用， 0： 不使用）,
					"isBuyYhq": this.emitorderfoodform.clickiconone == 1 ? 1 : 0, // 是否购买优惠券（ 1： 购买， 0： 不够买）
					"productId": '', //商品id
					"shopId": uni.cache.get('shopUser'),
					"userId": uni.cache.get('userInfo').id
				}
				console.log(data)
				uni.$u.debounce(this.socketSendMsg(data), 500)
			},
			async showpopupclickdd() {

				let res = await this.api.payorderPay({
					orderId: this.listinfo.id,
				}) //判断是否支付成功
				if (res.code == 0) {
					uni.showLoading({
						title: '加载中',
						mask: true
					})
					// #ifdef MP-WEIXIN
					uni.requestPayment({
						provider: 'wxpay', //支付类型-固定值
						partnerid: res.data.payAppId, // 微信支付商户号
						timeStamp: res.data.payTimeStamp, // 时间戳（单位：秒）
						nonceStr: res.data.paynonceStr, // 随机字符串
						package: res.data.payPackage, // 固定值
						signType: res.data.paySignType, //固定值
						paySign: res.data.paySign, //签名
						success: (res) => {
							uni.showToast({
								title: "支付成功"
							})
							setTimeout(res => {
								uni.redirectTo({
									url: '/pages/order/order_detail?orderId=' + this
										.listinfo.id
								});
							}, 1000)
							this.paymodfiyOrderInfo()
							// uni.redirectTo({
							// 	url: "/pages/mall/order/ordersuccess?id=" + datareslane.data
							// 		.order_id,
							// });
						},
						fail: (err) => {
							setTimeout(res => {
								uni.showToast({
									icon: 'none',
									title: '支付失败'
								})
								uni.hideLoading()
							}, 2000)
							uni.redirectTo({
								url: '/pages/order/order_detail?orderId=' + this.listinfo.id
							});
						}
					});
					// #endif	
				}
			},
			clickselect(b) {
				this.pay_type = b
			},
		},
		mounted() {
			//#ifdef MP-WEIXIN || H5
			var query = uni.createSelectorQuery().in(this).select('.fixedview')
			query.boundingClientRect(ele => {
				var that = this;
				that.height = (ele.height) + "px";
				that = null;
				// uni.getSystemInfo({
				// 	success(res) {

				// 	}
				// })
			}).exec();
			//#endif
			//#ifdef MP-ALIPAY
			my.createSelectorQuery().selectAll('.fixedview').boundingClientRect().exec(ele => {
				var nodeData = ele[0]
				var that = this;
				that.height = (nodeData.height) + "px";
				that = null;
			})
			//#endif
		},
	};
</script>

<style scoped lang="scss">
	.container {
		padding: 28upx;
	}

	.card {
		background-color: #fff;
		border-radius: 20upx;
		padding: 28upx;
		margin-bottom: 28upx;

		.head {
			display: flex;
			justify-content: space-between;

			&.border {
				padding-bottom: 28upx;
				border-bottom: 1upx solid #fafafa;
			}

			.status {
				color: #ff0000;
			}
		}

		.tag-wrap {
			padding: 28upx 0;
			display: flex;

			.tag {
				display: flex;
				align-items: center;
				justify-content: center;
				padding: 0 10upx;
				color: $color-priamry;
				border: 1px solid $color-priamry;
				font-size: 24upx;
			}
		}

		.number-wrap {
			background-color: #fafafa;
			border-radius: 16upx;
			padding: 28upx 0;
			display: flex;
			flex-direction: column;
			align-items: center;

			.number {
				font-size: 42upx;
				font-weight: bold;
			}
		}

		.price {
			.i {
				font-size: 20upx;
			}

			.num {
				font-size: 38upx;
			}
		}

		.shop-info {
			padding: 28upx 0;

			.item:nth-child(1) {
				margin-top: 0;
			}

			.item {
				margin-top: 16rpx;
				display: flex;

				.info {
					flex: 1;
					display: flex;
					flex-direction: column;
					justify-content: space-between;
					padding-left: 28upx;

					.n {
						font-size: 24upx;
						color: #999;
					}
				}
			}
		}

		.total-wrap {
			display: flex;
			justify-content: flex-end;
			padding-top: 28upx;
			border-top: 1upx solid #fafafa;
		}

		.order-info {
			.row {
				display: flex;
				justify-content: space-between;
				padding-top: 28upx;

				.t {
					font-size: 24upx;
					color: #999;
				}

				.info {
					font-size: 24upx;
				}
			}
		}
	}

	.sixcontent {
		width: 100%;
		background: #FFFFFF;
		padding: 32rpx;
		margin-top: 32rpx;
		border-radius: 12rpx;

		.sixcontentone {
			padding-bottom: 28rpx;
			border-bottom: 1px solid #F7F7F7;
			font-size: 28rpx;
			font-family: Source Han Sans CN-Medium, Source Han Sans CN;
			font-weight: 500;
			color: #333333;

			.sixcontent_one {
				font-size: 28rpx;
				font-family: Source Han Sans CN-Medium, Source Han Sans CN;
				font-weight: 500;
				color: #333333;
			}
		}

		.sixcontenttow {
			.sixcontenttowitem {
				margin-top: 16rpx;

				.sixcontenttowitemone {
					image {
						margin-left: 24rpx;
						width: 31.37rpx;
						height: 27.34rpx;
					}

					.sixcontenttowitemonetext {
						margin-left: 14rpx;
						font-size: 28rpx;
						font-family: Source Han Sans CN-Medium, Source Han Sans CN;
						font-weight: 500;
						color: #333333;
					}
				}
			}
		}
	}

	.fixedview {
		position: fixed;
		bottom: 0;
		left: 0;
		width: 100%;

		.flex-between {
			width: 100%;
			padding: 24rpx 28rpx;
			background: #FFFFFF;
			flex-wrap: nowrap;

			.fixedview_one {
				.fixedview_oneone {
					font-size: 28rpx;
					font-family: SourceHanSansCN-Bold-, SourceHanSansCN-Bold;
					font-weight: normal;
					color: #333333;
				}

				.fixedview_onetow {
					font-size: 44rpx;
					font-family: SourceHanSansCN-Bold-, SourceHanSansCN-Bold;
					font-weight: normal;
					color: #F45C4C;
					font-weight: bold;

					text {
						font-size: 28rpx;
					}
				}

				.fixedview_onethere {
					font-family: Source Han Sans CN, Source Han Sans CN;
					font-weight: 400;
					font-size: 24rpx;
					color: #999999;
				}
			}

			.fixedview_tow {
				background: red;
				border-radius: 34rpx;
				padding: 10rpx 44rpx;
				font-size: 32rpx;
				font-family: PingFang SC-Bold, PingFang SC;
				font-weight: bold;
				color: #FFFFFF;
			}
		}
	}
</style>