<template>
	<view class="pages">
		<view class="" :style="{height: navHeight+'px' }"></view>
		<!-- 自定义导航 -->
		<view class="navbar-box">
			<u-navbar title="信息填写" :safeAreaInsetTop="true" @leftClick="leftClick"></u-navbar>
		</view>
		<view class="page-wrap">
			<view class="form-wrap">
				<u--form labelPosition="left" :model="form" :rules="rules" ref="uForm" labelWidth="120rpx" >
					<u-form-item label="姓名" prop="name" borderBottom>
						<u--input v-model="form.name" placeholder="与证件名字一致" border="none"></u--input>
					</u-form-item>
					<u-form-item label="身份证" prop="idcard" borderBottom>
						<u--input v-model="form.idcard" placeholder="身份证号码" border="none"></u--input>
					</u-form-item>
					<u-form-item label="手机号" prop="mobile" borderBottom>
						<u--input v-model="form.mobile" placeholder="常用手机号" border="none"></u--input>
					</u-form-item>
				</u--form>
			<!-- 	<view class="set-default u-flex u-row-between">
					<view class="left">
						<view class="up">设置为默认信息</view>
						<view class="down">提醒：每次下单会默认使用该信息</view>
					</view>
					<view class="right">
						<u-switch v-model="form.default" activeValue='1' inactiveValue='0'></u-switch>
					</view>
				</view> -->
			</view>
			<view class="btn-wrap">
				<view class="full-btn" @click="submit">保存</view>
				<view class="full-btn red" v-if="type=='editVisitor'" @click="delVisitor">删除</view>
				<view class="full-btn white" @click="cancel">取消</view>
			</view>
		</view>
	</view>
</template>

<script>
	import { systemInfo } from "@/mixin.js";
	export default {
		mixins:[systemInfo],
		data() {
			return {
				type:'',
				fromPage:'',
				form: {
					id:null,
					name: '',
					idcard:'',
					mobile:'',
					// default:'0',
				},
				rules: {
					'name': [
						{
							type: 'string',
							required: true,
							message: '请填写姓名',
							trigger: ['blur', 'change']
						}
					],
					'idcard': [
						{
							type: 'string',
							required: true,
							message: '请输入身份证',
							trigger: ['blur', 'change']
						},
						// {
						// 	validator: (rule, value, callback) => {
						// 		// 上面有说，返回true表示校验通过，返回false表示不通过
						// 		// this.$u.test.mobile()就是返回true或者false的
						// 		return this.$u.test.idCard(value);
						// 	},
						// 	message: '请输入正确身份证',
						// 	// 触发器可以同时用blur和change
						// 	trigger: ['change', 'blur'],
						// }
					],
					mobile: [
						{
							type: 'string',
							required: true,
							message: '请输入手机号',
							trigger: ['blur', 'change']
						},
						{
							validator: (rule, value, callback) => {
								// 上面有说，返回true表示校验通过，返回false表示不通过
								// this.$u.test.mobile()就是返回true或者false的
								return this.$u.test.mobile(value);
							},
							message: '请输入正确手机号',
							// 触发器可以同时用blur和change
							trigger: ['change', 'blur'],
						}
					],
				},
			}
		},
		onLoad(page) {
			this.getSystemInfo();
			this.type = page.type;
			this.fromPage = page.fromPage;
			this.form.id = page.id;
			this.form.name = page.name;
			this.form.idcard = page.idcard;
			this.form.mobile = page.mobile;
		},
		onReady() {
			this.$refs.uForm.setRules(this.rules)
		},
		methods: {
			leftClick() {
			   let pages = getCurrentPages();
			   if(pages.length==1){
			   	uni.$u.route('/pages/index/index')
			   }else{
			   	uni.navigateBack()
			   };
			},
			submit() {
				// if(!this.form.idcard.startsWith("52")){
				// 	uni.$u.toast('仅允许以52开头的身份证')
				// 	return
				// }
				this.$refs.uForm.validate().then(res => {
					// uni.$u.toast('表单校验成功')
					console.log('this.type',this.type);
					console.log('form',this.form);
					this.$u.api.insertOrUpdateMember(this.form).then(res=>{
						uni.navigateBack()
						// if(this.type=='addVisitor'){
						// 	// this.addVisitor()
						// }else if(this.type=='editVisitor'){
						// 	// this.editVisitor()
						// }
					}).catch(err=>{
						console.log('insertOrUpdateMember',err);
					})
				}).catch(errors => {
					console.log('errors',errors);
					uni.$u.toast('表单校验失败')
				})
			},
			addVisitor(){
				console.log('form',this.form);
				this.$u.api.insertOrUpdateMember(this.form).then(res=>{
					// console.log('getSettlement',res.data);
					this.pageContent =  res.data;
				}).catch(err=>{
					console.log('insertOrUpdateMember',err);
				})
			},
			editVisitor(){
				console.log('form',this.form);
			},
			delVisitor(){
				console.log('form',this.form);
				uni.showModal({
				    title: '提示',
				    content: '确认删除吗！',
				    success: res => {
				        if (res.confirm) {
				            this.$u.api.deleteMember({id:this.form.id}).then(res=>{
				            	uni.navigateBack()
				            }).catch(err=>{
				            	console.log('deleteMember',err);
				            })
				        }
				    }
				});
			},
			cancel(){
				this.leftClick();
			}
		}
	}
</script>
<style>
page{
	background-color: #F4F5F7;
}	
</style>
<style lang="scss" scoped>
.form-wrap{
	background: #FFFFFF;
	box-shadow: 0rpx 4rpx 8rpx 0rpx rgba(226,226,226,0.5);
	border-radius: 24rpx;
	margin-bottom: 70rpx;
	padding: 10rpx 40rpx 24rpx;
}
.set-default{
	padding-top: 26rpx;
	.up{
		font-size: 28rpx;
		font-weight: 500;
		color: #606060;
		line-height: 42rpx;
		margin-bottom: 20rpx;
	}
	.down{
		font-size: 24rpx;
		font-weight: 400;
		color: #7F7F7F;
		line-height: 36rpx;
	}
}
</style>