<template>
	<view class="u-p-l-20 u-p-r-20">

		<!-- <view>
					<image src="@/me/static/ymf/1b3d78fa5f92af069751ea64d493c0d85050b1e03cc5b-tPC378.png" mode=""></image>
					<image src="@/me/static/ymf/390e926273eea85c894e613d42c9931ee2cd1f0a200fd-ApyNVV.png" mode=""></image>
					<image src="@/me/static/ymf/79cbb0e34c24915f1883feaa3cf3179b0a1ceb1362a18-unU6xQ.png" mode=""></image>
					<image src="@/me/static/ymf/cb319b604e49b816971b708b1231321318e0023610d8c-I24hXU.jpg" mode=""></image>
					<image src="@/me/static/ymf/cf2d172a6da494b2e577f699254a780907b7c222246d7-ehahn1.png" mode=""></image>
					<image src="@/me/static/ymf/eaae3095c9e433fb8a015aaebbe52047009dd863ff2b9-igSItK.png" mode=""></image>
					<image src="@/me/static/ymf/f3c60c6f31c74bdf38ba2d8993bcb443ab9bc335cc875-xfx4CJ.png" mode=""></image>
					<image src="@/me/static/ymf/c93b4cc9363d52cba140f7239516344ece9388141079a4-do8mFc.png" mode=""></image>
					<image src="@/me/static/ymf/031da1e7f043de5d5e1ee355994a93e5ec983aca44dc-sZf0Vl_fw1200.jpg" mode=""></image>
					<image src="@/me/static/ymf/c4e6c5023b94de32df2f51d9ebd633790cc6125a88e4-TIyAcR_fw1200.jpg" mode=""></image>
					<image src="@/me/static/ymf/f7da7a8ca57f9f0d56bbb3d7be1d1c86f96304ce4b3a-iVbD8U_fw1200.jpg" mode=""></image>
					<image src="@/me/static/ymf/2e521eee5a8c80ea6879028ee866c5d0531bc4f1d5e48-Bh95lN_fw1200.jpg" mode=""></image>
					<image src="@/me/static/ymf/38cf4e97e52b50be9dcdcdc1352c497e1acc9266cd52-aUOrxn_fw1200.webp" mode=""></image>
					<image src="@/me/static/ymf/73170e6a596b72538dbe743b06ef8b96e4885aff5b4c3-MsouS7_fw1200.jpg" mode=""></image>
					<image src="@/me/static/ymf/e9f9cb6b95c21c714015f15c372f595367aa553f443a-WpOCNo_fw1200.webp" mode=""></image>
					<image src="@/me/static/ymf/f5d96faaa94cb0151a5c95db44e27b80ed6eeac5cb32-rApi2S_fw1200.webp" mode=""></image>
					<image src="@/me/static/ymf/0e114b9ac6afc2b4c0f29520178be64915a111e218a8b5-mbMpg2_fw1200.jpg" mode=""></image>
					<image src="@/me/static/ymf/4783f7272bf31eeafbe1362605477de14b7cd3da24b0-ggkAnW_fw658webp.webp" mode=""></image>
					<image src="@/me/static/ymf/8dce1d3c3a8287c35bc1bf394048d57a64acfa047d4e-6wXnWE_fw658webp.webp" mode=""></image>
					<image src="@/me/static/ymf/ff919b6254e376cc902c89afdcc194ac2ab39c93793b-8TdL1Z_fw658webp.webp" mode=""></image>
					<image src="@/me/static/ymf/3c9a5848127a67f98e25176a042511b89afd31daccc2f-5EHbxD_fw658webp.webp" mode=""></image>
					<image src="@/me/static/ymf/6c9e57bcbcb047039a8edb30ecdd3b912bac99555a8fc-9EK0oO_fw658webp.webp" mode=""></image>
					<image src="@/me/static/ymf/78a020586cfbb791918b767520a8ff3c2ab9660f1ee1b-GX4zcc_fw658webp.webp" mode=""></image>
					<image src="@/me/static/ymf/babf535567728d84346fbb13348e94aae3ec8f82106e2-QKPJoe_fw658webp.webp" mode=""></image>
					<image src="@/me/static/ymf/d1e4d760358ba0733faee6bcad1e23cb4a1d6d76ba290-O3hUrf_fw240webp.webp" mode=""></image>
					<image src="@/me/static/ymf/dcb5a4b0b1060b569f9fc618e6e2708b584f7def1cadf-R7jpIr_fw658webp.webp" mode=""></image>
					<image src="@/me/static/ymf/e4330f184149f502f4948ce3b60059edaffc9c3d94ca4-B11p5c_fw658webp.webp" mode=""></image>
					<image src="@/me/static/ymf/fa22e2a27871e5f8841e8abb12cc4347a6b9440816068-3XsAnJ_fw658webp.webp" mode=""></image>
				</view> -->

		<!-- 视频简介 -->
		<view class="ymf-info flex align-center justify-center">
			<view class="ymf-info-box flex align-center">
				<view class="ymf-info-box-l">
					<image :src="titleImg" mode="aspectFill"></image>
				</view>
				<view class="ymf-info-box-r">
					<view class="ymf-info-box-r-t">
						{{type==1?name:courseDetailsName}}
					</view>
					<view class="ymf-info-box-r-b flex align-center" v-if="type == 1">
						全集价格：
						<view class="flex align-center">
							<image style="width: 38rpx;height: 38rpx;margin-right: 10rpx;"
								src="../../static/images/me/jindou.png" mode="">
							</image>
							{{ymfparsePrice(zongPrice)}}
						</view>
					</view>
					<view class="ymf-info-box-r-b flex align-center" v-else>
						单集价格：
						<view class="flex align-center">
							<image style="width: 38rpx;height: 38rpx;margin-right: 10rpx;"
								src="../../static/images/me/jindou.png" mode="">
							</image>
							{{ymfparsePrice(price)}}
						</view>
					</view>
				</view>
			</view>
		</view>
		<!-- 价格提示 -->
		<view class="price flex align-center justify-center">
			<view class="price-box flex align-center justify-between">
				<view class="price-box-title">
					需支付
				</view>
				<view class="price-box-price flex align-center" v-if="type==1">
					<image style="width: 38rpx;height: 38rpx;margin-right: 10rpx;"
						src="../../static/images/me/jindou.png" mode="">
					</image>
					{{ymfparsePrice(zongPrice)}}
				</view>
				<view class="price-box-price flex align-center" v-else>
					<image style="width: 38rpx;height: 38rpx;margin-right: 10rpx;"
						src="../../static/images/me/jindou.png" mode="">
					</image>
					{{ymfparsePrice(price)}}
				</view>
			</view>
		</view>
		<!-- 支付方式 -->
		<view v-if="payList.length>0" class="pay flex align-center justify-center">
			<view class="pay-box">
				<view class="pay-box-title">
					支付方式
				</view>
				<u-radio-group style="width: 100%;" size="44" v-model="payAway">
					<view class="pay-box-item flex align-center justify-between" v-for="(item,index) in payList"
						:key="index">
						<view class="pay-box-item-l flex align-center">
							<image :src="item.imgurl" mode=""></image>
							{{item.name}}
						</view>
						<view class="pay-box-item-r">
							<u-radio active-color="5074FF" :name="item.payAway"></u-radio>
						</view>
					</view>
				</u-radio-group>
			</view>
		</view>
		<view class="ymf-illustrate flex align-center justify-center">
			<view class="ymf-illustrate-box flex align-center">
				<u-checkbox v-model="checked" :size="40" shape="circle"></u-checkbox>
				<text @click="checked = !checked">我已经阅读并同意</text>
				<view @click="ymfgoXieyi()" class="ymf-illustrate-box-txt">
					《付费须知说明》
				</view>
			</view>
		</view>
		<!-- 支付 -->
		<view class="submit flex align-center justify-center">
			<view class="submit-box flex align-center justify-center" @click="ymfgetOrderInfo()">
				支付 ¥{{type==1?ymfparsePrice(zongPrice):ymfparsePrice(price)}}
			</view>
		</view>
		<u-toast ref="uToast" />
		<!-- 抖音im客服 -->
		<ttMsg />
	</view>
</template>

<script>
	import ttMsg from '../../components/ttMsg/ttMsg.vue'
	export default {
		components: {
			ttMsg
		},
		data() {
			return {
				ymfCourseId: '', //整部视频id
				courseDetailsId: '', //单集id
				titleImg: '', //图片
				courseDetailsName: '', //名称
				price: 0, //单价
				zongPrice: 0, //总价
				type: 1, //类型：1:整部 2:单部
				name: '', //全集名称
				payList: [],
				payAway: 1,
				checked: false
			};
		},
		onLoad(option) {
			// #ifdef APP
			if (plus.os.name.toLowerCase() === 'android') {
				this.payList = [{
						imgurl: '/static/images/pay/weixin.png',
						name: '微信支付',
						payAway: 1
					},
					{
						imgurl: '/static/images/pay/zhifubao.png',
						name: '支付宝支付',
						payAway: 2
					},
					{
						imgurl: '/static/images/pay/lingqian.png',
						name: '金币支付',
						payAway: 3
					}
				]
				this.payAway = 1
			} else {
				let checkIosPay = this.$queue.getData('checkIosPay');
				if (checkIosPay === '是') {
					this.payList = [{
						imgurl: '/static/images/pay/lingqian.png',
						name: '金币支付',
						payAway: 3
					}]
					this.payAway = 3
				} else {
					this.payList = [{
							imgurl: '/static/images/pay/weixin.png',
							name: '微信支付',
							payAway: 1
						},
						{
							imgurl: '/static/images/pay/zhifubao.png',
							name: '支付宝支付',
							payAway: 2
						},
						{
							imgurl: '/static/images/pay/lingqian.png',
							name: '金币支付',
							payAway: 3
						}
					]
					this.payAway = 1
				}
			}
			// #endif
			// #ifdef MP-WEIXIN
			this.payList = [{
				imgurl: '/static/images/pay/lingqian.png',
				name: '金币支付',
				payAway: 3
			}]
			this.payAway = 3
			// #endif
			// #ifdef H5
			let ua = navigator.userAgent.toLowerCase();
			if (ua.indexOf('micromessenger') !== -1) {
				this.payList = [{
						imgurl: '/static/images/pay/weixin.png',
						name: '微信支付',
						payAway: 1
					},
					{
						imgurl: '/static/images/pay/lingqian.png',
						name: '金币支付',
						payAway: 3
					}
				]
				this.payAway = 1
			} else {
				this.payList = [{
						imgurl: '/static/images/pay/zhifubao.png',
						name: '支付宝支付',
						payAway: 2
					},
					{
						imgurl: '/static/images/pay/lingqian.png',
						name: '金币支付',
						payAway: 3
					}
				]
				this.payAway = 2
			}
			// #endif
			// #ifdef MP-TOUTIAO
			this.payList = [{
					imgurl: '/static/images/pay/zhifubao.png',
					name: '抖音支付',
					payAway: 4
				},
				{
					imgurl: '/static/images/pay/lingqian.png',
					name: '金币支付',
					payAway: 3
				}
			]
			this.payAway = 4
			// #endif
			// #ifdef MP-KUAISHOU
			const osName = uni.getSystemInfoSync().osName;
			if (osName != 'ios') { //安卓
				this.payList = [{
						imgurl: '/static/images/pay/zhifubao.png',
						name: '快手支付',
						payAway: 5
					},
					{
						imgurl: '/static/images/pay/lingqian.png',
						name: '金币支付',
						payAway: 3
					}
				]
				this.payAway = 5
			} else {
				this.payList = [{
					imgurl: '/static/images/pay/lingqian.png',
					name: '金币支付',
					payAway: 3
				}]
				this.payAway = 3
			}
			// #endif
			if (option.info) {
				let info = JSON.parse(decodeURIComponent(option.info))
				this.ymfCourseId = info.ymfCourseId
				this.courseDetailsId = info.courseDetailsId
				this.titleImg = info.titleImg
				this.courseDetailsName = info.courseDetailsName
				this.price = info.price
				this.zongPrice = info.zongPrice
				this.name = info.name
				this.type = info.type
			}
		},
		methods: {
			//去付费须知说明
			ymfgoXieyi() {
				uni.navigateTo({
					url: '/me/setting/payXieYi'
				})
			},
			/**
			 * @param {Number} price
			 * 价格保留两位小数
			 */
			ymfparsePrice(price) {
				return price.toFixed(2)
			},
			//生成订单
			ymfgetOrderInfo() {
				if (this.checked == false) {
					this.$refs.uToast.show({
						title: '请阅读并同意《付费须知说明》',
						duration: 1500
					})
					return
				}
				let data = {
					ymfCourseId: this.ymfCourseId
				}
				if (this.type != 1) { //购买单集
					data.courseDetailsId = this.courseDetailsId
				}
				this.$Request.getT('/app/order/insertCourseOrders', data).then(res => {
					if (res.code == 0) {
						this.ymfpayOrder(res.data.orders.ordersId)
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
					}
				})


			},
			/**
			 * @param {Object} orderId 订单id
			 */
			ymfpayOrder(orderId) {
				let that = this
				if (that.payAway == 4) { //抖音支付
					uni.showLoading({
						title: '支付中...'
					})
					this.$Request.postT('/app/dyPay/payVirtualAppOrder', {
						orderId: orderId
					}).then(res => {
						if (res.code == 0) {
							tt.requestOrder({
								data: JSON.stringify(res.data.data), // 请勿在前端对data做任何处理
								byteAuthorization: res.data
									.byteAuthorization, // 请勿在前端对byteAuthorization做任何处理
								success(redd) {
									console.log(JSON.stringify(redd))
									tt.getOrderPayment({
										orderId: redd.orderId,
										success(reee) {
											uni.hideLoading()
											uni.showToast({
												title: '支付成功'
											})
											setTimeout(function() {
												let data = {
													flag: true
												}
												uni.$emit('back', data)
												uni.navigateBack();
											}, 1000)
										},
										fail(ee) {
											uni.hideLoading()
											uni.showToast({
												title: '支付失败',
												icon: 'none'
											})
											console.log(ee)
										}
									});

								},
								fail(e) {
									console.log(e)
								}
							});
						}
					})
				} else if (that.payAway == 5) { //快手支付
					uni.showLoading({
						title: '支付中...'
					})
					this.$Request.postT('/app/ksPay/payAppOrder', {
						orderId: orderId
					}).then(res => {
						if (res.code == 0) {
							ks.pay({
								serviceId: '1', //服务类型id（固定值为‘1’）
								orderInfo: res.data,
								success(succ) {
									uni.hideLoading()
									uni.showToast({
										title: '支付成功'
									})
									setTimeout(function() {
										let data = {
											flag: true
										}
										uni.$emit('back', data)
										uni.navigateBack();
									}, 1000)
								},
								fail(err) {
									uni.hideLoading()
									that.$refs.uToast.show({
										title: '支付失败，请重试!',
										duration: 1500
									})
								},
							})
						} else {
							uni.hideLoading()
							this.$refs.uToast.show({
								title: res.msg,
								duration: 1500
							})
						}
					})
				} else {
					switch (this.payAway) { //1:微信支付,2:支付宝支付 3:金币支付
						case 1: //微信支付
							// #ifdef MP-WEIXIN
							that.$Request.postT("/app/wxPay/wxPayJsApiOrder", {
								orderId: orderId,
							}).then(red => {
								if (red.code == 0) {
									uni.requestPayment({
										provider: 'wxpay',
										timeStamp: red.data.timestamp,
										nonceStr: red.data.noncestr,
										package: red.data.package,
										signType: red.data.signType,
										paySign: red.data.sign,
										success: function(redd) {
											uni.showLoading({
												title: '支付成功'
											});
											uni.hideLoading();
											setTimeout(function() {
												let data = {
													flag: true
												}
												uni.$emit('back', data)
												uni.navigateBack();
											}, 1000)
										},
										fail: function(err) {
											uni.hideLoading();
											that.$queue.showToast(
												'支付失败');
										}
									});
								} else {
									uni.showToast({
										title: red.msg,
										icon: 'none'
									})
								}
							});
							// #endif

							// #ifdef H5
							let ua = navigator.userAgent.toLowerCase();
							console.log(ua)
							if (ua.indexOf('micromessenger') !== -1) {
								that.$Request.postT("/app/wxPay/wxPayMpOrder", {
									orderId: orderId,
								}).then(red => {
									if (red.code == 0) {
										that.ymfcallPay(red.data);
									} else {
										that.isPay = true
										uni.showToast({
											title: red.msg,
											icon: 'none'
										})
									}
								});
							}

							// #endif

							// #ifdef APP-PLUS
							that.$Request.postT("/app/wxPay/payAppOrder", {
								orderId: orderId,
							}).then(red => {
								if (red.code == 0) {
									console.log(red, '+++++++++++++++++++++')
									that.ymfsetPayment('wxpay', JSON.stringify(red.data));
								} else {
									that.isPay = true
									uni.showToast({
										title: red.msg,
										icon: 'none'
									})
								}
							});
							// #endif
							break;
						case 2: //支付宝
							// #ifdef H5
							that.$Request.postT("/app/aliPay/ymfpayOrder", {
								orderId: orderId,
								classify: 2
							}).then(red => {
								if (red.code == 0) {
									const div = document.createElement('div')
									div.innerHTML = red.data //此处form就是后台返回接收到的数据
									document.body.appendChild(div)
									document.forms[0].submit()
								} else {
									uni.showToast({
										title: red.msg,
										icon: 'none'
									})
								}
							});
							// #endif
							// #ifdef APP-PLUS
							that.$Request.postT("/app/aliPay/ymfpayOrder", {
								orderId: orderId,
								classify: 1
							}).then(red => {
								if (red.code == 0) {
									that.ymfsetPayment('alipay', red.data);
								} else {
									uni.showToast({
										title: red.msg,
										icon: 'none'
									})
								}
							});
							// #endif
							break;
						default: //零钱
							that.$Request.postT("/app/order/ymfpayOrders", {
								orderId: orderId,
							}).then(res => {
								if (res.code == 0) {
									uni.showToast({
										title: '支付成功'
									})
									setTimeout(function() {
										let data = {
											flag: true
										}
										uni.$emit('back', data)
										uni.navigateBack();
									}, 1000)
								} else {
									// uni.showToast({
									// 	title: res.msg,
									// 	icon: 'none'
									// })
									uni.showModal({
										title: '提示',
										content: '支付失败，余额不足！',
										confirmText: '去充值',
										complete(ret) {
											if (ret.confirm) {
												uni.navigateTo({
													url: '/me/wallet/wallet'
												})
											}
										}
									})
								}
							});
							break;
					}
				}
			},
			ymfcallPay: function(response) {
				if (typeof WeixinJSBridge === "undefined") {
					if (document.addEventListener) {
						document.addEventListener('WeixinJSBridgeReady', this.ymfonBridgeReady(response), false);
					} else if (document.attachEvent) {
						document.attachEvent('WeixinJSBridgeReady', this.ymfonBridgeReady(response));
						document.attachEvent('onWeixinJSBridgeReady', this.ymfonBridgeReady(response));
					}
				} else {
					this.ymfonBridgeReady(response);
				}
			},
			ymfonBridgeReady: function(response) {
				let that = this;
				if (!response.package) {
					return;
				}
				WeixinJSBridge.invoke(
					'getBrandWCPayRequest', {
						"appId": response.appid, //公众号名称，由商户传入
						"timeStamp": response.timestamp, //时间戳，自1970年以来的秒数
						"nonceStr": response.noncestr, //随机串
						"package": response.package,
						"signType": response.signType, //微信签名方式：
						"paySign": response.sign //微信签名
					},
					function(res) {
						if (res.err_msg === "get_brand_wcpay_request:ok") {
							// 使用以上方式判断前端返回,微信团队郑重提示：
							//res.err_msg将在用户支付成功后返回ok，但并不保证它绝对可靠。
							uni.removeStorageSync('EditAddress')
							uni.showLoading({
								title: '支付成功'
							});
							uni.hideLoading();
							setTimeout(function() {
								let data = {
									flag: true
								}
								uni.$emit('back', data)
								uni.navigateBack();
							}, 1000)
						} else {
							uni.hideLoading();
						}
						WeixinJSBridge.log(response.err_msg);
					}
				);
			},
			ymfsetPayment(name, order) {
				let that = this;
				uni.requestPayment({
					provider: name,
					orderInfo: order, //微信、支付宝订单数据
					success: function(res) {
						uni.showLoading({
							title: '支付成功'
						});
						uni.hideLoading();
						setTimeout(function() {
							let data = {
								flag: true
							}
							uni.$emit('back', data)
							uni.navigateBack();
						}, 1000)
					},
					fail: function(err) {
						uni.hideLoading();
						console.log(err, 12)
					}
				});
			},
		}
	}
</script>

<style lang="scss">
	/deep/.u-radio {
		display: block !important;
	}

	.ymf-illustrate {
		width: 100%;
		margin: 30rpx 0;

		.ymf-illustrate-box {
			width: 686rpx;
		}

		.ymf-illustrate-box-txt {
			color: #5173F8;
		}

	}

	/deep/.u-radio__label {
		display: none !important;
	}

	.ymf-info {
		width: 100%;
		height: auto;

		.ymf-info-box {
			width: 686rpx;
			height: 100%;
			border-radius: 24rpx;
			padding: 30rpx;
			background-color: #ffffff;
		}

		.ymf-info-box-l {
			width: 200rpx;
			height: 150rpx;
			border-radius: 8rpx;

			image {
				width: 100%;
				height: 100%;
				border-radius: 8rpx;
			}
		}

		.ymf-info-box-r {
			margin-left: 30rpx;

			.ymf-info-box-r-t {
				font-weight: bold;
			}

			.ymf-info-box-r-b {
				margin-top: 20rpx;
				color: #5074FF;

				text {
					font-size: 32rpx;
					font-weight: bold;
				}
			}
		}
	}

	.price {
		width: 100%;
		height: auto;
		margin-top: 30rpx;

		.price-box {
			width: 686rpx;
			height: 100%;
			background-color: #ffffff;
			border-radius: 24rpx;
			padding: 30rpx;
		}

		.price-box-price {
			color: #5074FF;
			font-size: 32rpx;
			font-weight: bold;
		}
	}

	.pay {
		width: 100%;
		height: auto;
		margin-top: 30rpx;

		.pay-box {
			width: 686rpx;
			height: 100%;
			background-color: #ffffff;
			border-radius: 24rpx;
			padding: 30rpx;
		}

		.pay-box-title {
			width: 100%;
			font-size: 32rpx;
			font-weight: bold;

		}

		.pay-box-item {
			width: 100%;
			margin-top: 40rpx;

			.pay-box-item-l {
				font-weight: 500;

				image {
					width: 60rpx;
					height: 60rpx;
					margin-right: 30rpx;
				}
			}
		}
	}

	.submit {
		width: 100%;
		height: auto;
		position: fixed;
		bottom: 0;
		left: 0;
		background-color: #ffffff;
		padding: 20rpx 0;

		.submit-box {
			width: 686rpx;
			height: 88rpx;
			border-radius: 40rpx;
			background-color: #5074FF;
			color: #ffffff;
			font-weight: 500;
		}
	}
</style>