<template>
	<div class="content">
		<el-header>
			<h3 class="title">河北省计量业务应用平台</h3>
		</el-header>
		<div class="user-from">
			<el-form :model="userForm" :rules="rules"  ref="userForm" label-width="120px" class="demo-userForm">
				<el-form-item label="身份证号" prop="idNo">
					<el-input v-model="userForm.idNo" @blur="onInputBlur" placeholder="请输入身份证号" :maxlength="18"></el-input>
				</el-form-item>
				<el-form-item label="真实姓名" prop="name">
					<el-input v-model="userForm.name" placeholder="请输入真实姓名"></el-input>
				</el-form-item>
				<el-form-item label="个人手机号" prop="mobile">
					<el-input v-model="userForm.mobile" placeholder="请输入手机号" ></el-input>
					<el-button class="Tips" type="text" :disabled='isDisabled' @click.stop.prevent="getcode()">点击获取验证码 {{downTime ? downTime+'s':''}}</el-button>
				</el-form-item>
				<el-form-item label="手机验证码" prop="verificationCode">
					<el-input v-model="userForm.verificationCode" placeholder="请输入验证码"></el-input>
				</el-form-item>
				<el-form-item label="用户名">
					<el-input v-model="userForm.userName" :disabled="true"></el-input>
				</el-form-item>
				<el-form-item label="密码" prop="password">
					<paddwordinput v-model="userForm.password" placeholder="请输入密码" :pastips='true' :maxlength="10"></paddwordinput>
					<el-button class="Tips" type="text">密码必须为8至10位大小写字母和数字组合</el-button>
				</el-form-item>
				<el-form-item label="确认密码" prop="rePassword">
					<paddwordinput v-model="userForm.rePassword" placeholder="请再次输入密码" :maxlength="10"></paddwordinput>
				</el-form-item>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="身份证正面照" prop="idFrontPhoto">
            	<UploadImg @imgUrl="FrontPhotoUrl"></UploadImg>
            	<el-input v-model="userForm.idFrontPhoto" v-show="false"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="身份证背面照" prop="idBackPhoto">
            	<UploadImg @imgUrl="BackPhotoUrl"></UploadImg>
            	<el-input v-model="userForm.idBackPhoto" v-show="false"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
				<el-form-item style="text-align: center;">
					<el-button type="primary" @click="submitForm('userForm')" :disabled="btnDisabled">注册</el-button>
					<el-button @click="resetForm('userForm')">关闭</el-button>
				</el-form-item>
			</el-form>
		</div>
	</div>
</template>

<script>
	import {
		registUser,
		getVerificationCode
	} from '@/api/user1.js'
	import qs from 'qs'
	import UploadImg from '@/components/uploadFile/index.vue'
	import paddwordinput from '@/components/passwordSafe/index.vue'
	export default {
		components:{ UploadImg, paddwordinput },
		data() {
      var validatorPhone = function (rule, value, callback) {
        if (value === '') {
          callback(new Error('手机号不能为空'))
        } else if (!/^1\d{10}$/.test(value)) {
          callback(new Error('手机号格式错误'))
        } else {
          callback()
        }
      }
      var validatePass2 = (rule, value, callback) => {
      	  if (value === '') {
               callback(new Error('请再次输入密码'))
               // password 是表单上绑定的字段
            } else if (value !== this.userForm.password) {
               callback(new Error('两次输入密码不一致!'))
            } else {
             	 callback()
            }
          }
			return {
				downTime: '',
				isDisabled: false,
				userForm: {
					name: '',
					userName: '', //用户名
					password: '',
					rePassword: '',
					mobile: '', // 手机号
					verificationCode: '',
					idNo: '',
					idBackPhoto: '', // 反面
					idFrontPhoto: ''
				},
				btnDisabled: false,
				rule: [{required: true, message: '不能为空', trigger: 'blur'}],
        rules: {
          idNo: [
            { required: true, message: '请输入身份证号', trigger: 'blur' },
            { pattern: /(^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$)|(^[1-9]\d{5}\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{2}$)/,
              message: '身份证号格式错误', trigger: 'blur' ,
            }
          ],
          name: [
            { required: true, message: '请输入真实姓名', trigger: 'blur' }
          ],
          mobile: [
            { required: true, validator: validatorPhone, trigger: 'blur' }
          ],
          verificationCode: [
            { required: true, message: '请输入手机验证码', trigger: 'blur' }
          ],
          userName: [
            { required: true, message: '请输入身份证号', trigger: 'blur' }
          ],
          password: [
            { required: true, message: '请输入密码', trigger: 'blur' },
            { min: 8, max: 10, message: '长度在 8 到 10 个字符大小写字母和数字组合', trigger: 'blur' }
          ],
          rePassword: [
            { required: true, validator: validatePass2, trigger: 'blur' }
          ],
          idFrontPhoto: [
            { required: true, message: '请上传身份证正面', trigger: 'blur' }
          ],
          idBackPhoto: [
            { required: true, message: '请上传身份证背面', trigger: 'blur' }
          ],
        }
			};
		},
		methods: {
			// 手机验证码
			getcode() {
        let _this = this
        if (!/^1\d{10}$/.test(_this.userForm.mobile)) {
          this.$message({
            type: 'error',
            message: '手机号格式错误!'
          });
        }else {
          _this.downTime = 60
          let timeInterval = null
          _this.isDisabled = true
          timeInterval = setInterval(()=>{
          	--_this.downTime
          	if(_this.downTime == 0){
          		clearInterval(timeInterval)
          		_this.downTime = '';
             _this.isDisabled = false
          	}
          },1000)
          getVerificationCode({
          	mobile: _this.userForm.mobile
          }).then(res =>{
            // this.isDisabled = false
          })
        }
			},
			FrontPhotoUrl(url){
				this.userForm.idFrontPhoto = url.filePath
			},
			BackPhotoUrl(url){
				this.userForm.idBackPhoto = url.filePath
			},
			submitForm(formName) {
				this.btnDisabled = true
				this.$refs[formName].validate((valid) => {
					if (valid) {
						registUser(qs.stringify(this.userForm)).then((res) => {
							this.btnDisabled = false
							this.$confirm('注册成功, 是否关闭页面?', '提示', {
									confirmButtonText: '确定',
									cancelButtonText: '取消',
									type: 'success'
								}).then(() => {
									window.close();
								})
						}).catch(() => {
							this.btnDisabled = false
						})
					} else {
						console.log('error submit!!');
            this.btnDisabled = false
						return false;
					}
				});
			},
			resetForm(formName) {
        window.close();
				this.$refs[formName].resetFields();
			},
			onInputBlur() { // 用户名密码
				this.userForm.userName = this.userForm.idNo
			},
		}
	}
</script>

<style lang="scss" scoped>
  @import "~@/styles/variables.scss";
  .el-header{
    width: 100%;
    background-color: #0294d7;
    color: #fff;
    line-height: 60px;
    overflow: hidden;
    position: fixed;
    z-index: 99;
    top: 0;
    .title{
      float: left;
      font-size: 26px;
      margin-left: 35px;
    }
  }
  .content{
    padding: 50px 0;
    background-color: #f7f9fc;
  }
</style>

<style lang="scss">
  @import "~@/styles/variables.scss";
	.user-from {
		width: 860px;
		padding-top: 20px;
		margin: 0 auto;
		.el-input__inner:focus {
			border: 1px solid $border-color;
		}
		.Tips {
			padding-left: 10px;
			position: absolute;
			left: 100%;
			top: 0;
		}

		.pasTips {
			display: flex;
			justify-content: center;

			span {
				display: inline-block;
				margin: 15px 10px 0 10px;
				padding: 0 30px;
				line-height: 30px;
				border-top: 3px solid red;
				opacity: 0.3;
			}

			span:nth-child(2) {
				border-color: blue;
			}

			span:nth-child(3) {
				border-color: green;
			}
		}

		.avatar-uploader .el-upload {
			border: 1px dashed #d9d9d9;
			border-radius: 6px;
			cursor: pointer;
			position: relative;
			overflow: hidden;
		}

		.avatar-uploader .el-upload:hover {
			border-color: #409EFF;
		}

		.avatar-uploader-icon {
			font-size: 28px;
			color: #8c939d;
			width: 280px;
			height: 178px;
			line-height: 178px;
			text-align: center;
		}

		.avatar {
			width: 280px;
			height: 178px;
			display: block;
		}
	}
</style>
