<template>
  <div class="login-container">
    <div class="login-box">
      <el-form
        ref="loginForm"
        :model="loginForm"
        :rules="loginRules"
        class="login-form"
        auto-complete="on"
        label-position="left"
      >
        <div class="logo">
          <img
            src="http://jingcheng-h5temp.oss-cn-shanghai.aliyuncs.com/1543283673332.png?Expires=1545111060&OSSAccessKeyId=LTAIkc75dpkJw8Lb"
          >
        </div>
        <h3 class="title">智慧社区运营平台</h3>
        <el-form-item prop="username">
          <span class="svg-container">
            <svg-icon icon-class="user"/>
          </span>
          <el-input
            v-model="loginForm.username"
            name="username"
            type="text"
            auto-complete="on"
            placeholder="请输入手机号"
          />
        </el-form-item>
        <div class="pwdSend">
          <el-form-item prop="password">
            <el-input
              v-model="loginForm.password"
              name="password"
              type="text"
              auto-complete="on"
              placeholder="请输入验证码"
              @keyup.enter.native="handleLogin"
            />
          </el-form-item>
          <el-button v-if="isCodeNum == false" type="primary" @click="sendCode();">发送验证码</el-button>
          <el-button v-else type="primary" disabled>{{ codeNum + '秒后重新获取' }}</el-button>
        </div>
        <el-button
          :loading="loading"
          type="primary"
          style="width:100%;"
          @click.native.prevent="handleLogin"
        >登录</el-button>
      </el-form>
    </div>
    <span class="bottom-desc">Copyright © 南京锦城佳业网络科技有限公司, All Rights Reserved.</span>
  </div>
</template>

<script>
import { isvalidUsername, validatCode } from '@/utils/validate'

export default {
  name: 'Login',
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!isvalidUsername(value)) {
        callback(new Error('请输入正确的手机号'))
      } else {
        callback()
      }
    }
    const validateIndexCode = (rule, value, callback) => {
      if (!validatCode(value)) {
        callback(new Error('请输入验证码'))
      } else {
        callback()
      }
    }
    return {
      loginForm: {
        username: '',
        password: ''
      },
      loginRules: {
        username: [{ required: true, trigger: 'blur', validator: validateUsername }],
        password: [{ required: true, trigger: 'blur', validator: validateIndexCode }]
      },
      loading: false,
      redirect: undefined,
      isCodeNum: false,
      codeNum: 60 // 验证码倒计时 60 秒
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  methods: {
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.$store.dispatch('Login', this.loginForm).then((res) => {
            if (res.code === '0') {
              console.log('登陆结果: ', res)
              this.loading = false
              // this.$router.push({ path: this.redirect || '/index' })
              this.$router.push({ name: 'index' })
            } else {
              this.loading = false
            }
          }).catch(() => {
            this.loading = false
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
      // this.$router.push({ path: this.redirect || '/index' })
    },
    sendCode() {
      // 默认为1, 1表示不成功
      let resCode = 1
      this.$store.dispatch('SendCode', this.loginForm.username).then((res) => {
        // 成功则为 0
        resCode = res.code
        // 为0发送成功
        if (resCode === '0') {
          this.codeNum = 60
          this.isCodeNum = true
          const time = setInterval(() => {
            if (this.codeNum === 1) {
              clearInterval(time)
              this.isCodeNum = false
            }
            this.codeNum--
          }, 1000)
        }
      }).catch(() => {
        console.log('error sendcode')
      })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss">
$bg: #2d3a4b;
$light_gray: #eee;

/* reset element-ui css */
.login-container {

  .bottom-desc{
    position: absolute;
    top: 95%;
    left: 39.5%;
    color: #fff
  }

  .login-box{
    position: absolute;
    width: 400px;
    height: 425px;
    background-color: rgba(21,22,58,0.53);
    top: 28%;
    left: 40%;
    border-radius: 10px;
    box-shadow:  #666 0px 0px 10px inset;
    }

  .el-input {
    display: inline-block;
    height: 40px;
    width: 85%;
    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 4px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      &:-webkit-autofill {
        -webkit-box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: #fff !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 rel="stylesheet/scss" lang="scss" scoped>
.logo{
  text-align:center;
  img{
    margin-bottom:10px;
  }
}
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;
.login-container {
  position: fixed;
  height: 100%;
  width: 100%;
  background-color: $bg;
  .login-form {
    position: absolute;
    left: 0;
    right: 0;
    width: 382px;
    max-width: 100%;
    padding: 35px 35px 15px 35px;
    margin: 20px auto;
    .pwdSend {
      float: left;
      .el-form-item {
        float: left;
        width: 158px;
        .el-input {
          float: left;
        }
      }
      .el-button {
        margin-left: 10px;
      }
    }
  }
  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;
    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }
  .svg-container {
    padding: 0px 5px 0px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }
  .title {
    font-size: 26px;
    font-weight: 400;
    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;
  }
}
</style>
