<template>
  <div class="login-other">
    <div class="left"></div>
    <div class="right">
      <div class="content">
        <p class="title"><img src="../assets/image/logo.svg" alt="">云鹊医工作站</p>
        <div class="input-info" v-if="loginFlag">
          <div class="title-div">
            <p class="title-input">账号登录</p>
            <img src="../assets/image/underline.svg" alt="">
          </div>
          <el-form ref="loginForm" :model="loginData">
            <el-form-item label="" prop="mobile">
              <el-input v-model="loginData.mobile" placeholder="手机号" maxlength="11" :class="{errColor: loginMobileErr}">
                <i slot="prefix" class="el-input__icon my-icon mobile-icon"></i>
              </el-input>
              <p class="err-text" v-show="loginMobileErr"><img src="../assets/image/err-icon.svg" alt="">{{loginMobileErrText}}</p>
            </el-form-item>
            <el-form-item label="" prop="password">
              <el-input v-model="loginData.password" placeholder="密码" type="password" v-if="!showPSW" :class="{errColor: loginPWDErr}">
                <i slot="prefix" class="el-input__icon my-icon password-icon"></i>
                <i @click="showPWSBtn"  slot="suffix" class="el-input__icon my-icon open-eyes-icon"></i>
              </el-input>
              <el-input v-model="loginData.password" placeholder="密码" v-else :class="{errColor: loginPWDErr}">
                <i slot="prefix" class="el-input__icon my-icon password-icon"></i>
                <i @click="hidePWSBtn" slot="suffix" class="el-input__icon my-icon close-eyes-icon"></i>
              </el-input>
              <p class="err-text" v-show="loginPWDErr"><img src="../assets/image/err-icon.svg" alt="">{{loginPWDErrText}}</p>
            </el-form-item>
          </el-form>
          <div class="button-div">
            <div class="forget-password">
              <p @click="goToResetPassword">忘记密码？</p>
            </div>
            <button @click="login">登 录</button>
          </div>
        </div>

        <div class="input-info" v-else>
          <div class="title-div">
            <p class="title-input">重设密码</p>
            <img src="../assets/image/underline.svg" alt="">
          </div>
          <el-form ref="loginForm" :model="resetPassword" >
            <el-form-item label="">
              <el-input v-model="resetPassword.mobile" placeholder="手机号" maxlength="11" :class="{errColor: resetMobileErr}">
                <i slot="prefix" class="el-input__icon my-icon mobile-icon"></i>
              </el-input>
              <p class="err-text" v-show="resetMobileErr"><img src="../assets/image/err-icon.svg" alt="">{{resetMobileErrText}}</p>
            </el-form-item>
            <el-form-item label="">
              <el-input v-model="resetPassword.authCode" placeholder="短信验证码" :class="{errColor: resetCodeErr}">
                <i slot="prefix" class="el-input__icon my-icon auth-code-icon"></i>
                <i v-if="countDown == 0" @click="getAuthCode" slot="suffix" class="el-input__icon get-auth-code">获取验证码</i>
                <i v-else slot="suffix" class="el-input__icon get-auth-warning">{{countDown}}s后重发</i>
              </el-input>
              <p class="err-text" v-show="resetCodeErr"><img src="../assets/image/err-icon.svg" alt="">{{resetCodeErrText}}</p>
            </el-form-item>
            <el-form-item label="">
              <el-input v-model="resetPassword.password" placeholder="新密码" type="password" v-if="!showPSW" :class="{errColor: resetPWDErr}">
                <i slot="prefix" class="el-input__icon my-icon password-icon"></i>
                <i @click="showPWSBtn"  slot="suffix" class="el-input__icon my-icon open-eyes-icon"></i>
              </el-input>
              <el-input v-model="resetPassword.password" placeholder="新密码" v-else :class="{errColor: resetPWDErr}">
                <i slot="prefix" class="el-input__icon my-icon password-icon"></i>
                <i @click="hidePWSBtn" slot="suffix" class="el-input__icon my-icon close-eyes-icon"></i>
              </el-input>
              <p class="err-text" v-show="resetPWDErr"><img src="../assets/image/err-icon.svg" alt="">{{resetPWDErrText}}</p>
            </el-form-item>
          </el-form>
          <div class="button-div">
            <!--<div class="forget-password">
              <p>忘记密码？</p>
            </div>-->
            <button @click="resetPWD">重设密码</button>
            <div class="login-now">
              <p @click="goToLogin">立即登录</p>
            </div>
          </div>
        </div>

      </div>
      <div class="vrcode">
        <img v-show="showVRCode" src="../assets/image/VR-code.svg" alt="">
        <p slot="reference" @mouseenter="showVR"  @mouseleave="closeVR"><img src="../assets/image/VR-mobile.svg" alt="">下载云鹊医app</p>
      </div>
    </div>
  </div>
</template>

<script>
  import { ssoLogin2, getDeviceInfo } from '@/utils/utils';
  import md5 from 'js-md5';

  export default {
    name: "login",
    data() {
      return {
        loginFlag: true,
        loginData: {},
        resetPassword: {},
        showVRCode: false,
        showPSW: false,
        countDown: 0,
        timer: null,
        loginMobileErr: false,
        loginMobileErrText: '',
        loginPWDErr: false,
        loginPWDErrText: '',
        resetMobileErr: '',
        resetMobileErrText: '',
        resetCodeErr: '',
        resetCodeErrText: '',
        resetPWDErr: '',
        resetPWDErrText: ''
      }
    },
    created() {
      ssoLogin2() //初始化登录信息
    },
    methods: {
      checkPhone(val) {
        if(!(/^1[345678]\d{9}$/.test(val))) {
          return false;
        } else {
          return true;
        }
      },
      checkPwdVal(val) {
        if(!/(?=.*\d)/.test(val)){
          return false
        }
        if(!/(?=.*[A-Za-z])/.test(val)){
          return false
        }
        if(!/[`~!@#$%^&*()=|{}_+':]/.test(val)){
          return false
        }
        return true
      },
      checkPassword(val) {
        if(val.length < 8 || val.length > 16){
          return false;
        } else {
          return true;
        }
      },
      goToLogin() {
        this.loginFlag = true
        this.resetMobileErr = false;
        this.resetCodeErr = false;
        this.resetPWDErr = false
        this.resetPassword = {}
      },
      goToResetPassword() {
        this.loginFlag = false;
        this.loginMobileErr = false;
        this.loginPWDErr = false
        this.loginData = {}
      },
      showVR() {
        this.showVRCode = true;
      },
      closeVR() {
        this.showVRCode = false;
      },
      showPWSBtn() {
        this.showPSW = true
      },
      hidePWSBtn() {
        this.showPSW = false
      },
      getAuthCode() {
        if(!this.resetPassword.mobile) {
          this.resetMobileErr = true;
          this.resetMobileErrText = '请输入手机号';
          return;
        } else if(!this.checkPhone(this.resetPassword.mobile)) {
          this.resetMobileErr = true;
          this.resetMobileErrText = '请输入正确的手机号';
          return;
        } else {
          this.resetMobileErr = false;
          this.resetMobileErrText = '';
        }
        let params = {
          receiver: this.resetPassword.mobile,
          token: localStorage.getItem("token"),
          flag:  2, //1表示注册 2表示忘记密码
          gaoFlag: 1 ,//1表示saas 2表示高血压项目
          deviceInfo: getDeviceInfo()
        };
        this.saasPOST('/mobiles/sendCaptchaNew',params, 'application/x-www-form-urlencoded;charset=UTF-8').then(data => {
          if (data.mobileFlag == 2) {
            this.resetMobileErr = true;
            this.resetMobileErrText = '该手机号尚未注册！';
            return;
          } else {
            const TIME_COUNT = 60;
            if (!this.timer) {
              this.countDown = TIME_COUNT;
              this.timer = setInterval(() => {
                if (this.countDown > 0 && this.countDown <= TIME_COUNT) {
                  this.countDown--;
                } else {
                  this.countDown = 0;
                  clearInterval(this.timer);
                  this.timer = null;
                }
              }, 1000)
            }
          }

        }).catch( err => {
          this.$message({
            message: '系统错误！',
            type: 'error'
          });
        })

      },
      login() {
        if(!this.loginData.mobile) {
          this.loginMobileErr = true;
          this.loginMobileErrText = '请输入手机号';
        } else if(!this.checkPhone(this.loginData.mobile)) {
          this.loginMobileErr = true;
          this.loginMobileErrText = '请输入正确的手机号';
        } else {
          this.loginMobileErr = false;
          this.loginMobileErrText = '';
        }
        if(!this.loginData.password) {
          this.loginPWDErr = true;
          this.loginPWDErrText = '请输入密码';
        } else {
          this.loginPWDErr = false;
          this.loginPWDErrText = '';
        }

        if(!this.loginData.mobile||!this.checkPhone(this.loginData.mobile)||!this.loginData.password) {
          return;
        }
        let params = {
          mobile: this.loginData.mobile,
          OS: localStorage.getItem("OS"),
          browser: localStorage.getItem("browser"),
          terminalType: localStorage.getItem("terminalType"),
          ipAddress: localStorage.getItem("ipAddress"),
          password: md5(this.loginData.password).toUpperCase(),
          token: localStorage.getItem("token"),
        };
        this.saasGET('/registers/saasLogin',params).then(data => {
          if (data.mobileFlag == 1) {
            /*this.$message({
              message: '手机号或密码不正确！',
              type: 'error'
            });*/
            this.loginPWDErr = true;
            this.loginPWDErrText = '手机号或密码不正确！';
            return;
          }
          if (data.isExist == 2) {
            /*this.$message({
              message: '该手机号尚未注册！',
              type: 'error'
            });*/
            this.loginMobileErr = true;
            this.loginMobileErrText = '该手机号尚未注册！';
            return;
          }
          localStorage.setItem("token", data.token);
          localStorage.setItem("storageToken", data.token);
          localStorage.setItem('doctorId',data.picapDoctor.id);

          localStorage.setItem("mobilePhone", params.mobile);
          localStorage.setItem("pass", params.password);
          sessionStorage.setItem("mobile",params.mobile);
          sessionStorage.setItem("pass",params.password);
          this.$router.push('/');
        }).catch( err => {
          this.$message({
            message: '系统错误！',
            type: 'error'
          });
        })
      },
      resetPWD() {
        if(!this.resetPassword.mobile) {
          this.resetMobileErr = true;
          this.resetMobileErrText = '请输入手机号';
        } else if(!this.checkPhone(this.resetPassword.mobile)) {
          this.resetMobileErr = true;
          this.resetMobileErrText = '请输入正确的手机号';
        } else {
          this.resetMobileErr = false;
          this.resetMobileErrText = '';
        }
        if(!this.resetPassword.authCode) {
          this.resetCodeErr = true;
          this.resetCodeErrText = '请输入验证码';
        } else {
          this.resetCodeErr = false;
          this.resetCodeErrText = '';
        }
        if(!this.resetPassword.password) {
          this.resetPWDErr = true;
          this.resetPWDErrText = '请输入密码';
        } else if(!this.checkPassword(this.resetPassword.password)) {
          this.resetPWDErr = true;
          this.resetPWDErrText = '请输入8-16位的密码';
        } else if(!this.checkPwdVal(this.resetPassword.password)) {
          this.resetPWDErr = true;
          this.resetPWDErrText = '密码须同时包含数字，字母和字符';
        } else {
          this.resetPWDErr = false;
          this.resetPWDErrText = '';
        }
        if(!this.resetPassword.mobile
          ||!this.checkPhone(this.resetPassword.mobile)
          ||!this.resetPassword.authCode
          ||!this.resetPassword.password
          ||!this.checkPassword(this.resetPassword.password)
          ||!this.checkPwdVal(this.resetPassword.password)) {
          return;
        }
        let para = {
          mobile: this.resetPassword.mobile,
          authCode: this.resetPassword.authCode
        }
        this.saasGET('/mobiles/checkCaptcha',para).then(data => {
          let req = {
            mobile: this.resetPassword.mobile,
            password: md5(this.resetPassword.authCode).toUpperCase()
          }
          this.saasPUT('/login/password',req).then(data => {
            this.$message({
              message: '重设密码成功，请重新登录！',
              type: 'success'
            });
           setTimeout(function () {
             window.location.reload();
           }, 2000)
          }).catch( err => {
            this.$message({
              message: '系统错误！',
              type: 'error'
            });
          })

        }).catch( err => {
          /*this.$message({
            message: '验证码已过期或不存在！',
            type: 'error'
          });*/
          this.resetCodeErr = true;
          this.resetCodeErrText = '验证码已过期或不存在';
          return;
        })
      }
    }
  }
</script>

<style  lang="scss">

  .login-other {
    .my-icon {
      display: inline-block;
      height: 20px;
      margin-top: 9px;
    }
    .mobile-icon {
      background: url("../assets/image/mobile.svg") no-repeat;
    }
    .password-icon {
      background: url("../assets/image/password.svg") no-repeat;
    }
    .auth-code-icon {
      background: url("../assets/image/auth-code.svg") no-repeat;
    }
    .open-eyes-icon {
      background: url("../assets/image/open-eyes.svg") no-repeat;
      cursor: pointer;
    }
    .close-eyes-icon {
      background: url("../assets/image/close-eyes.svg") no-repeat;
      cursor: pointer;
    }
    .err-text {
      color: #FB5B52;
      height: 20px;
      line-height: 20px;
      margin-top: 4px;
      img {
        width: 14px;
        height: 14px;
        margin-right: 3px;
        vertical-align: middle;
      }
    }
    display: flex;
    height: 100%;
    flex-direction: row;
    .left {
      flex-grow: 1;
      /*大于1441*/
      @media only screen and (min-width: 1441px) {
        background: url("../assets/image/bg-big.png") no-repeat;
        background-size: cover;
      }
      /*小于1440*/
      @media only screen and (max-width: 1441px) {
        background: url("../assets/image/bg-small.png") no-repeat;
        background-size: cover;
      }
    }
    .right {
      position: relative;
      /*大于1441*/
      @media only screen and (min-width: 1441px) {
        width: 570px;
      }
      /*小于1440*/
      @media only screen and (max-width: 1441px) {
        width: 500px;
      }
      .content {
        /*大于1441*/
        @media only screen and (min-width: 1441px) {
          padding: 140px 50px 20px 50px;
        }
        /*小于1440*/
        @media only screen and (max-width: 1441px) {
          padding: 40px 50px 20px 50px;
        }
        .title {
          text-align: center;
          font-weight: 500;
          font-size: 34px;
          color: #333333;
          img {
            width: 54px;
            height: 54px;
            vertical-align: middle;
          }
        }
        .input-info {
          margin-top: 60px;
          .title-div {
            position: relative;
            margin-bottom: 30px;
            .title-input {
              font-size: 28px;
            }
            img {
              display: block;
              position: absolute;
              bottom: 3px;
            }
          }
          .el-form {
            .el-form-item {
              .el-form-item__content {
                position: relative;
                .el-input {
                  .el-input__inner {
                    border-radius: 0;
                    border: none;
                    border-bottom: 1px solid #D9D9D9;
                  }
                  .get-auth-code {
                    color: #666666;
                    cursor: pointer;
                  }
                  .get-auth-warning {
                    color: #666666;
                  }
                }
                .err-text{
                  position: absolute;
                }
                .errColor {
                  .el-input__inner {
                    border-color: #FB5B52;
                  }

                }

              }
            }

          }
          .button-div {
            button {
              width: 100%;
              height: 70px;
              background: #449284;
              color: #ffffff;
              font-size: 24px;
              margin-top: 20px;
              cursor: pointer;
              box-shadow: 0px 10px 8px 0px rgba(59,132,119,0.2);
            }
            .forget-password {
              margin-top: 52px;
              color:red;
              p {
                cursor: pointer;
                width: 80px;
                float: right;
                font-size: 16px;
                color: #449284;
              }
            }
            .login-now {
              margin-top: 24px;
              p {
                width: 80px;
                margin: 0 auto;
                font-size:20px;
                color: #449284;
                cursor: pointer;
              }
            }
          }
        }

      }
      .vrcode {
        position: absolute;
        bottom: 20px;
        width: 100%;
        text-align: center;
        p {
          text-align: center;
          font-size: 20px;
          color: #999999;
          cursor: pointer;
          img {
            vertical-align: middle;
            margin-right: 4px;
          }
        }
      }
    }

  }
</style>
