<template>
  <div id="register_pass">
    <Ediheader></Ediheader>
    <div class="register-container full-page">
      <el-row style="">
        <el-col :span="24" style="margin：auto" v-if="nav">
          <div class="grid-content bg-purple" style="width:40%;">
            <el-form
              :model="ruleForm2"
              status-icon
              :rules="rules2"
              ref="ruleForm2"
              label-width="120px"
              class="demo-ruleForm"
            >
              <el-form-item prop="sFullName" label="公司全称" class="register_item">
                <el-input v-model="ruleForm2.sFullName" auto-complete="off" placeholder="公司全称"></el-input>
              </el-form-item>
              <el-form-item prop="sEmail" label="常用邮箱" class="register_item">
                <el-input v-model="ruleForm2.sEmail" auto-complete="off" placeholder="常用邮箱"></el-input>
              </el-form-item>
              <el-form-item prop="verify_code" class="register_item verify_code" label="验证码">
                <el-input style="wihth:40%" v-model="ruleForm2.verify_code" placeholder="验证码"></el-input>
                <el-image
                  style="width: 100px; height: 40px;margin-left:15px;vertical-align: middle"
                  :src="url"
                ></el-image>
                <span style="margin-left:15px;" @click="switcher()">换一张</span>
              </el-form-item>
              <el-form-item class="register_item">
                <el-button type="primary" @click="next()" style="width:200px;">下一步</el-button>
              </el-form-item>
            </el-form>
          </div>
        </el-col>
        <el-col :span="24" style="margin：auto" v-if="!nav">
          <div class="grid-content bg-purple" style="width:40%;">
            <el-form
              :model="ruleForm"
              status-icon
              :rules="rules"
              ref="ruleForm"
              label-width="120px"
              class="demo-ruleForm"
            >
              <el-form-item prop="sPassword" label="新密码" class="register_item">
                <el-input
                  type="password"
                  v-model="ruleForm.sPassword"
                  auto-complete="off"
                  placeholder="输入密码(长度在 6 到 16个字符)"
                ></el-input>
              </el-form-item>
              <el-form-item prop="checkPass" label="确认新密码" class="register_item">
                <el-input
                  type="password"
                  v-model="ruleForm.checkPass"
                  auto-complete="off"
                  placeholder="确认新密码"
                ></el-input>
              </el-form-item>
              <el-form-item class="register_item">
                <el-button type="primary" @click="submitForm('ruleForm')" style="width:200px;">确定</el-button>
              </el-form-item>
            </el-form>
          </div>
        </el-col>
        <el-col :span="24">
          <!-- <el-button style="width:200px;" @click="submitForm('ruleForm2')" type="primary">注册</el-button> -->
          <div style="font-size:16px; margin-top: 30px;">
            <span class="login" @click="go_login">去登录</span>
          </div>
        </el-col>
      </el-row>
    </div>
  </div>
</template>
<script>
import Ediheader from './header.vue'
import axios from 'axios'
export default {
  components: {
    Ediheader
  },
  name: 'Register',
  data() {
    // const checkTel = (rule, value, callback) => {
    //   if (value === '') {
    //     return callback(new Error('请输入手机号码'))
    //   } else if (!this.checkMobile(value)) {
    //     return callback(new Error('手机号码不合法'))
    //   } else {
    //     callback()
    //   }
    // }
    //  <!--验证码是否为空-->
    const validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'))
      } else {
        // if (this.ruleForm2.checkPass !== '') {
        //   this.$refs.ruleForm2.validateField('checkPass')
        // }
        callback()
      }
    }
    // <!--二次验证密码-->
    const validatePass2 = (rule, value, callback) => {
      console.log(value)
      if (value === '') {
        callback(new Error('请再次输入密码'))
      } else if (value !== this.ruleForm.sPassword) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    // 邮箱的验证格式
    var checkEmail = (rule, value, callback) => {
      const mailReg = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/
      if (!value) {
        return callback(new Error('邮箱不能为空'))
      }
      setTimeout(() => {
        if (mailReg.test(value)) {
          callback()
        } else {
          callback(new Error('请输入正确的邮箱格式'))
        }
      }, 100)
    }
    return {
      ruleForm: {},
      ruleForm2: {},
      nav: true,
      module_name: '/mesAPI/user',
      userinfo: {},
      rules2: {
        sFullName: [
          { required: true, message: '请输入公司全称', trigger: 'change' }
        ],
        sEmail: [
          {
            validator: checkEmail,
            required: true
          }
        ]
      },
      rules: {
        sPassword: [
          { validator: validatePass, trigger: 'change', required: true },
          {
            min: 6,
            max: 16,
            message: '长度在 6 到 16个字符'
          }
        ],
        checkPass: [
          { validator: validatePass2, trigger: 'change', required: true }
        ]
      },
      buttonText: '发送验证码',
      isDisabled: false, // 是否禁止点击发送验证码按钮
      flag: true,
      sign: '',
      url: 'mesAPI/'
    }
  },
  mounted() {
    this.get_code()
  },
  methods: {
    // 验证用户
    set_user() {
      axios
        .post(`${this.module_name}/verify_userinfo/`, this.ruleForm2)
        .then(res => {
          if (res.data.message === '验证通过!') {
            this.nav = false
            console.log(this.nav)
          }
        })
    },
    switcher() {
      this.url = 'mesAPI/'
      this.get_code()
    },
    // 验证邀请链接是否过期
    get_code() {
      this.$API.get(`${this.module_name}/verify_code/`).then(res => {
        this.url = `${this.url}user/image/${res.data.filename}/`
      })
    },
    // <!--下一步-->
    next() {
      this.$refs['ruleForm2'].validate(valid => {
        if (valid) {
          this.set_user()
        } else {
          console.log('error submit!!')
          this.$message('请检查信息')
          return false
        }
      })
    },
    // 提交新密码
    set_pass() {
      const obj = {}
      obj.password = this.ruleForm.sPassword
      axios.post(`${this.module_name}/retrieve_password/`, obj).then(res => {
        this.nav = false
        this.$message.success('密码设置成功')
      })
    },
    submitForm(form) {
      this.$refs[form].validate(valid => {
        if (valid) {
          this.set_pass()
        } else {
          this.$message('请检查信息')
          return false
        }
      })
    },
    go_login() {
      this.$router.push({ path: 'login' })
    }
    // <!--发送验证码-->
    // 验证手机号
  },
  activated() {
    // this.get_code()
  }
}
</script>

<style scoped>
#register_pass {
  margin-top: 60px;
}
.grid-content {
  margin: 20px auto;
}
.register_item {
  margin-top: 20px;
}
.grid-content {
  width: 60%;
  height: 50%;
  margin-top: 90px;
}
.grid-content >>> h5 {
  margin-bottom: 30px;
}
.register_item_two >>> .el-input {
  width: 48%;
}
.login {
  color: #b0dfc5;
}
.register_item >>> .el-form-item__error {
  top: 100%;
}
.verify_code >>> .el-input {
  width: 55%;
}
.verify_code {
  text-align: left;
}
</style>