<template>
  <div class="login-container">
    <div id="particles-js" style="display: flex;align-items: center;justify-content: center">
      <canvas class="particles-js-canvas-el" width="1872" height="422" style="width: 100%; height: 100%;" />
    </div>
    <el-form class="login-form" autocomplete="on" label-position="left">

      <div class="title-container">
        <img src="~@/assets/images/logo.png">
      </div>

      <div id="login_container" />

    </el-form></div>
</template>

<script src="https://g.alicdn.com/dingding/dinglogin/0.0.5/ddLogin.js"></script>

<script>
import { setToken } from '@/utils/auth' // get token from cookie
export default {
  name: 'Login',
  data() {
    return {}
  },
  created() {
    // window.addEventListener('storage', this.afterQRScan)
  },

  mounted() {

    var thisHorse =window.location.origin;
    if(window.location.href.indexOf('?token=')>-1){
      var token =  window.location.href.substr( window.location.href.indexOf("?token=")+7);
      token =token.substring(0,token.indexOf('#'));
        this.$store.dispatch('user/login2', token).then(response=>{
          window.href=thisHorse
      })
    }
    else{
      var url = encodeURIComponent('http://wms.xingyunyi.cn/dingtalk/login?ReturnUrl='+thisHorse)
        var goto = encodeURIComponent('https://oapi.dingtalk.com/connect/oauth2/sns_authorize?appid=dingoaynjylnvs9mfgqekf&response_type=code&scope=snsapi_login&state=STATE&redirect_uri=' + url)
        var obj = DDLogin({
          id: 'login_container', // 这里需要你在自己的页面定义一个HTML标签并设置id，例如<div id="login_container"></div>或<span id="login_container"></span>
          goto: goto, // 请参考注释里的方式
          style: 'border:none;background-color:#FFFFFF;',
          width: '365',
          height: '400'
        })

        var handleMessage = function(event) {
          var origin = event.origin
          console.log('origin', event.origin)
          if (origin == 'https://login.dingtalk.com') { // 判断是否来自ddLogin扫码事件。
            var loginTmpCode = event.data // 拿到loginTmpCode后就可以在这里构造跳转链接进行跳转了
            console.log('loginTmpCode', loginTmpCode)

            window.location.href =
                    'https://oapi.dingtalk.com/connect/oauth2/sns_authorize?appid=dingoaynjylnvs9mfgqekf&response_type=code&scope=snsapi_login&state=STATE&url=' + url + '&loginTmpCode=' +
                    loginTmpCode
          }
        }
        if (typeof window.addEventListener != 'undefined') {
          window.addEventListener('message', handleMessage, false)
        } else if (typeof window.attachEvent != 'undefined') {
          window.attachEvent('onmessage', handleMessage)
        }
    }

     particlesJS('particles-js',

      {
        'particles': {
          'number': {
            'value': 80,
            'density': {
              'enable': true,
              'value_area': 800
            }
          },
          'color': {
            'value': '#ffffff'
          },
          'shape': {
            'type': 'circle',
            'stroke': {
              'width': 0,
              'color': '#000000'
            },
            'polygon': {
              'nb_sides': 5
            },
            'image': {
              'src': 'img/github.svg',
              'width': 100,
              'height': 100
            }
          },
          'opacity': {
            'value': 0.5,
            'random': false,
            'anim': {
              'enable': false,
              'speed': 1,
              'opacity_min': 0.1,
              'sync': false
            }
          },
          'size': {
            'value': 5,
            'random': true,
            'anim': {
              'enable': false,
              'speed': 40,
              'size_min': 0.1,
              'sync': false
            }
          },
          'line_linked': {
            'enable': true,
            'distance': 150,
            'color': '#ffffff',
            'opacity': 0.4,
            'width': 1
          },
          'move': {
            'enable': true,
            'speed': 6,
            'direction': 'none',
            'random': false,
            'straight': false,
            'out_mode': 'out',
            'attract': {
              'enable': false,
              'rotateX': 600,
              'rotateY': 1200
            }
          }
        },
        'interactivity': {
          'detect_on': 'canvas',
          'events': {
            'onhover': {
              'enable': true,
              'mode': 'repulse'
            },
            'onclick': {
              'enable': true,
              'mode': 'push'
            },
            'resize': true
          },
          'modes': {
            'grab': {
              'distance': 400,
              'line_linked': {
                'opacity': 1
              }
            },
            'bubble': {
              'distance': 400,
              'size': 40,
              'duration': 2,
              'opacity': 8,
              'speed': 3
            },
            'repulse': {
              'distance': 200
            },
            'push': {
              'particles_nb': 4
            },
            'remove': {
              'particles_nb': 2
            }
          }
        },
        'retina_detect': true,
        'config_demo': {
          'hide_card': false,
          'background_color': '#b61924',
          'background_image': '',
          'background_position': '50% 50%',
          'background_repeat': 'no-repeat',
          'background_size': 'cover'
        }
      }

    )

    // if (this.loginForm.username === '') {
    //   this.$refs.username.focus()
    // } else if (this.loginForm.password === '') {
    //   this.$refs.password.focus()
    // }
  }
}
</script>

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

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

@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: 85%;

    input {
      background: transparent;
      border: 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:#eee;

.login-container {
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  overflow: hidden;

  .login-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 160px 35px 0;
    margin: 0 auto;
    overflow: hidden;
  }

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

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    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>

<style lang="scss" scoped>
$bg:#fff;
$dark_gray:#fff;
$light_gray:#000;
#login_container{
    margin-left: -40px;
    margin-top: -20px;
}
.login-container {
	position:fixed;
  top:0px;
  left:0px;
  height:100%;
  width:100%;

  /*Fallback if gradeints don't work */
  background:url("~@/assets/images/bg.jpg") center center no-repeat  ;
  background-size: cover;
  /*Linear gradient... */

  .login-form {
    position: absolute;
    width: 360px;
    max-width: 100%;
    padding: 50px 35px 0px 35px;
    margin: 0 auto;
    top:50%;
    left:50%;
    overflow: hidden;
    background: #fff;
    box-shadow:0 0 5px #333;
    margin-top: -175px;
    margin-left: -175px;
    border-radius:5px;
  }

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

  .title-container {
    position: relative;

    .title {
      font-size: 24px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: left;
      font-weight: bold;
      color: #333;
    }
  }

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

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

</style>
