<template>
	<view class="page">
		<uni-card :border="false" :is-shadow="false" :padding="20">
			<uni-forms ref="form" :modelValue="formData" :rules="rules" label-position="top">
				<uni-forms-item label="发票类型" name="type">
					<uni-data-checkbox v-model="formData.type" :localdata="type" />
				</uni-forms-item>
				<uni-forms-item label="开票类型" name="invoiceType">
					<uni-data-checkbox v-model="formData.invoiceType" :localdata="invoiceType" />
				</uni-forms-item>
				<uni-forms-item label="名称" name="name" required>
					<uv-input class="input" v-model="formData.name" type="text" placeholder="请输入开票名称" />
				</uni-forms-item>
				<uni-forms-item label="税号" name="invoiceCode" required>
					<uv-input class="input" v-model="formData.invoiceCode" type="text" placeholder="请输入税号" />
				</uni-forms-item>
				<uni-forms-item label="公司地址" name="address" required>
					<uv-input class="input" v-model="formData.address" type="text" placeholder="请输入公司地址" />
				</uni-forms-item>
				<uni-forms-item label="公司电话" name="tel" required>
					<uv-input class="input" v-model="formData.tel" type="text" placeholder="请输入公司电话" />
				</uni-forms-item>
				<uni-forms-item label="开户行" name="bankName" required>
					<uv-input class="input" v-model="formData.bankName" type="text" placeholder="请输入开户行" />
				</uni-forms-item>
				<uni-forms-item label="开户账号" name="bankAccount" required>
					<uv-input class="input" v-model="formData.bankAccount" type="text" placeholder="请输入开户账号" />
				</uni-forms-item>
				<uni-forms-item label="手机号码" name="phone" required>
					<uv-input class="input" v-model="formData.phone" type="text" placeholder="请输入手机号码" />
				</uni-forms-item>
				<uni-forms-item label="电子邮箱" name="email" required>
					<uv-input class="input" v-model="formData.email" type="text" placeholder="请输入电子邮箱" />
				</uni-forms-item>
				<uni-forms-item label="备注" name="comments">
					<uv-textarea v-model="formData.comments" placeholder="请输入备注信息" auto-height></uv-textarea>
				</uni-forms-item>
				<uv-button class="add-emergency"
					:customStyle="{background: 'linear-gradient(to right, #70b915, #008e04)',borderRadius: '30px',color: '#ffffff'}"
					text="保存" @click="submit"></uv-button>
			</uni-forms>
		</uni-card>
	</view>
</template>

<script>
	import {
		useUserStore
	} from '@/store/modules/user';
	import * as Api from '@/api/booking/userInvoice';
	import {
		openUrl
	} from '@/utils/common';

	import {
		useTenantStore
	} from '@/store/modules/tenant';

	const tenantStore = useTenantStore();

	const userStore = useUserStore();

	export default {
		data() {
			return {
				list: [],
				statusBarHeight: 104,
				isLogin: false,
				fields: {},
				logo: '',
				bookingUser: {},
				server: [],
				order: [],
				// 表单数据
				formData: {
					type: 0,
					invoiceType: 0
				},
				// 发票类型
				type: [{
					text: '纸质',
					value: 0
				}, {
					text: '电子',
					value: 1
				}],
				// 开票类型
				invoiceType: [{
					text: '普票',
					value: 0
				}, {
					text: '专票',
					value: 1
				}],
				rules: {
					// 对name字段进行必填验证
					name: {
						rules: [{
							required: true,
							errorMessage: '请输入开票名称',
						}]
					},
					// 对email字段进行必填验证
					email: {
						rules: [{
							format: 'email',
							errorMessage: '请输入正确的邮箱地址',
						}]
					}
				}
			}
		},
		onLoad() {
			this.reload()
		},
		methods: {
			async reload(e) {
				const app = this
				// 当前登录用户信息
				const userInfo = await userStore.fetchUserInfo();
				if (Object.keys(userInfo).length > 0) {
					app.isLogin = true
					app.userInfo = userInfo
				} else {
					app.isLogin = false
				}

				// 读取最后一次
				Api.pageUserInvoice({
					userId: userInfo.userId,
					limit: 1
				}).then(res => {
					if(res.count > 0 && !app.formData.id){
						app.formData = res.list[0];
					}
				})
			},
			navTo(item) {
				if (!this.isLogin) {
					this.$refs.login.open('bottom')
					return false;
				}
				openUrl(item);
			},
			// 弹出登录提示框
			openLogin() {
				if (!this.isLogin) {
					this.$refs.login.open('bottom')
					return false;
				}
			},
			add() {
				this.$refs.addForm.open('center')
			},
			onDel(item) {
				const app = this
				uni.showModal({
					title: '确定要删除吗?',
					success(o) {
						if (o.confirm) {
							uni.showLoading({
								title: '请求中.'
							})
							Api.removeUserInvoice(item.id).then(res => {
								uni.showToast({
									title: '操作成功',
									icon: 'none'
								})
							})
							app.reload()
						}
					}
				});

			},
			save() {
				Api.addUserInvoice(this.form).then(msg => {
					uni.showToast({
						title: msg,
						icon: 'none'
					})
					this.$refs.addForm.close()
					this.reload()
				}).catch(() => {
					uni.showToast({
						title: '请检查您的网络.',
						icon: 'none'
					})
					uni.hideLoading()
					this.$refs.addForm.close()
					this.reload()
				})
			},
			/**
			 * 复写 binddata 方法，如果只是为了校验，无复杂自定义操作，可忽略此方法
			 * @param {String} name 字段名称
			 * @param {String} value 表单域的值
			 */
			// binddata(name,value){
			// // 通过 input 事件设置表单指定 name 的值
			//   this.$refs.form.setValue(name, value)
			// },
			// 触发提交表单
			submit() {
				this.$refs.form.validate().then(res => {
					const saveOrUpdate = this.formData.id ? Api.updateUserInvoice : Api.addUserInvoice
					saveOrUpdate(this.formData).then(message => {
						uni.showToast({
							title: '保存成功',
							icon: 'success'
						})
					}).catch((err) => {
						console.log(err);
					})
					console.log('表单数据信息：', res);
				}).catch(err => {
					console.log('表单错误信息：', err);
				})
			}
		}
	}
</script>
<style>
	page {
		background-size: 100%;
		background-repeat: no-repeat;
		background-color: #f0f2f5;
		width: 750rpx;
		overflow-x: hidden;
	}
</style>
<style lang="scss">
	.avatar {
		padding: 20px;
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
		font-size: 14px;
	}

	.save-user-profile {
		width: 700rpx;
		margin: 40rpx auto;
	}

	.add-emergency {
		width: 500rpx !important;
		margin: 40rpx auto;
	}

	.btn-bar {
		width: 700rpx;
		left: 25rpx;
		position: fixed;
		bottom: 30rpx;
		margin: auto;
		clear: both;
	}
</style>