<template>
	<view class="containers">
		 <!-- #ifdef MP-WEIXIN -->
		    <privacy-popup ref="privacyComponent"  @agree-privacy="onAgreePrivacy"
        @reject-privacy="onRejectPrivacy"></privacy-popup> 
		    <!-- #endif -->
		<view class="steps">
			<uni-steps :options="steps" :active="stepActive" active-color="#4f607e" />
		</view>
		<uni-forms ref="formdata" :model="form" :rules="rules" labelWidth="100px">
			<view class="formtext">基础信息</view>
			<uni-forms-item label="学习类型" required>
				<uni-data-select @change='changeType' v-model="form.type" :localdata="typeList">
				</uni-data-select>
			</uni-forms-item>
			<uni-forms-item label="姓名" required name="name">
				<uni-easyinput v-model="form.name" placeholder="请输入姓名" />
			</uni-forms-item>
			<uni-forms-item label="联系电话" required name="mobile">
				<uni-easyinput v-model="form.mobile" placeholder="请输入联系电话" />
			</uni-forms-item>
			<uni-forms-item label="身份证号" required name="idcard">
				<uni-easyinput type="idcard" v-model="form.idcard" placeholder="请输入证件号码" />
			</uni-forms-item>
		</uni-forms>
		<button class='study' type="primary" style="background-color: #4f607e;" @click="toUrl">开始学习</button>
	</view>
</template>

<script>
	import PrivacyPopup from '@/components/privacy-popup/privacy-popup.vue';
	export default{
		components: {
			PrivacyPopup
		},
		data(){
			return{
				steps: [{
					title: '填报'
				}, {
					title: '学习'
				}, {
					title: '完成'
				}],
				stepActive: 0,
				form:{
					type:1,
					name:'',
					idcard:'',
					mobile:''
				},
				rules:{
					name: {
						rules: [{
							required: true,
							errorMessage: '姓名不能为空'
						}]
					},
					type: {
						rules: [{
							required: true,
							errorMessage: '类型不能为空'
						}]
					},
					mobile: {
						rules: [{
							required: true,
							errorMessage: '联系电话不能为空'
						}, {
							pattern: '^[1][3-9][\\d]{9}',
							errorMessage: '联系号码格式错误'
						}]
					},
					idcard: {
						rules: [{
							required: true,
							errorMessage: '身份证号码不能为空'
						}, {
							pattern: '^[1-9]\\d{5}(18|19|20)\\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\\d{3}[0-9Xx]',
							errorMessage: '身份证格式错误'
						}]
					},
				},
				typeList:[{
					value:1,
					text:'普通访客'
				},{
					value:2,
					text:'施工人员'
				},{
					value:3,
					text:'物流司机'
				}]
			}
		},
		
		onLoad() {
			this.isStudy()
			let res = uni.getStorageSync('studydata')
			if(res){
				console.log(res)
				this.form = res
			}
		},
		methods:{
			onAgreePrivacy() {
				// 用户同意隐私政策
				// 在这里添加您想要执行的代码
				console.log('User agreed to the privacy policy');
			},
			onRejectPrivacy() {
				// 用户拒绝隐私政策
				// 在这里添加您想要执行的代码
				console.log('User rejected the privacy policy');
			},
			toUrl(){
				this.$refs['formdata'].validate().then(res => {
					uni.setStorageSync('studydata', this.form)
					uni.navigateTo({
						url: '/pages/visit/study?type=' + this.form.type
					})
				}).catch(err => {
					console.log('err', err);
				})
			},
			changeType(e){
				console.log(e)
				this.form.type = e
				this.isStudy()
			},
			isStudy(){
				let that = this
				this.util.request({
					api: '/api/mobile/visit/ask-log',
					method: "get",
					data:{
						type:that.form.type
					},
					utilSuccess: function(res) {
						if(res.msg=='学习有效中'){
							uni.showToast({
								title: '该类型培训已通过，无须重复培训',
								duration: 2000,
								icon: 'none'
							})
						}
					},
					utilFail: function(res) {
						// console.log(res)
						// uni.showToast({
						// 	title: res,
						// 	duration: 2000,
						// 	icon: 'none'
						// })
					}
				})
			}
			
			
		}
	}
</script>

<style>
	.containers {
		background-color: #fff;
		min-height: 100vh;
		padding: 20rpx;
		padding-bottom: 60rpx;
	}	
	.formtext {
		margin-bottom: 20rpx
	}
	.steps {
		margin-bottom: 40rpx
	}
	
	.study{
		position: absolute;
		bottom: 60rpx;
		margin: 20rpx;
		width: 90%;
	}
</style>