<?php
/**
 * @var array $value .
 * @var array $ajax  .
 *
 * @package DhlPolska\WooCommerceSettings
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

?>

<tr valign="top">
	<th scope="row" class="titledesc">
		<?php echo wp_kses_post( $value['title'] ); ?>
	</th>
	<td class="forminp forminp-text">
		<strong id="<?php echo esc_attr( $value['id'] ); ?>_result" class="js--result-field"><span style="float: left;" class="spinner is-active"></span></strong>

		<script type="text/javascript">
			document.addEventListener( "DOMContentLoaded", function ( event ) {
				var request = new XMLHttpRequest();
				var params = new FormData();
				params.append( 'action', '<?php echo esc_js( $ajax['action'] ); ?>' );
				params.append( '_wpnonce', '<?php echo esc_js( $ajax['nonce'] ); ?>' );

				request.open( 'POST', '<?php echo esc_url( $ajax['url'] ); ?>', true );

				request.onload = function () {
					if ( this.status >= 200 && this.status < 400 ) {
						var response = JSON.parse( this.response );
						var elem = document.getElementById( '<?php echo esc_js( $value['id'] ); ?>_result' );

						if ( response.success ) {
							elem.style.color = 'green';
						} else {
							elem.style.color = 'red';
						}

						elem.innerHTML = response.data.message;
					}
				};

				request.send( params );
			} );
		</script>
	</td>
</tr>
