<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-card char3">
			<view class="content">
				<view class="ciy-form">
					<label>可开票金额</label>
					<view style="text-align: left;">
						{{init.user.myinvmoney/100}}元
					</view>
				</view>
				<view class="ciy-form">
					<label>开票金额</label>
					<view>
						<ciy-inputbet bb v-model="editdata.money" unit="元" bet="100"></ciy-inputbet>
					</view>
				</view>
				<view class="ciy-form" v-if="init.details.length>0">
					<label>开票明细</label>
					<view>
						<ciy-select v-model="editdata.invoicedetailid" :range="init.details"></ciy-select>
					</view>
				</view>
				<view class="ciy-form">
					<label>发票抬头</label>
					<view>
						<ciy-selpage v-model="editdata.invoicetitleid" page="invoicetitle">
							<template v-slot:default="{data}">
								<view>{{data.name}}</view>
								<view class="txt1">{{data.taxnumber}}</view>
							</template>
						</ciy-selpage>
					</view>
				</view>
				<view class="ciy-form-bottom">
					<button class="btn lg cc" @tap="submit">申请开票</button>
				</view>
				<view class="ciy-tip"><ciy-markdown :md="init.tip"></ciy-markdown></view>
			</view>
		</view>
	</view>
	<ciy-swipelist title="开票历史" @tap="gourl" data-url="invoice_lst" more></ciy-swipelist>
</template>

<style>
</style>

<script>
	export default {
		data() {
			return {}
		},
		onLoad() {
			this.getinit();
		},
		methods: {
			async getinit() {
				var retjson = await this.callfunc({
					func: 'me.invoicing_init',
				});
				if (retjson.code != 1)
					return this.alert(retjson.errmsg);
				this.init = retjson;
				this.editdata.money = retjson.user.myinvmoney;
			},
			async submit() {
				if (this.editdata.money < 1000)
					return this.toast('开票金额10元');
				if (this.init.user.myinvmoney < this.editdata.money)
					return this.toast('开票金额不足');
				if (!this.editdata.invoicetitleid)
					return this.toast('请选择发票抬头');
				if (this.init.details.length > 0) {
					if (!this.editdata.invoicedetailid)
						return this.toast('请选择发票明细');
				}
				if (this._loading)
					return;
				this._loading = true;
				var retjson = await this.callfunc({
					func: 'me.invoicing_submit',
					data: this.editdata
				});
				this._loading = false;
				if (retjson.code != 1)
					return this.alert(retjson.errmsg);
				this.init.user.myinvmoney -= this.editdata.money;
				this.me.myinvmoney = this.init.user.myinvmoney;
				this.setstorage('me', this.me);
				this.toast('申请成功');
			}
		}
	}
</script>