export default {
    data() {
      return {
        fields: {},
        errors: {},
        success: false,
        loaded: true,
        action: '', //login action
        action2:''
      }
    },
      methods: {
  
      login() {
        
        if (this.loaded) {
          this.loaded = false;
          this.success = false;
          this.errors = {};
          axios.post(this.action, this.fields).then(response => {
            this.fields = {}; //Clear login input fields.
            this.loaded = true;
            this.success = true;
            //redirect
            if(response.data.status=="adminsuccess"){
              //redirect to the dashboard
              window.location.href ="/admin";
            }
            else if(response.data.status=="adminfailed"){
              alert("Invalid login credentials");
              window.location.href ="/admin/login";
            }
            else if(response.data.status=="customersuccess"){
              //console.log(response.data.textstate);
              window.location.href="/customer/otp?sent=new"; 
             // window.location.href="/customer";
             
            }
            else if(response.data.status=="customerfailed"){
              alert("Invalid login credentials");
              window.location.href="/customer/login";
            }
            else if(response.data.status=="usersuccess"){
            
             window.location.href="/customeruser/otp?sent=new";
              //window.location.href="/user";
              //console.log("success");
            }
            else if(response.data.status=="userfailed"){
             // console.log("failed");
             alert("Invalid login credentials");
              window.location.href="/login";
            }           
            else if(response.data.status=="notconnected"){
            
              console.log("not connected");
            }
          }).catch(error => {
            this.loaded = true;
            if (error.response.status === 422) {
              this.errors = error.response.data.errors || {};
              
            }
          });
        }
      },
      
      verifyCustomer() {
          if (this.loaded) {
          this.loaded = false;
          this.success = false;
          this.errors = {};
          axios.post(this.action2, this.fields).then(response => {
            this.fields = {}; //Clear login input fields.
            this.loaded = true;
            this.success = true;
            var back = this.redirect;
           
              if(response.data=="cust_success"){
              
                window.location.href="/bmp/customer";
              }
              else if(response.data=="cust_failed"){
                alert("invalid code");
               
                //swal("Failed!", "Invalid verification code!", "error");
                window.location.href="/home";
                
              }
             

           
          }).catch(error => {
            this.loaded = true;
            if (error=== 422) {
              this.errors = error || {};
              
            }
          });
        }
        
      },
      verifyUser() {
        if (this.loaded) {
        this.loaded = false;
        this.success = false;
        this.errors = {};
        axios.post(this.action2, this.fields).then(response => {
          this.fields = {}; //Clear login input fields.
          this.loaded = true;
          this.success = true;
         
          if(response.data=="user_success"){
              
            window.location.href="/bmp/user";
          }
          else if(response.data=="user_failed"){
           // swal("Failed!", "Invalid verification code!", "error");
            //alert("Invalid code");
            window.location.href="/home";
            
          }
            

         
        }).catch(error => {
          this.loaded = true;
          if (error=== 422) {
            this.errors = error|| {};
            
          }
        });
      }
      
    }   
    },
  }