<template>
  <el-dialog
    :title="$t('Administratorauthentication')"
    :visible.sync="dialogVisible"
    width="50%"
    :before-close="handleClose">
    <p class="tip" v-if="userRole === 1">登录成功</p>
    <div v-else>
      <p class="tip">{{$t('Notloggedinyet')}}</p>
      <el-form :model="dataForm" ref="dataForm" :rules="dataRule" :label-width="$i18n.locale === 'en-us' ? '' : '1.1rem'" size="mini">
        <el-form-item :label="$t('Administratorpassword')" prop="password">
          <el-input :placeholder="$t('Pleasepassword')" v-model="dataForm.password" id="password" show-password @focus="show" data-layout="normal"></el-input>
        </el-form-item>
      </el-form>
    </div>
    <el-row type="flex" justify="space-around" style="margin-top: .3rem">
      <el-col :span="7" v-if="userRole === 1"><button class="button_control button_control_disabled" @click="exitUser"><p>{{ $t('Logout') }}</p></button></el-col>
      <el-col :span="7" v-else><button class="button_control" @click="dataFormSubmit"><p>{{$t('Login')}}</p></button></el-col>
    </el-row>
    <vue-touch-keyboard id="keyboard" :options="options" v-if="visible" :layout="layout" :cancel="hide" :accept="accept" :input="input" :next="next" />
  </el-dialog>
</template>

<script>
import config from '../../../public/config.json'
import { mapGetters, mapActions } from 'vuex'
export default {
  data () {
    return {
      dialogVisible: false,
      dataForm: {
        password: ''
      },
      dataRule: {
        password: [
          { required: true, message: this.$t('Passwordnotempty'), trigger: 'blur' }
        ]
      },
      passwords: [], // 存储文件中的密码
      visible: false,
      layout: 'normal',
      input: null,
      options: {
        useKbEvents: false,
        preventClickEvent: false
      }
    }
  },
  computed: {
    ...mapGetters(['userRole']),
  },
  methods: {
    ...mapActions(['setUserRole']),
    init () {
      this.dialogVisible = true
    },
    exitUser () {
      this.dialogVisible = false
      this.visible = false
      this.setUserRole(2)
    },
    dataFormSubmit () {
      this.dialogVisible = false
      this.visible = false
      if (this.userRole === 1) {
        return
      }
      if (this.dataForm.password === config.password) {
        this.setUserRole(1)
        this.$message({
          message: '登录成功',
          type: 'success'
        })
      } else {
        this.$message.error('登录失败')
      }
      this.dataForm = {password: ''}
    },
    handleClose (done) {
      this.visible = false
      done()
    },
    show (e) {
      // 关闭中文keyboard
      let arr = document.querySelectorAll('.hg-theme-default')
      arr.forEach((ele) => {
        ele.style.visibility = 'hidden'
      })
      this.input = e.target
      this.layout = e.target.dataset.layout
      if (!this.visible) {
        this.visible = true
      }
    },
    hide () {
      this.visible = false
    },
    accept () {
      this.hide()
    },
    next () {
      let inputs = document.querySelectorAll('input')
      let found = false;
      [].forEach.call(inputs, (item, i) => {
        if (!found && item === this.input && i < inputs.length - 1 && this.input.dataset.next === '1') {
          found = true
          this.$nextTick(() => {
            inputs[i + 1].focus()
          })
        }
      })
      if (!found) {
        this.input.blur()
        this.hide()
      }
    }
  }
}
</script>

<style lang="stylus" scoped>
.tip
  font-size .2rem
  font-weight 700
  font-family 'SourceHanSansCN-Bold'
  line-height .2rem
  color #E54F29
  margin-bottom .1rem
</style>
