<template>
  <div class="login-container">
    <div class="login-container-box">
      <div class="left">
        <div>
          <img src="../../assets/login/yinke-logo.png" alt="">
        </div>
        <div>
          <img src="../../assets/login/logo.png" alt="">
        </div>
      </div>
      <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on" label-position="left">
        <div class="title">
          <h2>国内电商B端</h2>
        </div>
        <el-form-item prop="loginName" style="margin-bottom: 50px;">
          <span class="svg-container">
            <svg-icon icon-class="user" />
          </span>
          <el-input
            ref="loginName"
            v-model.trim="loginForm.loginName"
            placeholder="请输入用户名"
            name="loginName"
            type="text"
            tabindex="1"
            auto-complete="on"
          />
        </el-form-item>
        <el-form-item prop="password">
          <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>
        </el-form-item>

        <div class="tips">
          <span style="margin-right:20px;">忘记密码请联系管理员重置</span>
        </div>

        <el-button
          :loading="loading"
          type="primary"
          class="btn"
          @click.native.prevent="handleLogin"
        >
          登录
        </el-button>
      </el-form>

    </div>
  </div>
</template>

<script>
import { validUsername } from '@/utils/validate'
import request from '@/utils/request'
import md5 from 'js-md5'
export default {
  name: 'Login',
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!validUsername(value)) {
        callback(new Error('请输入用户名'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('请输入正确的密码'))
      } else {
        callback()
      }
    }
    return {
      loginForm: {
        loginName: '',
        password: ''
      },
      loginRules: {
        loginName: [{ 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
          // this.$store.dispatch('user/login', this.loginForm).then(() => {
          //   this.$router.push({ path: this.redirect || '/' })
          //   this.loading = false
          // }).catch(() => {
          //   this.loading = false
          // })

          const data = new FormData()
          data.append('loginName', this.loginForm.loginName)
          data.append('password', md5(this.loginForm.password))
          data.append('resources', 0)
          request({
            url: '/authority/do_login',
            method: 'post',
            data
          }).then(res => {
            if (res.success) {
              this.$store.dispatch('user/login', res.data)
              this.$router.push({ path: this.redirect || '/' })
              this.loading = false
            } else {
              this.$message.warning(res.msg)
              this.loading = false
            }
          }).finally(() => {
            this.loading = false
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss">
/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    width: 85%;
    input {
      background: transparent;
      border: 0px;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      // 光标
      // caret-color: #fff;
    }
  }

  .el-form-item {
    border: 1px solid #dcdfe6;
    background: #fff;
    border-radius: 5px;
    color: #454545;
  }
}
</style>

<style lang="scss" scoped>
.login-container {
  min-height: 100%;
  width: 100%;
  background-color: #D1EBFF;
  display: flex;
  justify-content: center;
  align-items: center;
  .login-container-box {
    height: 60vh;
    min-height: 540px;
    background: #fff;
    border-radius: 20px;
    display: flex;
    .left {
      width: 560px;
      background: #F9FCFF;
      padding: 20px;
      border-top-left-radius: 20px;
      border-bottom-left-radius: 20px;
      & > div {
        img {
          width: 100%;
          height: 100%;
        }
        &:first-child {
          width: 134px;
          height: 48px;
        }
        &:last-child {
          width: 420px;
          height: 413px;
          margin: 0 auto;
          margin-top: 30px;
        }
      }
    }
  }
  .login-form {
    position: relative;
    width: 480px;
    max-width: 100%;
    padding: 80px 35px 0;
    margin: 0 auto;
    overflow: hidden;
  }

  .title {
    text-align: center;
    margin-bottom: 50px;
    font-size: 24px;
  }

  .tips {
    font-size: 14px;
    color: #BABABA;
    margin-bottom: 10px;
    text-align: right;
  }
  .btn {
    width:100%;
    height: 43px;
    font-size: 16px;
    margin-bottom: 30px;
    margin-top: 30px;
  }
  .svg-container {
    padding: 6px 5px 6px 15px;
    color: #5AC4FC;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: #889aa4;
    cursor: pointer;
    user-select: none;
  }
}
</style>
