<template>
  <div class="login-container">
    <div class="login-logo">
      <img src="../../assets/login_logo.png" alt="">
    </div>
    <div class="login-wrap">
      <div class="login-logo1">
        <img src="../../assets/login_logo2.png" alt="">
      </div>
      <div class="login-form1">
        <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="" auto-complete="on"
          label-position="left">
          <div class="title-container">
            <h3 class="title">欢迎登录</h3>
            <div class="title-change">
              <span @click="changeLogin=false" :class="{color:!changeLogin}">账号登录</span>
              <span @click="changeLogin=true" :class="{color:changeLogin}">短信登录</span>
            </div>
          </div>

          <template v-if="!changeLogin">
            <el-form-item prop="username">
              <el-input ref="username" v-model="loginForm.username" placeholder="手机号/邮箱" name="username" type="text"
                tabindex="1" auto-complete="on" />
            </el-form-item>
            <el-form-item prop="password">
              <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>
            </el-form-item>
          </template>
          <template v-else>
            <el-form-item prop="username">
              <el-input ref="username" v-model="loginForm.username" placeholder="手机号" name="username" type="text"
                tabindex="1" auto-complete="on" />
            </el-form-item>
            <el-form-item prop="code" class="send_item">
              <el-input ref="code" v-model="loginForm.code" placeholder="验证码" name="code" type="text" tabindex="1"
                auto-complete="on">
              </el-input>
              <span class="senndCode">发送验证码</span>
            </el-form-item>
          </template>



          <el-button :loading="loading" type="primary" class="loginBtn"
            @click.native.prevent="handleLogin">登录</el-button>
        </el-form>
      </div>
    </div>

    <div class="login-footer">版权所有：苏州未来科技产业发展有限公司</div>
  </div>
</template>

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

  const defaultSettings = require('../../../src/settings.js')
  export default {
    name: 'Login',
    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 {
        title: "",
        changeLogin: false,
        loginForm: {
          username: '',
          password: ''
        },
        loginRules: {
          username: [{
            required: true,
            trigger: 'blur',
            validator: validateUsername
          }],
          password: [{
            required: true,
            trigger: 'blur',
            validator: validatePassword
          }]
        },
        loading: false,
        passwordType: 'password',
        redirect: undefined
      }
    },
    watch: {
      $route: {
        handler: function(route) {
          if(route.query && route.query.redirect==='/'){
            this.redirect = '/dashboard'
          }else{
            this.redirect = route.query && route.query.redirect
          }

        },
        immediate: true
      }
    },
    created() {
      this.title = defaultSettings.title;
    },
    methods: {
      // changeLogin(type){
      //   if(){}
      // },
      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
            this.$store.dispatch('user/login', this.loginForm).then(() => {

              this.$router.push({
                path: this.redirect || '/dashboard'
              })
              this.loading = false
            }).catch(() => {
              this.loading = false
            })
          } else {
            console.log('error submit!!')
            return false
          }
        })
      }
    }
  }
</script>

<style lang="scss">
  #particles-js {
    width: 100%;
    height: 99%;
    position: absolute;
  }

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

  $bg: #122583;
  $light_gray: #122583;
  $cursor: #122583;

  @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 #cdcdcd;
      background: #f4f4f4;
      border-radius: 5px;
      color: #454545;
      margin-bottom: 30px;
    }
  }
</style>

<style lang="scss" scoped>
  $bg: #122583;
  $dark_gray: #122583;
  $light_gray: #122583;

  .login-container {
    height: 100vh;
    width: 100vw;
    background: url("../../assets/login_bg.png") no-repeat;
    overflow: hidden;
    background-size: 100% 100%;
    position: relative;
    .login-footer{
      text-align: center;
      position: absolute;
      bottom:20px;
      left:0;
      width:100%;
      color:#274999;


    }
    .login-logo {
      // width: 20vw;
      width: 383px;
      margin: 0 auto;
      margin-top: 9vh;

      img {
        width: 100%;
      }
    }

    .login-wrap {
      margin-top: 11vh;
      margin-left: 20vw;
      margin-right: 15vw;
      display: flex;
      justify-content: space-between;
      align-items: end;

      .login-logo1 {
        // width: 16.5vw;
        width: 270px;

        img {
          width: 100%;
        }
      }

      .login-form1 {
        width: 30vw;
        // height: 49vh;
        // width: 593px;
        // height: 530px;
        background: url('../../assets/login_form_bg.png') no-repeat;
        background-size: 100% 100%;
        padding: 45px;
        padding-bottom: 90px;
        padding-top: 35px;
        // margin: 0 auto;

        .title-container {
          margin-bottom: 35px;

          .title {
            text-align: center;
            font-size: 28px;
            color: #274999;

            text-align: center;

          }

          .title-change {
            font-size: 18px;
            display: flex;
            justify-content: space-between;
            padding-bottom: 10px;
            border-bottom: 1px solid rgba(0, 0, 0, 0.1);

            span {
              cursor: pointer;
            }

            span.color {
              color: #c69c6d;
            }
          }
        }

        .send_item {
          .el-input {
            width: 70%;
          }

          .senndCode {
            background-color: #c69c6d;
            display: inline-block;
            width: 30%;
            text-align: center;
            color: #fff;
            height: 47px;
            line-height: 47px;
            border-radius: 0 5px 5px 0;
          }
        }

        .loginBtn {
          width: 100%;
          background: linear-gradient(to right, #284a99, #c69c6d);
          border: none;
          height: 47px;
          line-height: 47px;
          padding: 0;
          font-size: 18px;
        }

      }
    }

    .tips {
      font-size: 14px;
      color: #fff;
      margin-bottom: 10px;

      span {
        &:first-of-type {
          margin-right: 16px;
        }
      }
    }

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

  @media (max-width:492px) {
    .login-container {
      .login-logo{
        width:100%;
      }
      .login-wrap {
        margin-left: 0px;
        margin-right: 0px;

        .login-logo1 {
          display: none;
        }

        .login-form1 {
          width: 100%;
          margin: 0 auto // height: 530px;
        }
      }
    }
  }

  @media (min-width:493px) {
    .login-container {
      .login-wrap {
        margin-left: 0px;
        margin-right: 0px;

        .login-logo1 {
          display: none;
        }

        .login-form1 {
          width: 493px;
          margin: 0 auto // height: 530px;
        }
      }
    }
  }

  @media (min-width:768px) {
    .login-container {
      .login-wrap {
        margin-left: 100px;
        margin-right: 50px;

        .login-logo1 {
          display: none;
        }

        .login-form1 {
          width: 493px;
          margin: 0 auto // height: 530px;
        }
      }
    }
  }

  @media (min-width:992px) {
    .login-container {
      .login-wrap {
        margin-left: 100px;
        margin-right: 50px;

        .login-logo1 {
          display: block;
          width: 217
        }

        .login-form1 {
          width: 493px;
          margin: 0;
          // height: 530px;
        }
      }
    }
  }

  @media (min-width:1220px) {
    .login-container {
      .login-wrap {
        margin-left: 150px;
        margin-right: 100px;

        .login-logo1 {
          display: block;
        }

        .login-form1 {
          margin: 0
        }
      }
    }
  }

  @media (min-width:1320px) {
    .login-container {
      .login-wrap {
        margin-left: 15vw;
        margin-right: 10vw;

        .login-logo1 {
          display: block;
        }

        .login-form1 {
          margin: 0
        }
      }
    }
  }

  @media (min-width:1440px) {
    .login-container {
      .login-wrap {
        margin-left: 20vw;
        margin-right: 10vw;

        .login-logo1 {
          display: block;
        }

        .login-form1 {
          margin: 0
        }
      }
    }
  }

  // @media (min-width:992px) {
  //   .login-container {
  //     .login-wrap {
  //       margin-left: 200px;
  //       margin-right: 150px;
  //       .login-logo1 {
  //         display: block;
  //       }

  //       // .login-form1 {
  //       //   width: 593px;
  //       //   // height: 530px;
  //       // }
  //     }
  //   }
  // }
</style>
