<template>
  <div>
    <div class="weui-toptips_warn" v-if="logShow">{{errlog}}</div>

    <div class="weui-cell weui-cell_active" >
      <div class="weui-cell__hd"><label class="weui-label">用户名</label></div>
      <div class="weui-cell__bd weui-flex">
        <input  class="weui-input" autofocus="" type="text" pattern="" placeholder="请输入用户名" maxlength="16" v-model="username">
        <button  class="weui-btn_reset weui-btn_icon weui-btn_input-clear" @click="clearName" >
          <i class="weui-icon-clear" ></i>
        </button>
      </div>
    </div>

    <div class="weui-cell weui-cell_active">
      <div class="weui-cell__hd"><label class="weui-label">密码</label></div>
      <div class="weui-cell__bd weui-flex">
        <input class="weui-input" autofocus="" type="password" pattern="" placeholder="请输入密码" maxlength="16" v-model="password" >
        <button class="weui-btn_reset weui-btn_icon weui-btn_input-clear" @click="clearPass">
          <i class="weui-icon-clear" ></i>
        </button>
      </div>
    </div>

    <a class="weui-btn weui-btn_primary weui-btn_enabled" href="javascript:" @click="comfirm">登录</a>
    <a href="javascript:" class="weui-btn weui-btn_disabled weui-btn_primary" @click="signUp">注册</a>

  </div>
</template>

<script>
import HttpKit from '@/utils/http-kit'
export default {
  components: {
  },
  data() {
    return {
      username:'',
      password:'',
      errlog: '123',
      logShow: false,
    }
  },
  methods:{
    comfirm () {
      console.log(this.username+" "+this.password)

      HttpKit.post('/wx/user/login',{
          password: this.password,
          username: this.username
        })
        .then(res => {
            if(res.data.code==200){
            console.log('110'+res.data.data.id)
            console.log('111'+JSON.stringify(res.data.data.id))
              wx.setStorageSync('uid', JSON.stringify(res.data.data.id));
              wx.switchTab({ url: '../practise/frontPractise/main'})
            }else if(res.data.code==111){
              this.errlog = '密码错误'
              this.logShow = true
            }
            console.log(res)
        })
        .catch(err => {
          this.errlog = '用户不存在'
          this.logShow = true
          console.log(err)
        })

    },

    signUp(){
        HttpKit.post('/wx/user/register',{
          nickname: 'xx',
          password: this.password,
          username: this.username
        })
        .then(res => {
            if(res.data.code==200){

              wx.setStorageSync('uid', JSON.stringify(res.data.data.id));
              wx.switchTab({ url: '../practise/frontPractise/main'})
            }else{
              this.errlog = '用户已存在'
              this.logShow = true
            }
        })
        .catch(err => {
          console.log(err)
        })
    },

    clearName(){
      this.username = ''
    },
    clearPass(){
      this.password = ''
    }
  },

}
</script>

<style>
.weui-btn_input-clear {
    padding-left: 8px;
}
.weui-btn_icon {
    font-size: 0;
}
.weui-btn_reset::after {
    background: transparent;
    border: 0;
    padding: 0;
    outline: 0;
}
.weui-toptips_warn {
    background-color: #fa5151;
    background-color: var(--weui-RED);
}


</style>
