<template>
  <div class="login">
    <div class="login-back">
      <div class="login-img">
        <img :src="$config.logo" />
        <div class="txt">{{ $conf.sw_system }}</div>
      </div>
      <div class="login-box">
        <div class="title">{{ $conf.sw_system }}</div>
        <div class="titleBox">
          <div class="titleTxt">专业</div>
          <div class="titleWier"></div>
          <div class="titleTxt">可靠</div>
          <div class="titleWier"></div>
          <div class="titleTxt">安全</div>
          <div class="titleWier"></div>
          <div class="titleTxt">全域</div>
        </div>
      </div>
    </div>
    <el-form ref="form" :model="form" :rules="rules" class="register-form">
      <!-- 注册页 -->
      <div class="register-form-div">
        <div class="titleFlex">
          <div class="text">欢迎注册</div>
          <div class="txt">{{ $conf.sw_system }}</div>
        </div>
        <div class="titleHint">请补充以下信息并提交注册申请</div>
        <div class="alr">
          已有账号？<i class="alr-i" @click="goToLogin">立即登录</i>
        </div>
        <div class="wier">
          <div class="wier-i"></div>
          <div class="wier-n"></div>
        </div>

        <div class="form">
          <el-form-item prop="companyName">
            <el-input
              v-model="form.companyName"
              placeholder="企业名称"
            ></el-input>
          </el-form-item>
          <el-form-item prop="linkMan">
            <el-input
              v-model="form.linkMan"
              placeholder="企业联系人"
            ></el-input>
          </el-form-item>
          <el-form-item prop="phonenumber">
            <el-input
              v-model="form.phonenumber"
              placeholder="联系电话"
            ></el-input>
          </el-form-item>
          <el-form-item prop="unifiedCode">
            <el-input
              v-model="form.unifiedCode"
              placeholder="统一社会信用码"
            ></el-input>
          </el-form-item>
          <el-form-item prop="address">
            <el-input v-model="form.address" placeholder="地址"></el-input>
          </el-form-item>
          <el-form-item prop="applyReason">
            <el-input
              v-model="form.applyReason"
              placeholder="申请原因"
            ></el-input>
          </el-form-item>
          <el-form-item prop="licenseUrl">
            <el-row>
              <el-col :span="12">
                <image-upload
                  v-model="form.licenseUrl"
                  :isShowTip="false"
                  :limit="1"
                />
              </el-col>
              <el-col :span="12">
                <div class="upload-item-tip">
                  <div>营业执照扫描件</div>
                  <div>支持png/jpeg/jpg格式</div>
                  <div>大小不超过5M</div>
                </div>
              </el-col>
            </el-row>
          </el-form-item>

          <el-form-item prop="userName">
            <el-input v-model="form.userName" placeholder="用户名"></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              v-model="form.password"
              placeholder="密码"
              type="password"
            ></el-input>
          </el-form-item>
          <el-form-item prop="passwordConfirm">
            <el-input
              v-model="form.passwordConfirm"
              placeholder="确认密码"
              type="password"
            ></el-input>
          </el-form-item>
          <el-form-item prop="code" v-if="captchaEnabled">
            <el-input
              v-model="form.code"
              auto-complete="off"
              placeholder="验证码"
              style="width: 63%"
            >
            </el-input>
            <div class="register-code">
              <img :src="codeUrl" @click="getCode" class="register-code-img" />
            </div>
          </el-form-item>
        </div>
        <el-button class="btn" type="primary" @click="handleRegister" :disabled="loading">
          <span v-if="!loading">提交注册申请</span>
          <span v-else>注 册 中...</span>
        </el-button>
        <div class="bto">{{ $config.footer }}</div>
      </div>
    </el-form>
  </div>
</template>

<script>
import { getCodeImg, register } from "@/api/login";
import { isMobile } from "@/utils/validate";
export default {
  name: "Login",
  data() {
    // 校验手机
    const validateMobile = (rule, value, callback) => {
      if (value) {
        if (!isMobile(value)) {
          callback("请输入正确手机");
        }
        callback();
      }
      callback();
    };

    // 校验密码
    const equalToPassword = (rule, value, callback) => {
      if (this.form.password !== value) {
        callback(new Error("两次输入的密码不一致"));
      } else {
        callback();
      }
    };
    return {
      codeUrl: null,
      form: {},
      rules: {
        companyName: [
          { required: true, message: "请输入企业名称", trigger: "change" },
        ],
        linkMan: [
          { required: true, message: "请输入联系人", trigger: "change" },
        ],
        phonenumber: [
          { required: true, message: "请输入联系电话", trigger: "change" },
          { validator: validateMobile, trigger: "change" },
        ],
        licenseUrl: [
          { required: true, message: "请上传营业执照", trigger: "change" },
        ],
        unifiedCode: [
          {
            required: true,
            message: "请输入统一社会信用码",
            trigger: "change",
          },
        ],
        address: [{ required: true, message: "请输入地址", trigger: "change" }],
        applyReason: [
          { required: true, message: "请输入申请原因", trigger: "change" },
        ],
        userName: [
          { required: true, message: "请输入用户名", trigger: "change" },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "change" },
        ],
        passwordConfirm: [
          { required: true, message: "请再次输入密码", trigger: "change" },
          { required: true, validator: equalToPassword, trigger: "change" },
        ],
        code: [{ required: true, message: "请输入验证码", trigger: "change" }],
      },
      loading: false,
      captchaEnabled: true
    };
  },
  created() {
    this.getCode();
  },
  methods: {
    goToLogin() {
      this.$router.push({
        path: "/login",
      });
    },
    getCode() {
      getCodeImg().then((res) => {
        this.captchaEnabled =
          res.captchaEnabled === undefined ? true : res.captchaEnabled;
        if (this.captchaEnabled) {
          this.codeUrl = "data:image/gif;base64," + res.img;
          this.form.uuid = res.uuid;
        }
      });
    },
    handleRegister() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.loading = true;
          register(this.form)
            .then((response) => {
              console.log(response);
              this.$modal.msgSuccess("恭喜您，注册成功，请耐心等待审核");

              // 返回登录
              const self = this;
              setTimeout(function () {
                self.goToLogin();
              }, 2000);
            })
            .catch(() => {
              this.loading = false;
              if (this.captchaEnabled) {
                this.getCode();
              }
            });
        }
      });
    },
  },
};
</script>

<style rel="stylesheet/scss" lang="scss">
.login {
  display: flex;
  align-items: center;
  height: 100%;

  .login-back {
    width: 62.1%;
    padding: 70px 0 0 100px;
    box-sizing: border-box;
    height: 100%;
    background: url(../assets/images/i.png) center no-repeat;
    background-size: cover;

    .login-img {
      width: 400px;
      height: 50px;
    }

    .login-box {
      margin-top: 270px;
      margin-left: 77px;

      .title {
        font-size: 54px;
        font-family: PingFangSC-Semibold, PingFang SC;
        font-weight: 600;
        color: #ffffff;
      }

      .titleBox {
        display: flex;
        align-items: center;
        margin-top: 25px;

        .titleTxt {
          font-size: 30px;
          font-family: AlibabaPuHuiTi-Regular, AlibabaPuHuiTi;
          font-weight: 400;
          color: #ffffff;
          letter-spacing: 6px;
          opacity: 0.5;
        }

        .titleWier {
          width: 8px;
          height: 8px;
          background: #ffffff;
          border-radius: 100%;
          opacity: 0.5;
          margin: 0 30px;
        }
      }
    }
  }
}

.titleFlex {
  display: flex;
  align-items: center;
  margin-bottom: 10px;

  .text {
    font-size: 22px;
    font-family: PingFangSC-Semibold, PingFang SC;
    font-weight: 600;
    color: #191a24;
  }

  .txt {
    font-size: 22px;
    font-family: PingFangSC-Semibold, PingFang SC;
    font-weight: 600;
    color: #2866ff;
  }
}

.titleHint {
  font-size: 14px;
  font-family: PingFangSC-Regular, PingFang SC;
  font-weight: 400;
  color: #a7abb3;
  margin-bottom: 20px;
}

.alr {
  font-size: 14px;
  font-family: PingFangSC-Regular, PingFang SC;
  font-weight: 400;
  color: #848b99;
  margin-bottom: 20px;
  cursor: pointer;
}

.alr-i {
  color: #2866ff;
}

.wier {
  display: flex;
  align-items: flex-end;
  margin-bottom: 30px;

  .wier-i {
    width: 80px;
    height: 3px;
    background: #2866ff;
  }

  .wier-n {
    width: 340px;
    height: 1px;
    background: #e2e6f0;
  }
}

// .ipt {
//   margin-bottom: 20px;
// }

.upload {
  margin-bottom: 20px;
}

.btn {
  width: 400px;
  margin-top: 40px;
}

.bto {
  display: flex;
  justify-content: center;
  font-size: 12px;
  font-family: PingFangSC-Regular, PingFang SC;
  font-weight: 400;
  color: #848b99;
  padding: 40px 0 20px 0;
}

.btoOn {
  display: flex;
  justify-content: center;
  font-size: 12px;
  font-family: PingFangSC-Regular, PingFang SC;
  font-weight: 400;
  color: #848b99;
  padding: 40px 0 20px 0;
}

.register-form {
  position: absolute;
  right: 0;
  top: 0;
  background: #ffffff;
  width: 37.9%;
  height: 100%;
  box-sizing: border-box;
  display: flex;
  justify-content: center;
  padding-top: 100px;

  .register-form-div {
    height: 100%;
    width: 400px;
    // overflow: auto;
    // padding: 0 30px;
    overflow: hidden;
    display: flex;
    flex-direction: column;

    .form {
      width: 100%;
      flex-grow: 1;
      overflow-y: auto;
      padding-right: 60px;
    }

    .el-input {
      height: 38px;

      input {
        height: 38px;
      }
    }

    .input-icon {
      height: 39px;
      width: 14px;
      margin-left: 2px;
    }
  }
}

.login-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}

.login-code {
  width: 33%;
  height: 38px;
  float: right;

  img {
    cursor: pointer;
    vertical-align: middle;
  }
}

.el-login-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}

.login-code-img {
  height: 38px;
}

.upload-item-tip {
  display: flex;
  justify-content: center;
  flex-direction: column;
  width: 100%;
  height: 148px;
  color: #a7abb3;
}

.register-code {
  width: 109px;
  height: 38px;
  float: right;
  text-align: right;
  img {
    height: 38px;
    cursor: pointer;
    vertical-align: middle;
  }
}
</style>
