/* 

 * Version: 1.0

 */

    var delete_button_id    = 'delete-selected-error-log-ids',

        delete_image_id     = 'delete-selected-error-log-ids-img',

        delete_notice_to_id = 'delete-selected-error-log-ids-span',

		delete_all_error_log_ids = 'delete-all-error-log-ids';

		

    jQuery(window).load(function(){

        jQuery('#' + delete_button_id ).click(function(){

            var checked_tokens = jQuery("input[name='delete-error-logs[]']:checked");

            if( checked_tokens.length > 0 ){

                if( true === confirm('Confirm delete? Selected ' + checked_tokens.length + ' item(s)') ){

                    var delete_able_token_ids = []; // all ids in this array will be sent to server to delete

                    checked_tokens.each(function(){

                        delete_able_token_ids.push( jQuery(this).val());

                    });

                    o365_delete_token_by_id( delete_able_token_ids );

                }

            }else{

                o365_show_notice_span('No row selected.');

            }

        });

		

		jQuery('#'+delete_all_error_log_ids ).click(function(){

            o365_delete_all_log_data();        

		});

    });

    

	function o365_delete_all_log_data()

	{

		o365_show_loader_img(); // Show page is working

		var data = { action: 'delete_all_logged_errors' };

		jQuery.post(ajaxurl, data, function( success ){

			console.log( success );

			$res = JSON.parse( success )

			console.log( success );

			

			if( ( 'undefined' !== typeof($res.deleted_message) ) && $res.deleted_message== "success" ){

				location.reload();

			}

			else

			{

				o365_show_notice_span('Error in deletion.');

				o365_hide_loader_img();

			}

			

		})

		.fail(function(faild){

            o365_hide_loader_img();

            console.log( 'fail' );

            o365_show_notice_span('Post request faied.');

        });

	}

    

    // Delete token and clear from the page 

    function o365_delete_token_by_id( $logging_ids ){

        o365_show_loader_img(); // Show page is working

        

        var data = {

            action: 'delete_logged_errors_by_its_id',

            logging_ids: $logging_ids

        };

        console.log( data );

        

        jQuery.post(ajaxurl, data, function( success ){

            o365_hide_loader_img();

            console.log( 'success' );

            console.log( success );

            

            try{

                console.log( 'JSON parsing' );

                $res = JSON.parse( success );

                console.log( $res );

                // Atleast one item deleted

                if( ( 'undefined' !== typeof($res.deleted_tokens) ) && $res.deleted_tokens.length > 0 ){

                    o365_show_notice_span('Deleted ' + $res.deleted_tokens.length + ' items' );

                    $hide_rows = $res.deleted_tokens;

                    for(i=0;i<$hide_rows.length;i++){

                        // Deleted from server, remove from the page now

                        // SlideUp to show beautiful and then delete

                        var row_id = 'error-log-row-id-' + $hide_rows[i];

                        console.log( 'RowID hidding: ' + row_id );

                        jQuery('#' + row_id ).slideUp(1000, function(){

                            jQuery(this).text('');

                        }); 

                    }

                    

                }else if( 'undefined' !== typeof($res.error_message) ){

                    o365_show_notice_span( $res.error_message );

                    

                }else{

                    o365_show_notice_span( 'No item deleted!' );

                }

            } catch(err){

                console.log( 'Error in catch block' );

                console.log( err.message );

            }

                       

        })

        .fail(function(faild){

            o365_hide_loader_img();

            console.log( 'fail' );

            console.log( faild );

            o365_show_notice_span('Post request faied.');

        });

        

    }

    

    

    

    // Make empty notice span after a specific period 

    function o365_clear_notice_span(){

        jQuery('#' + delete_notice_to_id).text('');

    }

    

    // show notice

    function o365_show_notice_span( msg ){

        jQuery('#'+delete_notice_to_id).text( ' ' + msg );

    }

    

    function o365_show_loader_img(){

        o365_clear_notice_span(); // Delete notice before doing something

        jQuery('#'+delete_image_id).show();

    }

    function o365_hide_loader_img(){

        jQuery('#'+delete_image_id).hide();

    }

