<template>
	<view class="cash">
		<!-- 提现输入框 -->
		<far-bottom></far-bottom>
		<view class="cashInput">
			<view class="cashInputPackage">
				<view><span>提现金额</span></view>
				<view>
					<span>¥</span>
					<input placeholder="请输入金额" v-model="settlementmoney.money" />
				</view>
				<view>
					<block class="dis-flex">
						<span style="line-height: 20upx;font-size: 23upx;">可提现(元)：{{ creditCashInfo.nowmoney}}</span>
						<span style="line-height: 20upx;font-size: 23upx;">预留金额(元)：{{ creditCashInfo.reservemoney }}</span>
					</block>

					<view @click="allWithdrawal(0)"><span>全部提现</span></view>
				</view>
				<view style="color: #FF4444;font-size: 22upx;">提现金额{{ creditCashInfo.lowsetmoney }}元起, 
				<text v-if="creditCashInfo.maxsetmoney !== 999999999">本次最高提现金额{{creditCashInfo.maxsetmoney}}元</text> 提现手续费{{ creditCashInfo.syssalepercent }}%</view>
			</view>
		</view>
		<!-- 提现输入框 -->

		<!-- 选择支付方式 -->
		<view class="checkPayType">
			<view class="checkPayTypePackage">
				<view><span>提现到</span></view>
				<!-- 微信 -->
				<view class="type" v-if="creditCashInfo.wechatstatus == 1" @click="cashtype = 2">
					<view class="typePackage">
						<view>
							<image :src="imgfixUrls + 'merchant/wechatPay.svg'" />
							<span>微信零钱</span>
						</view>
						<view class="checked" v-if="cashtype == 2">
							<image :src="imgfixUrls + 'merchant/payCheck.svg'" />
						</view>
						<view v-else></view>
					</view>
				</view>
				<!-- 微信 -->
				<!-- 渠道1 -->
				<view class="type" v-if="creditCashInfo.alipaystatus == 1" @click="cashtype = 1">
					<view class="typePackage">
						<view>
							<image :src="imgfixUrls + 'merchant/payPal.svg'" />
							<span>{{examineing==0?'支付宝':'渠道1'}}</span>
						</view>
						<view class="checked" v-if="cashtype == 1">
							<image :src="imgfixUrls + 'merchant/payCheck.svg'" />
						</view>
						<view v-else></view>
					</view>
					<view class="yhkInput" v-if="cashtype == 1">
						<view>
							<span>真实姓名</span>
							<input placeholder="请填写真实姓名" v-model="creditCashInfo.realname" />
						</view>
						<view>
							<span>{{examineing==0?'支付宝':'渠道1'}}</span>
							<input :placeholder="examineing==0?'请输入支付宝账号':'请输入渠道1账号'" v-model="creditCashInfo.alipay" />
						</view>
					</view>
				</view>
				<!-- 渠道1 -->
				<!-- 渠道2 -->
				<view class="type" v-if="creditCashInfo.bankcardstatus == 1" @click="cashtype = 3">
					<view class="typePackage">
						<view>
							<image :src="imgfixUrls + 'merchant/yhk.png'" />
							<span>{{examineing==0?'银行':'渠道2'}}账户</span>
						</view>
						<view class="checked" v-if="cashtype == 3">
							<image :src="imgfixUrls + 'merchant/payCheck.svg'" />
						</view>
						<view v-else></view>
					</view>
					<view class="yhkInput" v-if="cashtype == 3">
						<view>
							<span>真实姓名</span>
							<input placeholder="请输入姓名" v-model="creditCashInfo.bank_username" />
						</view>
						<view>
							<span>开户{{examineing==0?'银行':'渠道2'}}</span>
							<input placeholder="请输入开户行名称/地址" v-model="creditCashInfo.bank_name" />
						</view>
						<view>
							<span>{{examineing==0?'银行卡':'渠道2'}}号</span>
							<input :placeholder="examineing==0?'请输入银行卡号':'请输入渠道2号'" type="number" v-model="creditCashInfo.card_number" />
						</view>
					</view>
				</view>
				<!-- 渠道2 -->
				<view class="inputFoot">
					<span></span>
					<view>
						<span>请务必填写真实信息</span>
						<span>*</span>
					</view>
				</view>
			</view>
		</view>
		<!-- 选择支付方式 -->

		<!-- 提交按钮 -->
		<view class="cashSubmit" @click="creditCashing"><span>提交审核</span></view>
		<!-- 提交按钮 -->
	</view>
</template>

<script>
	import App from '@/common/js/app.js';

	// #ifdef H5
	import wxApi from '@/common/js/wxApi.js';
	// #endif

	export default {
		data() {
			return {
				creditCashInfo: {},
				settlementmoney: {
					money: ''
				},
				cashtype: '',
				setWith: '',
				storeid: null,
				examineing:1
			};
		},
		onLoad: function() {
			this.init();
			let TextSubstitution = uni.getStorageSync('TextSubstitution');
		this.examineing = TextSubstitution.examineing || 0;
		},
		methods: {
			allWithdrawal(index) {
				let withMoney = Number(this.creditCashInfo.nowmoney);
				console.log(withMoney, Number(this.creditCashInfo.maxsetmoney))
				if (index !== 0) {
					if(index == 1){
						this.$set(this.settlementmoney, 'money', Number(this.creditCashInfo.maxsetmoney));
						return
					}else{
						this.$set(this.settlementmoney, 'money', Number(this.creditCashInfo.nowmoney));
						return
					}
				} else {
					if (withMoney > Number(this.creditCashInfo.maxsetmoney)) {
						uni.showToast({
							title: `单次最大提现金额为${this.creditCashInfo.maxsetmoney}元`,
							icon: 'none'
						})
						this.$set(this.settlementmoney, 'money', Number(this.creditCashInfo.maxsetmoney));
						return
					} else if ((withMoney == Number(this.creditCashInfo.maxsetmoney) || withMoney < Number(this.creditCashInfo.maxsetmoney)) &&
						withMoney > 0) {
						uni.showToast({
							title: `本次最大提现金额为${withMoney}元`,
							icon: 'none'
						})
						this.$set(this.settlementmoney, 'money', Number(this.creditCashInfo.nowmoney));
						return
					} else {
						this.$set(this.settlementmoney, 'money', 0);
						return
					}
				}

			},
			init() {
				let _this = this;
				uni.getStorage({
					key: 'checkStoreid',
					success: function(res) {
						_this.storeid = res.data;
						_this.creditCashPage(res.data);
					}
				});
			},
			// 商户提现页面
			creditCashPage(storeid) {
				let _this = this,
					data = {
						storeid: storeid
					};
				App._post_form(
					'&p=store&do=cashPage',
					data,
					res => {
						_this.creditCashInfo = res.data;
						if(Number(_this.creditCashInfo.maxsetmoney) == 0){
							_this.creditCashInfo.maxsetmoney = 999999999;
						}
						if (res.data.wechatstatus == 1) {
							_this.cashtype = 2;
						} else {
							if (res.data.alipaystatus == 1) {
								_this.cashtype = 1;
							} else {
								_this.cashtype = 3;
							}
						}
					},
					fail => {
						uni.navigateBack({
							delta: 1
						});
					}
				);
			},
			// 确认提现
			withdraw(data) {
				App._post_form('&p=store&do=cashApply', data, res => {
					App.showSuccess('提现申请成功', () => {
						uni.navigateBack();
					});
					return;
				});
			},
			// 余额提现申请
			creditCashing() {
				let _this = this,
					datas = {
						storeid: _this.storeid,
						settlementmoney: _this.settlementmoney.money,
						cashtype: _this.cashtype,
						alipay: _this.creditCashInfo.alipay,
						realname: _this.creditCashInfo.realname || '',
						bank_name: _this.creditCashInfo.bank_name,
						card_number: _this.creditCashInfo.card_number,
						bank_username: _this.creditCashInfo.bank_username
					};
				// console.log(_this.settlementmoney.money, '0000')
				if (_this.settlementmoney.money === '') {
					uni.showToast({
						icon: 'none',
						title: '提现金额不能为空',
						duration: 2000
					});
					return;
				}
				if (_this.cashtype == 1) {
					if (_this.creditCashInfo.alipay == '') {
						uni.showToast({
							icon: 'none',
							title: _this.examineing?'支付宝账号不能为空':'渠道1账号不能为空',
							duration: 2000
						});
						return;
					}
					if (!_this.creditCashInfo.realname) {
						uni.showToast({
							icon: 'none',
							title: '真实姓名不能为空',
							duration: 2000
						});
						return;
					}
				}
				if (_this.cashtype == 3) {
					if (_this.creditCashInfo.bank_username == '') {
						uni.showToast({
							icon: 'none',
							title: '真实姓名不能为空',
							duration: 2000
						});
						return;
					}
					if (_this.creditCashInfo.bank_name == '') {
						uni.showToast({
							icon: 'none',
							title: '开户行不能为空',
							duration: 2000
						});
						return;
					}
					if (_this.creditCashInfo.card_number.length < 13 || _this.creditCashInfo.card_number.length > 25) {
						uni.showToast({
							icon: 'none',
							title: _this.examineing?'银行卡号格式不正确':'渠道2格式不正确',
							duration: 2000
						});
						return;
					}
				}
				console.log(Number(_this.settlementmoney.money), Number(_this.creditCashInfo.nowmoney), 1111);
				if (Number(_this.settlementmoney.money) > Number(_this.creditCashInfo.maxsetmoney) || Number(_this.settlementmoney.money) >
					Number(_this.creditCashInfo.nowmoney)) {
					if (Number(_this.settlementmoney.money) > Number(_this.creditCashInfo.maxsetmoney)) {
						uni.showToast({
							icon: 'none',
							title: `单次余额提现最大为${_this.creditCashInfo.maxsetmoney}`,
							duration: 2000
						});
						_this.allWithdrawal(1);
					} else if (Number(_this.settlementmoney.money) > Number(_this.creditCashInfo.nowmoney)) {
						uni.showToast({
							icon: 'none',
							title: `本次余额提现最大为${_this.creditCashInfo.nowmoney}`,
							duration: 2000
						});
						_this.allWithdrawal(2);
					}

					return;
				}

				let currentPrice = _this.settlementmoney.money; //实际金额
				let serviceCharge = _this.creditCashInfo.syssalepercent; //手续费
				let calculate = {
					money: currentPrice,
					percent: serviceCharge
				};
				App._post_form('&p=pay&do=calculationCash', calculate, res => {
					if (res.errno === 0) {
						let conpmoney = res.data.realmoney;
						if(Number(conpmoney) < 1){
							uni.showToast({
								title:`最低实得金额不能小于1元,当前实得金额为${conpmoney}元`,
								icon:'none'
							});
							return
						}
						App.showError(
							`确认提现${currentPrice}元,实得${conpmoney}元`,
							data => {
								if (data.confirm) {
									!!data && _this.withdraw(datas);
								}
							},
							true
						);
					}
				});
				// let realTimePrice = Math.round((currentPrice - (Number(serviceCharge) / 100 * currentPrice)) * 100) / 100;//四舍五入
				// App.showError(
				// 	`确认提现${currentPrice}元,实得${realTimePrice}元`,
				// 	data => {
				// 		if (data.confirm) {
				// 			!!data && _this.withdraw(datas);
				// 		}
				// 	},
				// 	true
				// );
			}
		}
	};
</script>

<style lang="less" scoped>
	.cash {
		font-size: 0;
		border: 1upx solid rgba(0, 0, 0, 0);
		background: #f6f6f6;
	}

	page {
		background: #f6f6f6;
		overflow-x: hidden;
	}

	.cashInput {
		margin: 20upx auto 0 auto;
		width: 688upx;
		background: rgba(255, 255, 255, 1);
		border-radius: 10upx;
	}

	.cashInputPackage {
		padding: 30upx;

		>view:nth-child(1) {
			>span {
				font-size: 28upx;

				color: rgba(51, 51, 51, 1);
			}
		}

		>view:nth-child(2) {
			margin-top: 48upx;
			padding-bottom: 20upx;
			border-bottom: 1upx solid rgba(246, 246, 246, 1);

			>span {
				vertical-align: bottom;
				font-size: 24upx;

				font-weight: bold;
				color: rgba(51, 51, 51, 1);
			}

			>input {
				display: inline-block;
				width: 500upx;
				font-size: 36upx;

				font-weight: bold;
				color: rgba(51, 51, 51, 1);
			}
		}

		>view:nth-child(3) {
			margin: 20upx 0 0 0;
			display: flex;
			justify-content: space-between;

			>span {
				line-height: 70upx;
				font-size: 24upx;

				color: rgba(153, 153, 153, 1);
			}

			>view {
				display: inline-block;
				width: 184upx;
				height: 70upx;
				background: rgba(244, 79, 68, 1);
				border-radius: 35upx;
				text-align: center;

				>span {
					line-height: 70upx;
					font-size: 24upx;

					color: rgba(255, 255, 255, 1);
				}
			}
		}
	}

	.checkPayType {
		margin: 20upx auto 0 auto;
		width: 688upx;
		background: rgba(255, 255, 255, 1);
		border-radius: 10upx;
	}

	.checkPayTypePackage {
		padding: 30upx;

		>view:nth-child(1) {
			>span {
				font-size: 30upx;

				color: rgba(51, 51, 51, 1);
			}
		}

		>view:nth-child(2) {
			margin-top: 10upx;
		}
	}

	.typePackage {
		padding: 20upx 0;
		display: flex;
		justify-content: space-between;
		border-bottom: 1upx solid rgba(246, 246, 246, 1);

		>view:nth-child(1) {
			vertical-align: middle;

			>image {
				vertical-align: middle;
				width: 60upx;
				height: 60upx;
			}

			>span {
				margin-left: 15upx;
				vertical-align: middle;
				font-size: 28upx;

				color: rgba(81, 81, 81, 1);
			}
		}

		>view:nth-child(2) {
			margin-top: 15upx;
			vertical-align: middle;
			width: 35upx;
			height: 35upx;
			border: 2upx solid rgba(238, 238, 238, 1);
			border-radius: 50%;

			>image {
				width: 35upx;
				height: 35upx;
			}
		}
	}

	.yhkInput {
		padding: 20upx;

		>view {
			border-bottom: 1upx solid rgba(246, 246, 246, 1);
			padding: 20upx 0;

			>span {
				vertical-align: middle;
				display: inline-block;
				width: 100upx;
				font-size: 24upx;

				color: rgba(81, 81, 81, 1);
			}

			>input {
				padding-left: 30upx;
				vertical-align: middle;
				width: 400upx;
				display: inline-block;
				font-size: 24upx;

				font-weight: 400;
				color: rgba(81, 81, 81, 1);
			}
		}
	}

	.inputFoot {
		margin-top: 20upx;
		display: flex;
		justify-content: space-between;

		>view {
			display: inline-block;

			>span:nth-child(1) {
				vertical-align: top;
				font-size: 24upx;

				font-weight: 400;
				color: rgba(153, 153, 153, 1);
			}

			>span:nth-child(2) {
				vertical-align: top;
				font-size: 24upx;

				font-weight: 400;
				color: #ef4949;
			}
		}
	}

	.cashSubmit {
		margin: 20upx auto 0 auto;
		width: 688upx;
		height: 84upx;
		background: rgba(244, 79, 68, 1);
		border-radius: 42upx;
		text-align: center;

		>span {
			line-height: 84upx;
			font-size: 28upx;

			font-weight: 400;
			color: rgba(255, 255, 255, 1);
		}
	}

	.checked {
		width: 35px;
		height: 35px;
		background: #f44f44;
		border-radius: 50%;
	}
</style>
