//login
$("#loginForm").on('submit',function(e){
   var form_data = $(this).serialize();
    
    var email = $("#email").val();
    var password = $("#password").val();
    if(email !== '' && password !== ''){
      
      $("#loginBtn").html('<span class="spinner-border spinner-border-sm text-light" role="status" aria-hidden="true"></span> Checking...');
       $.ajax({ //make ajax request to cart_process.php
          url: "process/login.php",
              type: "POST",
              //dataType:"json", //expect json value from server
              data: form_data
          }).done(function(dataResult){ //on Ajax success
           
             $("#loginResponse").fadeIn();
            $("#loginBtn").html('Login');
            var data = JSON.parse(dataResult);
            if(data.code == 1){
               $("#loginResponse").html('<p class="alert alert-success text-center">'+data.msg+'</p>');
               setTimeout(function(){
                 window.location = "index";
               },1000);
            }else if(data.code == 2){
               $("#loginResponse").html('<p class="alert alert-danger text-center">'+data.msg+'</p>');
              setTimeout(function(){
                  $("#loginResponse").fadeOut();
              },1500);
            }else{
              $("#loginResponse").html('<p class="alert alert-danger text-center">Unknown error occured try again later!</p>');
               setTimeout(function(){
                  $("#loginResponse").fadeOut();
              },1500);
            }

       });
     
    }else{
      $("#loginResponse").html('<p class="alert alert-danger text-center">Both fields are required</p>');
       setTimeout(function(){
                  $("#loginResponse").fadeOut();
              },1500);
    }
    e.preventDefault();
    e.stopImmediatePropagation();
});

//add user
$("#addUserForm").on('submit',function(e){
   var form_data = $(this).serialize();
    
    var email = $("#email").val();
    var password = $("#password").val();
    var fullname = $("#fullname").val();
    var phone = $("#phone").val();
    if(email !== '' && password !== '' && phone !== '' && fullname !== ''){
      
      $("#addUserBtn").html('<span class="spinner-border spinner-border-sm text-light" role="status" aria-hidden="true"></span> Saving...');
       $.ajax({ //make ajax request to cart_process.php
          url: "process/users.php",
              type: "POST",
              //dataType:"json", //expect json value from server
              data: form_data
          }).done(function(dataResult){ //on Ajax success
           
             $("#userAddResponse").fadeIn();
            $("#addUserBtn").html('Save Changes');
            var data = JSON.parse(dataResult);
           
           $("#email").val(" ");
           $("#password").val("");
           $("#fullname").val(" ");
           $("#phone").val(" ");
           
            if(data.code == 1){
               $("#userAddResponse").html('<p class="alert alert-success text-center">'+data.msg+'</p>');
               setTimeout(function(){
                 window.location = "users";
               },1000);
            }else if(data.code == 2){
               $("#userAddResponse").html('<p class="alert alert-danger text-center">'+data.msg+'</p>');
              setTimeout(function(){
                  $("#userAddResponse").fadeOut();
              },1500);
            }else{
              $("#userAddResponse").html('<p class="alert alert-danger text-center">Unknown error occured try again later!</p>');
               setTimeout(function(){
                  $("#userAddResponse").fadeOut();
              },1500);
            }

       });
     
    }else{
      $("#userAddResponse").html('<p class="alert alert-danger text-center">All fields are required</p>');
       setTimeout(function(){
                  $("#userAddResponse").fadeOut();
              },1500);
    }
    e.preventDefault();
    e.stopImmediatePropagation();
});

//edit user
function editUser(id){
    $("#editUserForm"+id).on('submit',function(e){
   var form_data = $(this).serialize();
    
    var email = $("#eemail").val();
    var eUid = id;
    var fullname = $("#efullname").val();
    var phone = $("#ephone").val();
    if(email !== '' && eUid !== '' && phone !== '' && fullname !== ''){
      
      $("#editUserBtn"+id).html('<span class="spinner-border spinner-border-sm text-light" role="status" aria-hidden="true"></span> Saving...');
       $.ajax({ //make ajax request to cart_process.php
          url: "process/users.php",
              type: "POST",
              //dataType:"json", //expect json value from server
              data: form_data
          }).done(function(dataResult){ //on Ajax success
           
             $("#userEditResponse"+id).fadeIn();
            $("#editUserBtn"+id).html('Save Changes');
            var data = JSON.parse(dataResult);
           
           $("#eemail").val(" ");
           $("#epassword").val("");
           $("#efullname").val(" ");
           $("#ephone").val(" ");
           
            if(data.code == 1){
               $("#userEditResponse"+id).html('<p class="alert alert-success text-center">'+data.msg+'</p>');
               setTimeout(function(){
                 window.location = "users";
               },1000);
            }else if(data.code == 2){
               $("#userEditResponse"+id).html('<p class="alert alert-danger text-center">'+data.msg+'</p>');
              setTimeout(function(){
                  $("#userEditResponse"+id).fadeOut();
              },1500);
            }else{
              $("#userEditResponse"+id).html('<p class="alert alert-danger text-center">Unknown error occured try again later!</p>');
               setTimeout(function(){
                  $("#userEditResponse"+id).fadeOut();
              },1500);
            }

       });
     
    }else{
      $("#userEditResponse"+id).html('<p class="alert alert-danger text-center">All fields are required</p>');
       setTimeout(function(){
                  $("#userEditResponse"+id).fadeOut();
              },1500);
    }
    e.preventDefault();
    e.stopImmediatePropagation();
});

}
    
//delete user
function deleteUser(uid){
    $("#delBtn"+uid).html('<span class="spinner-border spinner-border-sm text-light" role="status" aria-hidden="true"></span> Deleting...');
     $.ajax({
        type: "POST",
        url: "process/users.php",
        data: {
            uid: uid
        },
        success : function(dataResult){
            $("#userDelResponse"+uid).fadeIn();
            $("#addUserBtn"+uid).html('Save Changes');
            var data = JSON.parse(dataResult);
            if(data.code == 1){
               $("#userDelResponse"+uid).html('<p class="alert alert-success text-center">'+data.msg+'</p>');
               setTimeout(function(){
                 window.location = "users";
               },1000);
            }else{
                $("#userDelResponse"+uid).html('<p class="alert alert-success text-center">'+data.msg+'</p>');
                $("#userDelResponse"+uid).fadeOut();
            }
        }
    });
}

//add advert
$("#addAdvertForm").on('submit',function(e){
   var form_data = new FormData(this);
      
      $("#addSliderBtn").html('<span class="spinner-border spinner-border-sm text-light" role="status" aria-hidden="true"></span> Saving...');
        
        $.ajax({ //make ajax request to cart_process.php
          url: 'process/adverts.php',
          type: 'post',
          data : form_data,
          contentType: false,
          cache: false,
          processData:false,
          success: function(dataResult){ //on Ajax success
             $("#sliderAddResponse").fadeIn();
            $("#addSliderBtn").html('Save');
            var data = JSON.parse(dataResult);
          
            if(data.code == 1){
               $("#sliderAddResponse").html('<p class="alert alert-success text-center">'+data.msg+'</p>');
               setTimeout(function(){
                 window.location = "adverts";
               },1000);
            }else if(data.code == 2){
               $("#sliderAddResponse").html('<p class="alert alert-danger text-center">'+data.msg+'</p>');
              setTimeout(function(){
                  $("#sliderAddResponse").fadeOut();
              },1500);
            }else{
              $("#sliderAddResponse").html('<p class="alert alert-danger text-center">Unknown error occured try again later!</p>');
               setTimeout(function(){
                  $("#sliderAddResponse").fadeOut();
              },1500);
            }
              },
       });

    e.preventDefault();
    e.stopImmediatePropagation();
});

//delete advert
function deleteAdvert(id){
    $("#delBtn"+id).html('<span class="spinner-border spinner-border-sm text-light" role="status" aria-hidden="true"></span> Deleting...');
     $.ajax({
        type: "POST",
        url: "process/adverts.php",
        data: {
            id: id
        },
        success : function(dataResult){
            console.log(dataResult);
            $("#sliderDelResponse"+id).fadeIn();
            $("#delBtn"+id).html('Save Changes');
            var data = JSON.parse(dataResult);
            if(data.code == 1){
               $("#sliderDelResponse"+id).html('<p class="alert alert-success text-center">'+data.msg+'</p>');
               setTimeout(function(){
                 window.location = "adverts";
               },1000);
            }else{
                $("#sliderDelResponse"+id).html('<p class="alert alert-success text-center">'+data.msg+'</p>');
                $("#sliderDelResponse"+id).fadeOut();
            }
        }
    });
}

//landing page
//add
$("#addSliderForm").on('submit',function(e){
   var form_data = new FormData(this);
    
    var caption1 = $("#caption1").val(); 
    var caption2 = $("#caption2").val();
    
    if(caption1 !== "" && caption2 !== ""){
             $("#addSliderBtn").html('<span class="spinner-border spinner-border-sm text-light" role="status" aria-hidden="true"></span> Saving...');
        
        $.ajax({ //make ajax request to cart_process.php
          url: 'process/landing_page.php',
          type: 'post',
          data : form_data,
          contentType: false,
          cache: false,
          processData:false,
          success: function(dataResult){ //on Ajax success
             $("#sliderAddResponse").fadeIn();
            $("#addSliderBtn").html('Save');
            var data = JSON.parse(dataResult);
          
            if(data.code == 1){
               $("#sliderAddResponse").html('<p class="alert alert-success text-center">'+data.msg+'</p>');
               setTimeout(function(){
                 window.location = "landing_page";
               },1000);
            }else if(data.code == 2){
               $("#sliderAddResponse").html('<p class="alert alert-danger text-center">'+data.msg+'</p>');
              setTimeout(function(){
                  $("#sliderAddResponse").fadeOut();
              },1500);
            }else{
              $("#sliderAddResponse").html('<p class="alert alert-danger text-center">Unknown error occured try again later!</p>');
               setTimeout(function(){
                  $("#sliderAddResponse").fadeOut();
              },1500);
            }
              },
       });

    }else{
        $("#sliderAddResponse").html('<p class="alert alert-danger text-center">All fields are required!</p>');
    }
      
 
    e.preventDefault();
    e.stopImmediatePropagation();
});

//edit
function editSlider(id){
   $("#editSliderForm"+id).on('submit',function(e){
   var form_data = new FormData(this);
    
    var caption1 = $("#ecaption1").val(); 
    var caption2 = $("#ecaption2").val();
    
    if(caption1 !== "" && caption2 !== ""){
        $("#editCorouselBtn"+id).html('<span class="spinner-border spinner-border-sm text-light" role="status" aria-hidden="true"></span> Saving...');
        
        $.ajax({ //make ajax request to cart_process.php
          url: 'process/landing_page.php',
          type: 'post',
          data : form_data,
          contentType: false,
          cache: false,
          processData:false,
          success: function(dataResult){ //on Ajax success
             $("#sliderEditResponse"+id).fadeIn();
            $("#editCorouselBtn"+id).html('Save');
            var data = JSON.parse(dataResult);
          
            if(data.code == 1){
               $("#sliderEditResponse"+id).html('<p class="alert alert-success text-center">'+data.msg+'</p>');
               setTimeout(function(){
                 window.location = "landing_page";
               },1000);
            }else if(data.code == 2){
               $("#sliderEditResponse"+id).html('<p class="alert alert-danger text-center">'+data.msg+'</p>');
              setTimeout(function(){
                  $("#sliderEditResponse"+id).fadeOut();
              },1500);
            }else{
              $("#sliderEditResponse"+id).html('<p class="alert alert-danger text-center">Unknown error occured try again later!</p>');
               setTimeout(function(){
                  $("#sliderEditResponse"+id).fadeOut();
              },1500);
            }
                  },
           });

        }else{
            $("#sliderEditResponse"+id).html('<p class="alert alert-danger text-center">All fields are required!</p>');
        }
      

        e.preventDefault();
        e.stopImmediatePropagation();
    });
 
}

//delete
function deleteCorousel(id){
    $("#delBtn"+id).html('<span class="spinner-border spinner-border-sm text-light" role="status" aria-hidden="true"></span> Deleting...');
     $.ajax({
        type: "POST",
        url: "process/landing_page.php",
        data: {
            id: id
        },
        success : function(dataResult){
            $("#sliderDelResponse"+id).fadeIn();
            $("#delBtn"+id).html('Save Changes');
            var data = JSON.parse(dataResult);
            if(data.code == 1){
               $("#sliderDelResponse"+id).html('<p class="alert alert-success text-center">'+data.msg+'</p>');
               setTimeout(function(){
                 window.location = "landing_page";
               },1000);
            }else{
                $("#sliderDelResponse"+id).html('<p class="alert alert-success text-center">'+data.msg+'</p>');
                $("#sliderDelResponse"+id).fadeOut();
            }
        }
    });
}

//company profiles
//add
$("#addCompanyProfileForm").on('submit',function(e){
   var form_data = new FormData(this);
    
    var caption1 = $("#profile_name").val(); 
    
    if(caption1 !== "" ){
             $("#addSliderBtn").html('<span class="spinner-border spinner-border-sm text-light" role="status" aria-hidden="true"></span> Saving...');
        
        $.ajax({ //make ajax request to cart_process.php
          url: 'process/company_profiles.php',
          type: 'post',
          data : form_data,
          contentType: false,
          cache: false,
          processData:false,
          success: function(dataResult){ //on Ajax success
             $("#profileAddResponse").fadeIn();
            $("#addSliderBtn").html('Save');
            var data = JSON.parse(dataResult);
          
            if(data.code == 1){
               $("#profileAddResponse").html('<p class="alert alert-success text-center">'+data.msg+'</p>');
               setTimeout(function(){
                 window.location = "company_profiles";
               },1000);
            }else if(data.code == 2){
               $("#profileAddResponse").html('<p class="alert alert-danger text-center">'+data.msg+'</p>');
              setTimeout(function(){
                  $("#profileAddResponse").fadeOut();
              },1500);
            }else{
              $("#profileAddResponse").html('<p class="alert alert-danger text-center">Unknown error occured try again later!</p>');
               setTimeout(function(){
                  $("#profileAddResponse").fadeOut();
              },1500);
            }
              },
       });

    }else{
        $("#profileAddResponse").html('<p class="alert alert-danger text-center">All fields are required!</p>');
    }
      
 
    e.preventDefault();
    e.stopImmediatePropagation();
});

//edit
function editProfile(id){
   $("#editCompanyForm"+id).on('submit',function(e){
   var form_data = new FormData(this);
    
    var caption1 = $("#eprofile_name").val(); 
    
    if(caption1 !== ""){
        $("#editProfilelBtn"+id).html('<span class="spinner-border spinner-border-sm text-light" role="status" aria-hidden="true"></span> Saving...');
        
        $.ajax({ //make ajax request to cart_process.php
          url: 'process/company_profiles.php',
          type: 'post',
          data : form_data,
          contentType: false,
          cache: false,
          processData:false,
          success: function(dataResult){ //on Ajax success
             $("#profileEditResponse"+id).fadeIn();
            $("#editProfilelBtn"+id).html('Save');
            var data = JSON.parse(dataResult);
          
            if(data.code == 1){
               $("#profileEditResponse"+id).html('<p class="alert alert-success text-center">'+data.msg+'</p>');
               setTimeout(function(){
                 window.location = "company_profiles";
               },1000);
            }else if(data.code == 2){
               $("#profileEditResponse"+id).html('<p class="alert alert-danger text-center">'+data.msg+'</p>');
              setTimeout(function(){
                  $("#profileEditResponse"+id).fadeOut();
              },1500);
            }else{
              $("#profileEditResponse"+id).html('<p class="alert alert-danger text-center">Unknown error occured try again later!</p>');
               setTimeout(function(){
                  $("#profileEditResponse"+id).fadeOut();
              },1500);
            }
                  },
           });

        }else{
            $("#profileEditResponse"+id).html('<p class="alert alert-danger text-center">All fields are required!</p>');
        }
      

        e.preventDefault();
        e.stopImmediatePropagation();
    });
 
}

//delete
function deleteCompanyProfile(id){
    $("#delBtn"+id).html('<span class="spinner-border spinner-border-sm text-light" role="status" aria-hidden="true"></span> Deleting...');
     $.ajax({
        type: "POST",
        url: "process/company_profiles.php",
        data: {
            id: id
        },
        success : function(dataResult){
            $("#profileDelResponse"+id).fadeIn();
            $("#delBtn"+id).html('Save Changes');
            var data = JSON.parse(dataResult);
            if(data.code == 1){
               $("#profileDelResponse"+id).html('<p class="alert alert-success text-center">'+data.msg+'</p>');
               setTimeout(function(){
                 window.location = "company_profiles";
               },1000);
            }else{
                $("#profileDelResponse"+id).html('<p class="alert alert-success text-center">'+data.msg+'</p>');
                $("#profileDelResponse"+id).fadeOut();
            }
        }
    });
}

//TEAM
//add
$("#addTeamMemberForm").on('submit',function(e){
   var form_data = new FormData(this);
    
    var fullname = $("#fullname").val(); 
    var position = $("#position").val();
    var background = $("#background").val();
    
    if(fullname !== "" && position !== "" && background !== ""){
        $("#addUserBtn").html('<span class="spinner-border spinner-border-sm text-light" role="status" aria-hidden="true"></span> Saving...');
        
        $.ajax({ //make ajax request to cart_process.php
          url: 'process/team.php',
          type: 'post',
          data : form_data,
          contentType: false,
          cache: false,
          processData:false,
          success: function(dataResult){ //on Ajax success
              
             $("#memberAddResponse").fadeIn();
            $("#addUserBtn").html('Save');
            var data = JSON.parse(dataResult);
          
            if(data.code == 1){
               $("#memberAddResponse").html('<p class="alert alert-success text-center">'+data.msg+'</p>');
               setTimeout(function(){
                 window.location = "team";
               },1000);
            }else if(data.code == 2){
               $("#memberAddResponse").html('<p class="alert alert-danger text-center">'+data.msg+'</p>');
              setTimeout(function(){
                  $("#memberAddResponse").fadeOut();
              },1500);
            }else{
              $("#memberAddResponse").html('<p class="alert alert-danger text-center">Unknown error occured try again later!</p>');
               setTimeout(function(){
                  $("#memberAddResponse").fadeOut();
              },1500);
            }
              },
       });

    }else{
        $("#memberAddResponse").html('<p class="alert alert-danger text-center">All fields are required!</p>');
    }
      
 
    e.preventDefault();
    e.stopImmediatePropagation();
});

//delete
function deleteTeamMember(id){
    $("#delBtn"+id).html('<span class="spinner-border spinner-border-sm text-light" role="status" aria-hidden="true"></span> Deleting...');
     $.ajax({
        type: "POST",
        url: "process/team.php",
        data: {
            id: id
        },
        success : function(dataResult){
            $("#userDelResponse"+id).fadeIn();
            $("#delBtn"+id).html('Save Changes');
            var data = JSON.parse(dataResult);
            if(data.code == 1){
               $("#userDelResponse"+id).html('<p class="alert alert-success text-center">'+data.msg+'</p>');
               setTimeout(function(){
                 window.location = "team";
               },1000);
            }else{
                $("#userDelResponse"+id).html('<p class="alert alert-success text-center">'+data.msg+'</p>');
                $("#userDelResponse"+id).fadeOut();
            }
        }
    });
}

//edit member info
function editMember(id){
   $("#editTeamMemberForm"+id).on('submit',function(e){
   var form_data = new FormData(this);
    
    var efullname = $("#efullname").val(); 
    var eposition = $("#eposition").val(); 
    var ebackground = $("#ebackground").val(); 
    
    if(efullname !== "" && eposition !=="" && ebackground !==""){
        $("#editProfilelBtn"+id).html('<span class="spinner-border spinner-border-sm text-light" role="status" aria-hidden="true"></span> Saving...');
        
        $.ajax({ //make ajax request to cart_process.php
          url: 'process/team.php',
          type: 'post',
          data : form_data,
          contentType: false,
          cache: false,
          processData:false,
          success: function(dataResult){ //on Ajax success
             $("#profileEditResponse"+id).fadeIn();
            $("#editProfilelBtn"+id).html('Save');
            var data = JSON.parse(dataResult);
          
            if(data.code == 1){
               $("#profileEditResponse"+id).html('<p class="alert alert-success text-center">'+data.msg+'</p>');
               setTimeout(function(){
                 window.location = "team";
               },1000);
            }else if(data.code == 2){
               $("#profileEditResponse"+id).html('<p class="alert alert-danger text-center">'+data.msg+'</p>');
              setTimeout(function(){
                  $("#profileEditResponse"+id).fadeOut();
              },1500);
            }else{
              $("#profileEditResponse"+id).html('<p class="alert alert-danger text-center">Unknown error occured try again later!</p>');
               setTimeout(function(){
                  $("#profileEditResponse"+id).fadeOut();
              },1500);
            }
                  },
           });

        }else{
            $("#profileEditResponse"+id).html('<p class="alert alert-danger text-center">All fields are required!</p>');
        }
      

        e.preventDefault();
        e.stopImmediatePropagation();
    });
 
}

//change team picture
function changePic(id){
   $("#pictureForm"+id).on('submit',function(e){
   var form_data = new FormData(this);
    
    var pic_id = $("#pic_id").val(); 

    
    if(pic_id !== ""){
        $("#picBtn"+id).html('<span class="spinner-border spinner-border-sm text-light" role="status" aria-hidden="true"></span> Saving...');
        
        $.ajax({ //make ajax request to cart_process.php
          url: 'process/team.php',
          type: 'post',
          data : form_data,
          contentType: false,
          cache: false,
          processData:false,
          success: function(dataResult){ //on Ajax success
//              console.log(dataResult);
             $("#userPictureResponse"+id).fadeIn();
            $("#picBtn"+id).html('Save');
            var data = JSON.parse(dataResult);
          
            if(data.code == 1){
               $("#userPictureResponse"+id).html('<p class="alert alert-success text-center">'+data.msg+'</p>');
               setTimeout(function(){
                 window.location = "team";
               },1000);
            }else if(data.code == 2){
               $("#userPictureResponse"+id).html('<p class="alert alert-danger text-center">'+data.msg+'</p>');
              setTimeout(function(){
                  $("#userPictureResponse"+id).fadeOut();
              },1500);
            }else{
              $("#userPictureResponse"+id).html('<p class="alert alert-danger text-center">Unknown error occured try again later!</p>');
               setTimeout(function(){
                  $("#userPictureResponse"+id).fadeOut();
              },1500);
            }
                  },
           });

        }else{
            $("#userPictureResponse"+id).html('<p class="alert alert-danger text-center">All fields are required!</p>');
        }
      

        e.preventDefault();
        e.stopImmediatePropagation();
    });
 
}

//change team portfolio
function changePort(id){
   $("#PortForm"+id).on('submit',function(e){
   var form_data = new FormData(this);
    
    var pic_id = $("#port_teamid").val(); 

    
    if(pic_id !== ""){
        $("#portBtn"+id).html('<span class="spinner-border spinner-border-sm text-light" role="status" aria-hidden="true"></span> Saving...');
        
        $.ajax({ //make ajax request to cart_process.php
          url: 'process/team.php',
          type: 'post',
          data : form_data,
          contentType: false,
          cache: false,
          processData:false,
          success: function(dataResult){ //on Ajax success
//              console.log(dataResult);
             $("#userPortFolioResponse"+id).fadeIn();
            $("#portBtn"+id).html('Save');
            var data = JSON.parse(dataResult);
          
            if(data.code == 1){
               $("#userPortFolioResponse"+id).html('<p class="alert alert-success text-center">'+data.msg+'</p>');
               setTimeout(function(){
                 window.location = "team";
               },1000);
            }else if(data.code == 2){
               $("#userPortFolioResponse"+id).html('<p class="alert alert-danger text-center">'+data.msg+'</p>');
              setTimeout(function(){
                  $("#userPortFolioResponse"+id).fadeOut();
              },1500);
            }else{
              $("#userPortFolioResponse"+id).html('<p class="alert alert-danger text-center">Unknown error occured try again later!</p>');
               setTimeout(function(){
                  $("#userPortFolioResponse"+id).fadeOut();
              },1500);
            }
                  },
           });

        }else{
            $("#userPortFolioResponse"+id).html('<p class="alert alert-danger text-center">All fields are required!</p>');
        }
      

        e.preventDefault();
        e.stopImmediatePropagation();
    });
 
}
