jQuery( document ).ready( function ( $ ) {
	$( '.plugin-note' ).each( function () {
		var slug = $( this ).attr( 'id' );

		$( document ).on( "click", '#maintainn_note_submit_' + slug, function () {

			var policeComment = {
				'action':  'maintainn_tools_receive_comment',
				'note': $( '#maintainn_note_' + slug ).val(),
				'slug':    slug
			};

			$.post( ajaxurl, policeComment, function ( response ) {
				$( '#' + slug ).html( response );
			} );

			return false;
		} );

		$( document ).on( "click", '#maintainn_note_link_' + slug, function () {
			$( '#maintainn_note_div_' + slug ).show();
			// $( '.plugin_notes_' + slug ).emojiPicker(); temp disable.
			$( '#maintainn_note_link_' + slug ).hide();
			$( '#maintainn_note_' + slug ).focus();
		} );

		$( document ).on( "click", '#maintainn_lock_update_' + slug, function () {
			// Get the file of the plugin.
			var plugin = $( "#the-list" ).find( "[data-slug='" + slug + "']" ).data( "plugin" );

			$( '#' + slug ).html( m_messages.loading_message );

			var lockUpdates = {
				'action': 'maintainn_tools_lock_updates',
				'slug':   slug,
				'plugin': plugin
			};

			$.post( ajaxurl, lockUpdates, function ( response ) {
				$( '#' + slug ).html( response );

				// Hide the notice.
				$( "tr#" + slug + "-update" ).hide();
			} );
		} );

		$( document ).on( "click", '#maintainn_auto_update_' + slug, function () {

			$( '#' + slug ).html( m_messages.loading_message );

			var toggleUpdate = {
				'action': 'maintainn_tools_toggle_updates',
				'slug':   slug
			};

			$.post( ajaxurl, toggleUpdate, function ( response ) {
				$( '#' + slug ).html( response );
			} );

		} );

		$( this ).on( "click", ".dashicons-trash", function () {

			var pluginNote = $( this ).parents( 'li' );

			var deleteNote = {
				'action':  'maintainn_tools_delete_plugin_note',
				'note': pluginNote.attr( 'data-id' ),
				'nonce': m_messages.delete_plugin_note
			};

			$.post( ajaxurl, deleteNote, function ( response ) {
				if ( response && response.ID == pluginNote.attr( 'data-id' ) ) {
					pluginNote.fadeOut( 300 );
				} else {
					alert( m_messages.note_delete_error );
				}
			} );

			return false;
		} );
	} );
} );
