<template>
	<view :class="[AppTheme]" id="withdraw">
		<view class="block">
			<view class="zh">提现金额</view>
			<view class="money">
				<u--input placeholder="请输入提现金额" fontSize="25" color="#000000" prefixIcon="rmb" v-model="money"
					border="bottom" prefixIconStyle="font-size: 20px;color: #909399"></u--input>
			</view>
		</view>
		<view class="tx">
			<view class="title">提现方式</view>
			<view class="uni-list">
				<radio-group @change="radioChange">
					<label class="uni-list-cell uni-list-cell-pd"
						v-if="item.isopen && ((item.value=='weixin' && weixin) || (item.value=='zfb' && zfb)) || (item.value == 'bank' && zfb)"
						v-for="(item, index) in items" :key="item.value">
						<u-icon :name="item.img" :color="item.color" size="40"></u-icon>
						<view>{{ item.name }}</view>
						<view>
							<radio :value="item.value" :checked="index === current" :color="primary" />
						</view>
					</label>
				</radio-group>
			</view>
			<view v-if="sitem.value=='zfb'" class="u-demo-block">
				<view style="padding-bottom: 30rpx;">支付宝姓名</view>
				<u--input placeholder="支付宝姓名" prefixIcon="account" v-model="apply_name"
					prefixIconStyle="font-size: 22px;color: #909399"></u--input>
				<view style="padding: 30rpx 0rpx;">支付宝账号</view>
				<u--input placeholder="支付宝账号" prefixIcon="zhifubao" v-model="apply_account"
					prefixIconStyle="font-size: 22px;color: #909399"></u--input>
				<view class="bor_solp"></view>
			</view>
			<view v-if="sitem.value=='bank'" class="u-demo-block">
				<view style="padding-bottom: 30rpx;">姓名</view>
				<u--input placeholder="姓名" prefixIcon="account" v-model="bank_name"
					prefixIconStyle="font-size: 22px;color: #909399"></u--input>
				<view style="padding: 30rpx 0rpx;">银行卡号</view>
				<u--input placeholder="银行卡号" prefixIcon="zhifubao" v-model="bank_number"
					prefixIconStyle="font-size: 22px;color: #909399"></u--input>
				<view class="bor_solp"></view>
				<view style="padding: 30rpx 0rpx;">所属银行</view>
				<u--input placeholder="所属银行" prefixIcon="zhifubao" v-model="branch"
					prefixIconStyle="font-size: 22px;color: #909399"></u--input>
				<view class="bor_solp"></view>
			</view>
		</view>
		<view class="btn">
			<view class="save bg-linear-gradient" @click="doSve">提交</view>
		</view>
	</view>
</template>

<script>
	import txApi from '@/api/wall/index.js';

	export default {
		name: 'Withdraw',
		data() {
			return {
				bank_name: '',
				bank_number: '',
				branch: '',
				primary: this.$theme.primary,
				settingFile: getApp().globalData.siteinfo,
				money: 0,
				items: [{
						value: 'weixin',
						color: '#62b900',
						isopen: true,
						name: '微信',
						img: 'weixin-fill'
					},
					{
						value: 'zfb',
						color: 'rgb(85, 170, 255)',
						isopen: true,
						name: '支付宝',
						img: 'zhifubao'
					},
					{
						value: 'bank',
						color: 'rgb(85, 170, 255)',
						isopen: true,
						name: '银行卡',
						img: 'rmb-circle'
					}
				],
				apply_account: '',
				apply_name: '',
				sitem: {
					value: ''
				},
				zfb: false,
				weixin: false,
				txtype: 'vip',
				current: -1,
				systype: ''
			};
		},
		onLoad(options) {
			let that = this;
			if (options && options.txtype) {
				that.txtype = options.txtype
			}
			// #ifdef APP-PLUS
			that.systype = 'app'
			// #endif
			// #ifdef H5
			that.systype = 'h5'
			// #endif
			// #ifdef MP-WEIXIN
			that.systype = 'mpweixin'
			// #endif
			console.log('this.txtype', this.txtype)
			let config = that.$init_config(1, function(res) {
				if (res && res.app) {
					that.weixin = res.app.cash.wx.isopen;
					that.zfb = res.app.cash.ali.isopen;
				}
				if(res && res.bank) {
					that.bank_name = res.bank.bank_name
					that.bank_number = res.bank.bank_number
					that.branch = res.bank.branch
				}
			});
		},
		methods: {
			radioChange: function(evt) {
				for (let i = 0; i < this.items.length; i++) {
					if (this.items[i].value === evt.target.value) {
						this.current = i;
						this.sitem = this.items[i]
						break;
					}
				}
			},
			doSve() {
				// //订阅模板消息
				let that = this
				let tmplIdsone = that.$config.temlist;
				if (tmplIdsone) {
					that.$until.requestSubscribeMessage([tmplIdsone['withdrawalResult']], tmplIdsone, function() {
						that.doSve1()
					});
				} else {
					that.doSve1()
				}
			},
			doSve1() {
				let that = this;
				that.$u.throttle(() => {
					if (!that.money) {
						that.$api.msg('请填写金额');
						return;
					} else if (isNaN(that.money)) {
						that.$api.msg('请填写正确的提现金额');
						return;
					}
					console.log(that.sitem.value)
					if (that.txtype == 'farm') {
						let type = 0;
						if (that.sitem.value == 'weixin') {
							type = 2;
						} else if (that.sitem.value == 'zfb') {
							type = 1;
						} else {
							that.$api.msg('请选择提现方式');
							return;
						}
						txApi.tx_farm({
							cash_money: that.money,
							apply_name: that.apply_name,
							apply_account: that.apply_account,
							systype: that.systype,
							type: type,
						}).then(res => {
							if (res.status == 1) {
								console.log(res, "提现")
								this.$api.msg(res.msg);
								setTimeout(() => {
									uni.navigateTo({
										url: `/pagesC/pages/merchantdetails/property?id=` +
											this.farm_id
									});
								}, 2000);
							} else {
								this.$api.msg(res.msg);
							}
						});
					} else {
						if (that.sitem.value == 'weixin') {
							txApi.wxTxOrder({
								txprice: that.money,
								systype: that.systype
							}).then(res => {
								if (res.status == 200) {
									console.log(res, "提现")
									that.$api.msg(res.msg);
									setTimeout(() => {
										uni.redirectTo({
											url: '/pagesA/pages/mypurse/index'
										});
									}, 1000);
								} else {
									that.$api.msg(res.msg);
								}
							});
						} else if (that.sitem.value == 'zfb') {
							txApi.aliTxOrder({
								txprice: that.money,
								apply_account: that.apply_account,
								apply_name: that.apply_name,
								systype: that.systype
							}).then(res => {
								if (res.status == 200) {
									console.log(res, "提现")
									that.$api.msg(res.msg);
									setTimeout(() => {
										uni.redirectTo({
											url: '/pagesA/pages/mypurse/index'
										});
									}, 1000);
								} else {
									that.$api.msg(res.msg);
								}
							});
						} else if (that.sitem.value == 'bank') {
							txApi.bankTx({
								bank_name: that.bank_name,
								bank_number: that.bank_number,
								branch: that.branch,
								txprice: that.money,
								systype: that.systype
							}).then(res => {
								if (res.status == 200) {
									that.$api.msg(res.msg);
									setTimeout(() => {
										uni.redirectTo({
											url: '/pagesA/pages/mypurse/index'
										});
									}, 1000);
								} else {
									that.$api.msg(res.msg);
								}
							})
						} else {
							that.$api.msg('请选择提现方式');
							return;
						}
					}
				});

			}
		}
	};
</script>

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

	.u-demo-block {
		padding: 30rpx;
	}

	#withdraw {
		width: 100%;
		background-color: #ffffff;

		.block {
			overflow: hidden;

			.zh {
				font-size: 30rpx;
				font-family: PingFang SC;
				font-weight: bold;
				color: #333333;
				padding: 30rpx;
			}

			.money {
				display: flex;
				font-size: 35rpx;
				font-family: PingFang SC;
				font-weight: bold;
				align-items: center;
				padding: 30rpx;
				color: #000000;
				margin-bottom: 20rpx;
			}
		}

		.tx {
			width: 100%;
			background-color: #ffffff;
			padding-bottom: 35rpx;

			.title {
				font-size: 30rpx;
				font-family: PingFang SC;
				font-weight: bold;
				color: #333333;
				margin: 33rpx 0 33rpx 36rpx;
			}

			.flex {
				display: flex;
			}

			.uni-list-cell.uni-list-cell-pd {
				position: relative;
				display: flex;
				font-size: 30rpx;
				padding-left: 40rpx;
				font-family: PingFang SC;
				font-weight: 500;
				color: #333333;
				margin-bottom: 30rpx;
				align-items: center;

				image {
					width: 46rpx;
					height: 41rpx;
					margin-right: 25rpx;
					margin-left: 37rpx;
				}

				radio {
					position: absolute;
					top: 0;
					right: 20rpx;
				}
			}
		}

		.btn {
			width: 100%;
			height: 210rpx;
			display: flex;
			justify-content: center;
			align-items: center;
			background-color: #ffffff;

			.save {
				margin: 0 auto;
				width: 582rpx;
				height: 90rpx;
				font-size: 32rpx;
				font-family: PingFang SC;
				font-weight: 800;
				color: #ffffff;
				text-align: center;
				line-height: 90rpx;
				color: #ffffff;
				border-radius: 45rpx;
			}
		}
	}
</style>