<template>
  <div class="conatiner">
    <div class="login-wrap">
      <div>
        <div class="logo">
          <span> 营销管理平台</span><br>
          <small>SALE-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="rules2" ref="loginForm"  :model="loginForm">
                <el-form-item prop="phonenumber">
                  <el-input v-model="loginForm.phonenumber" placeholder="账号" @keyup.native.enter="loginbyaccount"></el-input>
                </el-form-item> 
                <el-form-item prop="password">
                  <el-input v-model="loginForm.password" type="password" placeholder="密码" @keyup.native.enter="loginbyaccount">
                  </el-input>
                </el-form-item>
                <el-form-item>
                  <div class="flex-between">
                    <el-checkbox v-model="checked">记住账号</el-checkbox>
                  </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-tab-pane label="短信登录" name="second">
              <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>
                  </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-tabs>
        </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
    }
  },
  created () {
    this.$store.dispatch('DrawerShowChange',false)
  },
  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')
        sessionStorage.setItem('account_list',JSON.stringify(res.account_list))
        sessionStorage.setItem('active_account',JSON.stringify(res.account_list[0]))
        this.basicData.query_userauth().then(()=>{
          this.basicData.querySite_Parameter()
          this.$router.push({path:'/main'})
          this.getAccountInfo()
        })
      } else {
        this.$notify({
          title: '失败',
          message:res.msg,
          type: 'error'
        })
       
      }
    },
    /* 获取账户信息 */
    async getAccountInfo () {
      let res = await this.$api.requested({
        "classname": "common.usercenter.usercenter",
        "method": "queryUserMsg",
        "content": {
        }
      })
      window.sessionStorage.setItem('accountinfo',JSON.stringify(res.data))
    },
    // 账号密码登录
    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')
        sessionStorage.setItem('account_list',JSON.stringify(res.account_list))

        sessionStorage.setItem('active_account',JSON.stringify(res.account_list[0]))

        this.basicData.query_userauth().then(()=>{
          this.basicData.querySite_Parameter()
          this.$router.push({path:'/main'})
          this.getAccountInfo()
        })
      } else {
        this.$notify({
          title: '失败',
          message: res.msg,
          type: 'error'
        })
      }
    },
  }
}

</script>
<style>
.login-form .el-input__inner{
  color:#fff;
  border: 1px solid #0000004d;
  background: rgba(0, 0, 0, 0.1);
  box-shadow: 2px 8px 8px 0 rgb(0 0 0 / 10%) inset;
}
.login-form .el-tabs__item{
  color: #ffffff7d;
}
.login-form  .el-tabs__item.is-active {
  color:#fff
}
.login-form  .el-tabs__nav-wrap::after {
  background-color: #e4e7ed33;
}
.login-form .el-tabs__active-bar{
  background-color: #fff;
}
.login-form .el-checkbox__input.is-checked+.el-checkbox__label {
  color:#fff
}
</style>
<style scoped>
.logo{
  text-align: center;
  margin-bottom: 30px;
  color:#fff;
}
.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: #fff;
  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: url("../../assets/wallhaven-0pq8gm.jpeg") no-repeat;
  background-size: 100% 100%;
}
.login-form{
  /* min-height: 500px; */
  width: 400px;
  padding: 30px;
  color:#fff;
  background: rgb(255, 255, 255,.2);
  backdrop-filter: blur(30px);
  border-radius: 10px;
  box-shadow: 2px 8px 8px 0 rgb(0 0 0 / 20%);
}
.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>