<template>
  <div class="login" >
    <div style="display: flex;" class="nbse">
      <div style="margin-bottom: 30px;">
        <img style="width: 380px; position: relative;top:0; left:0;" src="../assets/images/pic_gddl_tp.png" alt="">
      </div>
      <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form">
        <h3 class="title" style="margin-top: 30px;"><img src="../assets/images/icon_dl_logo.png" alt="" style="margin-right: 5px;">中新云客户服务工单系统管理平台</h3>
         <h3 class="title">登录</h3>
        <!-- <img src="../assets/images/pic_htdl_logo.png" alt="" style=" width: 100%; margin-bottom: 10px;"> -->
        <!-- <div class="zhengxi_og" > -->
          <!-- <p :class="iskj == 1 ?'isacti' :''" @click="islk(1)">账号登录</p> -->
         <!-- <p :class="iskj == 2 ?'isacti' :''" @click="islk(2)">短信登录</p>
          <p :class="iskj == 3 ?'isacti' :''" @click="islk(3)">重置密码</p> -->
          <!-- <div class="nbgsq"></div> -->
        <!-- </div> -->
        <el-form-item prop="username" v-if='iskj == 1' class="nkgon">
          <el-input
            v-model="loginForm.username"
            type="text"
            auto-complete="off"
            placeholder="账号"
          >
            <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon" />
          </el-input>
        </el-form-item>
        <el-form-item prop="password" v-if='iskj == 1' class="nkgon">
          <el-input
            v-model="loginForm.password"
            type="password"
            auto-complete="off"
            placeholder="密码"
            @keyup.enter.native="handleLogin"
          >
            <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />
          </el-input>
        </el-form-item>
        <!-- v-if='!xgmemberMe' -->
        <el-form-item prop="code"  v-if="captchaEnabled"   >
          <el-input
            v-model="loginForm.code"
            auto-complete="off"
            placeholder="验证码"
            style="width: 63%"
            @keyup.enter.native="handleLogin"
          >
            <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon" />
          </el-input>
          <div class="login-code">
            <img :src="codeUrl" @click="getCode" class="login-code-img"/>
          </div>
        </el-form-item>
        <el-checkbox v-if='iskj == 1'  v-model="loginForm.rememberMe" style="margin:0px 0px 25px 0px;">记住密码</el-checkbox>
        <div style="float: right;" v-if="register">
          <router-link class="link-type" :to="'/register'">立即注册</router-link>
        </div>

        <el-form-item  prop="username" v-if='iskj == 2' class="lisevf">
          <el-input v-model="loginForm.username"  placeholder="联系方式" type="number" >
            <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon" />
          </el-input>
        </el-form-item>
        <div class="nkg" v-if='iskj == 2'>
          <el-form-item  prop="code"  >
            <el-input v-model="loginForm.code"  placeholder="验证码" type="number" >
              <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />
            </el-input>
          </el-form-item>
           <p class="sokspan" @click="fasog" v-show="show">
           <span></span>发送验证码
           </p>
           <p class="sokspan"  v-show="!show">
             <span></span>{{count}}秒重新发送
           </p>
        </div>
        <el-form-item  prop="username" v-if='iskj == 3' class="lisevf">
          <el-input v-model="loginForm.username"  placeholder="联系方式" type="number" >
            <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon" />
          </el-input>
        </el-form-item>
        <div class="nkg" v-if='iskj == 3'>
          <el-form-item  prop="code"  >
            <el-input v-model="loginForm.code"  placeholder="验证码" type="number" >
              <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />
            </el-input>
          </el-form-item>
           <p class="sokspan" @click="fasogs" v-show="shows">
           <span></span>发送验证码
           </p>
           <p class="sokspan"  v-show="!shows">
             <span></span>{{counts}}秒重新发送
           </p>
        </div>
        <el-form-item prop="password" v-if='iskj == 3' class="nkgon" style="margin-top: 25px;">
          <el-input
            v-model="loginForm.password"
            type="password"
            auto-complete="off"
            placeholder="密码"
            show-password
            @keyup.enter.native="handleLogin"
          >
            <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />
          </el-input>
        </el-form-item>
        <!-- v-if='xgmemberMe' -->

        <!-- <el-checkbox v-model="xgmemberMe"  @change=" xgmes " style="margin:0px 0px 25px 25px;">修改密码</el-checkbox> -->
        <el-form-item style="width:100%;margin-bottom: 0;" v-if='iskj == 1' >
          <el-button
            :loading="loading"
            size="medium"
            type="primary"
            style="width: 100%;background: linear-gradient(180deg, #00B278 0%, #00B278 100%);height: 45px; border-color: #00B278 ;color: #fff; border-radius: 6px;"
            @click.native.prevent="handleLogin"
          >
            <span v-if="!loading">登 录</span>
            <span v-else>登 录 中...</span>
          </el-button>

         <!-- <p class="div_p">
            * 使用快捷登录，请先绑定第三方账号
          </p> -->
        </el-form-item>
        <el-form-item  style="width:100%;margin-bottom: 0; margin-top: 20px;" v-if='iskj == 2'>
          <el-button
            :loading="loading"
            size="medium"
            type="primary"
            style="width: 100%;background: linear-gradient(180deg, #00A9F0 0%, #00A9F0 100%);height: 45px; border-color: #00A9F0 ;color: #fff; border-radius: 6px;"
            @click.native.prevent="handleLogin"
          >
            <span v-if="!loading">登 录</span>
            <span v-else>登 录 中...</span>
          </el-button>
          <div style="float: right;" v-if="register">
            <router-link class="link-type" :to="'/register'">立即注册</router-link>
          </div>
          <!-- <p class="div_p">
            * 使用快捷登录，请先绑定第三方账号
          </p> -->
        </el-form-item>
        <el-form-item  style="width:100%;margin-bottom: 0; margin-top: 20px;" v-if='iskj == 3'>
          <el-button
            :loading="loading"
            size="medium"
            type="primary"
            style="width: 100%;background: linear-gradient(180deg, #00A9F0 0%, #00A9F0 100%);height: 45px; border-color: #00A9F0 ;color: #fff; border-radius: 6px;"
            @click.native.prevent="handleLogin"
          >
            <span v-if="!loading">登 录</span>
            <span v-else>登 录 中...</span>
          </el-button>
          <div style="float: right;" v-if="register">
            <router-link class="link-type" :to="'/register'">立即注册</router-link>
          </div>
          <!-- <p class="div_p">
            * 使用快捷登录，请先绑定第三方账号
          </p> -->
        </el-form-item>
      </el-form>
    </div>


    <!--  底部  -->
    <div class="el-login-footer">
      <!-- <span>版本号：ZXY_tj_1.2</span> -->
    </div>
  </div>
</template>

<script>
import { getCodeImg,postphone,fsefsaxiu,postphonef } from "@/api/login";
import Cookies from "js-cookie";
import { encrypt, decrypt } from '@/utils/jsencrypt'
 import {setToken} from '@/utils/auth'
  import resetPwd from "./system/user/profile/resetPwd.vue";
  import { updateUserPwds } from "@/api/system/user";

export default {
  name: "Login",
  data() {
    return {
      codeUrl: "",
      loginForm: {
        username: "",
        password: "",
        rememberMe: false,
        code: "",
        uuid: "",
        type:1
      },
      loginRules: {

      },
      loading: false,
      // 验证码开关
      captchaEnabled: false,
      // 注册开关
      register: true,
      redirect: undefined,
      xgmemberMe:false,  //修改密码
      iskj:1,
      show:true,
      timecont:60,
      count: '',
      shows:true,
      timeconts:60,
      counts: '',
      timer: null,
      timers:null,
    };
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true
    }
  },
  created() {
    this.getCode();
    this.getCookie();
  },
  methods: {
    getCodd(formData){
                if (!this.timer) {
                    this.count = this.timecont;
                    this.show = false;
                    let nhge={username:this.loginForm.username}
                    postphone(nhge).then(res => {
                      this.$modal.msgSuccess('操作成功');
                    });
                    this.timer = setInterval(() => {
                      if (this.count > 0 && this.count <= this.timecont) {
                        this.count--;
                      } else {
                        this.show = true;
                        clearInterval(this.timer);
                        this.timer = null;
                      }
                    }, 1000)
                  }
            },
    getCoddf(formData){
                if (!this.timer) {
                    this.counts = this.timeconts;
                    this.shows = false;
                    let nhge={username:this.loginForm.username}
                    postphonef(nhge.username).then(res => {
                      this.$modal.msgSuccess('操作成功');
                    });
                    this.timers = setInterval(() => {
                      if (this.counts > 0 && this.counts <= this.timeconts) {
                        this.counts--;
                      } else {
                        this.shows = true;
                        clearInterval(this.timers);
                        this.timers = null;
                      }
                    }, 1000)
                  }
            },
    islk(val){
     this.iskj =val
     if(val == 1){
       this.xgmemberMe = false
     }else{
       this.xgmemberMe = true
     }
     this.loginForm={
       username: "",
       password: "",
       rememberMe: false,
       code: "",
       uuid: "",
       type:val
     }
    },
    fasog(){

      this.getCodd()

    },
    fasogs(){

      this.getCoddf()

    },
    getCode() {
      getCodeImg().then(res => {
        this.captchaEnabled = res.captchaEnabled === undefined ? true : res.captchaEnabled;
        if (this.captchaEnabled) {
          this.codeUrl = "data:image/gif;base64," + res.img;
          this.loginForm.uuid = res.uuid;
        }
      });
    },
    getCookie() {
      const username = Cookies.get("username");
      const password = Cookies.get("password");
      const rememberMe = Cookies.get('rememberMe')
      const type = Cookies.get('type')
      this.loginForm = {
        username: username === undefined ? this.loginForm.username : username,
        password: password === undefined ? this.loginForm.password : decrypt(password),
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe),
        type: type == undefined ? this.iskj : this.iskj,
      };
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          if(this.iskj == 1){
            // 账号登录
            if(this.loginForm.username == null || this.loginForm.username==''){
              this.$message.error('请输入账号');
              return
            }
            if(this.loginForm.password == null || this.loginForm.password==''){
              this.$message.error('请输入密码');
              return
            }
          }else if(this.iskj == 2){
            // 验证码
            if(this.loginForm.username == null || this.loginForm.username==''){
              this.$message.error('请输入账号');
              return
            }
            if(this.loginForm.code == null || this.loginForm.code==''){
              this.$message.error('请输入验证码');
              return
            }
          }
          this.loading = true;
          if (this.loginForm.rememberMe) {
            Cookies.set("username", this.loginForm.username, { expires: 30 });
            Cookies.set("password", encrypt(this.loginForm.password), { expires: 30 });
            Cookies.set('rememberMe', this.loginForm.rememberMe, { expires: 30 });
          } else {
            Cookies.remove("username");
            Cookies.remove("password");
            Cookies.remove('rememberMe');
          }
          this.loginForm.type = this.iskj
           this.loginForm.username == null
          console.log(this.loginForm,1)
          if(this.iskj != 3){
            this.$store.dispatch("Login", this.loginForm).then(() => {
              console.log(this.loginForm,1)
              this.$router.push({ path: this.redirect || "/" }).catch(()=>{});
            }).catch(() => {
              this.loading = false;
              if (this.captchaEnabled) {
                this.getCode();
              }
            });
          }else{
            this.loginForm.phone = this.loginForm.username
            this.loginForm.username = undefined
            fsefsaxiu(this.loginForm).then(
              response => {
                console.log(response.msg,36)
                if(response.msg == '操作成功'){
                   this.$modal.msgSuccess(response.msg);
                   this.islk(1);
                }else{
                  this.$message.error(response.msg);
                }
                this.loading = false;

              })
          }

        }
      });
    },
    // 修改密码
    handleLogins(){
     if(this.loginForm.username == ""){
       this.$message.error('请输入账号');
       return
     }
     if(this.loginForm.oldPassword == undefined){
       this.$message.error('请输入旧密码');
       return
     }
     if(this.loginForm.newPassword == undefined){
       this.$message.error('请输入新密码');
       return
     }

     if(this.loginForm.confirmPassword == undefined){
       this.$message.error('请输入确认密码');
       return
     }
     this.loading = true;
     let nhse={
       userName:''
     }
     nhse.userName = this.loginForm.username
     updateUserPwds(this.loginForm.oldPassword, this.loginForm.newPassword,this.loginForm.username).then(
       response => {
         console.log(response.msg,36)
         if(response.msg == '操作成功'){
            this.$modal.msgSuccess(response.msg);
            this.xgmemberMe = false
            this.getCode();
            this.$set(this.loginForm,'password','')
            this.$set(this.loginForm,'uuid','')
            this.$set(this.loginForm,'rememberMe',false)
            this.$set(this.loginForm,'code','')
            this.$set(this.loginForm,'confirmPassword',undefined)
            this.$set(this.loginForm,'oldPassword',undefined)
            this.$set(this.loginForm,'newPassword',undefined)
            // this.$set(this.loginForm,'username','')
         }else{
           this.$message.error(response.msg);
         }

         this.loading = false;

       }
     );
    },
    // 确认密码失去焦点
    cpass(){
     if(this.loginForm.confirmPassword !== this.loginForm.newPassword){
       this.$message.error('两次密码不一致');
     }
    },
    // 复选框选择修改密码
    xgmes(){
      if(this.xgmemberMe == false){
        this.$set(this.loginForm,'confirmPassword',undefined)
        this.$set(this.loginForm,'oldPassword',undefined)
        this.$set(this.loginForm,'newPassword',undefined)
        this.$set(this.loginForm,'username','')
      }else{
        this.$set(this.loginForm,'username','')
        this.$set(this.loginForm,'password','')
        this.$set(this.loginForm,'uuid','')
        this.$set(this.loginForm,'rememberMe',false)
        this.$set(this.loginForm,'code','')
      }
    },
    zhuc(){
     this.xgmemberMe = true
    },
    zhucs(){
      this.xgmemberMe = false
    }
  }
};
</script>
<style lang="scss">
  .login{
    .el-checkbox__input.is-checked + .el-checkbox__label{
      color:#333333;
    }
    .el-checkbox__input.is-checked .el-checkbox__inner{
      background-color:rgba(0,0,0,0);
      color:#00B278 ;
      border-color: #00B278;
    }
    .el-checkbox__inner::after{
     border-color: #00B278;
    }
    .link-type
    {
       color:#00B278 ;
    }
     .link-type:focu{
       color:#00B278 ;
    }
    .svg-icon{
      color:#00B278 ;
    }
  }

</style>

<style rel="stylesheet/scss" lang="scss">
  .nkg{
    border: 1px solid #A7A7A7;
    border-radius: 6px;
    display: flex;
    justify-content: space-between;
     background-color: rgba(255,255,255,0.7) !important;
    .el-input input{
      border: 0 !important;
      background-color: rgba(255,255,255,0.2) !important;
    }
    .sokspan{
      height: 45px;
      line-height: 45px;
      width: 35%;
      text-align: center;
      font-weight: bold;
      font-size: 15px;
      color: #004BF6;
      cursor: pointer;
      margin: 0;
      display: flex;
      align-items: center;
      justify-content: space-between;
      padding-right: 20px;
      span{
        height: 15px;
        width: 1px;
        background-color: #C1C1C1;
      }
    }
    .el-form-item{
      margin-bottom: 0 !important;
    }
  }
  .lisevf{
     .el-input input{
      border: 1px  solid #A7A7A7 !important;
      border-radius: 6px !important;
       background-color: rgba(255,255,255,0.7) !important;
     }

  }
  .nkgon{
    .el-input input{
     border: 1px  solid #A7A7A7 !important;
     border-radius: 6px !important;
      background-color: rgba(255,255,255,0.7) !important;
    }
  }
  .zhengxi_og{
    display: flex;
    position: relative;
    margin-bottom: 20px;
    cursor:pointer;
    p{
      position: relative;
      z-index: 4;
      font-weight: 400;
      font-size: 14px;
      color: #545454;
      margin: 0;
       margin-right: 20px;
      padding-bottom: 5px;
      padding-top: 10px;
    }
    .isacti{
      color: #00A9F0;
      border-bottom: 1px solid #00A9F0;
    }
    .nbgsq{
      width: 100%;
      height: 2px;
      background-color: #E6E6E6;
      position: absolute;
      bottom: 0;
      left: 0;
      z-index: 3;
    }
  }
.login {
  display: flex;
  justify-content: center;
  align-items: center;
  // flex-direction: column;
  height: 100%;
  background-image: url("../assets/images/pic_dl_bg.png");
  background-size: cover;
  .el-input__prefix{
    color: #5974E0;
    top:2px;
  }
  // margin-left: 300px;
}
.nbse{
  background-image: url("../assets/images/pic_gddl_dlkbg.png");
  background-size: cover;
  padding: 30px;
}
.title {
  margin: 0 auto 20px auto;
  text-align: center;
  font-size: 18px;
  font-family: PingFang SC;
  font-weight: 800;
  color: #343434;

}

.login-form {
  border-radius: 6px;
  // background: #000a48;
  // background-color:#fff;
  width: 400px;
  margin-left: -3.2%;
  // margin-right: -30%;
  margin-left: 10px;
  padding: 15px 35px 5px 35px;
  .el-input {
    height: 45px;
    input {
      height: 45px;
      border: 1px solid #0391FD;
      border-radius: 21px;
      background-color: #0391FD;
      // border-color: #F7F6FC;s
      background-color:#fff ;
    }
  }
  .el-checkbox__inner{
    background-color: #2174f3;
    border-color: rgba(1, 152, 221,0.2);
    background-color:rgba(0,0,0,0.3)
  }
  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 2px;
  }
  .el-button--primary{
    margin-bottom: 20px;
    // background-color: #A6A1EB !important;

  }
  .el-form-item{
    margin-bottom: 25px;
  }
}
.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;
}
.nli{
  .el-button--primary{
    background-color: #A6A1EB;
    border-color: #A6A1EB !important;
  }
}
.njuyehaf{
  display: flex;
  p{
    flex: 1;
    font-size: 14px;
    font-family: PingFang SC;
    font-weight: 400;
    color: #C8C8C8;
    margin: 0;
    text-align: right;
    span{
      color: #FFB339;
      font-weight: 700;
      cursor:pointer;
    }
  }
}
.njhyter{
  .el-upload{
    width: 100%;
  }
  .el-button--mini{
    width: 100% !important;
    padding: 11px 15px;
    // text-align: left;
    margin: 0;
  }
  .el-button--primary{
    background-color: #F7F6FC;
    border-color: #F7F6FC;
    color: #A6A1EB;
  }
  .el-upload__tip{
    font-size: 12px;
  }
}
</style>
