
<template>
  <div class="page">
    <!-- <span class="title">{{ projectName }}</span> -->
    <div class="box_9">
      <span class="text_1">用户登录</span>
      <div style="width: 100%;height: 40px;"></div>
      <el-form ref="loginForm" :model="loginForm" :rules="loginRules" auto-complete="on" label-position="left">
        <el-form-item prop="username">
          <el-input tabindex="1" ref="username" v-model="loginForm.username" placeholder="请输入用户名" name="username"
            type="text" auto-complete="on">
            <template #prefix>
              <img style="position: relative;top: 5px;" src="../../assets/编组 4.png">
            </template>
          </el-input>
        </el-form-item>
        <div style="width: 100%;height: 20px;"></div>
        <el-form-item prop="password">
          <el-input tabindex="2" :key="passwordType" ref="password" v-model="loginForm.password" :type="passwordType"
            placeholder="请输入密码" name="password" auto-complete="on" @keyup.enter.native="handleLogin">
            <template #prefix>
              <img style="position: relative;top: 5px;" src="../../assets/icon-lock.png">
            </template></el-input>
        </el-form-item>
        <div style="width: 100%;height: 20px;"></div>
        <el-form-item v-if="isShowCode" prop="code">
          <el-input v-model="loginForm.code" class="checkCode" placeholder="请输入校验码">
            <template #prefix>
              <img style="position: relative;top: 5px;" src="../../assets/编组 5.png">
            </template></el-input>

        </el-form-item>
        <div class="codeImg">
          <img :src="codeUrl" @click="getCode" width="100px">
        </div>
      </el-form>
      <!-- <div style="position: relative;">
        <div class="text-wrapper_1" @click="handleLogin">
          <span>登&nbsp;录</span>
        </div>
      </div> -->
      <el-button class="text-wrapper_1" :loading="loading" @click.native.prevent="handleLogin">
        <span>登&nbsp;录</span>
      </el-button>
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      constants: {},
    };
  },
  methods: {},
};
</script>


<script>
import { validUsername } from '@/utils/validate'
import { login, getCodeImg, getInfo, userRouters,projectName } from '@/api/user'
import { nextTick } from 'process'
import debounce from '@/utils/debounce'
export default {
  name: 'Login',
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!validUsername(value)) {
        callback(new Error('Please enter the correct user name'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('The password can not be less than 6 digits'))
      } else {
        callback()
      }
    }
    return {
      isShowCode: false,
      codeUrl: '',
      loginForm: {
        username: "",
        password: "",
        code: '',
        uuid: ''
      },
      loginRules: {
        username: [{ required: true, trigger: 'blur', message: '请输入用户名！' }],
        password: [{ required: true, trigger: 'blur', message: '请输入密码！' }],
        code: [{ required: true, message: '请输入验证码！', trigger: 'blur' },]
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined,
      projectName:'',
    }
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  created() {
    this.getCodeImg()  
    projectName().then(res => {        
      this.projectName = res      
    })   
  },
  methods: {
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    getCode() {
      this.getCodeImg()
    },
    getCodeImg() {
      getCodeImg().then(res => {
        if (res.code == 200) {          
          if (res.captchaEnabled) {
            this.isShowCode = true
              this.codeUrl = "data:image/gif;base64," + res.img;
              this.loginForm.uuid = res.uuid
            } else {
              this.isShowCode = false
            }          
        }
      })
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {      
          this.loading = true   
            this.$store.dispatch('user/login', this.loginForm).then(res => {                                                                       
            this.$router.push({ path:  '/' })
            this.$store.dispatch('app/closeSideBar', { withoutAnimation: false })   
            this.loading = false                   
          }).catch(err => {            
            this.loading = false
          })       
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    // handleLogin:debounce(function(){
    //   this.$refs.loginForm.validate(valid => {
    //     if (valid) {      
    //       this.loading = true   
    //         this.$store.dispatch('user/login', this.loginForm).then(res => {                                                                       
    //         this.$router.push({ path:  '/' })
    //         this.$store.dispatch('app/closeSideBar', { withoutAnimation: false })                      
    //       }).catch(err => {            
    //         this.loading = false
    //       })        
    //     } else {
    //       console.log('error submit!!')
    //       return false
    //     }
    //   })
    // },2000)
  }
}
</script>



<style lang="scss" scoped>
.page {
  position: relative;
  // background-image: url('../../assets/bg.png');
  background: url('../../assets/ceshibeijing.png'); 
  background-repeat: no-repeat;
  background-position: center;
  background-attachment: fixed;
  background-size: cover;
  -webkit-background-size: cover;
  -moz-background-size: cover;
  -o-background-size: cover;   
  background-position:0px -1px;
  min-width: 700px;
  min-height: 970px;
  height: 100vh;
  text-align: center;

  .title {
    position: absolute;
    left: 60px;
    top: 46px;
    font-size: 32px;
    color: #FFFFFF;
    font-family: PingFangSC-Medium, PingFang SC;
    font-weight: 500;
    text-shadow: 0px 2px 4px rgba(0, 0, 0, 0.5);
  }

  .image_22 {
    position: relative;
    top: 50px;
    // display: block;
    // margin: 0 auto;
    // padding-top: 50px;
  }

  .box_9 {
    position: absolute;
    right: 164px;
    top: 245px;
    padding: 72px 56px 40px;
    width: 472px;
    height: 572px;
    text-align: center;
    background: url('../../assets/登录框.png') no-repeat center center;

    .codeImg {
      img {
        display: block;
        float: right;
        position: relative;
        top: -60px;
        right: 3px;
      }
    }

    .text_1 {
      color: rgba(255, 255, 255, 1);
      font-size: 32px;
      font-family: PingFangSC-Medium, PingFang SC;
      font-weight: 500;
    }

    .text-wrapper_1 {
      position: relative;
      top: 110px;
      bottom: 0;
      width: 100%;
      height: 48px;
      border: 0;
      background-color: #254DD3;
      cursor: pointer;
      font-size: 24px;
      font-family: PingFangSC-Medium, PingFang SC;
      font-weight: 500;
      color: #FFFFFF;
    }

    .text-wrapper_1:hover {
      background-color: #0156FD;
    }

    .text-wrapper_1:active {
      background-color: #194DA4;
    }

    ::v-deep .el-icon-loading {
      position: relative;
      top: 0px;
    }
  }
}

::v-deep .el-input__inner {
  font-size: 18px;
  background-color: transparent !important;
  border-radius: 0;
  border-top: 0;
  border-left: 0;
  border-right: 0;
  border-bottom: 1px solid #528CD7 !important;
  color: #fff;
  padding-left: 40px;
  // margin-bottom: 20px;
}

::v-deep .el-input__inner:-webkit-autofill {
  -webkit-text-fill-color: #fff !important;
  font-size: 18px;
  transition: background-color 5000s ease-in-out 0s;
  caret-color: #fff !important;
}

::v-deep .el-form-item__error {
  // position: relative;
  // top: -20px;
  // left: -130px;
}</style>
