<template>
  <div class="login">
    <span class="title">欢迎回来！</span>
    <div class="inputLabel">
      <van-icon name="manager" class="icon"/>

      <input type="text" placeholder="手机号码" v-model="form.mobile">
    </div>
    <div class="inputLabel" v-if="type == '1'">
      <van-icon name="lock" class="icon"/>
      <input type="password" placeholder="密码" v-model="password1" >
    </div>
    <div class="inputLabel" v-if="type == '2'">
      <van-icon name="lock" class="icon"/>
      <input type="text" placeholder="手机验证码" v-model="password2">
      <div class="code" @click="goTimeOver">{{ over?'获取验证码':number }}</div>
    </div>
    <span class="change" @click="changeType" v-if="type == '1'">手机验证码登录</span>
    <span class="change" @click="changeType"v-if="type == '2'">密码登录</span>
    <div class="btn" @click="goLogin">
      <span>登录</span>
    </div>
  </div>
</template>

<script>
export default {
  name: "login",
  methods:{
    changeType(){
      this.type = this.type == '1'?'2':'1'
    },
    goLogin(){
      let params = {
        ...this.form,
        securityCode:this.type == '1'?this.password1:this.password2,
        type:this.type
      }
      this.$api.login(params).then(res=>{
        console.log(res)
        this.$router.push({name:'home'})
      })

    },
    goTimeOver(){
      if(this.over){

        this.$api.verificationCode({mobile:this.form.mobile}).then(res=>{
          this.number = 60
          this.over = false
          this.timer = setInterval(()=>{
            this.number --
            if(this.number == 0){
              this.over = true
              clearInterval(this.timer)
            }
          },1000)
        })
      }else{

      }

    },
  },
  data(){
    return {
      form:{
        mobile:'',
        securityCode:''
      },
      password1:'',
      password2:'',
      type:'1',
      timer:null,
      over:true,
      number:60
    }
  },
}
</script>

<style scoped lang="less">
.login{
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  .inputLabel{
    margin-bottom: 20px;
    position: relative;
    .icon{
      position: absolute;
      left:15px;top:12px;
      font-size: 22px;
    }
    input{
      width: 219px;
      padding: 0 50px;
      height: 48px;
      background: #F4F5F6;
      border-radius: 8px;
      opacity: 0.8;
      border: none;
      outline: none;
    }
    .code{
      position: absolute;
      background-color: #6C5DD3;
      color: white;
      right: 0;top:0;
      display: flex;
      align-items: center;
      justify-content: center;
      height: 30px;
      padding: 10px 20px;
      border-radius: 8px;
    }
  }
  .change{
    margin-bottom: 20px;
    display: inline-block;
    width: 90%;
    text-align: right;
    margin-right: 10%;
  }
  .btn{
    width: 319px;
    height: 56px;
    background: #6C5DD3;
    border-radius: 16px;
    text-align: center;
    line-height: 56px;
    color: white;
  }
  .title{
    width: 90%;
    display: inline-block;
    text-align: left;
    height: 36px;
    font-size: 28px;
    font-family: PingFangSC-Medium, PingFang SC;
    font-weight: 500;
    color: #01040D;
    line-height: 36px;
    margin-left: 10%;
    margin-bottom: 60px;
    margin-top: 30px;
  }
}
</style>