<template>
	<view>
		<view class="content">
			<view class="row">
				<view class="nominal">
					发票抬头
				</view>
				<view class="input">
					<input placeholder="请输入发票抬头" type="text" v-model="name" />
				</view>
			</view>
			<view class="row">
				<view class="nominal">
					单位税号
				</view>
				<view class="input">
					<input placeholder="请输入单位税号" type="text" v-model="duty_paragraph" />
				</view>
			</view>
			<view class="row">
				<view class="nominal">
					注册地址
				</view>
				<view class="input">
					<input placeholder="请输入注册地址" type="text" v-model="address" />
				</view>
			</view>
			<view class="row">
				<view class="nominal">
					注册电话
				</view>
				<view class="input">
					<input placeholder="请输入注册电话" type="text" v-model="telphone" />
				</view>
			</view>
			<view class="row">
				<view class="nominal">
					开户银行
				</view>
				<view class="input">
					<input placeholder="请输入开户银行" type="text" v-model="bank" />
				</view>
			</view>
			<view class="row">
				<view class="nominal">
					账号
				</view>
				<view class="input">
					<input placeholder="请输入开户银行" type="text" v-model="account" />
				</view>
			</view>
			<view class="row">
				<view class="nominal">
					联系人
				</view>
				<view class="input">
					<input placeholder="请输入联系人" type="text" v-model="user" />
				</view>
			</view>
			<view class="row">
				<view class="nominal">
					联系电话
				</view>
				<view class="input">
					<input placeholder="请输入联系电话" type="text" v-model="invoice_tel" />
				</view>
			</view>
			<view class="row">
				<view class="nominal">
					收票地址
				</view>
				<view class="input">
					<input placeholder="请输入收票地址" type="text" v-model="invoice_addr" />
				</view>
			</view>
			<!-- <view class="row">
				<view class="nominal">
					所在地区
				</view>
				<view class="input" @tap="chooseCity">
					{{region.label}}
				</view>
				
			</view> -->
			<view class="row">
				<view class="nominal">
					设置默认
				</view>
				<view class="input switch">
					<switch color="#f06c7a" :checked="often" @change=isDefaultChange />
				</view>
			</view>
			<view class="row" v-if="editType=='edit'" @tap="del">
				<view class="del">
					删除
				</view>
			</view>
		</view>
		<view class="protocol">
			<view class="checkbox-box">
				<view class="checkbox" @tap="selected">
					<view :class="[portocol?'on':'']"></view>
				</view>
				<view class="text">我已阅读并同意<view class="linkText" @tap="protocolIndex">《用户服务协议》</view>和<view class="linkText" @tap="protocolPrivacy">《隐私政策》</view>
				</view>
			</view>
			<!-- <checkbox v-model="portocol"></checkbox> -->
			
		</view>
		<view class="save">
			<view v-if="portocol" class="btn on" @tap="save">
				保存
			</view>
			<view v-else class="btn">
				保存
			</view>
		</view>
		<mpvue-city-picker :themeColor="themeColor" ref="mpvueCityPicker" :pickerValueDefault="cityPickerValue"
			@onCancel="onCancel" @onConfirm="onConfirm"></mpvue-city-picker>
	</view>
</template>

<script>
	import mpvueCityPicker from '@/components/mpvue-citypicker/mpvueCityPicker.vue'
	export default {
		components: {
			mpvueCityPicker
		},
		data() {
			return {
				editType: 'edit',
				id: null,
				name: '',
				duty_paragraph: '',
				telphone: '',
				address: '',
				bank: '',
				account: '',
				often: false,
				cityPickerValue: [0, 0, 1],
				themeColor: '#007AFF',
				user:'',
				invoice_tel:'',
				invoice_addr:'',
				portocol:false,
				// region:{label:"请点击选择地址",value:[],cityCode:""}
			};
		},
		methods: {
			selected(){
				this.portocol = !this.portocol;
			},
			protocolIndex(){
				uni.navigateTo({
					url:"../../../protocol/index"
				})
			},
			protocolPrivacy(){
				uni.navigateTo({
					url:"../../../protocol/Privacy"
				})
			},
			onCancel(e) {
				console.log(e)
			},
			chooseCity() {
				this.$refs.mpvueCityPicker.show()
			},
			onConfirm(e) {
				this.region = e;
				this.cityPickerValue = e.value;
			},
			isDefaultChange(e) {
				this.often = e.detail.value;
			},
			del() {
				var that = this
				uni.showModal({
					title: '删除提示',
					content: '你将删除这个开票信息',
					success: (res) => {
						if (res.confirm) {
							that.$u.api.delInvoice(this.id).then(res=>{
								uni.navigateBack({
									
								})
							})

						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});

			},
			save() {
				let data = {
					"name": this.name,
					"duty_paragraph": this.duty_paragraph,
					"address": this.address,
					"telphone": this.telphone,
					"bank": this.bank,
					"account": this.account,
					"often": this.often,
					"user": this.user,
					"invoice_tel": this.invoice_tel,
					"invoice_addr": this.invoice_addr,
				}
				if (this.editType == 'edit') {
					data.id = this.id
				}
				if (!data.name) {
					uni.showToast({
						title: '请输入发票抬头',
						icon: 'none'
					});
					return;
				}
				if (!data.duty_paragraph) {
					uni.showToast({
						title: '请输入税号',
						icon: 'none'
					});
					return;
				}
				uni.showLoading({
					title: '正在提交'
				})
				//实际应用中请提交ajax,模板定时器模拟提交效果
				if(data.id){
					this.$u.api.editInvoice(data.id, data).then(res=>{
						uni.navigateBack({
							
						})
					})
				}else{
					this.$u.api.addInvoice(data).then(res=>{
						uni.navigateBack({
							
						})
					})
				}


			}
		},
		onLoad(e) {
			//获取传递过来的参数

			this.editType = e.type;
			if (e.type == 'edit') {
				uni.getStorage({
					key: 'invoice',
					success: (e) => {
						this.id = e.data.id;
						this.name = e.data.name;
						this.duty_paragraph = e.data.duty_paragraph;
						this.address = e.data.address;
						this.telphone = e.data.telphone;
						this.bank = e.data.bank;
						this.account = e.data.account;
						this.often = e.data.often;
						this.user = e.data.user;
						this.invoice_tel = e.data.invoice_tel;
						this.invoice_addr = e.data.invoice_addr;
						// this.cityPickerValue = e.data.address.region.value;
						// this.region = e.data.address.region;
					}
				})
			}

		},
		onBackPress() {
			if (this.$refs.mpvueCityPicker.showPicker) {
				this.$refs.mpvueCityPicker.pickerCancel();
				return true;
			}
		},
		onUnload() {
			if (this.$refs.mpvueCityPicker.showPicker) {
				this.$refs.mpvueCityPicker.pickerCancel()
			}
		}
	};
</script>
<style lang="scss">
	.protocol{
		font-size: 26upx;
		.checkbox-box{
			display: flex;
			align-items: center;
			padding-bottom: 120upx;
			justify-content: center;
			.checkbox{
				width: 35upx;
				height: 35upx;
				border-radius: 100%;
				border: solid 2upx #f06c7a;
				display: flex;
				justify-content: center;
				align-items: center;
				.on{
					width: 25upx;
					height: 25upx;
					border-radius: 100%;
					background-color: #f06c7a;
				}
			}
			.text{
				display: flex;
				margin-left: 10upx;
				.linkText{
					color: #007AFF;
				}
			}
		}
		
	}
	
	.save {
		view {
			display: flex;
		}

		position: fixed;
		bottom: 0;
		width: 100%;
		height: 120upx;
		display: flex;
		justify-content: center;
		align-items: center;

		.btn {
			box-shadow: 0upx 5upx 10upx rgba(0, 0, 0, 0.4);
			width: 70%;
			height: 80upx;
			border-radius: 80upx;
			background-color: #CCCCCC;
			color: #fff;
			justify-content: center;
			align-items: center;

			.icon {
				height: 80upx;
				color: #fff;
				font-size: 30upx;
				justify-content: center;
				align-items: center;
			}

			font-size: 30upx;
		}
		.on{
			background-color: #f06c7a;
		}
	}

	.content {
		display: flex;
		flex-wrap: wrap;
		// padding-bottom: 120upx;

		view {
			display: flex;
		}

		.row {
			width: 100%;

			margin: 0 3%;
			border-top: solid 1upx #eee;

			.nominal {
				width: 30%;
				height: 120upx;
				font-weight: 200;
				font-size: 30upx;
				align-items: center;
			}

			.input {
				width: 70%;
				padding: 20upx 0;
				align-items: center;
				font-size: 30upx;

				&.switch {
					justify-content: flex-end;
				}

				.textarea {
					margin: 20upx 0;
					min-height: 120upx;
				}
			}

			.del {
				width: 100%;
				height: 100upx;
				justify-content: center;
				align-items: center;
				font-size: 36upx;
				color: #f06c7a;
				background-color: rgba(255, 0, 0, 0.05);
				border-bottom: solid 1upx #eee;
			}
		}
	}
</style>
