<template>
  <div class="reg-form-box">
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm forms">
      <el-form-item label="学校" prop="region">
        <el-select v-model="ruleForm.region" placeholder="请选择学校" class="width100">
          <el-option label="学校一" value="shanghai"></el-option>
          <el-option label="学校二" value="beijing"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="姓名" prop="name">
        <el-input v-model="ruleForm.name" placeholder="请输入您的姓名"></el-input>
      </el-form-item>
      <el-form-item label="手机号" prop="phone">
        <el-input v-model="ruleForm.phone" placeholder="请输入您的手机号"></el-input>
      </el-form-item>
      <el-form-item label="验证码" prop="code">
        <el-input v-model="ruleForm.phone" placeholder="请输入您的手机号"></el-input>
      </el-form-item>
      <el-form-item label="身份证号" prop="codeId">
        <el-input v-model="ruleForm.codeId" placeholder="请输入您的身份证号"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="pass">
        <el-input type="password" v-model="ruleForm.pass" placeholder="请输入您的密码"></el-input>
      </el-form-item>
      <el-form-item label="确认密码" prop="checkPass">
        <el-input type="password" v-model="ruleForm.checkPass" placeholder="请输入您的密码"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')" class="sub-btn">立即注册</el-button>
        <!-- <el-button @click="resetForm('ruleForm')">重置</el-button> -->
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
  export default {
    data() {
      const checkPhone = (rule, value, callback) => { 
        const reg = /^(13[0-9]|14[01456879]|15[0-3,5-9]|16[2567]|17[0-8]|18[0-9]|19[0-3,5-9])\d{8}$/;
        if(!reg.test(value)){
          return callback(new Error())
        }
      }
      const checkCodeId = (rule, value, callback) => { 
        const reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
        if(!reg.test(value)){
          return callback(new Error())
        }
      }
      const validatePass = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入密码!'))
        } else {
          if (this.ruleForm.checkPass !== '') {
            this.$refs.ruleForm.validateField('checkPass')
          }
          callback()
        }
      }
      const validatePass2 = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请再次输入密码'))
        } else if (value !== this.ruleForm.pass) {
          callback(new Error('两次输入密码不一致!'))
        } else {
          callback()
        }
      }
      return {
        ruleForm: {
          name: '',
          region: '',
          phone: '',
          codeId: '',
          delivery: false,
          type: [],
          checkPass: '',
          pass: '',
          code: ''
        },
        rules: {
          name: [
            { required: true, message: '请输入姓名', trigger: 'blur' }
          ],
          region: [
            { required: true, message: '请选择学校', trigger: 'change' }
          ],
          phone: [
            { required: true, message: '请输入手机号', trigger: 'blur' },
            { required: true, message: '手机号不正确', validator: checkPhone, trigger: 'blur' }
          ],
          code: [
            { required: true, message: '请输入验证码', trigger: 'blur' },
            { validator: validatePass, trigger: 'blur' }
          ],
          codeId: [
            { required: true, message: '请输入身份证号', trigger: 'blur' },
            { required: true, message: '身份证号不正确', validator: checkCodeId, trigger: 'blur' }
          ],
          pass: [
            { required: true, message: '请输入密码', trigger: 'blur' },
            { validator: validatePass, trigger: 'blur' }
          ],
          checkPass: [
            { required: true, message: '请输入密码', trigger: 'blur' },
            { validator: validatePass2, trigger: 'blur' }
          ]
        }
      };
    },
    methods: {
      isRules(n) {
        console.log(n)
      },
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            alert('submit!');
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
    }
  }
</script>
<style lang="scss" scoped>
.reg-form-box{
  width: 380px;
  margin: 0 auto;
  padding: 24px 0;
  .width100{
    width: 100%;
  }
}
::v-deep{
  .forms{
    i {
      font-size: 20px;
    }
    input{
      border-radius: 4px;
    }
  }
}
.sub-btn{
  width: 270px;
  height: 40px;
  background: linear-gradient(315deg, rgba(225, 47, 116, 0.83) 0%, #C01540 100%);
  border-radius: 4px;
}
</style>
