<template>
  <el-card class="rpm-sign-in">
    <div slot="header" align="center">
      <span>登入内业管理系统</span>
    </div>
    <el-form ref="loginForm" :model="loginForm" :rules="loginRules" label-width="0">
      <el-form-item prop="email">
        <el-input
          ref="username"
          placeholder="输入账号"
          type="text"
          tabindex="1"
          v-model="loginForm.username"
        />
        <div class="rpm-sign__tip cprimary" >
          仅内部账号使用
        </div>
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          ref="password"
          placeholder="输入密码"
          type="password"
          tabindex="2"
          :key="passwordType"
          v-model="loginForm.password"
          @keyup.native="checkCapslock"
          @blur="capsTooltip = false"
          @keyup.enter.native="handleLogin"
        />
        <div class="rpm-sign-in__tip cdanger" @click="qrcode=true" >
          忘记密码
        </div>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click.native.prevent="handleLogin">
          登入
        </el-button>
      </el-form-item>
    </el-form>

    <el-dialog
      title="微信小程序扫码登入"
      class="fc tc"
      :visible.sync="qrcode"
      width="500px"
      :append-to-body="true"
      @close="qrcode=false"
    >
      <div class="qrcode">
        <img src="../../../assets/qrcode.jpg" />
      </div>
    </el-dialog>

  </el-card>
</template>

<script>
import { validateLoginname, validatePassword } from '@/utils/validate'
import md5 from '@/utils/md5'
import { mapActions } from "vuex";



export default {
  name: 'Login',
  data() {
    return {
      visibleForm: 'signIn',
      loginForm: {
        username: '',
        password: ''
      },
      loginRules: {
        username: [{ required: true, trigger: 'blur', validator: validateLoginname }],
        password: [{ required: true, trigger: 'blur', validator: validatePassword }]
      },
      qrcode: false,
      passwordType: 'password',
      capsTooltip: false,
      loading: false,
      showDialog: false,
      redirect: undefined,
      otherQuery: {}
    }
  },
  mounted() {
    if (this.loginForm.username === '') {
      this.$refs.username.focus()
    } else if (this.loginForm.password === '') {
      this.$refs.password.focus()
    }
  },
  created(){
    console.log( this.$route.query  )
     this.redirect = this.$route.query.redirect
  },
  methods: {
    ...mapActions("user", ["login", "loadDepartment"]),
    checkCapslock(e) {
      const { key } = e
      this.capsTooltip = key && key.length === 1 && (key >= 'A' && key <= 'Z')
    },
    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)  return;
        this.loading = true
        let param = { username: this.loginForm.username, password: md5(this.loginForm.password) }
        let path = this.redirect || '/';
        this.login( param).then(() => {
          this.$router.push({ path, query: this.otherQuery })
          this.loadDepartment()
          this.loading = false
        }).catch((err) => {
          console.log("err", err)
          this.loading = false
        })
      })
    }
  }
}
</script>


<style lang="scss" scoped>
.el-button {
  width: 100% !important;
}
.rpm-sign-in {
  position: absolute;
  top: 20px;
  right: 30px;
  width: 350px;
}
.rpm-sign-in__tip {
  padding-top: 2px;
  padding-bottom: 2px;
  cursor: pointer;
  display: inline-block;
  line-height: 100%;
  position: relative;
  font-size: 14px;
}
</style>
