<template>
	<!--promote/withdrawal/index/index.wxml-->
	<view>
		<view class=" mt20 r10">
			<block v-if="wechat == 0">
				<icon class="iconfont icon-tianjia"></icon>
				<view class="top">
					<view class="add ddflex" v-if="!settlementInfo" @click="toaddCard()">
						<icon class="icon iconfont icontianjia"></icon>
						<text>添加银行卡</text>
					</view>
					<view class="information" v-else>
						<view class="li ddflex">
							<image src="../../static/promote/images/txico1.png" class="txico"></image>
							<view class="fflex">
								<view class="t-name ddflex">
									<view class="fflex">{{ settlementInfo.legalCertName }}</view>
									<view class="t-sta ddflex">
										<!-- <image src="../../static/promote/images/txico3.png"></image> -->
										{{ getStateText(1) }}
										<image src="../../../static/pages/images/crico1.png" class="rico"></image>
									</view>
								</view>
								<view class="t-bri">{{ styleHintText(settlementInfo.legalCertId) }}</view>
							</view>
						</view>
						<view class="li ddflex" @click="toaddCard()">
							<image src="../../static/promote/images/txico2.png" class="txico"></image>
							<view class="fflex">
								<view class="t-name ddflex">
									<view class="fflex">{{ getBankName(settlementInfo.bankCode) }}</view>
									<view class="t-sta ddflex">
										<!-- <image src="../../static/promote/images/txico3.png"></image> -->
										{{ getStateText(2) }}
										<image src="../../../static/pages/images/crico1.png" class="rico"></image>
									</view>
								</view>
								<view class="t-bri" v-if="settlementInfo.cardNo">{{ '尾号' + getbankNo(settlementInfo.cardNo) }}</view>
							</view>
						</view>
					</view>
				</view>
			</block>
			<view class="pdlr20 pd50 bgfff cont">
				<view class="txje ddflex">
					<view class="fflex">提现金额</view>
					<navigator url="/promote/withdrawal/income/income" hover-class="none" class="ddflex">
						收益账户明细
						<image src="../../../static/pages/images/crico1.png" class="rico"></image>
					</navigator>
				</view>
				<view class="dflex money pd50">
					<text>￥</text>
					<input adjust-position="false" placeholder="输入提现金额" placeholder-class="placeholder" type="number" :value="allmoney" @input="getwithdrawal" />
				</view>
				<view class="dflex tx mt20">
					<text class="mcolor">可提现金额 {{ countMoney.ktx }}元</text>
					<text @tap="allmoneyFun">全部提现</text>
				</view>
				<view class="btn mt50" @tap="withdrawmoney">提现</view>
				<view class="mt20">
					<text class="mcolor " v-if="minMoney > 0">最小提现金额 {{ minMoney }}元</text>
				</view>
			</view>
		</view>
		<navigator url="/promote/withdrawal/record/record" hover-class="none" class="jlbtn">历史提现记录</navigator>
	</view>
</template>

<script>
// promote/withdrawal/index/index.js
const app = getApp();
const util = require('../../../utils/util.js');
const { msg } = require('../../../utils/request.js');
const req = require('../../../utils/request.js');
const hfParams = require('../../../utils/hfParams.js');

export default {
	data() {
		return {
			txmoney: 0,
			wechat: 0,
			minMoney: '',
			// card: '',
			// isShow: false,
			// Firstcard: '',
			// Lastcard: '',
			allmoney: '',
			countMoney: {},
			settlementInfo: '',
			bankList: []
		};
	},

	components: {},
	props: {},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
		this.getminMoney();
		this.bankList = hfParams.bank();
	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function() {
		this.getMoney(); //分销统计
		// this.getCard();
		this.getProfit(); //用户收益统计
		this.getSettlementInfo();
	},
	methods: {
		getbankNo(bankNo) {
			if (bankNo) {
				return bankNo.substring(bankNo.length - 4, bankNo.length);
			}
		},

		getBankName(code) {
			if (code) {
				for (var i = 0; i < this.bankList.length; i++) {
					var bankObj = this.bankList[i];
					if (bankObj.code == code) {
						return bankObj.name;
					}
				}
			}
		},

		getStateText(type) {
			var text = '';
			if (this.settlementInfo && this.settlementInfo.channelList && this.settlementInfo.channelList.length > 0) {
				var obj = this.settlementInfo.channelList[0];
				if (type == 1) {
					//商户状态 -1 资料完善中 0、审核中 1、审核成功 2、审核失败
					if (obj.state == -1) {
						text = '资料完善中';
					} else if (obj.state == 0) {
						text = '审核中';
					} else if (obj.state == 1) {
						text = '已实名';
					} else if (obj.state == 2) {
						text = '审核失败';
					}
				}

				if (type == 2) {
					//结算账户状态 0、未创建 1、正常 2、已删除 3、删除失败 4、创建失败 ,
					if (obj.settlementState == 0) {
						if (obj.errMsg) {
							text = obj.errMsg;
						} else {
							text = '审核中';
						}
					} else if (obj.settlementState == 1) {
						text = '已绑定';
					} else if (obj.settlementState == 4) {
						text = obj.errMsg;
					}
				}
			}
			return text;
		},

		styleHintText(str) {
			return util.styleHintText(str);
		},

		//是否可提现
		isOkState() {
			var is = false;
			if (this.settlementInfo && this.settlementInfo.channelList && this.settlementInfo.channelList.length > 0) {
				var obj = this.settlementInfo.channelList[0];
				if (obj.state == 1 && obj.settlementState == 1) {
					is = true;
				}
			}
			return is;
		},

		getProfit() {
			req.getRequest('/api/rrLogs/profit', {}, res => {
				this.countMoney = res;
			});
		},
		getminMoney() {
			req.g('/api/config', res => {
				req.setStorage('configRes', JSON.stringify(res)); // 获取最小金额

				if (res.CONFIG_WITHDRAW_Min && res.CONFIG_WITHDRAW_Min >= 1) {
					this.setData({
						minMoney: res.CONFIG_WITHDRAW_Min
					});
				} else {
					this.setData({
						minMoney: 1
					});
				} //获取是否开通提现到微信零钱

				if (res.withdraw_wechat_small) {
					this.wechat = res.withdraw_wechat_small;
				}
			});
		},

		getSettlementInfo() {
			req.getRequest('/api/v3/settlement/info', '', data => {
				this.settlementInfo = data;
			});
		},

		// // 获取用户银行卡信息
		// getCard() {
		// 	req.getRequest('/api/bank', '', data => {
		// 		console.log(data);

		// 		if (data) {
		// 			let Firstcard = data.card.substring(0, 4);
		// 			let Lastcard = data.card.substring(data.card.length - 4, data.card.length);
		// 			console.log(Firstcard, Lastcard);
		// 			this.setData({
		// 				card: data,
		// 				isShow: false,
		// 				Firstcard: Firstcard,
		// 				Lastcard: Lastcard
		// 			});
		// 		} else {
		// 			this.setData({
		// 				isShow: true
		// 			});
		// 		}
		// 	});
		// },

		// 获取用户金额
		getMoney() {
			req.getRequest('/api/distribution/info', {}, data => {
				this.setData(data);
				// console.log(this);
			});
		},

		// 提现
		withdrawmoney() {
			if (this.wechat == 0) {
				if (!this.isOkState()) {
					return req.msg('请先添加正确的银行卡信息');
				}
			}
			if (this.txmoney == 0) {
				return req.msg('请输入提现金额');
			} else if (this.txmoney > this.countMoney.ktx) {
				return req.msg('提现金额不能大于可提金额');
			} else if (this.txmoney < this.minMoney) {
				return req.msg('提现金额不能低于最小提现金额');
			} else if (this.txmoney > 0 && this.txmoney <= this.countMoney.ktx && +this.txmoney >= +this.minMoney) {
				req.postRequest(
					'/api/withdraw',
					{
						money: this.txmoney
					},
					res => {
						console.log(res);
						req.msg('您的提现申请已受理，请等待平台打款', success => {
							setTimeout(function() {
								uni.navigateBack();
							}, 1500);
						});
					}
				);
			}
		},

		//全部提现
		allmoneyFun() {
			this.setData({
				allmoney: this.countMoney.ktx,
				txmoney: this.countMoney.ktx
			});
		},

		// 获取提现金额
		getwithdrawal(e) {
			this.setData({
				txmoney: e.detail.value
			});
		},

		toaddCard() {
			var url = '/promote/withdrawal/bindCard/bindCard';
			if (this.settlementInfo) {
				if (this.isOkState()) {
					url = '/promote/withdrawal/bindCardCheck/bindCardCheck';
				} else {
					url = url + '?isEnd=true';
				}
			}
			uni.navigateTo({
				url: url
			});
		}
	}
};
</script>
<style>
@import './index.css';
</style>
