$( 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;   
   }
   


   /*********change status**********/
    $("body").on("click",'.changeStatus', function(event){
        dataString       = {"id":$(this).data('id'),"status":$(this).data('status')};  
        var UrlValue     = $(this).data('url');
        var status       = $(this).data('status');
        var changeStatus = $(this);

        Swal.fire({
            title: 'Are you sure you want to change status?',
            icon: 'warning',
            showDenyButton: true,
            showCancelButton: false,
            confirmButtonText: 'Yes',
            denyButtonText: 'No',
        }).then((result) => {
            /* Read more about isConfirmed, isDenied below */
            if (result.isConfirmed) {
                $.ajax({
                    url     : UrlValue,
                    method  : 'post',
                    data    :dataString,
                    headers:
                    {
                        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                    },
                    beforeSend: function( xhr ) {
                        // xhr.overrideMimeType( "text/plain; charset=x-user-defined" );
                    },
                    success : function(response){
                        if(response.success){
                            var ErroMsg = $(this).printErrorMsg(response.message);
                            $(this).Toastshow('success',ErroMsg);

                            setTimeout(function(){ location.reload(); }, 1000);
                        } else {
                            var ErroMsg = $(this).printErrorMsg(response.message);
                            if (ErroMsg === '') {
                                ErroMsg = "Something went wrong!";
                            }
                            $(this).Toastshow('error',ErroMsg);

                            if (status == 1) {
                                changeStatus.parent().find('.ui-switcher').attr('aria-checked', false);
                            } else {
                                changeStatus.parent().find('.ui-switcher').attr('aria-checked', true);
                            }
                        }
                    },
                    error: function (data) {
                        console.log("error ",data);

                        if (status == 1) {
                            changeStatus.parent().find('.ui-switcher').attr('aria-checked', false);
                        } else {
                            changeStatus.parent().find('.ui-switcher').attr('aria-checked', true);
                        }
                    }   
                });
            }
            else if (result.isDenied) {
                if (status == 1) {
                    changeStatus.parent().find('.ui-switcher').attr('aria-checked', false);
                } else {
                    changeStatus.parent().find('.ui-switcher').attr('aria-checked', true);
                }
            }
            //return false;
        });
    //return false;
    });



    /*********Menu Module status**********/
    $("body").on("click",'.changeStatusModule', function(event){
        dataString    = {"id":$(this).data('id'),"status":$(this).data('status')};  
        var UrlValue  = $(this).data('url');
        //alert(dataValue);
        // console.log(UrlValue);
        // console.log(dataString);
        Swal.fire({
            title: 'Are you sure you want to change status?',
            icon: 'warning',
            showDenyButton: true,
            showCancelButton: false,
            confirmButtonText: 'Yes',
            denyButtonText: 'No',
        }).then((result) => {
            /* Read more about isConfirmed, isDenied below */
            if (result.isConfirmed) {
                $.ajax({
                    url     : UrlValue,
                    method  : 'post',
                    data    :dataString,
                    headers:
                    {
                        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                    },
                    beforeSend: function( xhr ) {
                        // xhr.overrideMimeType( "text/plain; charset=x-user-defined" );
                    },
                    success : function(response){
                        // console.log(response.data);
                        // console.log(response);
                        // if($.isEmptyObject(response.success)){
                        if(response.success){
                            var ErroMsg = $(this).printErrorMsg(response.message);
                                //
                            $(this).Toastshow('success',ErroMsg);
                            if(response.data.redirect){
                                // alert(response.data.redirect);
                                 window.location.href = response.data.redirect;
                            }

                        }else{
                                var ErroMsg = $(this).printErrorMsg(response.message);
                                //
                                $(this).Toastshow('error',ErroMsg);
                        }
                    },
                    error: function (data) {
                      console.log("error ",data);
                    }   
                });
            }
            else if (result.isDenied) {
            
            }
            //return false;
        });
    //return false;
    });




    /*********delete data**********/
    $("body").on("click",'.deleteData', function(event){
        dataString    = {"id":$(this).data('id')};  
        var UrlValue  = $(this).data('url');
        //alert(dataValue);
        console.log($('meta[name="csrf-token"]').attr('content'));
        console.log(dataString); 

        Swal.fire({
            title: 'Are you sure you want to delete this?',
            icon: 'warning',
            showDenyButton: true,
            showCancelButton: false,
            confirmButtonText: 'Yes',
            denyButtonText: 'No',
        }).then((result) => {
            /* Read more about isConfirmed, isDenied below */
            if (result.isConfirmed) {
                $.ajax({
                url     : UrlValue,
                method  : 'post',
                data    :{
                        "_token": $('meta[name="csrf-token"]').attr('content'),
                        "id": $(this).data('id')
                },
                headers:
                {
                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                },
                beforeSend: function( xhr ) {
                    // xhr.overrideMimeType( "text/plain; charset=x-user-defined" );
                },
                success : function(response){
                    console.log(response.data);
                    if(response.success){
                        var ErroMsg = $(this).printErrorMsg(response.message);
                            //
                        $(this).Toastshow('success',ErroMsg);
                        if(response.data.redirect){
                            // alert(response.data.redirect);
                             window.location.href = response.data.redirect;
                        }
                        if(response.data.reload){
                            // alert(response.data.redirect);
                             window.location.reload();
                        }

                    }else{
                            var ErroMsg = $(this).printErrorMsg(response.message);
                            //
                            $(this).Toastshow('error',ErroMsg);
                    }
                },
                error: function (data) {
                  console.log("error ",data);
                }   
            });
            }
            else if (result.isDenied) {
            
            }
            //return false;
        });
    });



    //Form Image With Data
   $("body").on("submit",".ajaxformfileupload", function(event){
    event.preventDefault();
    // if($(this).parsley().isValid() ) {
        // alert(11);
        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('button').prop('disabled', true);
       $(this).find('button').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){
               //
               // alert(ButtonText);
               /*formdata.find('button').prop('disabled', false);
               formdata.find('button').html(ButtonText);*/

               formdata.find('button[type="submit"]').prop('disabled', false);
               formdata.find('button[type="submit"]').html(ButtonText);

               console.log(response);
               // if($.isEmptyObject(response.success)){
               if(response.success){
                   var ErroMsg = $(this).printErrorMsg(response.message);
                       //
                   $(this).Toastshow('success',ErroMsg);
                   if(response.data.redirect){
                       // alert(response.data.redirect);
                        window.location.href = response.data.redirect;
                   }
                   if(response.data.reload){
                       setTimeout(function(){ location.reload(); }, 3000);
                   }
                   if(response.data.hide_id){

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

               } else {
                       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;
//    }
});




  /*********delete book issue data**********/
  $("body").on("click",'.deletebookData', function(event){
    var dataString    = $(this).data('id');  
    var mainid    = $(this).data('mainid');  
    var UrlValue  = $(this).data('url');
    console.log(dataString,'id'); 
    console.log(mainid,'mainid'); 
    console.log(UrlValue,'UrlValue'); 
    Swal.fire({
        title: 'Are you sure you want to delete this?',
        icon: 'warning',
        showDenyButton: true,
        showCancelButton: false,
        confirmButtonText: 'Yes',
        denyButtonText: 'No',
    }).then((result) => {
        /* Read more about isConfirmed, isDenied below */
        if (result.isConfirmed) {
            if(mainid)
            {
                $.ajax({
                    url     : UrlValue,
                    method  : 'post',
                    data    :{
                            "_token": $('meta[name="csrf-token"]').attr('content'),
                            "id": mainid
                    },
                    headers:
                    {
                        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                    },
                    beforeSend: function( xhr ) {
                        // xhr.overrideMimeType( "text/plain; charset=x-user-defined" );
                    },
                    success : function(response){
                        $(this).closest("tr").remove();
                        console.log(response.data);
                        if(response.success){
                            var ErroMsg = $(this).printErrorMsg(response.message);
                                //
                            $(this).Toastshow('success',ErroMsg);
                            $(this).closest("tr").remove();
                            if(response.data.redirect){
                                // alert(response.data.redirect);
                                 //window.location.href = response.data.redirect;
                                 window.location.reload();
                                 $(this).closest("tr").remove();
                            }
                            if(response.data.reload){
                                // alert(response.data.redirect);
                                 window.location.reload();
                                 $(this).closest("tr").remove();
                            }
    
                        }else{
                                var ErroMsg = $(this).printErrorMsg(response.message);
                                //
                                $(this).Toastshow('error',ErroMsg);
                        }
                    },
                    error: function (data) {
                      console.log("error ",data);
                    }   
                });
            }
            else
            {
                $(this).closest("tr").remove();
            }
           
            
         
        }
        else if (result.isDenied) {
    
        }
        //return false;
    });
});







 /*********Create Libraycard issue data**********/
 $("body").on("submit",'.librarycardAjax', function(event){
    event.preventDefault();
    // if($(this).parsley().isValid() ) {
        // alert(11);
        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('button').prop('disabled', true);
       $(this).find('button').html('Loading ...');*/
       $(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 ...');
    Swal.fire({
        title: 'Are you sure you want to Generate Library card this?',
        icon: 'warning',
        showDenyButton: true,
        showCancelButton: false,
        confirmButtonText: 'Yes',
        denyButtonText: 'No',
    }).then((result) => {
        /* Read more about isConfirmed, isDenied below */
        if (result.isConfirmed) {
            $.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){
                    //
                    // alert(ButtonText);
                    /*formdata.find('button').prop('disabled', false);
                    formdata.find('button').html(ButtonText);*/
                    formdata.find('.btn-primary').prop('disabled', false);
                    formdata.find('.btn-primary').html(ButtonText);
                    console.log(ButtonText);
                    formdata.find('button[type="submit"]').prop('disabled', false);
                    formdata.find('button[type="submit"]').html(ButtonText);
     
                    console.log(response);
                    // if($.isEmptyObject(response.success)){
                    if(response.success){
                        var ErroMsg = $(this).printErrorMsg(response.message);
                            //
                        $(this).Toastshow('success',ErroMsg);
                        if(response.data.redirect){
                            // alert(response.data.redirect);
                             window.location.href = response.data.redirect;
                        }
                        if(response.data.reload){
                            setTimeout(function(){ location.reload(); }, 3000);
                        }
                        if(response.data.hide_id){
     
                             $('#'+response.data.hide_id).fadeOut(300);
                        }
     
                    }else{
                            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);
                }
            });
         
        }
        else if (result.isDenied) {
    
        }
        //return false;
    });
});



/*************************************************Delete Bulk Book Data ********************/
$("body").on("click",'.deletebulkbookData', function(event){
    var dataString    = $(this).data('id');  
    var mainid    = $(this).data('mainid');  
    var UrlValue  = $(this).data('url');
    console.log(dataString,'id'); 
    console.log(mainid,'mainid'); 
    console.log(UrlValue,'UrlValue'); 
    Swal.fire({
        title: 'Are you sure you want to delete this?',
        icon: 'warning',
        showDenyButton: true,
        showCancelButton: false,
        confirmButtonText: 'Yes',
        denyButtonText: 'No',
    }).then((result) => {
        /* Read more about isConfirmed, isDenied below */
        if (result.isConfirmed) {
            if(mainid)
            {
                $.ajax({
                    url     : UrlValue,
                    method  : 'post',
                    data    :{
                            "_token": $('meta[name="csrf-token"]').attr('content'),
                            "id": mainid
                    },
                    headers:
                    {
                        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                    },
                    beforeSend: function( xhr ) {
                        // xhr.overrideMimeType( "text/plain; charset=x-user-defined" );
                    },
                    success : function(response){
                        $(this).closest("tr").remove();
                        console.log(response.data);
                        if(response.success){
                            var ErroMsg = $(this).printErrorMsg(response.message);
                                //
                            $(this).Toastshow('success',ErroMsg);
                            $(this).closest("tr").remove();
                            if(response.data.redirect){
                                // alert(response.data.redirect);
                                 //window.location.href = response.data.redirect;
                                 window.location.reload();
                                 $(this).closest("tr").remove();
                            }
                            if(response.data.reload){
                                // alert(response.data.redirect);
                                 window.location.reload();
                                 $(this).closest("tr").remove();
                            }
    
                        }else{
                                var ErroMsg = $(this).printErrorMsg(response.message);
                                //
                                $(this).Toastshow('error',ErroMsg);
                        }
                    },
                    error: function (data) {
                      console.log("error ",data);
                    }   
                });
            }
            else
            {
                $(this).closest("tr").remove();
            }
           
            
         
        }
        else if (result.isDenied) {
    
        }
        //return false;
    });
});


    
});