<template>
  <div class="register">
    <div>
      <el-form :model="enterprise1" :rules="rules1" ref="enterprise1" label-width="100px" class="demo-ruleForm">
        <el-form-item prop="spaceName">
          <el-input type="text"
                    v-model="enterprise1.spaceName"
                    v-bind:class="{ active: isSpaceNameExist }"
                    auto-complete="off"
                    placeholder="企业名称"
          ></el-input>
          <span class="tip exist" v-show="isSpaceNameExist">该企业已被注册，请确认。<a href="https://www.usoftchina.com/contact">仍有问题？</a></span>
        </el-form-item>
        <el-form-item prop="businessCode">
          <el-input type="text"
                    v-model="enterprise1.businessCode"
                    v-bind:class="{ active: isBusinessCodeExist }"
                    auto-complete="off"
                    placeholder="营业执照号"></el-input>
          <span class="tip exist" v-show="isBusinessCodeExist">该企业已被注册，请确认。<a href="https://www.usoftchina.com/contact">仍有问题？</a></span>
        </el-form-item>
        <el-form-item prop="vipName" v-if="!isHasRegister">
          <el-input type="text" v-model="enterprise1.vipName" auto-complete="off" placeholder="管理员姓名"></el-input>
        </el-form-item>
        <el-form-item prop="password" v-if="!isHasRegister">
          <el-input type="password" v-model="enterprise1.password" auto-complete="off" placeholder="登录密码"></el-input>
          <div class="pwd sm" v-show="showMsgTip1">密码强度 <em></em><em></em><em></em><span>弱</span></div>
          <div class="pwd md" v-show="showMsgTip2">密码强度 <em></em><em></em><em></em><span>中</span></div>
          <div class="pwd lar" v-show="showMsgTip3">密码强度 <em></em><em></em><em></em><span>强</span></div>
          <div class="pwd low" v-show="showMsgTip4">密码强度 <em></em><em></em><em></em></div>
        </el-form-item>
        <el-form-item prop="password" v-if="isHasRegister">
          <el-input type="password"
                    v-model="enterprise1.password"
                    auto-complete="off"
                    placeholder="密码确认"
                    v-bind:class="{ correct: this.passwordChecked }"></el-input>
          <span class="sure" v-if="isHasRegister" v-show="phoneIsRegisterTip">手机号已注册，请输入原密码 <a>忘记密码?</a></span>
        </el-form-item>
        <el-form-item prop="confirm" v-if="!isHasRegister">
          <el-input type="password"
                    auto-complete="off"
                    placeholder="密码确认"
                    v-model="enterprise1.confirm"></el-input>
        </el-form-item>
        <el-form-item prop="email" v-if="!isHasEmail">
          <el-input type="text" v-model="enterprise1.email" auto-complete="off" placeholder="联系邮箱"></el-input>
        </el-form-item>
        <a class="btn finish"
           :disabled="!isHasRegister ? !spaceNameChecked || !businessCodeChecked || !vipNameChecked || !passwordChecked || !passwordConfirmChecked || !emailChecked || !checked : !isHasEmail ? !spaceNameChecked || !businessCodeChecked || !passwordChecked || !emailChecked || !checked : !spaceNameChecked || !businessCodeChecked || !passwordChecked || !checked"
           @click="sureRegister">确认注册
        </a>
        </el-form-item>

        <el-form-item>
          <el-checkbox name="type" v-model="checked" @click="checkboxIsChecked"></el-checkbox>
          <span class="agree">我已阅读并同意 <a href="/common/agreement">《优软云服务条款》</a></span>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
  export default {
    name: 'EnterpriseRegistrationSecondStep',
    data () {
//      企业注册第二步
      var validateSpaceName = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请填写正确的企业名称'))
          this.spaceNameChecked = false
        } else {
          if (this.enterprise1.spaceName !== '') {
            if (value.length > 20) {
              callback(new Error('输入长度过长，20个字符以内'))
              this.spaceNameChecked = false
            } else {
              this.$http.get(`/api/userspace/checkSpaceName`, {params: {spaceName: this.enterprise1.spaceName}})
                .then(response => {
                  if (response.data.success) {
                    this.spaceNameChecked = true
                    this.isSpaceNameExist = false
                  } else {
                    this.spaceNameChecked = false
                    this.isSpaceNameExist = true
                    return Promise.reject(response.data)
                  }
                }).catch(err => {
                  // console.log(err)
                  this.$message.error(err.errMsg)
                })
            }
          }
          callback()
        }
      }
      var validateBusinessCode = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请填写正确的营业执照号'))
          this.businessCodeChecked = false
        } else {
          if (this.enterprise1.businessCode !== '') {
            let reg = /^[0-9]*$/
            if (reg.test(value)) {
              if (value.length > 20) {
                callback(new Error('输入长度过长，20个字符以内'))
                this.businessCodeChecked = false
                this.isBusinessCodeExist = false
              } else {
                this.$http.get(`/api/userspace/checkBusinessCode`, {params: {businessCode: this.enterprise1.businessCode}})
                  .then(response => {
                    if (response.data.success) {
                      this.businessCodeChecked = true
                      this.isBusinessCodeExist = false
                    } else {
                      this.businessCodeChecked = false
                      this.isBusinessCodeExist = true
                      return Promise.reject(response.data)
                    }
                  }).catch(err => {
                    // console.log(err)
                    this.$message.error(err.errMsg)
                  })
              }
            } else {
              callback(new Error('请填写正确的营业执照号'))
              this.businessCodeChecked = false
              this.isBusinessCodeExist = false
            }
          }
          callback()
        }
      }
      var validateVipName = (rule, value, callback) => {
        if (this.isHasRegister) {
          this.vipNameChecked = true
        } else {
          if (value === '') {
            callback(new Error('请填写正确的管理员姓名'))
            this.vipNameChecked = false
          } else {
            if (this.enterprise1.vipName !== '') {
              if (value.length > 20) {
                callback(new Error('输入长度过长，20个字符以内'))
              } else {
                this.vipNameChecked = true
              }
            }
            callback()
          }
        }
      }
      var validatePassword = (rule, value, callback) => {
        if (this.enterprise1.password !== '') {
          if (value.length <= 20 && value.length >= 8) {
            var reg1 = /^(?=.{8,20})(((?=.*[0-9])(?=.*[a-z])(?=.*[A-Z]))|((?=.*[0-9])((?=.*[a-zA-Z]))(?=.*[^a-zA-Z0-9]))).*$/
            var reg2 = /^(?=.{8,20})(((?=.*[0-9])(?=.*[a-z]))|((?=.*[0-9])(?=.*[A-Z]))).*$/
            if (reg1.test(value)) {
//                callback(new Error('密码强度强'))
              this.showMsgTip3 = true
              this.showMsgTip2 = false
              this.showMsgTip1 = false
              this.showMsgTip4 = false
              this.passwordChecked = true
            } else if (reg2.test(value)) {
//                callback(new Error('密码强度中'))
              this.showMsgTip2 = true
              this.showMsgTip3 = false
              this.showMsgTip1 = false
              this.showMsgTip4 = false
              this.passwordChecked = true
            } else {
              this.showMsgTip1 = true
              this.showMsgTip3 = false
              this.showMsgTip2 = false
              this.showMsgTip4 = false
              this.passwordChecked = false
            }
          } else {
            this.showMsgTip3 = false
            this.showMsgTip2 = false
            this.showMsgTip1 = false
            this.showMsgTip4 = true
            this.passwordChecked = false
          }
        }
        callback()
      }
      var validatePasswordTip = (rule, value, callback) => {
        if (this.isHasRegister) {
          if (value === '') {
            callback(new Error('请输入密码'))
            this.passwordChecked = false
            this.phoneIsRegisterTip = false
          } else {
            this.passwordChecked = true
          }
        } else {
          if (value === '') {
            callback(new Error('请输入密码'))
            this.passwordChecked = false
          } else {
            if (this.enterprise1.password !== '') {
              if (value.length <= 20 && value.length >= 8) {
                var reg1 = /^(?=.{8,20})(((?=.*[0-9])(?=.*[a-z])(?=.*[A-Z]))|((?=.*[0-9])((?=.*[a-zA-Z]))(?=.*[^a-zA-Z0-9]))).*$/
                var reg2 = /^(?=.{8,20})(((?=.*[0-9])(?=.*[a-z]))|((?=.*[0-9])(?=.*[A-Z]))).*$/
                if (reg1.test(value)) {
                  this.passwordChecked = true
                } else if (reg2.test(value)) {
                  this.passwordChecked = true
                } else {
                  callback(new Error('密码须为8-20字符的英文、数字混合'))
                  this.passwordChecked = false
                }
              } else {
                callback(new Error('密码须为8-20字符的英文、数字混合'))
                this.passwordChecked = false
              }
            }
            callback()
          }
        }
      }
      var validateConfirm = (rule, value, callback) => {
        if (this.isHasRegister) {
          this.passwordConfirmChecked = true
        } else {
          if (value === '') {
            callback(new Error('请再次输入密码'))
            this.passwordConfirmChecked = false
          } else if (value !== this.enterprise1.password) {
            callback(new Error('两次输入密码不一致!'))
          } else {
            this.passwordConfirmChecked = true
            callback()
          }
        }
      }
      var validateEmail = (rule, value, callback) => {
        if (this.isHasEmail) {
          this.emailChecked = true
        } else {
          if (!value) {
            callback(new Error('请填写正确的联系邮箱'))
            this.emailChecked = false
          } else {
            if (this.enterprise1.email) {
              var reg = /@/
              if (!reg.test(value)) {
                callback(new Error('请输入正确的邮箱地址格式'))
                this.emailChecked = false
              } else {
                this.emailChecked = true
              }
            }
            callback()
          }
        }
      }
      return {
//        企业注册第二步
        enterprise1: {
          spaceName: '',
          businessCode: '',
          vipName: '',
          password: '',
          confirm: '',
          email: ''
        },
        checked: true,
        codeChecked: false,
        enterprise_time: 0,
        showMsgTip1: false,
        showMsgTip2: false,
        showMsgTip3: false,
        showMsgTip4: false,
        spaceNameChecked: false,
        businessCodeChecked: false,
        vipNameChecked: false,
        passwordChecked: false,
        passwordConfirmChecked: false,
        emailChecked: false,
        isHasRegister: false,
        isHasEmail: false,
        phoneIsRegisterTip: true,
        isSpaceNameExist: false,
        isBusinessCodeExist: false,
//        企业注册第二步
        rules1: {
          spaceName: [
            {validator: validateSpaceName, trigger: 'blur'}
          ],
          businessCode: [
            {validator: validateBusinessCode, trigger: 'blur'}
          ],
          vipName: [
            {validator: validateVipName, trigger: 'blur'}
          ],
          password: [
            {validator: validatePassword, trigger: 'change'},
            {validator: validatePasswordTip, trigger: 'blur'}
          ],
          confirm: [
            {validator: validateConfirm, trigger: 'blur'}
          ],
          email: [
            {validator: validateEmail, trigger: 'blur'}
          ]
        }
      }
    },
    methods: {
//      我同意是否被选中
      checkboxIsChecked () {
        this.checked = !this.checked
      },
//      确认注册
      sureRegister () {
        if (!this.isHasRegister) {
          if (this.spaceNameChecked && this.businessCodeChecked && this.vipNameChecked && this.passwordChecked && this.passwordConfirmChecked && this.emailChecked && this.checked) {
            var url = window.location.search
            var request = {}
            if (url.indexOf('?' !== -1)) {
              var str = url.substr(1)
              var strs = str.split('&')
              for (var i = 0; i < strs.length; i++) {
                request[strs[i].split('=')[0]] = decodeURI(strs[i].split('=')[1])
              }
            }
            var appId = request['appId'] || ''
            let param = new FormData()
            param.append('spaceName', this.enterprise1.spaceName)
            param.append('businessCode', this.enterprise1.businessCode)
            param.append('vipName', this.enterprise1.vipName || '')
            param.append('password', this.enterprise1.password)
            param.append('email', this.enterprise1.email || '')
            param.append('appId', appId)
            let config = {
              headers: {'Content-Type': 'multipart/form-data'}
            }
            this.$http.post('/sso/userspace/register', param, config)
              .then(response => {
                if (response.data.success) {

                } else {
                  return Promise.reject(response.data)
                }
              }).catch(err => {
                this.$message.error(err.errMsg)
              })
          }
        } else {
          if (this.isHasEmail) {
            if (this.spaceNameChecked && this.businessCodeChecked && this.passwordChecked) {
              let param = new FormData()
              param.append('spaceName', this.enterprise1.spaceName)
              param.append('businessCode', this.enterprise1.businessCode)
              param.append('vipName', this.enterprise1.vipName || '')
              param.append('password', this.enterprise1.password)
              param.append('email', this.enterprise1.email || '')
              param.append('appId', appId)
              let config = {
                headers: {'Content-Type': 'multipart/form-data'}
              }
              this.$http.post('/sso/userspace/register', param, config)
                .then(response => {
                  if (response.data.success) {

                  } else {
                    return Promise.reject(response.data)
                  }
                }).catch(err => {
                  this.$message.error(err.errMsg)
                })
            }
          } else {
            if (this.spaceNameChecked && this.businessCodeChecked && this.passwordChecked && this.emailChecked) {
              let param = new FormData()
              param.append('spaceName', this.enterprise1.spaceName)
              param.append('businessCode', this.enterprise1.businessCode)
              param.append('vipName', this.enterprise1.vipName || '')
              param.append('password', this.enterprise1.password)
              param.append('email', this.enterprise1.email || '')
              param.append('appId', appId)
              let config = {
                headers: {'Content-Type': 'multipart/form-data'}
              }
              this.$http.post('/sso/userspace/register', param, config)
                .then(response => {
                  if (response.data.success) {
                    window.location.href = '/overRegister/overEnterprise'
                  } else {
                    return Promise.reject(response.data)
                  }
                }).catch(err => {
                  this.$message.error(err.errMsg)
                })
            }
          }
        }
      }
    }
  }
</script>
<style lang="scss" scoped>
  .register {
    margin: 0 auto;
    width: 100%;
    background: #fff;
    .container{
      padding-top: 50px;
      margin: 0 auto;
      width: 980px;
      text-align: center;
      .content{
        padding: 0 50px;
        margin: 50px auto 0;
        width: 100%;
        text-align: center;
        background: #fff;
          .content-top{
            height: 80px;
            line-height: 80px;
            border-bottom: 1px solid #dcdcdc;
            h3{
              font-family: 'SimHei';
              font-size: 24px;
              color: #000;
            }
          }
          form {
            padding-bottom: 44px;
            margin-top: 35px;
              span.sure{
                position: absolute;
                top: 0;
                right: -240px;
                font-size: 13px;
                color: #8c8c8c;
              }
              span.codeError-tip{
                position: absolute;
                top: 3px;
                right: -105px;
                color: #ff4949;
                font-size: 12px;
              }
              input{
                padding: 0 0 0 18px;
                width: 360px;
                height: 44px;
                line-height: 44px;
                font-size: 14px;
                color: #000;
                border-radius: 0;
              }
              i{
                position: absolute;
                top: 10px;
                right: 18px;
                font-size: 24px;
                color: #a0a0a0;
                cursor: pointer;
              }
              .pwd {
                margin: 6px 0 -15px 0;
                text-align: left;
                font-size: 13px;
              em{
                display: inline-block;
                margin: 0 8px 2px 0;
                width: 24px;
                height: 6px;
              &:first-child{
                 margin-left: 10px;
               }
              }
              span{
                margin-left: 10px;
                font-size: 13px;
              }
              }
              .pwd.sm{
                color: #8c8c8c;
              em {
                background: #bfbfbf;
              &:first-child{
                 background: #ff4e00;
               }
              }
              span{
                color: #ff4e00;
              }
              }
              .pwd.md{
                color: #8c8c8c;
              em {
                background: #22ac38;
              &:nth-child(3){
                 background: #bfbfbf;
               }
              }
              span{
                color: #22ac38;
              }
              }
              .pwd.lar{
                color: #8c8c8c;
              em {
                background: #00a0e9;
              }
              span{
                color: #00a0e9;
              }
              }
              .pwd.low{
                color: #8c8c8c;
              em {
                background: #bfbfbf;
              }
              span{
                color: #00a0e9;
              }
              }
              span.tip{
                position: absolute;
                top: 0;
                right: -165px;
                font-size: 13px;
                color: #8c8c8c;
              }
              span.tip.exist{
                right: -239px;
                a{
                  color: #2d8cf0;
                }
              }
              input.msg{
                float: left;
                width: 210px;
              }
              span.msg{
                float: right;
                width: 130px;
                height: 44px;
                line-height: 44px;
                font-size: 14px;
                color: #5a5a5a;
                background: #f4f4f4;
                border: 1px solid #dcdcdc;
                cursor: pointer;
              }
              span.msg.send{
                background: #d2d2d2;
                color: #fff;
              }
              input[type='checkbox']{
                margin: 0 14px 0 55px;
                float: left;
                width: 16px;
                height: 16px;
              }
              span.agree{
                float: left;
                margin: 0 0 0 10px;
                font-size: 14px;
                color: #8b8b8b;
                a{
                  color: #0076ad;
                }
              }
              .form-group.agree{
                margin: 20px auto 0 !important;
              }
              .btn {
                margin: 34px 0 16px 0;
                width: 360px;
                height: 44px;
                line-height: 44px;
                font-size: 16px;
                color: #fff;
                background: #0076AD;
                border-radius: 3px;
              }
              }
              }
            .login{
              margin-top: 20px;
              font-size: 14px;
              color: #8c8c8c;
              a{
                font-size: 14px;
                color: #0076ad;
              }
            }
    }
  }
</style>
