<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>
		<view class="ciy-list" v-for="(item,index) in init.cashs" :key="item.id">
			<view class="l2">
				<text class="kbd">{{cashtype==1?'银行':'微信'}}</text>
				{{item.taxmoney==-1?'企业开具发票':'代缴个税 ' + (item.taxmoney/100)+' 元'}}
			</view>
			<ciy-showimgs label="发票" :src="item.invfile"></ciy-showimgs>
			<view class="l2" v-if="item.invcode">发票号码 {{item.invcode}}</view>
			<view class="l2" v-if="item.cashtimes>0">操作时间 {{todatetime(item.cashtimes)}}</view>
			<view class="l2" v-if="item.cashno">支付编号 {{item.cashno}}</view>
			<view class="l2" v-if="item.auditmsg">拒绝原因 {{item.auditmsg}}</view>
			<view class="l2" v-if="item.bankaccount">账户名称 {{item.bankaccount}}</view>
			<view class="l2" v-if="item.bankno">银行卡号 {{item.bankno}}</view>
			<view class="l2" v-if="item.bankname">开户银行 {{item.bankname}}</view>
			<view class="l2" v-if="item.bankcode">联行号 {{item.bankcode}}</view>

			<view v-if="item.cashstatus==20 && item.cashtype==2">
				<button class="btn" @tap="wxrecvpay(index)">立即收款</button>
			</view>
			<view v-if="item.cashstatus==20 && item.cashtype==1">
				<button class="btn" @tap="showinv(index)">上传发票</button>
			</view>
			<view class="rt"><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 txt-lgg txt-wb">{{item.cashmoney/100}}</view>

		</view>
		<view class="ciy-card char3" v-if="init.cashs.length==0">
			<view class="content">
				<view class="ciy-form">
					<label>余额</label>
					<view class="txt-left">
						{{init.user.mycashmoney/100}}元
					</view>
				</view>
				<view class="ciy-form">
					<label>可提现</label>
					<view class="txt-left">
						<ciy-inputbet left bb v-model="editdata.cash" unit="元" bet="100"></ciy-inputbet>
					</view>
				</view>
				<view class="ciy-form">
					<label>提现流向</label>
					<view class="txt-left">
						<ciy-selbool v-model="editdata.cashtype" y="银行" n="微信"></ciy-selbool>
					</view>
				</view>
				<view class="ciy-form">
					<label>税务方案</label>
					<view class="txt-left">
						{{editdata.cashtype==2?'代扣缴个税':(me.truename == me.bankaccount?'代扣缴个税':'对公开具发票')}}
					</view>
				</view>
				<view class="ciy-form" v-if="editdata.cashtype==1">
					<label>银行账户</label>
					<view class="txt-left">
						{{me.bankaccount}}<br />
						{{me.bankname}}<br />
						{{me.bankno}}<br />
					</view>
				</view>
				<view class="ciy-form-bottom">
					<button class="btn lg cc" @tap="cashsubmit">申请提现</button>
				</view>
				<view class="ciy-tip"><ciy-markdown :md="init.tip"></ciy-markdown></view>
			</view>
		</view>
		<ciy-swipelist v-else title="可提现余额" :right="init.user.mycashmoney/100+'元'"></ciy-swipelist>
	</view>
	<ciy-swipelist title="提现历史记录" @tap="gourl" data-url="$/pages/me/cashout_lst" more></ciy-swipelist>

	<ciy-anipop v-model="popinv" :zindex="991" maskbg="#00000077" title="上传发票">
		<view class="bg1 pop">
			<view class="ciy-form">
				<label class="imp">发票编号</label>
				<view>
					<ciy-input bb v-model="invedit.invcode" type="text"></ciy-input>
				</view>
			</view>
			<view class="ciy-form">
				<label class="imp">上传发票</label>
				<view style="height:10em;">
					<ciy-upload left num="1" path="cashinv" v-model="invedit.invfile" imgwidth="1600" sourcetype="camera,album,message"></ciy-upload>
				</view>
			</view>
			<view class="px4 py4 txt-center">
				<button class="btn lg" @tap="invsubmit">提交</button>
			</view>
		</view>
	</ciy-anipop>
	<view :style="{height:header_statusbar_height+'px'}"></view>
</template>

<style>
</style>

<script>
	export default {
		data() {
			return {
				popinv: false,
				invedit: {}
			}
		},
		onLoad() {
			this.getinit();
		},
		methods: {
			async getinit() {
				var retjson = await this.callfunc({
					func: 'me.cashout_cash_init',
				});
				if (retjson.code != 1)
					return this.alert(retjson.errmsg);
				this.init = retjson;
				this.editdata.cashtype = 2;
				this.editdata.cash = Math.floor(this.init.user.mycashmoney / 100) * 100;
				if (this.editdata.cash < 0)
					this.editdata.cash = 0;
			},
			async cashsubmit() {
				await this.getauth('bank');
				if (this.editdata.cash <= 0)
					return this.toast('不可提现');
				if (this._loading)
					return;
				this._loading = true;
				var retjson = await this.callfunc({
					func: 'me.cashout_cash_submit',
					data: this.editdata
				});
				this._loading = false;
				if (retjson.code != 1)
					return this.alert(retjson.errmsg);
				this.init.user.mycashmoney -= this.editdata.cash;
				this.editdata.cash = 0;
				this.toast('申请成功');
			},
			async wxrecvpay(idx) {
				if (this._loading)
					return;
				this._loading = true;
				var retjson = await this.callfunc({
					func: 'me.cashout_cash_wxrecvpay',
					data: {
						id: this.init.cashs[idx].id
					}
				});
				this._loading = false;
				if (retjson.code != 1)
					return this.alert(retjson.errmsg);
				wx.requestMerchantTransfer({
					mchId: retjson.transfer.mchid,
					appId: retjson.transfer.appid,
					package: retjson.transfer.package_info,
					success: res => {
						this.init.cashs.splice(idx, 1);
						console.log('success Transfer:', res);
					},
					fail: (res) => {
						console.log('fail Transfer:', res);
					},
				});
			},
			showinv(idx) {
				this.invedit.id = this.init.cashs[idx].id;
				this.popinv = true;
			},
			async invsubmit(e) {
				if (this._loading)
					return;
				this._loading = true;
				var retjson = await this.callfunc({
					func: 'me.cashout_cash_inv',
					data: this.invedit
				});
				this._loading = false;
				if (retjson.code != 1)
					return this.alert(retjson.errmsg);
				this.objtolist(this.init.cashs, retjson.data);
				this.toast('上传发票成功');
				this.popinv = false;
			},
		}
	}
</script>