<template>
	<view class="container cashier">
		<view class="container-cash clearfix">
			<view class="container-wrapper">
				<view class="pay-content">
					<view class="pay-p"><text>待付金额</text></view>
					<view class="pay-money">
						<text class="pay-sm">￥</text> <text class="pay-bg">{{ handelPayMsg.price | NumFormat }}</text>
					</view>
					<view class="pay-mouth">套餐：{{ handelPayMsg.duration }}个月</view>
				</view>
				<view class="pay-check">
					<view class="pay-checked">
						<view class="pay-card-main" v-if="cardsList.length > 0">
							<view class="pay-item-cell" @click="tabClick(3)">
								<view class="item-icon">
									<image
										:src="
											'https://static.caimei365.com/app/img/pay/icon_' +
												defaultCards.bankCode +
												'_@2x.png'
										"
									></image>
								</view>
								<view class="item-texts"
									>{{ defaultCards.bankName }}(
									{{ defaultCards.quickPayBankNumber | bankNumeberFilter }} )</view
								>
								<view class="item-checked">
									<text
										class="iconfont"
										:class="[tabCurrentIndex === 3 ? 'icon-yixuanze3' : 'icon-weixuanze']"
									></text>
								</view>
							</view>
							<view class="pay-item-cell add" @click="hanldeShowPopup(1)">
								<view class="item-texts add"><text>查看全部银行卡 / 添加新卡支付</text></view>
								<view class="item-checked"> <text class="iconfont icon-xiayibu"></text> </view>
							</view>
						</view>
						<view class="pay-item" @click="handleAddCard(2)" v-else>
							<view class="pay-item-cell">
								<view class="item-texts add"><text>添加银行卡快捷付款</text></view>
								<view class="item-checked"> <text class="iconfont icon-xiayibu"></text> </view>
							</view>
						</view>
<!-- 						<view class="pay-item" @click="tabClick(0)">
							<view class="pay-item-cell">
								<view class="item-icon"><text class="iconfont icon-weixinzhifu"></text></view>
								<view class="item-texts"><text>微信支付</text></view>
								<view class="item-checked">
									<text
										class="iconfont"
										:class="[tabCurrentIndex === 0 ? 'icon-yixuanze3' : 'icon-weixuanze']"
									></text>
								</view>
							</view>
						</view> -->
						<view class="pay-item" @click="tabClick(1)">
							<view class="pay-item-cell">
								<view class="item-icon"><text class="iconfont icon-qiyewangyinzhifu"></text></view>
								<view class="item-texts"><text>企业网银支付</text></view>
								<view class="item-checked">
									<text
										class="iconfont"
										:class="[tabCurrentIndex === 1 ? 'icon-yixuanze3' : 'icon-weixuanze']"
									></text>
								</view>
							</view>
						</view>
						<view class="pay-item" @click="tabClick(2)">
							<view class="pay-item-cell">
								<view class="item-icon"><text class="iconfont icon-gerenwangyinzhifu"></text></view>
								<view class="item-texts"><text>个人网银支付</text></view>
								<view class="item-checked">
									<text
										class="iconfont"
										:class="[tabCurrentIndex === 2 ? 'icon-yixuanze3' : 'icon-weixuanze']"
									></text>
								</view>
							</view>
						</view>
					</view>
				</view>
			</view>
			<view class="pay-button" :style="{ paddingBottom: isIphoneX ? '68rpx' : '24rpx' }">
				<view class="btn" @click.stop="buttonSubMit" :style="{ background: btnColor }">{{ buttonText }}</view>
			</view>
		</view>
		<!-- 选择快捷支付银行 -->
		<cm-cards :show="popupShow1" :list="cardsList" @handleChoiceaCards="handleChoiceaCards" v-if="popupShow1"></cm-cards>
		<!-- 网银链接说明弹窗 -->
		<view class="alert spec" :class="specClass" v-if="isShowTip">
			<view class="payun-alert" @tap="hideTips">
				<view class="content">
					<view class="title">
						<text>支付链接</text> <text class="iconfont icon-iconfontguanbi" @click.stop="hideTips"></text>
					</view>
					<view class="text-content">
						<view class="text"
							>请复制以下链接，并发送至电脑端，在浏览器访问该链接并选择银行尽快完成支付</view
						>
						<view class="text-p">{{ payHttpUrl }}</view> <view class="text-b">链接有效期为72小时</view>
					</view>
					<view class="text-button" @click.stop="clipboard(payHttpUrl)">复制链接</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
import authorize from '@/common/config/authorize.js'
const thorui = require('@/components/clipboard/clipboard.thorui.js')
import payMixins from '@/mixins/payMixins.js'
import cardMixins from '../pay/mixins/cardMixins.js'
import cmCards from '../pay/components/cm-cards-popup'
export default {
	mixins: [payMixins, cardMixins],
	components: {
		cmCards
	},
	data() {
		return {
			isIphoneX: this.$store.state.isIphoneX,
			CustomBar: this.CustomBar, // 顶部导航栏高度
			isShowTip: false,
			handelPayMsg: {},
			payType: 0,
			isPaySwitch: true,
			payParams: {
				payType: 'XCX',
				code: 0,
				vipRecordId: 0
			},
			payHttpUrl: 'https://www.caimei365.com',
			popupShow1: false,
			quickParams: {
			    userId: 0, 					//机构UserId
			    quickPayBankNumber: '', 	//快捷支付用户银行卡号/信用卡号
			    quickPayMobile: '', 		//快捷支付银行卡绑定手机号
			    quickPayBankExpireTime: '', //快捷支付信用卡过期时间,只包含年月，格式yy-MM
			    quickPayUserName: '', 		//快捷支付用户姓名
			    idCard: '', 				//身份证号
			    cvvCode: '',				//信用卡安全码
			    quickPayFlag: 2,
			    vipRecordId: 0,
			},
		}
	},
	onLoad(option) {
		this.initGetStotage(option)
	},
	filters: {
		NumFormat(value) {
			if (!value) return '0.00'
			/*原来用的是Number(value).toFixed(0)，这样取整时有问题，例如0.51取整之后为1，感谢Nils指正*/
			/*后来改成了 Number(value)|0,但是输入超过十一位就为负数了，具体见评论 */
			var intPart = Number(value) - (Number(value) % 1) //获取整数部分（这里是windy93的方法）
			var intPartFormat = intPart.toString().replace(/(\d)(?=(?:\d{3})+$)/g, '$1,') //将整数部分逢三一断
			var floatPart = '.00' //预定义小数部分
			var value2Array = value.toString().split('.')
			//=2表示数据有小数位
			if (value2Array.length == 2) {
				floatPart = value2Array[1].toString() //拿到小数部分

				if (floatPart.length == 1) {
					//补0,实际上用不着
					return intPartFormat + '.' + floatPart + '0'
				} else {
					return intPartFormat + '.' + floatPart
				}
			} else {
				return intPartFormat + floatPart
			}
		}
	},
	methods: {
		async initGetStotage(option) {
			let data = JSON.parse(option.data)
            this.tabCurrentIndex = 3
            this.tabClick(this.tabCurrentIndex)
			this.payParams.vipRecordId = this.quickParams.vipRecordId = data.vipRecordId
			this.handelPayMsg = data
			this.PayOrderOnLineSwitch()
			this.GetUserClubBanks()
			console.log(this.handelPayMsg)
		},
		PayOrderOnLineSwitch() {
			// 线上支付开关
			this.PayService.PayOrderOnLineSwitch()
				.then(response => {
					if (response.data === 0) {
						this.isPaySwitch = true
					} else {
						this.isPaySwitch = false
					}
				})
				.catch(error => {
					console.log('线上支付开关异常')
				})
		},
		async GetUserClubBanks() {
		    //获取列表
		    try {
		        const user = await this.$api.getStorage()
		        const res = await this.UserService.userClubBanks({ userId:user.userId })
		        this.cardsList = res.data
		        this.defaultCards = this.cardsList[0]
		        this.quickParams.userId = user.userId 
		        this.quickParams.quickPayBankNumber = this.defaultCards.quickPayBankNumber 
		        this.quickParams.quickPayMobile = this.defaultCards.quickPayMobile 
		        this.quickParams.quickPayBankExpireTime = this.defaultCards.quickPayBankExpireTime ? this.defaultCards.quickPayBankExpireTime : ''
		        this.quickParams.quickPayUserName = this.defaultCards.quickPayUserName 
		        this.quickParams.idCard = this.defaultCards.idCard 
		        this.quickParams.cvvCode = this.defaultCards.cvvCode ? this.defaultCards.cvvCode : '' 
		    } catch (error) {
		        console.log(error)
		    }
		},
		handleAddCard(index) {
			//跳转添加卡号支付
			//quickPayFlag 2超级会员,vipRecordId :会员购买记录Id
			const data = {
				payData: {
					quickPayFlag: 2,
					vipRecordId: this.payParams.vipRecordId
				},
				payAmount: this.handelPayMsg.price
			}
			this.$api.navigateTo(`/pages/user/pay/card-comfirm?type=${index}&data=${JSON.stringify(data)}`)
		},
		buttonSubMit() {
			// 友盟埋点收集选择支付方式
			if (process.env.NODE_ENV != 'development') {
				this.$uma.trackEvent('Um_Event_userClubMemberChooseWechatPay', {
					Um_Key_PageName: '支付超级会员',
					Um_Key_SourcePage: '支付超级会员',
					Um_Key_PayName: `${this.buttonText}`,
					Um_Key_Month: `${this.handelPayMsg.duration}个月`
				})
			}
			if (this.isPaySwitch) {
				this.$util.msg('支付系统遇到点小问题，请稍后重试')
				return
			}
			switch (this.tabCurrentIndex) {
				case 0: // 微信小程序支付
					this.MiniWxPayFor()
					break
				case 1: // 生成企业网银链接
					this.handlePayOrderPayLink()
					break
				case 2: // 生成个人网银链接
					this.handlePayOrderPayLink()
					break
				case 3: // 快捷支付
                    if(!this.quickParams.quickPayBankNumber){
                        this.$util.msg('请先添加快捷支付银行卡', 2000)
                        return
                    }
					this.orderPayQuickPay()
					break
			}
		},
		async orderPayQuickPay() {
			//快捷支付
			try {
				const res = await this.PayService.orderPayQuickPay(this.quickParams)
				const data = {
					params: this.quickParams,
					payData: res.data,
					payAmount: this.handelPayMsg.price
				}
				this.$api.navigateTo(
					`/pages/user/pay/card-comfirm-sub?type=3&data=${JSON.stringify(data)}`
				)
			} catch (error) {
				this.$util.msg(error.msg, 2000)
			}
		},
		async handlePayOrderPayLink() {
			// 生成网银支付链接
			try {
				const linkParams = {
					vipRecordId: this.payParams.vipRecordId,
					payType: this.payType
				}
			    const res = await this.PayService.PayOrderPayLink(linkParams)
				this.payHttpUrl = res.data
				this.isShowTip = true
				console.log('isShowTip',this.isShowTip)
			} catch (error) {
			    console.log(error)
			}
		},
		async MiniWxPayFor() {
			// 使用微信支付
			this.payParams.code = await authorize.getCode('weixin')
			this.weChatMiniVipWxPay(this.payParams)
		},
		WxRequestPayment(data) {
			// 微信支付Api
			let self = this
			console.log('data', data)
			wx.requestPayment({
				timeStamp: data.timeStamp,
				nonceStr: data.nonceStr,
				package: data.package,
				signType: data.signType,
				paySign: data.paySign,
				success: function(res) {
					wx.reLaunch({ url: '/pages/tabBar/user/user' })
				},
				fail: function(res) {
					self.$util.msg('用户取消支付~')
				},
				complete: function(res) {}
			})
		},
		hideTips() {
			//隐藏弹窗
			this.isShowTip = false
		},
		clipboard(data) {
			thorui.getClipboardData(data, res => {
				if (res) {
					this.isShowTip = false
					this.$util.msg('复制成功', 2000, true, 'success')
					// // 友盟埋点收集复制网银链接
					// if(process.env.NODE_ENV != 'development'){
					// 	this.$uma.trackEvent('Um_Event_CopyUnionPay', {
					// 		Um_Key_PageName: '网银支付',
					// 		Um_Key_SourcePage: '线上支付',
					// 		Um_Key_PayName:`${this.buttonText}`,
					// 		Um_Key_PayOrderID:`${this.orderId}`
					// 	})
					// }
				} else {
					this.$util.msg('复制失败', 2000, true, 'none')
				}
			})
		}
	},
	onShow() {}
}
</script>

<style lang="scss">
page {
	height: auto !important;
	background-color: #ffffff;
}
.container-cash {
	width: 100%;
	margin-top: 40rpx;
	.container-wrapper {
		width: 100%;
		box-sizing: border-box;
		padding: 0 24rpx;
		.pay-content {
			width: 100%;
			height: 254rpx;
			padding: 92rpx 40rpx 16rpx 40rpx;
			box-sizing: border-box;
			background: url(https://static.caimei365.com/app/img/icon/icon-member-pay@2x.png) no-repeat;
			background-size: cover;
			float: left;
			position: relative;
			.pay-p {
				font-size: $font-size-26;
				color: #4e4539;
				line-height: 36rpx;
			}
			.pay-money {
				color: #4e4539;
				line-height: 84rpx;
				font-weight: bold;
				.pay-sm {
					font-size: $font-size-26;
				}
				.pay-bg {
					font-size: 50rpx;
				}
			}
			.pay-mouth {
				position: absolute;
				right: 40rpx;
				bottom: 16rpx;
				line-height: 36rpx;
				font-size: $font-size-26;
				color: #4e4539;
			}
		}
		.pay-check {
			width: 100%;
			height: auto;
			float: left;
			margin-top: 40rpx;
			.check-title {
				width: 622rpx;
				height: 40rpx;
				line-height: 40rpx;
				padding: 0 20rpx;
				margin-top: 24rpx;
				.text {
					font-size: $font-size-28;
					color: $text-color;
					text-align: left;
					float: left;
				}
				.icon {
					width: 40rpx;
					height: 40rpx;
					border-radius: 50%;
					line-height: 40rpx;
					text-align: center;
					color: #ffffff;
					font-size: $font-size-24;
					background: radial-gradient(
						circle,
						rgba(225, 86, 22, 1) 0%,
						rgba(255, 170, 0, 1) 67%,
						rgba(249, 185, 156, 1) 100%
					);
					float: right;
				}
			}
			.pay-checked {
				width: 100%;
				height: auto;
				.pay-card-main {
					width: 100%;
					height: auto;
					float: left;
					border-top: 12px solid #f7f7f7;
					border-bottom: 12px solid #f7f7f7;
					box-sizing: border-box;
					padding: 0 32rpx;
					.pay-item-cell {
						height: 100rpx;
						box-sizing: border-box;
						padding: 26rpx 0;
						background-color: #ffffff;
						border-bottom: 1px solid #e1e1e1;
						&.add {
							border-bottom: none;
							padding-left: 66rpx;
						}
						.item-icon {
							width: 48rpx;
							height: 48rpx;
							margin-right: 20rpx;
							float: left;
							image {
								width: 48rpx;
								height: 48rpx;
								display: block;
							}
						}
						.item-texts {
							line-height: 48rpx;
							font-size: $font-size-28;
							color: $text-color;
							float: left;
							&.add {
								font-weight: bold;
							}
						}
						.item-checked {
							width: 48rpx;
							height: 48rpx;
							float: right;
							text-align: center;
							line-height: 48rpx;
							font-size: $font-size-40;
							color: #ffffff;
							.icon-weixuanze {
								color: #b2b2b2;
							}
							.icon-yixuanze3 {
								color: #ff5b00;
							}
							.icon-xiayibu {
								color: #b2b2b2;
							}
						}
					}
				}
				.pay-item {
					width: 100%;
					height: auto;
					box-sizing: border-box;
					float: left;
					padding: 0 32rpx;
					.pay-item-cell {
						height: 100rpx;
						box-sizing: border-box;
						padding: 26rpx 0;
						background-color: #ffffff;
						border-bottom: 1px solid #e1e1e1;
						.item-icon {
							width: 48rpx;
							height: 48rpx;
							float: left;
							text-align: center;
							line-height: 48rpx;
							margin-right: 20rpx;
							.iconfont {
								font-size: 48rpx;
							}
							.icon-weixinzhifu {
								color: #0abc64;
							}
							.icon-gerenwangyinzhifu {
								color: #16afe8;
							}
							.icon-qiyewangyinzhifu {
								color: #007acc;
							}
						}
						.item-texts {
							line-height: 48rpx;
							font-size: $font-size-28;
							color: $text-color;
							float: left;
							&.add {
								font-weight: bold;
							}
						}
						.item-checked {
							width: 48rpx;
							height: 48rpx;
							float: right;
							text-align: center;
							line-height: 48rpx;
							font-size: $font-size-40;
							color: #ffffff;
							.icon-weixuanze {
								color: #b2b2b2;
							}
							.icon-yixuanze3 {
								color: #ff5b00;
							}
							.icon-xiayibu {
								color: #b2b2b2;
							}
						}
					}
				}
			}
		}
	}
	.pay-button {
		width: 600rpx;
		background-color: #ffffff;
		position: fixed;
		height: 88rpx;
		padding: 24rpx 75rpx 0 75rpx;
		bottom: 0;
		.btn {
			width: 100%;
			height: 88rpx;
			border-radius: 44rpx;
			font-size: $font-size-28;
			line-height: 88rpx;
			color: #ffffff;
			margin: 0 auto;
			text-align: center;
			background: $btn-confirm;
		}
	}
	.pay-bring-wrapper {
		width: 100%;
		padding: 24rpx 0;
		background-color: #ffffff;
		display: flex;
		align-items: center;
		flex-direction: column;
		.pay-bring-content {
			width: 654rpx;
			height: auto;
			padding: 0 24rpx;
			.text {
				font-size: $font-size-24;
				color: #666;
				line-height: 44rpx;
				text-align: center;
				&.bg-color {
					color: $color-system;
					line-height: 88rpx;
				}
			}
			.text-v {
				font-size: $font-size-28;
				color: #999;
				line-height: 70rpx;
				text-align: left;
				&.title {
					font-size: $font-size-28;
					color: #666666;
				}
				&.bg-color {
					line-height: 44rpx;
					color: $color-system;
				}
				.clipboard {
					width: 84rpx;
					height: 36rpx;
					background: linear-gradient(34deg, rgba(255, 41, 41, 1) 0%, rgba(255, 109, 27, 1) 100%);
					text-align: center;
					font-size: $font-size-24;
					color: #ffffff;
					border-radius: 4rpx;
					line-height: 36rpx;
					display: inline-block;
					margin-left: 10rpx;
				}
			}
		}
	}
}
.payun-alert {
	width: 100%;
	height: 100%;
	background: rgba(0, 0, 0, 0.5);
	position: fixed;
	top: 0;
	left: 0;
	z-index: 8888;
	transition: all 0.4s;
	&.none {
		display: none;
	}
	&.show {
		display: block;
	}
	.content {
		width: 422rpx;
		height: 434rpx;
		position: absolute;
		background: $bg-color;
		left: 0;
		right: 0;
		bottom: 0;
		top: 0;
		margin: auto;
		padding: 20rpx 32rpx;
		border-radius: 12rpx;
		.title {
			width: 100%;
			height: 68rpx;
			line-height: 68rpx;
			font-size: $font-size-28;
			color: $text-color;
			text-align: center;
			position: relative;
			.icon-iconfontguanbi {
				width: 68rpx;
				height: 68rpx;
				text-align: center;
				line-height: 68rpx;
				position: absolute;
				right: 0;
				top: 0;
				font-size: $font-size-36;
				color: #999999;
			}
		}
		.text-content {
			width: 100%;
			height: auto;
			.text {
				padding: 20rpx 0 0 0;
				line-height: 44rpx;
				font-size: $font-size-26;
				color: #666666;
				text-align: justify;
			}
			.text-p {
				width: 100%;
				line-height: 44rpx;
				font-size: $font-size-26;
				color: $color-system;
				text-align: left;
				text-overflow: ellipsis;
				display: -webkit-box;
				word-break: break-all;
				-webkit-box-orient: vertical;
				-webkit-line-clamp: 1;
				overflow: hidden;
			}
			.text-b {
				line-height: 44rpx;
				font-size: $font-size-24;
				color: #999999;
				text-align: left;
			}
		}
		.text-button {
			width: 100%;
			height: 88rpx;
			line-height: 88rpx;
			background: $btn-confirm;
			font-size: $font-size-28;
			border-radius: 44rpx;
			color: #ffffff;
			text-align: center;
			margin-top: 20rpx;
		}
	}
}
</style>
