<template>
  <div class="login-container" :style="backgrounddiv">
    <div class="login-container-header">
      <div><img class="login-container-header-img" src="@/assets/login/logo-white.png" alt=""></div>
      <el-divider style="margin-left: 10px;margin-right: 10px" direction="vertical" />
      <div class="login-title">一站式智慧实验室管理服务</div>
    </div>
    <div class="login-container-mid">
      <div class="login-container-mid-top"><img src="@/assets/rms/login_white.png" alt=""></div>
      <img class="login-container-mid-mid" src="@/assets/login/分隔条.png" alt="">
      <div class="login-container-mid-bottom">为您提供基于物联网的智慧实验室物资管理解决方案</div>
    </div>
    <div>
      <el-card class="login-form-container">
        <el-form
          ref="loginForm"
          label-position="top"
          :model="loginForm"
          :rules="loginRules"
          class="login-form"
          auto-complete="on"
        >

          <div class="title-container">
            <h3 class="title">欢迎登录</h3>
          </div>

          <el-form-item prop="user_name" label="用户名">
            <div class="el-form-item-content">
              <span class="svg-container">
                <svg-icon icon-class="user" />
              </span>
              <el-input
                ref="username"
                v-model="loginForm.user_name"
                placeholder="请输入用户名"
                name="user_name"
                type="text"
                tabindex="1"
              />
            </div>

          </el-form-item>

          <el-form-item prop="password" label="密码">
            <div class="el-form-item-content">
              <span class="svg-container">
                <svg-icon icon-class="password" />
              </span>
              <el-input
                :key="passwordType"
                ref="password"
                v-model="loginForm.password"
                :type="passwordType"
                placeholder="请输入用户名密码"
                name="password"
                tabindex="2"
                auto-complete="on"
                @keyup.enter.native="handleLogin"
              />
              <span class="show-pwd" @click="showPwd">
                <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
              </span>
            </div>
          </el-form-item>

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

        </el-form>
        <div class="forget-pwd"><a>忘记密码？</a></div>
      </el-card>
    </div>
    <div class="statement">COPYRIGHT © 2022 杭州研一智控科技有限公司 版权所有</div>
  </div>
</template>

<script>
// import { validUsername } from '@/utils/validate'
import stringify from '@/utils/stringify'
import { login } from '@/api/user/user'
import { client_list } from '@/api/common'

export default {
  name: 'Login',
  data() {
    // const validateUsername = (rule, value, callback) => {
    //   if (!validUsername(value)) {
    //     callback(new Error('Please enter the correct userinfo 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 {
      backgrounddiv: {
        'background-image': 'url(' + require('@/assets/login/login.png') + ')'
      },
      loginForm: {
        user_name: '',
        password: ''
      },
      loginRules: {
        // user_name: [{ required: true, trigger: 'blur', validator: validateUsername }],
        // password: [{ required: true, trigger: 'blur', validator: validatePassword }]
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  methods: {
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          login(stringify(this.loginForm)).then(res => {
            this.$store.commit('user/SET_TOKEN', res.data.token)
            this.$store.dispatch('permission/generateRoutes')
            this.$router.replace('/home')
            // 登录后立刻获取client数据供后续使用
            client_list().then(
              res => {
                this.$store.commit('app/SET_COPTS', res.data.data_list)
              }
            )
          }).finally(
            () => {
              this.loading = false
            }
          )
          // this.$store.dispatch('userinfo/login', this.loginForm).then(() => {
          //   this.$router.push({ path: this.redirect || '/' })
          //   this.loading = false
          // }).catch(() => {
          //   this.loading = false
          // })
        } else {
          return false
        }
      })
    }
  }
}
</script>
<style scoped>
.statement {
  width: 100%;
  font-size: 1.25rem;
  font-weight: 400;
  color: #FFFFFF;
  text-align: center;
  bottom: 1rem;
  position: absolute;
}

.login-title {
  padding-left: 1rem;
  font-size: 1.25rem;
  font-weight: 400;
  color: #FFFFFF;
}

.login-form-container {
  color: #C0C4CC;
  width: 27.5rem;
  height: 33.75rem;
  position: absolute;
  right: 8.0625rem;
  top: 50%;
  margin-top: -16.87rem;
}

.login-container-header-img {
  height: 4.6875rem;
}

.forget-pwd {
  width: 100%;
  text-align: center;
  color: #409EFF;
  margin-left: 1rem;
}

>>> .el-input__inner {
  border: none;
}
</style>

<style lang="scss" scoped>
/* 修复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 {
  background: linear-gradient(135deg, rgba(69, 63, 244, 0.7700) 0%, #3F47F4 100%) no-repeat;
  background-size: 100% 100%;
  opacity: 1;

  .login-container-header {
    margin-left: 7.5rem;
    margin-top: 5rem;
    display: flex;
    align-items: center;
  }

  .login-container-mid {
    margin-left: 7.5rem;
    margin-top: 16rem;

    .login-container-mid-mid {
      height: 0.75rem;
      margin-top: 1.25rem;
      margin-bottom: 1.25rem;
    }

    .login-container-mid-top {
      img {
        width: 49.3125rem;
      }
    }

    .login-container-mid-bottom {
      font-size: 28px;
      font-family: Microsoft YaHei-Regular, Microsoft YaHei, serif;
      font-weight: 400;
      color: #FFFFFF;
    }
  }

  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0rem;
      -webkit-appearance: none;
      border-radius: 0rem;
      padding: 0.75rem 0.3125rem 0.75rem 0.9375rem;
      color: #DCDFE6;
      height: 2.9375rem;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item-content {
    //border: 1px solid rgba(255, 255, 255, 0.1);
    //background: rgba(0, 0, 0, 0.1);
    //border-radius: 5px;
    //color: #454545;
    border: 1px solid #DCDFE6;
  }
}
</style>

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

.login-container {
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  overflow: hidden;

  .login-form {
    position: relative;
    width: 27.5rem;
    max-width: 100%;
    padding-top: 4rem;
    padding-left: 1rem;
    overflow: hidden;
  }

  .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;

    .title {
      padding-left: 1rem;
      font-size: 1.625rem;
      color: #409EFF;
      //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;
  }
}
</style>
