<template>
	<ciy-header title="提现"></ciy-header>
	<view v-if="init.code != 1">
		<view class="px4 py4">
			<view style="height:2em;width:60%;" class="ciy-skeleton"></view>
			<view style="height:1em;width:40%;margin-left:1em;" class="ciy-skeleton"></view>
			<view style="height:1em;width:50%;" class="ciy-skeleton"></view>
			<view style="height:6em;width:100%;" class="ciy-skeleton"></view>
		</view>
	</view>
	<view v-else>
		<form @submit="submit" class="char3">
			<view class="ciy-card">
				<view class="content">
					<view class="ciy-form">
						<label>余额</label>
						<view style="text-align: left;">
							{{init.user.mycashmoney/100}}元
						</view>
					</view>
					<view class="ciy-form">
						<label>可提现</label>
						<view style="text-align: left;">
							{{init.user.cash}}元
						</view>
					</view>
					<view class="ciy-form">
						<label>提现流向</label>
						<view style="text-align: left;">
							<ciy-selbool name="cashtype" v-model="me.cashtype" y="银行" n="微信"></ciy-selbool>
						</view>
					</view>
					<view class="ciy-form-bottom">
						<button class="btn lg cc" form-type="submit">申请提现</button>
					</view>
					<view class="ciy-tip">
						每天可提交1次，以元为单位申请。
					</view>
					<view class="ciy-tip">
						由于微信支付限制，申请提现平台审核通过后，还需手动点击收款操作。
					</view>
				</view>
			</view>
		</form>
	</view>
	<view class="ciy-list" v-for="(item,index) in init.cash" :key="item.id">
		<view class="l2">
			<text class="kbd" :class="ccode(g.cashstatus, item.cashstatus,'clas')">{{ccode(g.cashstatus, item.cashstatus)}}</text>
		</view>
		<view class="lb">{{todatetime(item.addtimes)}}</view>
		<view class="rb">
			<text class="txt-lgg txt-wb px2">{{item.cashmoney/10000}}</text>
			<text class="txt-smm">元</text>
		</view>
		<view class="txt-right" v-if="item.cashtype==2">
			<button class="btn" @tap="wxrecvpay(index)">立即收款</button>
		</view>
		<view class="txt-right" v-if="item.cashtype==1">
			<button class="btn">上传发票</button>
		</view>
	</view>
	<ciy-swipelist title="设置提现账户" @tap="gourl" data-url="$/pages/main/me_cash_bank" more></ciy-swipelist>
	<ciy-swipelist title="提现历史记录" @tap="gourl" data-url="$/pages/main/me_cash_out" more></ciy-swipelist>
</template>

<style>
</style>

<script>
	export default {
		data() {
			return {}
		},
		onLoad() {
			this.getinit();
		},
		methods: {
			async getinit() {
				var retjson = await this.callajax({
					func: 'me.cash_get',
					data: {}
				});
				if (retjson.code != 1)
					return this.alert(retjson.errmsg);
				this.init = retjson;
				this.init.user.cash = Math.floor(this.init.user.mycashmoney / 100);
				if (this.init.user.cash < 0)
					this.init.user.cash = 0;
			},
			async submit(e) {
				if (this.init.user.cash <= 0)
					return this.toast('不可提现');
				if (this._loading)
					return;
				this._loading = true;
				var retjson = await this.callajax({
					func: 'me.cash',
					data: {
						cash: this.init.user.cash
					}
				});
				this._loading = false;
				if (retjson.code != 1)
					return this.alert(retjson.errmsg);
				this.init.user.mycashmoney -= this.init.user.cash * 100;
				this.init.user.cash = 0;
				this.toast('申请成功');
			},
			async wxrecvpay(idx) {
				if (this._loading)
					return;
				this._loading = true;
				var retjson = await this.callajax({
					func: 'me.wxrecvpay',
					data: {
						id: this.init.cash[idx].id
					}
				});
				this._loading = false;
				if (retjson.code != 1)
					return this.alert(retjson.errmsg);
				this.init.cash.splice(idx, 1);
				wx.requestMerchant({
					mchId: retjson.transfer.mchid,
					appId: retjson.transfer.appid,
					package: retjson.transfer.package_info,
					success: (res) => {
						console.log('success Transfer:', res);
					},
					fail: (res) => {
						console.log('fail Transfer:', res);
					},
				});
			}
		}
	}
</script>