<template>
    <div class="container">
<div class="row justify-content-center mt-5">
        <div class="col-lg-5 col-md-7">
          <div class="card border-0 mb-0">
            <div class="card-body px-lg-5 py-lg-5">
              <div class="container d-flex justify-content-center mb-4">
                <img :src="logo" alt="..." style="max-height:100%; width: 290px;" />
              </div>
              <form role="form" ref="form">
                <div class="form-group mb-3">
                  <div class="input-group input-group-merge input-group-alternative">
                    <div class="input-group-prepend">
                      <span class="input-group-text"><i class="ni ni-email-83"></i></span>
                    </div>
                    <input id="email" type="email" class="form-control" ref="focusCu" v-model="email" required
                                           autofocus autocomplete="off" >
                  </div>
                </div>
                <div class="form-group">
                  <div class="input-group input-group-merge input-group-alternative">
                    <div class="input-group-prepend">
                      <span class="input-group-text"><i class="ni ni-lock-circle-open"></i></span>
                    </div>
                     <input id="password" type="password" class="form-control" v-model="password" v-on:keyup="sigin($event)"
                                           required autocomplete="off" >
                  </div>
                </div>
                <div class="custom-control custom-control-alternative custom-checkbox">
                  <input class="custom-control-input" id="remember" v-model="remember" type="checkbox">
                  <label class="custom-control-label" for="remember">
                    <span class="text-muted">ຈື່ຂ້ອຍໄວ້ ໃນລະບົບ</span>
                  </label>
                </div>
                <div class="text-center">
                  <button type="button" class="btn btn-primary my-4" :disabled="LoginBt" @click="handleSubmit"><span v-html="labelBt"></span></button>
                </div>
              </form>
            </div>
          </div>
          <div class="row mt-3">

          </div>
        </div>
      </div>

    </div>
</template>

<script>


export default {
    data() {
        return {
            email: "",
            password: "",
            remember:false,
            error: null,
            polling: null,
            LoginBt:false,
            logo:window.location.origin+'/images/soneitcomputer_logo.jpg',


        }
    },
    computed:{
        labelBt(){
            if(!this.LoginBt){
                return 'ເຂົ້າສູ່ລະບົບ'
            } else { return '<i class="fas fa-sync-alt fa-spin fa-1x"></i> ກຳລັງກວດຊອບ...'}
        }
    },
    methods: {
        sigin(e){
             if (e.which === 13) {
            this.handleSubmit(e);
            }
        },
        handleSubmit(e) {

           //console.log(this.$refs.form.validate())
            /*const isValid = this.$refs.formligon.validate(true)
            if (!isValid) {
            return
            } */

            if(this.email != '' && this.password !=''){
                this.LoginBt = true
                e.preventDefault()
                if (this.password.length > 0) {
                    this.$axios.get('/sanctum/csrf-cookie').then(response => {
                        this.$axios.post('api/login', {
                            email: this.email,
                            password: this.password,
                            remember:this.remember
                        })
                            .then(response => {
                               /// console.log(response.data)
                                if (response.data.success) {
                                    this.$router.go('/dashboard')
                                // this.checkA()
                                } else {
                                    this.LoginBt = false
                                    //this.error = response.data.message
                                     this.$notify({ group: "error", title: "ບໍ່ສຳເລັດ!", text: 'ຜິດຜາດ: '+response.data.message }, 4000)
                                }
                            })
                            .catch(function (error) {
                                console.error(error);
                            });
                    })
                }
            } else { this.error = 'ກະລຸນາປ້ອນ ອີເມວລ໌ ແລະ ລະຫັດຜ່ານ' }

        },
        checkA(){
            this.$axios.get(`/api/users/checkauth`).then((response) => {
                console.log(response.data.isLogin)
      if (!response.data.isLogin) {
       // window.location.href = "/";
      }
    });
        }

    },
    beforeRouteEnter(to, from, next) {
       // console.log(window.Laravel.isLoggedin)
        if (window.Laravel.isLoggedin) {
            return next('dashboard');
        }
        next();
    },
    created() {
        this.polling = setInterval(() => {
        this.$refs.focusCu.focus();
        clearInterval(this.polling);
      }, 900);
    }
}
</script>