$( document ).ready(function() {
    
    // console.log( "ready!" );
 // Toast Show
$.fn.Toastshow = function(error_type,message_data) { 
	toastr.options = {
	  "closeButton": true,
	  "debug": false,
	  "newestOnTop": true,
	  "progressBar": true,
	  "positionClass": "toast-bottom-right",
	  "preventDuplicates": false,
	  "onclick": null,
	  "showDuration": "300",
	  "hideDuration": "1000",
	  "timeOut": "5000",
	  "extendedTimeOut": "1000",
	  "showEasing": "swing",
	  "hideEasing": "linear",
	  "showMethod": "fadeIn",
	  "hideMethod": "fadeOut",
      
	}
	if(error_type == "success"){
	  toastr.success(message_data, 'Success');
	}
	else{
		toastr.error(message_data, 'Error!');	
	}
 }
 // Error or success message 
 $.fn.printErrorMsg = function(msg) { 
 			msg_value = "";
            $.each( msg, function( key, value ) {
                msg_value = msg_value+ value+'<br />';
            });
         return  msg_value;   
   }
   


  









    //Form Image With Data
   $("body").on("submit",".ajaxformfileupload", function(event){
    event.preventDefault();
        var formdata   = $(this);
       var dataValue  = new FormData(this);//new FormData(formdata[0]);
       var UrlValue   = $(this).attr('action');

       var ButtonText = $(this).find('button[type="submit"]').html();
       
       $(this).find('.btn-primary').prop('disabled', true);
       $(this).find('.btn-primary').html('Loading ...');

       
       $(this).find('button[type="submit"]').prop('disabled', true);
       $(this).find('button[type="submit"]').html('Loading ...');

       //alert(dataValue);
       $.ajax({
           url     : UrlValue,
           method  : 'post',
           data    :dataValue,
           headers:
           {
               'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
           },
           processData: false,
           contentType: false,
           beforeSend: function( xhr ) {
               
           },
           success : function(response){
               formdata.find('.btn-primary').prop('disabled', false);
               formdata.find('.btn-primary').html(ButtonText);
               formdata.find('button[type="submit"]').prop('disabled', false);
               formdata.find('button[type="submit"]').html(ButtonText);

               //console.log(response);
               if(response.success){
                   var ErroMsg = $(this).printErrorMsg(response.message);
                       //
                   $(this).Toastshow('success',ErroMsg);
                   if(response.data.current_step==2)
                   {
                       //console.log(response);
                       $("#step-1").css("display", "none");
                       $("#step-2").css("display", "block");
                       $("#company").addClass("active");
                       if(response.data.distributors=='2')
                       {
                        console.log('distributors2');
                            $('.distributors2').css("display", "block");
                             $('.distributors1').css("display","none");
                       }
                       else
                       {
                         console.log('distributors1');
                             $('.distributors2').css("display", "none");
                             $('.distributors1').css("display","block");
                       }
                       $('#distributors_id').val(response.data.distributors);
                       $('#second_step_user_id').val(response.data.user_id);
                   }
                   else if(response.data.current_step==3)
                   {
                        $("#step-1").css("display", "none");
                        $("#step-2").css("display", "none");
                        $("#step-3").css("display", "block");
                        $("#company").addClass("active");
                       if(response.data.distributors=='1')
                       {
                            $('.step3distributors_1').css("display", "block");
                            $('.step3distributors_2').css("display", "none");
                       }
                       else if(response.data.distributors=='2')
                       {
                            $('.step3distributors_1').css("display", "none");
                            $('.step3distributors_2').css("display", "block");
                       }
                       $('#distributors_id_step_3').val(response.data.distributors);
                       $('#third_step_user_id').val(response.data.user_id);
                       $('#listing_id').val(response.data.listing_id);
                   }
                   if(response.data.redirect){
                        window.location.href = response.data.redirect;
                       
                   }
                   
                   if(response.data.hide_id){

                        $('#'+response.data.hide_id).fadeOut(300);
                   }

               }
               else
               {
                        if(response.step==2)
                        {
                            $("#step-1").css("display", "none");
                               $("#step-2").css("display", "block");
                               $("#company").addClass("active");
                               if(response.distributors=='2')
                               {
                                    $('.distributors2').css("display", "block");
                                    $('.distributors1').css("display","none");
                               }
                        }

                        if(response.step==3)
                        {
                            $("#step-1").css("display", "none");
                            $("#step-2").css("display", "none");
                            $("#step-3").css("display", "block");
                            $("#company").addClass("active");
                        }
                       var ErroMsg = $(this).printErrorMsg(response.message);
                       //
                       $(this).Toastshow('error',ErroMsg);
               }

           },
           error: function(data) {
               console.log("error ",data);
               console.log(ButtonText);
               formdata.find('button[type="submit"]').prop('disabled', false);
               formdata.find('button[type="submit"]').html(ButtonText);
           }
       });
       //return false;
//    }
});














    
});