<template>
  <div class="login-container">
    <van-row class="m-header" type="flex" justify="space-between">
      <!-- <van-col span="8">
        <div><span class="header-title">xxx管理系统</span></div>
      </van-col>
      <van-col span="8" class='r-header'>
        <svg-icon icon-class="network" />
        <span class="date-time">08:30</span>
        <div class="header-infos">
          <div class="header-infos-item">2023-02-06</div>
          <div class="header-infos-item">
            <span class="item-text">星期一</span>
            <span class="item-text">下午</span>
          </div>
        </div>
      </van-col> -->
    </van-row>
    <!-- <div class="outer-shadows outer-border"> -->
    <!-- <div class="outer-shadows outer-border1"> -->
    <div class="outer-shadows">
      <el-form ref="loginForm" :model="loginForm " :rules="loginRules" class="login-form" auto-complete="on" label-position="left">

        <div class="title-container">
          <!-- <div class="circle"></div> -->
          <h3 class="title">一体机管理系统</h3>
        </div>

        <el-form-item prop="username">

          <el-input ref="username" v-model="loginForm.username" placeholder="请输入账号" name="username" type="text" tabindex="7" auto-complete="on" prefix-icon="el-icon-s-custom" />
        </el-form-item>

        <el-form-item prop="password">

          <el-input :key="passwordType" ref="password" v-model="loginForm.password" :type="passwordType" placeholder="请输入密码" name="password" tabindex="3" prefix-icon="el-icon-lock" auto-complete="on" @keyup.enter.native="handleLogin">
            <!-- <i class="el-icon-edit el-input__icon" slot="suffix" @click="handleIconClick"></i> -->
          </el-input>

        </el-form-item>
        <el-form-item prop="captchaCode" class="captchDiv">
          <el-input ref="captchaCode" v-model="loginForm.captchaCode" placeholder="请输入验证码" name="captchaCode" type="text" tabindex="3" class="validationCode" @keyup.enter.native="handleLogin()" />
          <img :src="captcha" alt="验证码" class="captchImg" @click="getCaptcha()" />
        </el-form-item>

        <el-button :loading="loading" type="primary" style="width:100%;margin-bottom:30px;" @click.native.prevent="handleLogin">登 录</el-button>

        <!-- <div class="tips">
              <span style="margin-right:20px;">username: admin</span>
              <span> password: any</span>
            </div> -->

      </el-form>
    </div>
    <!-- </div> -->
    <!-- </div> -->
    <van-row class="footer" type="flex" justify="space-between">
      <span class="footer-text">
        版权归紫光软件所有
      </span>

    </van-row>
  </div>
</template>

<script>
import { validUsername } from '@/utils/validate'
import { captcha, login } from "@/api/user"
import {
  getToken,
  setToken,
  removeToken,
} from "@/utils/auth"
export default {
  name: 'Login',
  data () {
    const validateUsername = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入用户名！"))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (value !== "" && value.length < 6) {
        callback(new Error("密码输入错误！"))
      } else if (value === "") {
        callback(new Error("请输入密码！"))
      } else {
        callback()
      }
    }
    return {
      loginForm: {
        username: '',
        password: '',
        captchaCode: ''
      },
      loginRules: {
        username: [{ required: true, trigger: 'blur', validator: validateUsername }],
        password: [{ required: true, trigger: 'blur', validator: validatePassword }],
        captchaCode: [{ required: true, trigger: 'blur' }]
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined,
      captcha: 'data:image/png;base64, ',
    }
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  mounted () {
    if (this.loginForm.username === "") {
      this.$refs.username.focus()
    } else if (this.loginForm.password === "") {
      this.$refs.password.focus()
    } /* else if (this.validationCode === '') {
      this.$refs.validationCode.focus()
    } */
    this.getCaptcha()
  },
  methods: {
    showPwd () {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    // 获取验证码
    getCaptcha () {
      captcha().then(res => {
        this.captcha = window.URL.createObjectURL(res)
      })
        .catch(err => {
          console.log(err)
        })
    },
    // 验证码验证
    validationCaptcha () {
      let params = {
        key: this.key,
        verCode: this.loginForm.captchaCode
      }
      verifyCaptcha(params)
        .then(res => {
          if (res.code == 200) {
            this.flag = true
            // console.log(this.flag,'flag')
          } else {
            this.flag = false
            // console.log(this.flag,'flag')
            this.getCaptcha()
          }
        })
        .catch(err => {
          console.log(err, "err验证码")
        })
    },
    handleLogin () {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          let loginInfo = {
            userName: this.loginForm.username,
            userPassword: this.loginForm.password,
            identifyCode: this.loginForm.captchaCode
          }
          login(loginInfo).then(res => {
            if (res.code == 200) {
              this.loading = false
              this.$message({
                message: "登录成功",
                type: "success"
              })
              localStorage.setItem("userInfo",JSON.stringify(res.data))
              this.$router.push({ path: this.redirect || '/dashboard' })
            } else {
              this.loading = false
              alert(res.msg);
            }
          }).catch(err => {
            this.loading = false
            console.log(err)
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    handleIconClick (ev) {
      console.log("------------",ev);
    }
  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg: #283443;
$light_gray: #fff;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    // color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    // width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      // padding: 12px 5px 12px 15px;
      // color: $light_gray;
      // color: #000;
      height: 47px;
      // caret-color: $cursor;
      caret-color: #000;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px #fff inset !important;
        // -webkit-text-fill-color: $cursor !important;
      }
    }
  }
}
</style>

<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;

.login-container {
  min-height: 100%;
  width: 100%;
  // background-color: $bg;
  background: url('"~@/assets/login/bg.png') no-repeat;
  background-size: 100% 100%;
  // background-attachment:fixed;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: space-between;

  .login-form {
    /* position: relative;
    left: 400px;
    top: 178px; */
    // align-self: flex-end;
    width: 340px;
    max-width: 100%;
    margin: 0 auto;
    // margin-right: 150px;
    overflow: hidden;
    // background-color: rgba(255, 255, 255, 0.3);
    // border: 1px solid #fff;
    // border-radius: 0 60px;
    padding: 10px 30px 25px 30px;
    display: flex;
    flex-direction: column;
    align-items: center;
    .el-form-item {
      width: 100%;
      margin-bottom: 15px;
      .el-input {
        border: 1px solid rgba(255, 255, 255, 0.1);
        background: rgba(255, 255, 255, 1);
        color: #000;
        border-radius: 5px;
        width: 100%;
      }
    }
    .captchDiv {
      // width: 60%;

      ::v-deep.el-form-item__content {
        display: flex;
        align-items: center;
        // place-items: center;
        .el-input {
          width: 60%;
          // margin-left: auto;
          // flex: 1;
        }
      }
    }
    // .el-form-item__content {
    //   display: flex;
    //   align-items: center;
    // }
    /*     .validationCode {
      width: 60%;

    } */
    .captchImg {
      width: 130px;
      height: 47px;
      // background: cadetblue;
      // vertical-align: middle;
      cursor: pointer;
      // display: inline-block;
    }
    /* .el-form-item:last-child {
      width: 60%;
    } */
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;
    .circle {
      width: 50px;
      height: 50px;
      border-radius: 50%;
      background-color: #a2c0f3;
    }
    .title {
      font-size: 40px;
      color: #648fe9;
      font-family: "MicrosoftYaHei", "微软雅黑", sans-serif;
      font-weight: bolder;
      text-align:justify;text-justify:distribute-all-lines;text-align-last:justify;
      // margin: 0px auto 40px auto;
      // text-align: center;
      // font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }

  .footer {
    display: flex;
    height: 30px;
    font-size: 12px;
    // background-color: green;
    align-items: center;
    justify-content: center;
    color: #fff;
  }
}

.outer-shadows {
  background: url('"~@/assets/login/圆.png') no-repeat;
  background-size: 100% 100%;
  width: 615px;
  height: 615px;
  display: flex;
  justify-content: center;
  align-items: center;
  // box-shadow: rgb(21, 17, 24) 0px 0px 20px;
  // filter: drop-shadow(rgb(21, 17, 24) 0px 0px 20px);
}
.outer-border {
  width: 550px;
  height: 550px;
  border: 1px solid blue;
  border-radius: 50%;
  display: flex;
  justify-content: center;
  align-items: center;
}
.outer-border1 {
  width: 500px;
  height: 500px;
  border: 1px solid blue;
  border-radius: 50%;
  display: flex;
  justify-content: center;
  align-items: center;
}
.outer-border2 {
  width: 450px;
  height: 450px;
  border: 1px solid blue;
  border-radius: 50%;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: rgba(255, 255, 255, 0.3);
}
.el-button--primary {
  color: #fff;
  background-color: #3469d9;
  border-color: #3469d9;
}

.el-button--primary:focus,
.el-button--primary:hover {
  background: #3469d9;
  border-color: #3469d9;
  color: #fff;
}
</style>
