<template>
  <div class="login-container">

    <div style="height: 18%"></div>


    <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" autocomplete="on" label-position="left" >

      <div class="title-container">

<!--        <h1 class="title">{{ loginForm.zxtmc }}集团信息管理系统</h1>-->
        <h1 class="title">{{ loginForm.zxtmc }}钢加生产管控系统</h1>
        <h3 class="title">{{ loginForm.zxtmc }}登录</h3>
      </div>

      <div v-if="loginForm.zxtbm === 'MS'" class="cgdl-select">
        <el-select v-model="loginForm.cgdlm" style="width:100%">
          <el-option v-for="item in cgdlList" :key="item.itemCode" :value="item.remark" :label="item.itemValue" />
        </el-select>
      </div>

      <el-form-item prop="username">
        <span class="svg-container">
          <svg-icon icon-class="user" />
        </span>
        <el-input
          ref="username"
          v-model="loginForm.username"
          placeholder="用户名"
          name="username"
          type="text"
          tabindex="1"
          autocomplete="on"
          @blur="handblur(loginForm.username)"
        />
      </el-form-item>

      <el-tooltip v-model="capsTooltip" content="Caps lock is On" placement="right" manual>
        <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"
            autocomplete="on"
            @keyup.native="checkCapslock"
            @blur="capsTooltip = false"
            @keyup.enter.native="handleLogin"
          />
          <span class="show-pwd" @click="showPwd">
            <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
          </span>
        </el-form-item>
      </el-tooltip>
      <template >
        <div align="center" >
          <el-button :loading="loading" type="primary" style="width:100%;margin-bottom:30px;" @click.native.prevent="handleLogin">账号登陆</el-button>
        </div>

      </template>




    </el-form>

  </div>
</template>

<script>
  import { validUsername } from '@/utils/validate'
  import SocialSign from './components/SocialSignin'
  import {saveLog} from "@/api/user";
  // import { getByCode,getUser } from '@/api/sy/xtgl'

  export default {
    name: 'Login',
    components: { SocialSign },
    data() {
      const validateUsername = (rule, value, callback) => {
        if (!validUsername(value)) {
          callback(new Error('Please enter the correct user 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 {
        cgdl:{
          dictCode: 'CGDL'
        },
        cgdlList:[],
        cgdlm:"",
        loginForm: {
          username: '',
          password: '',
          zxtbm: '',
          zxtmc: '',
          uuid: '',
          cgdlm: ''
        },
        loginRules: {
          username: [{ required: true, trigger: 'blur', validator: null }],
          password: [{ required: false, trigger: 'blur', validator: null }]
        },
        passwordType: 'password',
        capsTooltip: false,
        loading: false,
        showDialog: false,
        redirect: undefined,
        otherQuery: {},
        loginType:'',
      }
    },
    watch: {
      $route: {
        handler: function(route) {
          const query = route.query
          if (query) {
            this.loginForm.zxtbm = query.zxtbm
            this.loginForm.zxtmc = query.zxtmc
            this.redirect = '/'// query.redirect
            // this.otherQuery = this.getOtherQuery(query)
          }
        },
        immediate: true
      }
    },
    created() {
      // window.addEventListener('storage', this.afterQRScan)
      // getByCode(this.cgdl).then(res=>{
      //   this.cgdlList = res.data
      // })
    },
    mounted() {
      if (this.loginForm.username === '') {
        this.$refs.username.focus()
      } else if (this.loginForm.password === '') {
        this.$refs.password.focus()
      }
    },
    destroyed() {
      // window.removeEventListener('storage', this.afterQRScan)
    },
    methods: {
      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() {
        if(this.loginForm.zxtbm === 'MS'){
          if(this.loginForm.cgdlm !== ''){
            if(this.loginForm.cgdlm === 'FJ' || this.loginForm.cgdlm === 'WZ'){
              this.$refs.loginForm.validate(valid => {
                if (valid) {
                  this.loading = true
                  this.loginForm.uuid=''
                  console.log(this.loginForm)
                  this.$store.dispatch('user/login', this.loginForm)
                    .then(() => {
                      saveLog({
                        type:'LOGIN',
                        result:'登录成功',
                        content:'登录'
                      })
                      this.$router.push({ path: this.redirect || '/' })
                      this.loading = false
                    })
                    .catch(() => {
                      this.loading = false
                    })
                } else {
                  return false
                }
              })
            }else{
              this.$message({message:"该模块未开放，敬请期待", type:'info'})
            }
          }else{
            this.$message.error("请选择登录模块")
          }
        }else{
          this.loginForm.uuid=''
          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
                  //启用静默方式,记录登录日志
                  this.$store.dispatch('page/handler', true).then(()=>{
                    saveLog({
                      type:'LOGIN',
                      result:'登录成功',
                      content:'登录'
                    }).then(()=>{
                      this.$store.dispatch('page/handler', false)
                    })
                  })
                })
                .catch(() => {
                  this.loading = false
                })
            } else {
              return false
            }
          })
        }
      },

      fingerprintLogin() {
        var uuid = guid();
        window.location.href="zwlr://"+uuid;
        console.log(uuid);
        this.loginForm.uuid=uuid;
        this.loginForm.username="";
        this.$store.dispatch('user/login', this.loginForm)
          .then(() => {
            this.$router.push({ path: this.redirect || '/' })
            this.loading = false
          })
          .catch(() => {
            this.loading = false
          })
      },
      getOtherQuery(query) {
        return Object.keys(query).reduce((acc, cur) => {
          if (cur !== 'redirect') {
            acc[cur] = query[cur]
          }
          return acc
        }, {})
      },
      handblur(name){
        // getUser({'name':name}).then(res=>{
        //   console.log(res)
        //   this.loginType= res.data.login_type
        // })
      }
    }
  }

  //用于生成uuid
  function S4() {
    return (((1+Math.random())*0x10000)|0).toString(16).substring(1);
  }
  function guid() {
    return ("login-"+S4()+S4()+"-"+S4()+"-"+S4()+"-"+S4()+"-"+S4()+S4()+S4());
  }

</script>

<style lang="scss">
  /* 修复input 背景不协调 和光标变色 */
  /* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

  $bg:#283443;
  $light_gray:#000;
  $cursor: #000;

  @supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
    .login-container .el-input input {
      color: $cursor;
    }
  }

  /* reset element-ui css */
  .login-container {
    .el-input {
      display: inline-block;
      height: 47px;
      width: 87%;
      background-color: white;
      border-radius: 5px;
      input {
        background: transparent;
        border-bottom: 1px solid black;
        border-top: 0px ;
        border-right: 0px ;
        border-left: 0px ;
        -webkit-appearance: none;
        border-radius: 0px;
        /*padding: 12px 5px 12px 15px;*/
        color: $light_gray;
        height: 47px;
        caret-color: $cursor;

        //账号密码自动填充
        //&:-webkit-autofill {
        //  box-shadow: 0 0 0px 1000px $bg inset !important;
        //  -webkit-text-fill-color: $cursor !important;
        //}
      }
    }

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

<style lang="scss" scoped>


  $bg:#2d3a4b;
  $dark_gray:#889aa4;
  $light_gray: #000000;
  .login-container {
    /*min-height: 100%;*/
    /*width: 100%;*/
    /*background-color: white;*/
    /*overflow: hidden;*/
    width: 100%;
    height: 100%;
    background-image: url("../../assets/bjt1.jpg");
    background-size: cover;
    background-position: center;
    position: relative;

    .login-form {
      position: relative;
      width: 520px;
      max-width: 100%;
      padding: 40px 35px 0;
      line-height: 100%;
      margin: 0 auto;
      overflow: hidden;
      background: #00000050;
    }

    .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;
      color: white;
      vertical-align: middle;
      width: 30px;
      display: inline-block;
    }

    .title-container {
      position: relative;

      .title {
        font-size: 32px;
        //color: $light_gray;
        color:white;
        margin: 0px auto 40px auto;
        text-align: center;
        font-weight: 400;
      }
    }

    .cgdl-select {
      width: 100%;
      margin: 0 auto;
    }

    .show-pwd {
      position: absolute;
      right: 10px;
      top: 7px;
      font-size: 16px;
      color: white;
      cursor: pointer;
      user-select: none;
    }

    .thirdparty-button {
      position: absolute;
      right: 0;
      bottom: 6px;
    }

    @media only screen and (max-width: 470px) {
      .thirdparty-button {
        display: none;
      }
    }
  }
</style>

