<template>
	<view>
		<view class="concont width94 mart10 backcorfff height45 alijusstart paddtop5 paddbotm5">
			<input class="width100 padleft15 " v-model="customerMobile" placeholder="请输入充值手机号" />
		</view>
		<view class="concont width94 mart10 backcorfff">
			<view class="width94 paddtop10 font14 fcor333">
				充值详情
			</view>
			<view class="width100 bor-botm1 mart10 marb10"></view>
			<view class="width100">
				<view class="width100 alijusstart mart10 bor-botm1 paddbotm10">
					<view class="width70 padleft15">
						<view class="width100 font14 fcor333 fontwig6">{{goodName}}</view>
						<view class="width100 font12 fcor999 mart5">
						</view>
					</view>
					<view class="width10 font12 fcor999">x1</view>
					<view class="width20">
						<view class="width100 fcor333 font12">¥<text class="fontwig6 font16">{{totalprice}}</text>
						</view>
						<view class="width100 fcor999 font12" v-if="totalprice != oldPrice"
							style="text-decoration: line-through;">¥{{oldPrice}}
						</view>
					</view>
				</view>
				<view class="width90 fotrt height50 font13 fcor999">
					共 <text class="fcor333 fontwig6" style="margin: 0px 5px;"> 1 </text> 件 小计:
					<text class="font16 colorc3 fontwig6 margle"> ¥{{totalprice}}</text>
					<text class="font12 fcor999 fontwig6 margle" style="text-decoration: line-through;">
						¥{{orderPrice}}</text>
				</view>
			</view>
		</view>
		<view class="width94 concont mart10 backcorfff marb100">
			<view class="height50 width100 paddtop5 backcorfff" @click="showPopup()">
				<view class="width50 flleft fcor333 fontwig6 font16" style="padding-left: 4%;">
					优惠抵扣<text class="yhqky">{{OrderPreList.length}}张可用</text>
				</view>
				<view class="width40 flright fotrt paddtright10 font15 fontwig6 fcor666 alijun"
					style="align-items: center;" v-if="deductionPrice == 0">
					未使用
					<image style="width: 40rpx;height: 40rpx;" src="../../static/img/jt.png"></image>
				</view>
				<view class="width40 flright fotrt paddtright10 font15 fontwig6 fcoreb5 alijun"
					style="align-items: center;" v-else>
					-￥{{deductionPrice}}
					<image style="width: 40rpx;height: 40rpx;" src="../../static/img/jt.png"></image>
				</view>
			</view>
			<view class="width94 line1 mart5 marb5"></view>
			<view class="Bubble alijusstart font14 fcor999">
				选取: <text class="tests fotct font14 colorc3 paddleft5 paddtright5">{{priceValue[1]}}</text> 分
			</view>
			<view class="width92 alijusstart paddtop10">
				<view class="width30 fcor333 fontwig6 font16">
					积分抵扣
				</view>
				<view class="cj-slider">
					<view class="flleft fotct font14 fcor666" style="width: 15%;">0</view>
					<cj-slider style="width: 60%; float:left;" v-if="isDiscount" v-model="priceValue" :min="0"
						:max="availIntegal" :step="1" :blockWidth="40" @start="blockStart" @moving="blockMoving"
						@end="blockEnd" />
					<view class="flright fotrt font14 fcor666" style="width: 25%;">{{availIntegal}}</view>
				</view>
			</view>
			<view class="width94 line1 mart15 marb5"></view>
			<view class="height50 width100 backcorfff">
				<view class="width70 flleft fcor666 font16 text1" style="padding-left: 4%;">
					抵扣金额
				</view>
				<view class="width20 flright fotrt paddtright10 font15 fcor666 alijun" style="align-items: center;">
					¥{{priceValue[1] / 100}}
				</view>
			</view>
			<view class="width94 line1 marb5"></view>
			<view class="height50 width100 backcorfff">
				<view class="width40 flleft fcor333 fontwig6 font15 text1 alijusstart" style="padding-left: 4%;">
					<image src="../../static/img/rebate.png" mode="widthFix" style="width: 25px;"></image>
					<text>下单返积分</text>
				</view>
				<view class="width50 flright fotrt paddtright10 font15 colore95 alijun" v-if="Integrastu == 1"
					style="align-items: center;">
					返{{rebateIntegral}}积分
				</view>
				<view class="width50 flright fotrt paddtright10 font15 colore95 alijun" v-if="Integrastu == 2"
					style="align-items: center;">
					{{rebateIntegral}}
				</view>
			</view>
			<view class=" width100 backcorfff alijusstart paddbotm10" style="padding-left: 4%;">
				<image src="../../static/img/sigh.png" mode="widthFix" class="icon20"></image>
				<text class="fcor999 font13">积分抵扣与优惠券抵扣不同享</text>
			</view>
			<view class="width94 line1 marb5"></view>
			<view class="height50 width100">
				<view class="width100 flleft fcor333 fontwig6 font16" style="padding-left: 4%;">
					支付方式
				</view>
			</view>
			<view class="width100" v-for="(item,index) in paytypeList" :key="index">
				<view class="width94 line1 mart5 marb5" v-if="item == 4"></view>
				<view class="height50 width100 backcorfff" @tap="paytype='4'" v-if="item == 4">
					<view class="width50 flleft fcor666  font16" style="padding-left: 4%;">
						银联支付
					</view>
					<view class="width40 flright fotrt paddtright10 font15 fontwig6 fcor666 alijun"
						style="align-items: center;" v-if="isUse">
						<radio :checked="paytype=='4'" color="#c72a20" />
					</view>
				</view>
				<view class="width94 line1 mart5 marb5" v-if="item == 3"></view>
				<view class="height50 width100 backcorfff" @tap="paytype='3'" v-if="item == 3">
					<view class="width70 flleft fcor666  font16 text1" style="padding-left: 4%;">
						汇联通工会卡<text class="font14 fcor666 margle">可用余额: {{tongCardPrice}}元</text>
					</view>
					<view class="width20 flright fotrt paddtright10 font15 fontwig6 fcor666 alijun"
						style="align-items: center;" v-if="isUse">
						<radio :checked="paytype=='3'" @click="changeRiado()" color="#c72a20" />
					</view>
				</view>
			</view>
		</view>

		<view class="footbtn width100 height50 backcor33">
			<view class="width94 alijusstart">
				<view class="width70 fcorfff">
					合计: ¥<text class="font16 fontwig6" style="margin: 0px 6px;">{{payPrice}}</text>
				</view>
				<view class="width30 fotrt" @click="qzaddorder()">
					<view class="btnpay fotct font13 fcorfff" style="background-color: #c72a20;">去支付
					</view>
				</view>
			</view>
		</view>

		<wybPopup ref="popup" type="bottom" width="500" :scrollY="true" radius="6" :showCloseIcon="true">
			<view class="fotct font18 fontwig6 fcor333 mart10 height30">优惠券选择</view>
			<view class="width92 height110 tccs mart10 alijus" v-for="(items, index) in OrderPreList" :key="index"
				@click="radioChanges(items)">
				<image mode="widthFix" :src="imageUrl+items.discountImg"></image>
				<view class="alijus width60 margle10">
					<view class="width80p">
						<view class="font16 fontwig6 fcor333 text1">{{items.discountName}}</view>
						<view class="font13 fcor999 mart5">有效期:{{items.useEndTime | timeFormat('yyyy-mm-dd hh:mm:ss')}}
						</view>
					</view>
					<view class="width20 fotrt">
						<view>
							<radio v-if="items.discountCondition && orderPrice > items.discountCondition"
								color="#c72a20" :checked="items.id == memDiscountId" />
							<radio v-if="items.discountCondition && orderPrice < items.discountCondition"
								color="#c72a20" disabled="true" :checked="items.id == memDiscountId" />
							<radio v-if="!items.discountCondition" color="#c72a20"
								:checked="items.id == memDiscountId" />
						</view>
					</view>
				</view>
			</view>
		</wybPopup>
		<ssPaymentPassword ref="paymentPassword" :mode="1" @submit="submitHandle" />
	</view>
</template>

<script>
	import cjSlider from '@/components/cj-slider/cj-slider.vue';
	import ssPaymentPassword from '@/components/sanshui-payment-password/index.vue';
	import wybPopup from '../../components/wyb-popup/wyb-popup.vue';
	import {
		xbkaddOrder,
		getThirdPartyByDetail,
		getHuiLianTongCardBalance,
		findUser,
		qianzhuorderToUnionPay,
		czhltUnionCardPay,
		getUserOrderPreList,
		getRebateIntegral
	} from '../../Utils/Api.js';
	// #ifdef H5
	var jweixin = require('jweixin-module');
	// #endif
	let app = getApp();
	export default {
		components: {
			cjSlider,
			ssPaymentPassword,
			wybPopup
		},
		data() {
			return {
				orderList: [],
				paytype: '4', //支付方式
				priceValue: [0, 0], // 积分可以指定默认值
				availIntegal: 0, // 可用积分
				totalprice: 0, //总价
				oldPrice: 0, //原价
				disconutprice: 0, //优惠价格
				totalnum: '', //总数量
				customerMobile: '', //电话号码

				paytypeList: [], //支付方式
				tongCardPrice: 0, //工会卡余额
				user: '', //个人信息
				isUse: true, // 是否禁用状态
				payPrice: 0, //支付价格
				isDiscount: true, //刷新组件
				showType: 3, //平台
				memDiscountId: '', //优惠券id
				PaymentPassword: '', //支付密码
				goodId: '', //商品id
				goodName: '', //商品name
				orderId: '', //订单id
				deductionPrice: '0.00', //优惠金额
				OrderPreList: [], //优惠券数据
				imageUrl: app.globalData.imgUrl, //图片地址
				orderPrice: 0, //订单价格
				rechargeDes: '', // 支付详情
				rebateIntegral: '', //返回积分
				Integrastu: 1 //返积分状态 
			}
		},
		onLoad(options) {
			this.goodId = options.goodid;
			this.oldPrice = options.oldprice;
			this.orderPrice = options.oldprice;
			this.totalprice = options.price;
			this.payPrice = options.price;
			this.goodName = options.name;
		},
		onShow() {
			this.customerMobile = app.globalData.userInfo.phone;
			this.findUser();
		},
		methods: {
			//查询支付方式
			getThirdPartyByDetail() {

				let datas = {
					platformId: this.showType,
					regionId: app.globalData.cityId,
					productType: 3
				}
				getThirdPartyByDetail(datas).then(res => {
					if (res.return_code == '000000') {
						this.rechargeDes = res.return_data;
						this.paytypeList = res.return_data.productPayType.split(',');
						this.paytypeList = this.paytypeList.slice(0, this.paytypeList.length - 1);
						let zkprice = parseFloat(res.return_data.integralDiscount / 100).toFixed(2);
						let pprice = parseFloat(this.totalprice * 100).toFixed(0)
						let payprice = parseFloat(this.totalprice * 100 * zkprice).toFixed(
							0);
						if (payprice > this.user.gold) {
							this.availIntegal = this.user.gold;
						} else {
							this.availIntegal = payprice;
						}
					}
				});
			},
			//查询积分
			findUser() {
				findUser().then(res => {
					if (res.return_code == '000000') {
						app.globalData.userInfo = res.return_data;
						this.user = res.return_data;
						this.getHuiLianTongCardBalance();
						this.getThirdPartyByDetail();
						this.getUserOrderPreList();
						this.getRebateIntegral();
						uni.setStorage({
							key: "user",
							data: res.return_data
						})
					}
				});
			},
			//支付返利积分
			getRebateIntegral() {
				let datas = {
					"companyId": app.globalData.companyId,
					"price": this.payPrice,
					"productId": 3,
					"type": 4,
				}
				getRebateIntegral(datas).then(res => {
					if (res.return_code == '000000') {
						this.rebateIntegral = res.return_data;
						this.Integrastu = 1;
					} else {
						this.rebateIntegral = res.return_msg;
						this.Integrastu = 2;
					}
				})
			},
			//查询工会卡详情
			getHuiLianTongCardBalance() {
				if (!this.user.hltCardNo) {
					return;
				}
				let params = {
					cardNo: this.user.hltCardNo.cardNo
				}
				getHuiLianTongCardBalance(params).then(res => {
					if (res.return_code == '000000') {
						this.tongCardPrice = res.return_data.balance;
					}
				});
			},
			//刷新组件
			reload() {
				this.isDiscount = false;
				this.$nextTick(() => (this.isDiscount = true))
			},
			//滑动开始
			blockStart() {},
			blockMoving() {
				// console.log('正在滑动中')
			},

			//滑动结束
			blockEnd() {
				this.isDiscount = 1;
				if (this.deductionPrice == 0) {
					this.payPrice = Math.abs(parseFloat(this.totalprice - parseFloat(this.priceValue[1] / 100).toFixed(2))
						.toFixed(
							2));
				} else {
					this.payPrice = Math.abs(parseFloat(this.orderPrice - parseFloat(this.priceValue[1] / 100).toFixed(2) -
						this
						.deductionPrice).toFixed(2));
				}
				if (this.payPrice == 0) {
					this.isUse = false;
					this.paytype = '';
				} else {
					this.isUse = true;
				}
				this.getRebateIntegral();
			},
			//获取选择支付方式
			changeRiado() {
				if (!this.user.isSetHltCard) {
					uni.showToast({
						icon: 'none',
						title: '当前账号还未绑定,前往绑定',
						duration: 2000,
						success() {
							setTimeout(() => {
								uni.navigateTo({
									url: '/pages/user/bindingCard/bindingCard'
								})
							}, 1000)
						}
					});
					return;
				}
			},
			//下单
			qzaddorder() {
				if (this.customerMobile == '') {
					uni.showToast({
						title: '请输入充值手机号',
						icon: 'none',
						duration: 2000
					})
					return;
				}
				if (this.payPrice == 0) {
					if (!this.user.isSetPayPwd) {
						uni.navigateTo({
							url: '../../pages/login/updatePas/updatePas'
						})
						return;
					}
					this.$refs.paymentPassword.modalFun('show');
					return;
				}

				if (this.paytype == '') {
					uni.showToast({
						title: '请选择支付方式',
						icon: 'none',
						duration: 2000
					})
					return;
				}
				uni.showLoading({
					title: '提交订单中...'
				})
				let datas = {
					"customerMobile": this.customerMobile,
					"regionId": app.globalData.cityId,
					"productType": 3,
					"platformId": this.showType,
					"password": this.PaymentPassword,
					"integralNum": this.priceValue[1],
					"memDiscountId": this.memDiscountId,
					"goodsId": this.goodId
				}

				xbkaddOrder(datas).then(res => {
					uni.hideLoading();
					if (res.return_code == '000000') {
						this.orderId = res.return_data.id;
						let params = {
							"orderId": res.return_data.id,
						}
						this.qianzhuorderToUnionPay(params);
					} else {
						uni.showToast({
							title: res.return_msg,
							icon: 'none',
							duration: 2000
						})
					}
				})
			},
			//查询优惠券
			getUserOrderPreList() {
				let params = {
					useScope: 6
				}
				getUserOrderPreList(params).then(res => {
					if (res.return_code == '000000') {
						this.OrderPreList = res.return_data;
					}
				});
			},
			//弹出优惠券
			showPopup() {
				this.$refs.popup.show();
			},
			//选择优惠券
			radioChanges(item) {
				this.rechangeload();
				if (this.memDiscountId == item.id) { // 如果已经选中，则取消选中
					this.memDiscountId = '';
					this.deductionPrice = '0.00';
					this.priceCaluc(this.totalprice);
				} else { // 否则进行选中赋值
					if (item.discountType == 1) {
						if (this.orderPrice > item.discountCondition) {
							this.memDiscountId = item.id;
							//满减价格
							this.deductionPrice = item.discountPrice;
							let oldprice = this.orderPrice - this.deductionPrice;
							this.priceCaluc(oldprice);
						} else {
							uni.showToast({
								title: '请满' + item.discountCondition + '使用此券',
								icon: 'none',
								duration: 2000
							})
						}
					}
					if (item.discountType == 2) {
						this.memDiscountId = item.id;
						//抵扣价格
						this.deductionPrice = item.discountPrice;
						let oldprice = this.orderPrice - this.deductionPrice;
						this.priceCaluc(oldprice);
					}
					if (item.discountType == 3) {
						this.memDiscountId = item.id;
						// 打折
						this.deductionPrice = parseFloat(this.orderPrice - (this.orderPrice * item.discountPrice))
							.toFixed(2);
						let oldprice = parseFloat(this.orderPrice * item.discountPrice).toFixed(2);
						this.priceCaluc(oldprice);

					}
				}
				this.$refs.popup.hide();
			},
			//计算价格
			priceCaluc(item) {
				let zkprice = parseFloat(this.rechargeDes.integralDiscount / 100).toFixed(2);
				let pprice = parseFloat(item * 100).toFixed(0);
				let payprice = parseFloat(item * 100 * zkprice).toFixed(0);
				if (payprice > this.user.gold) {
					this.availIntegal = this.user.gold;
					this.priceValue[1] = 0;
				} else {
					this.availIntegal = payprice;
					this.priceValue[1] = 0;
				}
				this.reload();
				this.calculatepayPrice();
			},
			//计算支付价格
			calculatepayPrice() {
				if (this.deductionPrice == 0) {
					this.payPrice = Math.abs(parseFloat(this.totalprice - (parseFloat(this.priceValue[1] / 100).toFixed(
							2)))
						.toFixed(2));
				} else {
					this.payPrice = Math.abs(parseFloat(this.orderPrice - (parseFloat(this.priceValue[1] / 100).toFixed(
						2)) - this
						.deductionPrice).toFixed(2));
				}
				
				this.getRebateIntegral();
			},
			rechangeload() {
				this.isUse = false;
				this.$nextTick(() => (this.isUse = true))
			},
			//刷新组件
			reload() {
				this.isDiscount = false;
				this.$nextTick(() => (this.isDiscount = true))
			},
			//获取订单数据
			qianzhuorderToUnionPay(item) {
				let that = this;
				if (that.paytype == '4') {
					qianzhuorderToUnionPay(item).then(res => {
						if (res.return_code == '000000') {
							that.uniontopay(res.return_data.prepayid);
						} else {
							uni.showToast({
								title: res.return_msg,
								duration: 2000,
								icon: 'none'
							})
						}
					})
				} else if (that.paytype == '3') {
					if (that.tongCardPrice < that.payPrice) {
						uni.showToast({
							icon: 'none',
							title: '工会卡余额不足',
							duration: 2000,
						});
						return;
					}
					if (!that.user.isSetPayPwd) {
						uni.navigateTo({
							url: '../../pages/login/updatePas/updatePas'
						})
						return;
					}
					if (!that.user.isSetHltCard) {
						uni.showToast({
							icon: 'none',
							title: '当前账号还未绑定,前往绑定',
							duration: 2000,
							success() {
								setTimeout(() => {
									uni.navigateTo({
										url: '../../pages/user/bindingCard/bindingCard'
									})
								}, 1000)
							}
						});
						return;
					}
					that.$refs.paymentPassword.modalFun('show');
				} else {
					uni.showToast({
						title: '请选择支付方式',
						icon: 'none',
						duration: 2000
					})
				}
			},
			//银联支付
			uniontopay(item) {
				let that = this;
				upsdk.pluginReady(function() {
					upsdk.pay({
						tn: item,
						success: function(res) {
							uni.showToast({
								title: '支付成功'
							})
							uni.reLaunch({
								url: '/qianzhu-KFC/order-details/order-details?id=' + this
									.orderId
							})
						},
						fail: function(err) {
							uni.showToast({
								title: err.msg,
								icon: 'none',
								duration: 2000
							})
							// 支付失败, err.msg 是失败原因描述, 比如TN号不合法, 或者用户取消了交易 等等。
						}
					});
				});
			},
			submitHandle(e) {
				uni.showLoading({
					title: '支付中...'
				})
				this.PaymentPassword = e.value;
				if (this.PaymentPassword == '') {
					uni.showToast({
						title: '请勿手动关闭弹窗',
						icon: 'none',
						duration: 2000
					})
					uni.hideLoading();
					return;
				}
				if (this.payPrice == 0) {
					let datas = {
						"customerMobile": this.customerMobile,
						"regionId": app.globalData.cityId,
						"productType": 3,
						"platformId": this.showType,
						"password": this.PaymentPassword,
						"integralNum": this.priceValue[1],
						"memDiscountId": this.memDiscountId,
						"goodsId": this.goodId
					}

					xbkaddOrder(datas).then(res => {
						uni.hideLoading();
						if (res.return_code == '000000') {
							this.orderId = res.return_data.id;
							uni.reLaunch({
								url: '/qianzhu-KFC/order-details/order-details?id=' + this.orderId
							})
							return;
						}
						if (res.return_code == '102130') {
							uni.navigateTo({
								url: '../../login/updatePas/updatePas'
							})
							return;
						}
						uni.showToast({
							title: res.return_msg,
							icon: 'none',
							duration: 2000
						});
					})
					return;
				}
				let params = {
					"orderId": this.orderId,
					"cardNo": this.user.hltCardNo.cardNo,
					"password": this.PaymentPassword
				}
				czhltUnionCardPay(params).then(res => {
					uni.hideLoading();
					if (res.return_code == '000000') {
						uni.showToast({
							title: '支付成功'
						})
						uni.reLaunch({
							url: '/qianzhu-KFC/order-details/order-details?id=' + this.orderId
						})
						return;
					}
					if (res.return_code == '102130') {
						uni.navigateTo({
							url: '../../login/updatePas/updatePas'
						})
						return;
					}
					uni.showToast({
						title: res.return_msg,
						icon: 'none',
						duration: 2000
					})
				})


			}
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #f0f0f0;
	}

	.headsbg {
		border-radius: 0 0 30px 30px;
	}

	.concont {
		border-radius: 12px;
	}

	.Bubble {
		margin-left: 50%;
		padding-top: 15px;
	}

	.tests {
		border-radius: 15px;
		height: 22px;
		line-height: 22px;
	}

	.jfrecharge {
		padding: 3px 20rpx;
		color: #fff;
		margin-left: 50%;
		justify-content: center;
		align-items: center;
		font-size: 30rpx;
		border-radius: 40rpx;
	}

	.footbtn {
		bottom: 0px;
		position: fixed;
	}

	.btnpay {
		width: 90px;
		border-radius: 22px;
		height: 36px;
		line-height: 36px;
	}

	.takemode {
		width: 48%;
		border-radius: 5px;
		border: 1px solid #999999;
	}

	.aftermode {
		background-color: #faf1f1;
		color: #c72a20;
	}

	.cj-slider {
		width: 100%;
		align-items: center;
		display: flex;
	}

	.yhqky {
		background: #ff0034;
		color: #ffffff;
		font-size: 14px;
		padding-left: 5px;
		padding-right: 5px;
		margin-left: 8px;
		padding-top: 2px;
		padding-bottom: 2px;
	}

	.yhprice {
		margin-top: -30px;
	}

	.contrig {
		margin-left: 180rpx;
	}

	.tccs {
		box-shadow: 0rpx 0rpx 10rpx rgba(0, 0, 0, 0.2);
		border: 1px solid #f6f6f6;
		border-radius: 8px;

		image {
			width: 160rpx;
			max-height: 160rpx;
		}
	}
</style>
