<template>
  <div class="conatiner">
    <div class="login-wrap">
      <div>
        <div class="logo">
          <span>美大营销管理平台</span><br>
          <small>MEIDA-MANAGE-SYSTEM</small>
        </div>
        <div class="login-form">
          <h4 class="title">登录</h4>
          <el-tabs v-model="activeName">
            <el-tab-pane label="短信登录" name="first">
              <el-form :rules="rules" ref="loginForm"  :model="loginForm">
                <el-form-item prop="phonenumber">
                  <el-input v-model="loginForm.phonenumber" placeholder="账号"></el-input>
                </el-form-item> 
                <el-form-item prop="password">
                  <el-input v-model="loginForm.password" placeholder="验证码">
                    <template slot="append">
                      <span v-if="count > 0">{{count === 0?'获取验证码':count+ 's'}}</span>
                      <el-button v-else @click="countDown" type="primary">{{count === 0?'获取验证码':count+ 's'}}</el-button>
                    </template>
                  </el-input>
                </el-form-item>
                <el-form-item>
                  <div class="flex-between">
                    <el-checkbox v-model="checked">记住账号</el-checkbox>
                    <!-- <el-button type="text">忘记密码</el-button> -->
                  </div>
                </el-form-item>
                <el-form-item>
                  <el-button type="primary" style="width:100%" @click="login">登 录</el-button>
                </el-form-item>
              </el-form>
            </el-tab-pane>
            <el-tab-pane label="账号登录" name="second">
              <el-form :rules="rules2" ref="loginForm"  :model="loginForm">
                <el-form-item prop="phonenumber">
                  <el-input v-model="loginForm.phonenumber" placeholder="账号"></el-input>
                </el-form-item> 
                <el-form-item prop="password">
                  <el-input v-model="loginForm.password" type="password" placeholder="密码">
                  </el-input>
                </el-form-item>
                <el-form-item>
                  <div class="flex-between">
                    <el-checkbox v-model="checked">记住账号</el-checkbox>
                    <!-- <el-button type="text">忘记密码</el-button> -->
                  </div>
                </el-form-item>
                <el-form-item>
                  <el-button type="primary" style="width:100%" @click="loginbyaccount">登 录</el-button>
                </el-form-item>
              </el-form>
            </el-tab-pane>
          </el-tabs>
          <!-- <el-divider><span style="color:#999;font-weight:300">其他登录方式</span></el-divider>
          <span>敬请期待</span> -->
        </div>
      </div>
      
    </div>
  </div>
</template>

<script>
import md5 from 'js-md5'

export default {
  data () {
    return {
      rules: {
        phonenumber: [
          { required: true, message: '请输入账号', trigger: 'blur' },
        ],
        password: [
          { required: true, message: '请输入验证码', trigger: 'blur' }
        ],
      },
      rules2:{
        phonenumber: [
          { required: true, message: '请输入账号', trigger: 'blur' },
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ],
      },
      loginForm:{
        phonenumber:''
      },
      activeName:'first',
      checked:false,
      count:0
    }
  },
  mounted () {
    if (localStorage.getItem('phonenumber')) {
      this.loginForm.phonenumber = localStorage.getItem('phonenumber')
      this.checked = true
    }
  },
  methods:{
    //验证码请求
    loginCode () {
      this.$api.loginCode({
        "phonenumber":this.loginForm.phonenumber,
        "systemclient": "web"
      }).then(res => {
        console.log(res)
        const h = this.$createElement;
        res.code === 1?this.$notify({
          title: '获取验证码成功!',
          message: h('i', { style: 'color: teal'}, res.msg)
        }):this.$notify({
          title: '失败',
          message: res.msg,
          type: 'error'
        })
      });
    },
    // 获取验证码计时
    async countDown () {
      await this.loginCode()
      this.count = 5
      var down = setInterval(()=>{
        this.count --
        this.count === 0? clearInterval(down):''
      },1000)
    },
    // 登录请求
    async login () {
      const res = await this.$api.login({
        "phonenumber":this.loginForm.phonenumber,
        "password":md5(this.loginForm.password),  
        "systemclient": "web"
      })
      let that = this
      if (res.code === 1) {
        this.checked?localStorage.setItem('phonenumber',this.loginForm.phonenumber):localStorage.clear('phonenumber')
        this.$router.push('/accounts')
        sessionStorage.setItem('account_list',JSON.stringify(res.account_list))
      } else {
        this.$notify({
          title: '失败',
          message:res.msg,
          type: 'error'
        })
      }
    },
    // 账号密码登录
    async loginbyaccount () {
      const res = await this.$api.loginbyaccount({
        "accountno": this.loginForm.phonenumber,
        "password": md5(this.loginForm.password),
        "systemclient":"web"
      })
      if (res.code === 1) {
        this.checked?localStorage.setItem('phonenumber',this.loginForm.phonenumber):localStorage.clear('phonenumber')
        this.$router.push('/accounts')
        sessionStorage.setItem('account_list',JSON.stringify(res.account_list))
      } else {
        this.$notify({
          title: '失败',
          message: res.msg,
          type: 'error'
        })
      }
    }
  }
}

</script>
<style>

</style>
<style scoped>
.logo{
  text-align: center;
  margin-bottom: 30px;
}
.logo span{
  display: block;
  font-size: 24px;
  line-height: 15px;

}
.logo small{
  color:#999
}
.title{
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 33px;
  margin: 0 0 30px;
  color: #202d40;
  font-size: 24px;
  font-weight: 600;
}
.login-wrap{
  display: flex;
  align-items: center;
  justify-content: space-around;
  width: 100%;
  height: 100vh;
  /* background: #fff no-repeat 50% url(../../assets/bg.png); */
  background: #f1f2f3;
  background-size: 2560px 1490px;
}
.login-form{
  /* min-height: 500px; */
  width: 400px;
  padding: 30px;
  background: #fff;
  border-radius: 10px;
}
.form-input-group{
  margin-bottom: 16px
}
.input-wrap{
  width: 100%;
}
.d-input{
  height: 40px;
  width: 100%;
}
.flex-between{
  display: flex;
  align-items: center;
  justify-content: space-between;
}
</style>