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 ="/fc/admin";
            }
            else if(response.data.status=="adminfailed"){
              alert("Invalid login credentials");
              window.location.href ="/fc/admin/login";
            }
            else if(response.data.status=="organizationsuccess"){
              window.location.href="/fc/organizations/dashboard"; 
             
            }
            else if(response.data.status=="organizationfailed"){
              alert("Invalid login credentials");
              window.location.href="/fc/organization/login";
            }
            else if(response.data.status=="usersuccess"){
            
            //  window.location.href="/customeruser/otp?sent=new";
              window.location.href="/fc/user";
              //console.log("success");
            }
            else if(response.data.status=="userfailed"){
             // console.log("failed");
             alert("Invalid login credentials");
              window.location.href="/fc/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="/fc/customer";
              }
              else if(response.data=="cust_failed"){
                alert("invalid code");
               
                //swal("Failed!", "Invalid verification code!", "error");
                window.location.href="/fc/customer/login";
                
              }
             

           
          }).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="/fc/user";
          }
          else if(response.data=="user_failed"){
           // swal("Failed!", "Invalid verification code!", "error");
            //alert("Invalid code");
            window.location.href="/fc/user/login";
            
          }
            

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