<template>
	<view class="container">
		<view class="item-bg">
			<uni-list-item :border="false" :title="$t('index.company-name')">
				<template v-slot:footer>
					<input class="list-item-left" v-model="invoiceInfo.companyName" type="text"
						:placeholder="$t('index.enter')+$t('index.company-name')" />
				</template>
			</uni-list-item>
		</view>
		<view class="item-bg">
			<uni-list-item :border="false" :title="$t('index.nashuirenshibiehao')">
				<template v-slot:footer>
					<input class="list-item-left" v-model="invoiceInfo.taxNum" type="text"
						:placeholder="$t('index.msg-identify')" />
				</template>
			</uni-list-item>
		</view>
		<view class="item-bg">
			<uni-list-item :border="false" :title="$t('index.bank')">
				<template v-slot:footer>
					<input class="list-item-left" v-model="invoiceInfo.openBank" type="text"
						:placeholder="$t('index.msg-bank')" />
				</template>
			</uni-list-item>
		</view>
		<view class="item-bg">
			<uni-list-item :border="false" :title="$t('index.account')">
				<template v-slot:footer>
					<input class="list-item-left" v-model="invoiceInfo.accountNum" type="text"
						:placeholder="$t('index.msg-account')" />
				</template>
			</uni-list-item>
		</view>
		<view class="item-bg">
			<uni-list-item :border="false" :title="$t('index.telephone')">
				<template v-slot:footer>
					<input class="list-item-left" v-model="invoiceInfo.tel" type="text"
						:placeholder="$t('index.msg-phone')" />
				</template>
			</uni-list-item>
		</view>
		<view class="item-bg">
			<uni-list-item :border="false" :title="$t('index.address')">
				<template v-slot:footer>
					<input class="list-item-left" v-model="invoiceInfo.address" type="text"
						:placeholder="$t('index.msg-address')" />
				</template>
			</uni-list-item>
		</view>
		<view class="item-bg">
			<uni-list-item :border="false" :title="$t('index.set-default')">
				<template v-slot:footer>
					<view class="switch-box">
						<switch class="deposit-switch" :checked="invoiceInfo.defaultFlag" style="transform:scale(0.7)"
							@change="switchChange" />
					</view>
				</template>
			</uni-list-item>
		</view>

		<view class="btn-box">
			<button class="btn" @click="submit">{{$t('index.sure')}}</button>
		</view>
	</view>
</template>

<script>
	import {
		saveInvoiceInfo,
		getInvoiceInfo
	} from '@/config/api.js'

	export default {
		name: 'addInvoice',
		components: {},
		data() {
			return {
				invoiceInfo: {
					companyName: '',
					taxNum: '',
					openBank: '',
					accountNum: '',
					tel: '',
					address: '',
					defaultFlag: false
				},
				isReadPolicy: true
			}
		},
		methods: {
			switchChange(e) {
				this.invoiceInfo.defaultFlag = e.detail.value
			},
			getInvoiceDetail(id) {
				let that = this
				getInvoiceInfo({
					id
				}).then(({
					content
				}) => {
					that.invoiceInfo = content
				})

			},
			submit() {
				console.log(">>>>>>>>>>>", this.invoiceInfo)
				if (!this.invoiceInfo.companyName.trim()) {
					uni.showToast({
						title: this.$t('index.msg-company'),
						icon: 'none'
					});
					return
				}
				if (!this.invoiceInfo.taxNum.trim()) {
					uni.showToast({
						title: this.$t('index.msg-identify'),
						icon: 'none'
					});
					return
				}
				const validateCreditCode = /^[0-9A-HJ-NPQRTUWXY]{2}\d{6}[0-9A-HJ-NPQRTUWXY]{10}$/;
				if (!validateCreditCode.test(this.invoiceInfo.taxNum)) {
					uni.showToast({
						title: this.$t('index.msg-identify-incorrect'),
						icon: 'none'
					});
					return
				}
				if (!this.invoiceInfo.openBank.trim()) {
					uni.showToast({
						title: this.$t('index.msg-bank'),
						icon: 'none'
					});
					return
				}
				if (!this.invoiceInfo.accountNum.trim()) {
					uni.showToast({
						title: this.$t('index.msg-account'),
						icon: 'none'
					});
					return
				}
				if (!this.isReadPolicy) {
					uni.showToast({
						title: this.$t('index.msg-protocol'),
						icon: 'none'
					});
					return
				}
				const validateMobile = /^[1]([3-9])[0-9]{9}$/;
				const validatePhone = /^((0\d{2,3}-\d{7,8})|(1[3584]\d{9}))$/;
				if (!validateMobile.test(this.invoiceInfo.tel) && !validatePhone.test(this.invoiceInfo.tel)) {
					uni.showToast({
						title: this.$t('index.msg-phone'),
						icon: 'none'
					});
					return
				}
				if (!this.invoiceInfo.address.trim()) {
					uni.showToast({
						title: this.$t('index.msg-address'),
						icon: 'none'
					});
					return
				}
				saveInvoiceInfo(this.invoiceInfo).then(({
					content
				}) => {
					setTimeout(() => {
						uni.showToast({
							title: this.$t('index.success')
						})
						setTimeout(() => {
							uni.navigateBack();
						}, 1500)
					}, 30)
				})
			},
			updateDeliveryInfo(id) {
				updateDelivery({
					cartOrderReceiveId: id,
					deliveryDate: this.deliveryDate,
					orderId: this.orderId
				}).then(res => {
					setTimeout(() => {
						uni.showToast({
							title: this.$t('index.success')
						});
						setTimeout(() => {
							uni.navigateBack({
								delta: 2
							});
						}, 1500)
					}, 30)
				})
			},
			changePolicy(flag) {
				this.isReadPolicy = !!flag
			}
		},
		onLoad(options) {

			if (options.id) { // 订单页跳转过来增加地址
				this.getInvoiceDetail(options.id)
			}
		}
	}
</script>

<style lang="scss" scoped>
	.container {
		height: 100vh;
		background-color: #FFFFFF;

		// /deep/ .file-picker__progress {
		// 	display: none;
		// }

		/deep/ .uni-list-item__content {
			flex: none;
		}
	}

	.switch-box {
		flex: auto;
		display: flex;
		justify-content: flex-end;

		.deposit-switch {
			height: 100%;
		}
	}

	.placeholder {
		color: #808080;
	}

	.btn-box {
		position: fixed;
		bottom: 34px;
		left: 0;
		right: 0;
		padding: 0 30rpx;

		.btn {
			background-color: #086DCA;
			font-size: 14px;
			color: #FFFFFF;
			height: 96rpx;
			line-height: 96rpx;
		}
	}

	.item-bg {
		margin: 0 20rpx;
		padding-top: 50rpx;
		border-bottom: 1px solid #f2f2f2;
	}

	.list-item-left {
		position: absolute;
		flex: auto;
		left: 240rpx;
		text-align: left;
		font-size: 14px;
		color: #191A23;
	}

	.custom-uni-list {
		padding: 20rpx;
		margin: 20rpx;
	}

	.custom-uni-value {
		width: 100%;
		height: 68rpx;
	}

	/deep/ .uni-forms-item__inner {
		padding: 20rpx 0;
		margin: 0 20rpx;
		border-bottom: 1rpx solid #D5E2F3;

	}

	.lastItem {
		/deep/ .uni-forms-item {
			border-bottom: none;
		}
	}

	/deep/ .uni-forms-item__content {
		display: flex;
		justify-content: center;
		align-items: center;
	}
</style>