<template>
  <div class="login">
    <div class="left">
      <div class="bgimg">
        <div class="p1">欢迎使用</div>
        <div class="p2">大数据经营管理支撑服务平台</div>
      </div>
    </div>
    <!--快捷登录-->
    <!--<el-form v-if="!islog && !islang && isDXlogin" ref="dxform" :model="dxform" :rules="dxRules" class="login-form">-->
      <!--<div class="logintitle">-->
        <!--<div @click="isDXlogin = false">密码登录</div>-->
        <!--<div class="on" @click="isDXlogin = true">快捷登录</div>-->
      <!--</div>-->
      <!--<el-form-item prop="phonenumber">-->
        <!--<el-input-->
          <!--v-model="dxform.phonenumber"-->
          <!--type="text"-->
          <!--auto-complete="off"-->
          <!--placeholder="请输入手机号码"-->
        <!--&gt;-->
          <!--<img class="img" slot="prefix" src="../assets/images/phone.png"/>-->
        <!--</el-input>-->
      <!--</el-form-item>-->
      <!--<el-form-item prop="captchaCode" v-if="captchaEnabled">-->
        <!--<el-input-->
          <!--v-model="dxform.captchaCode"-->
          <!--auto-complete="off"-->
          <!--placeholder="请输入图形验证码"-->
          <!--style="width: 290px;float: left;"-->
        <!--&gt;-->
          <!--<img class="img" slot="prefix" src="../assets/images/txyzm.png"/>-->
        <!--</el-input>-->
        <!--<div class="login-code">-->
          <!--<img :src="codeUrl" @click="getCode" class="login-code-img"/>-->
        <!--</div>-->
      <!--</el-form-item>-->
      <!--<el-form-item prop="smsCode">-->
        <!--<el-input-->
          <!--v-model="dxform.smsCode"-->
          <!--auto-complete="off"-->
          <!--placeholder="请输入验证码"-->
          <!--style="width: 290px;float: left;"-->
          <!--@keyup.enter.native="dxLogin"-->
        <!--&gt;-->
          <!--<img class="img" slot="prefix" src="../assets/images/validCode.png"/>-->
        <!--</el-input>-->
        <!--<div class="login-code">-->
          <!--<div class="hqyzm" @click="getsms" :class="{'disableds':smstitle!='获取验证码'&&smstitle!='重新获取'}">{{smstitle}}-->
          <!--</div>-->
        <!--</div>-->
      <!--</el-form-item>-->
      <!--<el-form-item style="width:100%;margin-top: 42px">-->
        <!--<el-button-->
          <!--size="medium"-->
          <!--type="primary"-->
          <!--style="width:100%;height: 48px;border-radius: 4px;font-size: 16px;"-->
          <!--@click.native.prevent="dxLogin"-->
        <!--&gt;-->
          <!--<span v-if="!loading">确认</span>-->
          <!--<span v-else>登 录 中...</span>-->
        <!--</el-button>-->
      <!--</el-form-item>-->
    <!--</el-form>-->
    <!--密码登录-->
    <div class="rightlogtin">
      <el-form v-if="!islog && !islang && !isDXlogin" ref="loginForm" :model="loginForm" :rules="loginRules"
               class="login-form ">
        <!--<div class="logintitle">-->
          <!--<div class="on" @click="isDXlogin = false">密码登录</div>-->
          <!--<div @click="isDXlogin = true">快捷登录</div>-->
        <!--</div>-->
        <div class="loginname"><img src="../assets/images/login/ztlogo.png">中建一局集团第二建筑有限公司</div>
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            type="text"
            auto-complete="off"
            placeholder="请输入登录账号"
          >
            <img class="img" slot="prefix" src="../assets/images/user.png"/>
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            auto-complete="off"
            placeholder="请输入账号密码"
            @keyup.enter.native="handleLogin"
          >
            <img class="img" slot="prefix" src="../assets/images/password.png"/>
          </el-input>
        </el-form-item>
        <el-form-item prop="code" v-if="captchaEnabled">
          <el-input
            v-model="loginForm.code"
            auto-complete="off"
            placeholder="请输入图形验证码"
            style="width: 290px;float: left;"
            @keyup.enter.native="handleLogin"
          >
            <img class="img" slot="prefix" src="../assets/images/txyzm.png"/>
          </el-input>
          <div class="login-code">
            <img :src="codeUrl" @click="getCode" class="login-code-img"/>
          </div>
        </el-form-item>
        <el-form-item style="width:100%;margin-top: 42px;">
          <el-button
            :loading="loading"
            size="medium"
            type="primary"
            style="width:100%;height: 48px;border-radius: 4px;font-size: 16px;"
            @click.native.prevent="handleLogin"
          >
            <span v-if="!loading">登录</span>
            <span v-else>登 录 中...</span>
          </el-button>
        </el-form-item>
        <div class="changepwd" @click="$message.warning('请联系企业管理员修改密码')">忘记密码</div>
      </el-form>
      <!--安全验证-->
      <el-form v-if="!islog && islang" ref="dxform" :model="dxform" :rules="dxRules" class="login-form">
        <h3 class="title"><img src="../assets/images/safe.png"/>请完成安全验证</h3>
        <el-form-item prop="phonenumber">
          <el-input
            v-model="dxform.phonenumber"
            type="text"
            auto-complete="off"
            placeholder="请输入手机号码"
          >
            <img class="img" slot="prefix" src="../assets/images/phone.png"/>
          </el-input>
        </el-form-item>
        <el-form-item prop="captchaCode" v-if="captchaEnabled && showcode">
          <el-input
            v-model="dxform.captchaCode"
            @focus="getCode"
            auto-complete="off"
            placeholder="请输入验证码"
            style="width: 290px;float: left;"
          >
            <img class="img" slot="prefix" src="../assets/images/txyzm.png"/>
          </el-input>
          <div class="login-code">
            <img :src="codeUrl" @click="getCode" class="login-code-img"/>
          </div>
        </el-form-item>
        <el-form-item prop="smsCode">
          <el-input
            v-model="dxform.smsCode"
            auto-complete="off"
            placeholder="请输入验证码"
            style="width: 290px;float: left;"
            @keyup.enter.native="dxLogin"
          >
            <img class="img" slot="prefix" src="../assets/images/validCode.png"/>
          </el-input>
          <div class="login-code">
            <div class="hqyzm" @click="getsms" :class="{'disableds':smstitle!='获取验证码'&&smstitle!='重新获取'}">{{smstitle}}
            </div>
          </div>
        </el-form-item>
        <el-form-item style="width:100%;margin-top: 42px">
          <el-button
            size="medium"
            type="primary"
            style="width:100%;height: 48px;border-radius: 4px;font-size: 16px;"
            @click.native.prevent="dxLogin"
          >
            <span>确认</span>
          </el-button>
        </el-form-item>
      </el-form>
      <!--选择租户企业-->
      <div v-if="islog" class="login-form choose-form">
        <h3 class="title i"><img src="../assets/images/login/title_choose.png"/>请选择你想要登录的企业</h3>
        <div class="enterpriselist">
          <div class="entername" v-for="(item,index) of tenanlist">
            <el-radio v-model="tenantId" :label="item.tenantId">{{item.companyName}}</el-radio>
          </div>
        </div>
        <el-button class="goin" type="primary" :disabled="tenantId == -1" @click="tochose">立即进入</el-button>
      </div>
    </div>
  </div>
</template>

<script>
  import store from '@/store'
  import {
    changeTenants,
    defaultTenantid,
    getCodeImg,
    getTenants,
    logincaptchaSms,
    smsLogin,
    userTenantList
  } from '@/api/login'
  import { decrypt, encrypt } from '@/utils/jsencrypt'
  import { Base64 } from 'js-base64'
  import { getTenantid, setTenantid, setToken } from '@/utils/auth'

  export default {
  name: "Login",
  data() {
    return {
      tenantId:-1,
      islog:false,
      isDXlogin:false,
      codeUrl: "",
      loginForm: {
        username: "",
        password: "",
        code: "",
        uuid: ""
      },
      loginRules: {
        username: [
          { required: true, trigger: "blur", message: "请输入您的账号" }
        ],
        password: [
          { required: true, trigger: "blur", message: "请输入您的密码" }
        ],
        code: [{ required: true, trigger: "change", message: "请输入验证码" }]
      },
      loading: false,
      // 验证码开关
      captchaEnabled: true,
      // 注册开关
      register: false,
      redirect: undefined,
      tenanlist:[],
      defalutId:'',//默认租户id
      //短信登录（安全验证）
      dxform:{
        phonenumber:'',
        uid:'',
        smsCode:'',
        captchaCode:'',
        templateId:'SMS_170345261',
      },
      dxRules: {
        phonenumber: [{ required: true, trigger: "blur", message: "请输入您的账号" }],
        captchaCode: [{ required: true, trigger: "blur", message: "请输入图形验证码" }],
        smsCode: [{ required: true, trigger: "blur", message: "请输入验证码" }]
      },
      //安全验证
      islang:false,
      getuid:false,
      timers:null,
      smstime:60,
      smstitle:'获取验证码',
      showcode:false,//是否展示验证码
      nosend:false,

    };
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true
    },
    isDXlogin(){
      this.smstitle = '获取验证码'
      this.resetForm('dxform')
      this.resetForm('loginForm')
      this.getCode()
    }

  },
  created() {
    this.getCode();
  },
  methods: {
    getCode() {
      getCodeImg().then(res => {
        this.captchaEnabled = res.data.captchaEnabled === undefined ? true : res.data.captchaEnabled;
        if (this.captchaEnabled) {
          this.codeUrl = "data:image/gif;base64," + res.data.img;
          this.loginForm.uuid = res.data.uuid;
          this.dxform.uid = res.data.uuid;
        }
      });
    },
    getsms(){
      if(this.nosend == true){
        return false
      }
      logincaptchaSms(this.dxform).then(res=>{
        if(res.code == 200){

          this.$message.success('已发送短信！')
          let _this = this
          _this.nosend = true
          this.timers = setInterval(()=>{
            _this.smstitle =  _this.smstime +'秒后重试'
            _this.smstime--;
            if(_this.smstime < 0){
              _this.smstitle = '重新获取'
              clearInterval(_this.timers)
              _this.nosend = false
              _this.smstime = 60
            }
          },1000)
        }else{
          this.$message.error(res.msg)
          this.getCode()
          this.showcode = true
        }
      }).catch(res=>{
        this.$message.error(res.msg)
      })
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true;
          let froms = JSON.parse(JSON.stringify(this.loginForm))
          froms.password = Base64.encode(this.loginForm.password)
          //密码登录时根据手机号获取是否有多个租户
          userTenantList({phone:this.loginForm.username}).then(res=>{
            if(res.data.length>1){ //需要安全验证（短信登录）
              let _this = this
              setTimeout(()=> {
                _this.islang = true
                _this.dxform.phonenumber = _this.loginForm.username
                _this.dxform.captchaCode = _this.loginForm.code
                _this.showcode = false
              },1000)
            }else{//无租户列表和一个租户直接登录
              this.$store.dispatch("Login", froms).then(() => {
                this.loading = false;
                this.$router.push({ path: this.redirect || "/" }).catch(()=>{});
              }).catch(() => {
                this.loading = false;
                if (this.captchaEnabled) {
                  this.getCode();
                }
              });
            }
          })
        }
      });
    },
    //短信登录
    dxLogin(){
      this.$refs.dxform.validate(valid => {
        if (valid) {
          let froms = {}
          froms.phonenumber = this.dxform.phonenumber
          froms.smsCode = this.dxform.smsCode
          smsLogin(JSON.stringify(froms)).then(res => {
            if(res.code == 200) {
              setToken(res.data.token)
              store.commit('SET_TOKEN', res.data.token)
              setTenantid(res.data.tenantId)
              defaultTenantid().then(res => {
                if (res.data.tenantId == "" || res.data.tenantId == null) {//无默认企业id
                  this.goin()
                } else {
                  this.$router.push({ path: this.redirect || "/" }).catch(() => {
                  });
                }
              })
            }else{
              this.$message.error(res.msg)
              this.getCode()
              this.showcode = true
            }
          })
        }
      })
    },
    goin(){
      this.tenantId = -1
      getTenants().then(res=>{
        if(res.code == 200 && res.data != []){
          this.tenanlist = res.data
          if(this.tenanlist.length>1){
            this.islog = true
          }else{ //只有一条就是登录返回的id  直接进入系统
            this.$router.push({ path: this.redirect || "/" }).catch(()=>{});
          }
        }
      })
    },
    tochose(){
      this.changeTenants(this.tenantId)
    },
    changeTenants(tenantId) {
      changeTenants(tenantId).then(res => {
        if (res.code == 200) {
          setToken(res.data.token)
          setTenantid(tenantId)
          store.commit('SET_TOKEN', res.data.token)
          this.$router.push({ path: this.redirect || '/' }).catch(() => {})
        }
      })
    }
  }
};
</script>

<style rel="stylesheet/scss" lang="scss">
.login {
  align-items: center;
  height: 100%;
  background-color: #fff;
  background-size: cover;
  overflow-y: hidden;
  width: 100%;
  min-width: 1200px;
  .left{
    float: left;
    width: 60%;
    /*margin-right: 170px;*/
    height: 100%;
  }
  .bgimg{
    width: 100%;
    height: 100%;
    background: url("../assets/images/login/img.png") no-repeat center;
    background-size: cover;
    padding-top: 10%;
    padding-left: 14%;
    .p1{
      height: 36px;
      font-size: 32px;
      font-weight: 400;
      color: #0081FF;
      line-height: 36px;
      margin-bottom: 24px;
    }
    .p2{
      height: 48px;
      font-size: 40px;
      font-weight: 700;
      color: #FFFFFF;
      line-height: 48px;
      letter-spacing: 3px;
    }
  }
}
.title {
  padding: 40px 0 25px 48px;
  border-bottom: 1px solid #F0F0F0;
  font-size: 18px;
  line-height: 26px;
  color: #232323;
  margin: 0 0 32px 0;
  img{
    width: 24px;
    height: 24px;
    margin-right: 12px;
    float: left;
  }
  &.i{
    padding: 40px 0 40px 20px;
    font-size: 20px;
    font-weight: 700;
    margin-bottom: 20px;
    line-height: 28px;
    img{
      float: left;
      width: 50px;
      height: 51px;
      margin-right: 16px;
      margin-top: -10px;
    }
  }
}
.rightlogtin{
  width: 40%;
  float: right;
  height: 100%;
  position: relative;
  min-width: 420px;
  .loginname{
    line-height: 30px;
    font-size: 22px;
    font-weight: 900;
    color: #232323;
    height: 30px;
    margin-bottom: 50px;
    padding-left: 10px;
    img{
      width: 22px;
      margin-right: 8px;
      margin-top: 4px;
      float: left;
    }
  }
}
.login-form {
  border-radius: 10px;
  background: #ffffff;
  width: 420px;
  margin: auto;
  position: absolute;
  margin-top: -200px;
  top: 50%;
  left: 50%;
  margin-left: -210px;
  /*padding: 25px 25px 5px 25px;*/
  .el-form-item{
    /*padding: 0 48px;*/
    margin-bottom:24px;
  }
  .el-input {
    width: 420px;
    height: 48px;
    background: #F2F4F9;
    border-radius: 4px;
    input {
      height: 48px;
      background: #F2F4F9;
      font-size: 14px;
    }
    .el-input__inner{
      padding-left: 61px;
      border: 0;
    }
  }
  .el-input__prefix{
    border-right: 1px solid #D8D8D8;
    height: 24px;
    margin: 12px 0;
    left: 0;
    width: 48px;
  }
  .img {
    height: 24px;
    width: 24px;
    margin: 0 12px;
  }
  .logintitle{
    border-bottom: 1px solid #F0F0F0;
    margin-bottom: 48px;
    padding: 50px 48px 14px;
    box-sizing: border-box;
    >div{
      display: inline-block;
      font-size: 18px;
      line-height: 26px;
      margin-right: 40px;
      color: #81878F;
      position: relative;
      cursor: pointer;
      &.on{
        color: #232323;
        &:after{
          content: ' ';
          width: 29px;
          height: 3px;
          background: #0081FF;
          position: absolute;
          margin: 37px -47px;
        }
      }
    }
  }
}
.changepwd{
  height: 12px;
  font-size: 14px;
  font-weight: 400;
  color: rgba(35,35,35,0.4);
  line-height: 12px;
  margin-top: -4px;
  text-align: center;
  cursor: pointer;
}
.login-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}
.login-code {
  width: 116px;
  height: 38px;
  float: left;
  margin-left: 14px;
  img {
    cursor: pointer;
    vertical-align: middle;
  }
  .hqyzm{
    width: 116px;
    height: 48px;
    border-radius: 4px;
    border: 1px solid #EFEFEF;
    text-align: center;
    color: rgba(35, 35, 35, 0.80);
    font-size: 14px;
    cursor: pointer;
    line-height: 48px;
  }
  .disableds{
    color: rgba(35, 35, 35, 0.40);
  }
}
.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 {
  width: 116px;
  height: 48px;
}
.enterpriselist{
  height: 296px;
  padding: 0 20px;
  border-bottom: 1px solid #EFEFEF;
  overflow: auto;
  .entername{
    height: 72px;
    border-radius: 8px;
    font-size: 16px;
    font-family: Microsoft YaHei-Regular, Microsoft YaHei;
    font-weight: 400;
    color: #3D3D3D;
    line-height: 24px;
    padding: 24px;
    &:hover{
      background: rgba(0,0,0,0.04);
    }
    .el-radio{
      width: 100%;
      .el-radio__input{
        float: right;
        .el-radio__inner{
          width: 18px;
          height: 18px;
          &::after{
            width: 10px;
            height: 10px;
          }
        }
      }
      .el-radio__label{
        display: block;
        width: 302px;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
        font-size: 16px;
      }
    }
  }
}
.goin{
  margin: 24px 40px;
  width: 340px;
  height: 48px;
  background: #0081FF;
  border-radius: 4px;
  font-size: 16px;
  font-weight: 700;
  font-family: Microsoft YaHei-Bold;
}
.enterpriselist::-webkit-scrollbar-track-piece {
  //滚动条凹槽的颜色，还可以设置边框属性
  background-color: #fff;
  height: 16px;
  padding: 0 4px;
}
//滚动条的宽度
.enterpriselist::-webkit-scrollbar {
  width: 8px;
  height: 16px;
  background-color: #fff;
  border-radius: 6px;
}
//滚动条的滑块
.enterpriselist::-webkit-scrollbar-thumb {
  border-radius: 8px;
  height: 8px;
  margin: 0 4px;
  background: rgba(0, 0, 0, 0.08);
  border: 4px solid #F3F4F5;
  &:hover{
    background: #566380;
  }
}
</style>
