<template>
	<view class="mine-cash-out">
		<uni-nav-bar :fixed="true" :shadow="false" :statusBar="true" color="#fff"
			:backgroundColor="scrollTop < 20 ?``:primaryColor" leftIcon="icon-left" title="提现">
		</uni-nav-bar>
		<view :style="{height:`${configInfo.navBarHeight}px`}"></view>
		<image mode="aspectFill" lazy-load class="common-bg abs" src="https://lbqny.migugu.com/admin/farm/bg-cash.png"></image>
		<view class="flex-column mt-md ml-lg mr-lg pd-lg fill-base radius-16">
			<view class="f-title c-title text-bold">备注</view>
			<view class="pt-lg pb-md b-1px-b">
				<input v-model="text" class="flex-1" type="text" placeholder="请输入到账信息" />
			</view>
			<view class="pt-lg f-title c-title text-bold">提现金额</view>
			<view class="flex-between pt-lg pb-md b-1px-b">
				<view class="flex-y-center">
					<view class="text-bold">¥</view>
					<input v-on:input="checkInput($event,'apply_price')" v-model="apply_price" class="flex-1 ml-sm"
						type="digit" placeholder="请输入提现金额" />
				</view>
			</view>

			<view class="space-lg"></view>
			<view @tap.stop="withAll" class="flex-y-baseline f-caption c-caption">可提现金额￥{{cash}}
				<view class="text-bold c-warning ml-md">
					全部提现</view>
			</view>
			<view class="flex-y-baseline f-caption c-caption">最低提现金额<view class="text-bold c-warning ml-md">
					¥{{configInfo.cash_mini || 0}}</view>
			</view>
			<view class="space-lg"></view>
			<view class="space-lg"></view>
			<view @tap.stop="submit" class="cash-out-btn flex-center f-title c-base radius-16"
				:style="{background:primaryColor}">提现</view>
			<view class="flex-center f-caption c-caption mt-md" v-if="options.type != 'distribution' && isLoad">
				每笔订单收取{{100-configInfo.cash_balance*1}}%手续费</view>
		</view>
		<view class="space-footer"></view>
	</view>
</template>

<script>
	import {
		mapState,
		mapActions
	} from "vuex"
	export default {
		components: {},
		data() {
			return {
				options: {},
				isLoad: false,
				scrollTop: 0,
				cash: '',
				apply_price: '',
				text: '',
				lockTap: false
			}
		},
		computed: mapState({
			primaryColor: state => state.config.configInfo.primaryColor,
			subColor: state => state.config.configInfo.subColor,
			configInfo: state => state.config.configInfo,
			userInfo: state => state.user.userInfo,
			mineInfo: state => state.user.mineInfo,
		}),
		onLoad(options) {
			this.$util.showLoading()
			this.options = options
			this.initIndex()
		},
		onPageScroll(e) {
			this.scrollTop = e.scrollTop
		},
		methods: {
			...mapActions(['getConfigInfo']),
			async initIndex() {
				await this.getConfigInfo()
				let {
					type
				} = this.options
				// type farmer 农场主；business 店主；distribution 分销商
				switch (type) {
					case 'farmer':
						let {
							cash
						} = await this.$api.farmer.farmerFinanceInfo()
						this.cash = cash
						break
					case 'business':
						this.cash = this.mineInfo.wallet_cash
						break
					case 'distribution':
						this.cash = this.mineInfo.fx_cash
						break
				}
				this.$util.hideAll()
				this.isLoad = true
			},
			initRefresh() {
				this.initIndex()
			},
			withAll() {
				this.apply_price = this.cash
			},
			checkInput(e, key) {
				let val = this.$util.formatMoney(e.detail.value)
				this.$nextTick(() => {
					this[key] = val
				})
			},
			async submit() {
				if (this.apply_price == '0.0') {
					this.$nextTick(() => {
						this.apply_price = '0'
					})
				}
				let {
					apply_price,
					text,
				} = this
				let {
					cash_mini
				} = this.configInfo
				let {
					cash
				} = this
				text = text ? text.replace(/(^\s*)|(\s*$)/g, "") : ''
				if (!text || !apply_price) {
					this.$util.showToast({
						title: !text ? `请输入到账信息` : `请输入提现金额`
					})
					return
				}
				if (parseFloat(apply_price) < parseFloat(cash_mini)) {
					this.$util.showToast({
						title: `提现金额不能低于最低提现金额${cash_mini}`
					})
					return
				}
				if (parseFloat(apply_price) > parseFloat(cash)) {
					this.$util.showToast({
						title: `提现金额不能大于可提现金额${cash}`
					})
					return
				}
				if (this.lockTap) return
				this.lockTap = true
				this.$util.showLoading()
				try {
					let {
						type: methodModel
					} = this.options
					if (methodModel === 'distribution') {
						methodModel = 'mine'
					}
					await this.$api[methodModel].applyWallet({
						apply_price,
						text
					})
					this.$util.hideAll()
					this.$util.showToast({
						title: `提交成功`
					});
					setTimeout(() => {
						this.$util.back()
						this.$util.goUrl({
							url: 1,
							openType: 'navigateBack'
						})
					}, 1000)
				} catch (e) {
					this.lockTap = false
					this.$util.hideAll()
				}
			}
		}
	}
</script>


<style lang="scss">
	.mine-cash-out {

		.can-out-money {
			font-size: 50rpx;
			line-height: 70rpx;

			.money {
				font-size: 70rpx;
			}
		}

		.cash-out-btn {
			width: 622rpx;
			height: 88rpx;
			margin: 0 auto;
		}
	}
</style>
