<template>
  <div>
    <div class="login-content">
      <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on" label-position="left">
        <div class="title">
          <h2>欢迎登录贸理通</h2>
        </div>
        <el-form-item prop="username" style="margin-bottom: 50px;">
          <div class="input-box">
            <span class="input-title">
              用户名
            </span>
            <el-input
              ref="username"
              v-model="loginForm.username"
              placeholder="请输入用户名"
              name="username"
              type="text"
              tabindex="1"
              auto-complete="on"
            />
          </div>

        </el-form-item>
        <el-form-item prop="password">
          <div class="input-box">
            <span class="input-title">
              密码
            </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>

        <div class="tips">
          <span style="margin-right:20px; cursor: pointer;" @click="changePassword">忘记密码？</span>
        </div>
        <el-button
          :loading="loading"
          type="primary"
          class="btn"
          @click.native.prevent="handleLogin"
        >
          登录
        </el-button>
      </el-form>
    </div>
    <cy-dialog ref="dialog" :modal="false" :style="isMobile() ? 'transform: scale(0.9)' : ''" title="重置密码">
      <change-password />
    </cy-dialog>
  </div>

</template>

<script>
import { validUsername } from '@/utils/validate'
import { isMobileDevice } from '@/utils/index'

export default {
  name: 'Login',
  components: {
    ChangePassword: () => import('./ChangePassword')
  },
  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: {
        username: '',
        password: ''
      },
      dialogKey: 0,
      // validator: validateUsername
      // validator: validatePassword
      loginRules: {
        username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
        password: [{ required: true, message: '请输入密码', trigger: 'blur' }]
      },
      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
          const postData = {
            username: this.loginForm.username,
            password: this.$md5(this.loginForm.password)
          }
          this.$store.dispatch('user/login', postData).then(() => {
            this.$router.push({ path: '/' })
            this.loading = false
          }).catch(() => {
            this.loading = false
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    changePassword() {
      // this.dialogKey += 1
      this.$refs.dialog.show = true
    },
    isMobile() {
      return isMobileDevice() && window.screen.width < 768
    }
  }
}
</script>

<style lang="scss" scoped>
.login-content {
  margin-left: 40px;
  .login-form {
    position: relative;
    width: 480px;
    max-width: 100%;
    padding: 60px 35px 0;
    margin: 0 auto;
    overflow: hidden;
    margin-top: 50px;
  }

  .title {
    text-align: left;
    margin-bottom: 50px;
    font-size: 30px;
    color: #2C4270;
  }

  .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;
  }
  .input-box {
    display: flex;
  }
  .input-title {
    font-size: 22px;
    color: #666;
    display: inline-block;
    width: 80px;
  }
  /*.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;
  }
}

@media screen and (max-width: 768px) {
  .login-content {
    margin-left: 0px;
    .login-form {
      position: relative;
      width: 480px;
      max-width: 100%;
      padding: 20px 35px 0;
      margin: 0 auto;
      overflow: hidden;
      margin-top: 50px;
    }
    .title {
      text-align: center;
      margin-bottom: 50px;
      font-size: 20px;
      color: #2C4270;
    }
    .input-box {
      display: flex;
    }
    .input-title {
      font-size: 16px;
      color: #666;
      display: inline-block;
      width: 80px;
    }
    .btn {
      width:100%;
      height: 43px;
      font-size: 16px;
      margin-bottom: 30px;
      margin-top: 30px;
    }
  }
}
</style>

<style lang="scss">
.login-content {
  .el-button--primary {
    background-color: #FE7D0B;
    border-color: #FE7D0B;
  }
  .el-button--primary:focus, .el-button--primary:hover {
    background-color: #FE7D0B;
    border-color: #FE7D0B;
  }
  .el-input {
    display: inline-block;
    width: 85%;
    input {
      background: transparent;
      border: 0px;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
    }
  }

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