<template>
	<view class="payment">
		<uni-nav-bar dark :fixed="true" background-color="#00aaff" :border="false" status-bar title="加油" />

		<view class="container">
			<uni-section title="油号" type="line" titleFontSize="34rpx"></uni-section>
			<view class="select-oil">
				<view class="oil-item">
					<view class="oil active-oil">
						<!-- <view class="unit">￥</view> -->
						<view class="number">#95</view>
					</view>
				</view>
			</view>

			<uni-section title="输入金额" type="line" titleFontSize="34rpx"></uni-section>
			<view class="money-input">
				<image src="/static/payment/money.png" mode="widthFix"></image>
				<input v-model="form.money" @focus="focus" placeholder="请输入加油金额" type="number" />
			</view>

			<view v-if="form.money" class="tip">
				折后共计{{ 
				(form.money * 
				(configList.preferential ?
				configList.preferential.keyCentent
				: 1)).toFixed(2)
				}}元
			</view>
			<view class="select-money">
				<view v-for="(item, index) in rechargeList" class="money-item" :key="index">
					<view @click="selectMoney(item.price,index)" :class="{ 'active-money' : currentIndex == index }"
						class="money">
						<view class="unit">￥</view>
						<view class="number">{{ item.price }}</view>
					</view>
				</view>
			</view>
			<view class="sumit" @click="submit">提交订单</view>

		</view>

		<PrivacyAgreementPoup ref="showPrivacy"></PrivacyAgreementPoup>

		<uni-fab ref="fab" :content="content" :horizontal="horizontal" :vertical="vertical" :direction="direction"
			@trigger="clickMenu" />

	</view>
</template>

<script setup>
	import PrivacyAgreementPoup from "@/components/PrivacyAgreementPoup/PrivacyAgreementPoup.vue";
	import { mapState } from 'vuex'
	export default {
		name: 'Payment',
		components: {
			PrivacyAgreementPoup
		},
		computed : {
			...mapState(['configList']),
		},
		data() {
			return {
				form: {
					money: ''
				},
				resolvePrivacyAuthorization: {},
				currentIndex: 0,
				content: [{
					iconPath: '/static/payment/wedding-celebration.png',
					text: '婚庆服务',
					active: false,
					path: '/pages/weddingCelebration/weddingCelebration'
				}],
				horizontal: 'right',
				vertical: 'bottom',
				direction: 'vertical',
				rechargeList : []
			}
		},
		onShow() {
			if (wx.onNeedPrivacyAuthorization) {
				console.log('onNeedPrivacyAuthorization');
				wx.onNeedPrivacyAuthorization(resolve => {
					console.log('onNeedPrivacyAuthorization');
					this.resolvePrivacyAuthorization = resolve
					this.$refs.showPrivacy.init(resolve)
				})
			}
			// wx.getPrivacySetting({
			// 	success: res => {
			// 		console.log(res)
			// 		if (res.needAuthorization) {
			// 			// 需要弹出隐私协议
			// 			this.$refs.showPrivacy.init()
			// 		}
			// 	},
			// 	fail: () => {}
			// })
			this.getRechargePage()
		},
		methods: {
			
			// 用户选择加油金额
			selectMoney(money, item) {
				this.form.money = money
				this.currentIndex = item
			},

			//输入框获得焦点
			focus() {

			},

			//用户点击了悬浮按钮
			clickMenu({
				item
			}) {
				uni.navigateTo({
					url: item.path
				})
			},

			submit() {
				if (!uni.getStorageSync('token')) {
					return uni.navigateTo({
						url: '/pages/login/login'
					})
				}
				this.$api('createOrderPay', this.form, res => {
					this.form.money = ''
					if(res.code == 200){
						uni.requestPayment({
							provider: 'wxpay', // 服务提提供商
							timeStamp: res.result.timeStamp, // 时间戳
							nonceStr: res.result.nonceStr, // 随机字符串
							package: res.result.packageValue,
							signType: res.result.signType, // 签名算法
							paySign: res.result.paySign, // 签名
							success: function (res) {
								console.log('支付成功',res);
								uni.switchTab({
									url: '/pages/center/center'
								})
							},
							fail: function (err) {
								console.log('支付失败',err);
								uni.showToast({
									icon:'none',
									title:"支付失败"
								})
							}
						});
						
						// uni.showToast({
						// 	icon : 'none',
						// 	title: '支付成功'
						// });
					}
				})
			},
			
			//获取充值套餐
			getRechargePage(){
				this.$api('getRechargePage', res => {
					this.rechargeList = res.result.records
				})
			}
		}
	}
</script>

<style scoped>
	.payment {
		height: 100vh;
		background: #F1F5F8;
		width: 750rpx;
		margin: 0 auto;
	}

	.container {
		width: 96%;
		margin: 0rpx auto;
		border-radius: 20rpx;
		box-sizing: border-box;
		padding: 20rpx;
		overflow: hidden;
		background: white;
		margin-top: 20rpx;
	}

	.money-input {
		display: flex;
		align-items: center;
		background: #F6F7FB;
		padding: 30rpx 10rpx;
		border-radius: 20rpx;
	}

	.tip {
		color: #00aaff;
		margin-top: 10rpx;
	}

	.money-input image {
		width: 45rpx;
	}

	.money-input input {
		font-size: 36rpx;
	}

	.select-oil,
	.select-money {
		display: flex;
		justify-content: space-between;
		flex-wrap: wrap;
		margin: 30rpx 0rpx;
	}

	.select-oil {
		margin: 0;
	}

	.select-oil .oil-item,
	.select-money .money-item {
		width: 32.33%;
		background: #F1F5F8;
		border-radius: 20rpx;
		margin-bottom: 20rpx;
		overflow: hidden;
	}

	.select-oil .oil,
	.select-money .money {
		display: flex;
		align-items: center;
		justify-content: center;
		padding: 30rpx 0rpx;
		box-sizing: border-box;
		color: #5D5C61;
	}

	.select-oil .active-oil,
	.select-money .active-money {
		background: #00aaff;
		color: white;
	}

	.select-money .unit {
		font-size: 26rpx;
	}

	.select-money .number {
		font-size: 34rpx;
	}


	.sumit {
		background: #33a5fc;
		color: white;
		font-size: 36rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		height: 80rpx;
		border-radius: 20rpx;
	}
</style>