<template>
	<view class="app">
		<view class="bg">
		</view>
		<view class="moneyBox">
			<view class="price-box">
				<text>提前还款金额</text>
			</view>
			<view class="moneyNumBox flex">
				<text class="fh">￥</text>
				<input class="moneyValue" type="text" v-model="money" />
			</view>
			<view class="day">
				服务天数剩余{{day}}天
			</view>
		</view>
		<view class="moneyBox margin-t-30 padding-t-30">
			<view class="yt-list">
				<view class="yt-list-cell" @click="payType='yue'">
					<view class="cell-tit flex">
						<image class="orderIcon" src="../../../static/icon/ye.png" mode="widthFix"></image>
						<text class="margin-l-10">余额({{now_money}})</text>
					</view>
					<image class="checked" v-if="payType=='yue'" src="../../../static/icon/addressIconXz.png"
						mode="widthFix">
					</image>
					<view v-else class="noChecked"></view>
				</view>
			</view>
		</view>
		<view class="base-buttom" :class="{ clickbg: payLoding }" @click="!payLoding ? confirm() : ''">确认支付</view>
	</view>
</template>

<script>
	import {
		balance
	} from '@/api/wallet.js';
	import {
		getCarInfo,
		prepayment
	} from '@/api/user.js';
	export default {
		data() {
			return {
				payType: 'yue',
				money: 0, //订单金额
				allMoney: 0, //总的还款金额
				now_money: 0.0, //余额
				car_number: '', //保存订单id
				payLoding: false, //判断是否支付中
				minMoney: 0, //最低还款金额
			};
		},
		onLoad(options) {
			this.car_number = options.id;
			// 载入余额
			balance({}).then(({
				data
			}) => {
				// 获取余额
				this.now_money = data.now_money;
			});
			getCarInfo({
				car_number: this.car_number
			}).then((e) => {
				this.money = e.data.surplus;
				this.allMoney = e.data.surplus;
				this.minMoney = +e.data.day_deducted
				this.day = e.data.day
			})
		},
		methods: {
			// 支付成功跳转
			paySuccessTo() {
				uni.hideLoading();
				uni.redirectTo({
					url: '/pages/user/money/paySuccess?type=1'
				});
			},
			//确认支付
			confirm: async function() {
				let obj = this;
				// 判断是否余额不足
				if (obj.payType == 'yue' && +obj.now_money < obj.money) {
					uni.showModal({
						title: '提示',
						content: '账户余额不足！',
						showCancel: false,
					});
					return;
				}
				// 判断是否为未支付订单中跳转进入
				if (this.money < this.minMoney) {
					uni.showModal({
						title: '提示',
						content: '还款金额不可低于' + this.minMoney + '元',
						showCancel: false,
					});
					return
				}
				if (this.money % this.minMoney != 0 && this.money < this.allMoney) {
					uni.showModal({
						title: '提示',
						content: '还款金额必须为' + this.minMoney + '的倍数',
						showCancel: false,
					});
					return
				}

				// 支付中
				uni.showLoading({
					title: '支付中',
					mask: true
				})
				obj.payLoding = true;
				prepayment({
					'car_number': this.car_number,
					money: this.money
				}).then((e) => {
					uni.hideLoading()
					uni.showToast({
						title: e.msg
					});
					obj.payLoding = false;
					uni.showModal({
						title: '提示',
						content: e.msg,
						showCancel: false,
						cancelText: '取消',
						confirmText: '返回',
						success: res => {
							if(res.confirm){
								uni.navigateBack()
							}
						},
						fail: () => {},
						complete: () => {}
					});
				}).catch((e) => {
					obj.payLoding = false;
					uni.hideLoading()
					console.log(e);
				})
			}
		}
	};
</script>

<style lang="scss">
	.app {
		width: 100%;
		padding-top: 30rpx;
	}

	.moneyBox {
		position: relative;
		margin: 0 $page-row-spacing;
		border-radius: 20rpx;
		overflow: hidden;
		background-color: #FFFFFF;
		padding: 50rpx 30rpx 30rpx;

		.moneyNumBox {
			color: $font-color-base;
			border-bottom: 1px solid $page-color-light;

			.fh {
				font-size: $font-base + 2rpx;

			}

			.moneyValue {
				font-size: $font-lg + 16rpx;
			}
		}

		.day {
			padding-top: 30RPX;
			font-size: $font-sm - 2rpx;
			color: $font-color-light;
		}
	}

	.bg {
		background-color: $color-green;
		width: 950rpx;
		position: absolute;
		left: -100rpx;
		border-bottom-left-radius: 1000rpx;
		border-bottom-right-radius: 1000rpx;
		top: 0rpx;
		height: 300rpx;
	}

	.price-box {
		background-color: #fff;
		padding-bottom: 60rpx;
		font-size: $font-base;
		color: $font-color-base;
		font-weight: bold;

		.price {
			font-size: 50upx;
			color: #303133;
			margin-top: 12upx;

			&:before {
				content: '￥';
				font-size: 40upx;
			}
		}
	}

	.pay-type-list {
		margin-top: 20upx;
		background-color: #fff;
		padding-left: 60upx;

		.type-item {
			height: 120upx;
			padding: 20upx 0;
			display: flex;
			justify-content: space-between;
			align-items: center;
			padding-right: 60upx;
			font-size: 30upx;
			position: relative;
		}

		.icon {
			width: 100upx;
			font-size: 52upx;
		}

		.iconyue {
			color: #fe8e2e;
		}

		.iconweixin {
			color: #36cb59;
		}

		.iconzhifubao {
			color: #01aaef;
		}

		.tit {
			font-size: $font-lg;
			color: $font-color-dark;
			margin-bottom: 4upx;
		}

		.con {
			flex: 1;
			display: flex;
			flex-direction: column;
			font-size: $font-sm;
			color: $font-color-light;
		}
	}

	.clickbg {
		background-color: $color-gray !important;
	}

	.yt-list {
		background: #fff;
		border-radius: 20rpx;
		width: 100%;
	}

	.yt-list-cell {
		display: flex;
		align-items: center;
		justify-content: space-between;
		line-height: 70rpx;
		position: relative;

		.checked,
		.noChecked {
			width: 36rpx;
			height: 36rpx;
		}

		.noChecked {
			border: 1px solid $font-color-light;
			border-radius: 100rpx;
		}

		&.cell-hover {
			background: #fafafa;
		}

		&.b-b:after {
			left: 30rpx;
		}

		.cell-icon {
			height: 32rpx;
			width: 32rpx;
			font-size: 22rpx;
			color: #fff;
			text-align: center;
			line-height: 32rpx;
			background: #f85e52;
			border-radius: 4rpx;
			margin-right: 12rpx;

			&.hb {
				background: #ffaa0e;
			}

			&.lpk {
				background: #3ab54a;
			}
		}

		.cell-more {
			align-self: center;
			font-size: 24rpx;
			color: $font-color-light;
			margin-left: 8rpx;
			margin-right: -10rpx;
		}

		.cell-tit {
			font-size: 26rpx;
			color: $font-color-light;
			margin-right: 10rpx;

			.orderIcon {
				width: 48rpx;
			}
		}

		.cell-tip {
			font-size: 26rpx;
			color: $font-color-dark;

			&.disabled {
				color: $font-color-light;
			}

			&.active {
				color: $base-color;
			}

			&.red {
				color: $base-color;
			}
		}

		&.desc-cell {
			.cell-tit {
				max-width: 90rpx;
			}
		}

		.desc {
			text-align: right;
			font-size: $font-base;
			color: $font-color-light;
		}
	}
</style>
