<?php
 /**
 * CV Monitor
 *
 * @package   CV_Monitor_Admin
 * @author    Gabriel Mariani <gabriel@coursevector.com>
 * @license   GPL-2.0+
 * @link      http://coursevector.com
 * @copyright 2019 CourseVector
 */

// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) die;

//if ( ! current_user_can( 'view_site_health_checks' ) ) { // only exists in v5+
if ( ! current_user_can( 'manage_options' ) ) {
	wp_die( __( 'Sorry, you are not allowed to access the debug data.' ), '', 403 );
}

?>
<div class="wrap">
	<?php

	global $wp_version;
	if ( version_compare( $wp_version, '3.8', '<' ) ) {
		screen_icon();
		echo '<h2>' . esc_html( get_admin_page_title() ) . '</h2>';
	} else {
		echo '<h1>' . esc_html( get_admin_page_title() ) . '</h1>';
	}
?>
<div class="wrap-body">
<?php
	///////////////////
	// Notifications //
	///////////////////
	// Display notice at the top. Use updated for yellow box, error for red

	global $wpdb;

	// Manually send update
	if ( isset( $_POST["cv-monitor-status"] ) && wp_verify_nonce( $_POST['status-site-nonce'], 'status-site' ) ) {
		if ( function_exists("SimpleLogger") ) {
			$current_user = wp_get_current_user();
			SimpleLogger()->info("User '{$current_user->user_login}' sent a CV Monitor site status.");
		}
		$result = $this->main->site->send_data();
		if ( true === $result ) {
			echo '<div class="notice is-dismissible updated"><p>'  . __( 'Status update sent successfully.', $this->main->plugin_name ) . '</p></div>';
		} else {
			//echo '<div class="error"><p>' . __( 'There was a problem sending status update:<br>', $this->main->plugin_name ) . '<pre>' . print_r(error_get_last(), true) .'</pre>' . '</p></div>';
			echo '<div class="notice is-dismissible error"><p>' . __( 'There was a problem sending status update:<br>', $this->main->plugin_name ) . $result . '</p></div>';
		}
	}

	// ajaxurl added in 2.8+
	if ( version_compare( get_bloginfo('version'), '2.8', '<' )) {
		?>
		<script type="text/javascript">
			var ajaxurl = '<?php echo admin_url( 'admin-ajax.php' ); ?>';
		</script>
		<?php
	}
	?>
	<div class="clear"></div>

	<div id="health-check-debug" class="health-check-accordion">
	<?php

	$sizes_fields = array( 'uploads_size', 'themes_size', 'plugins_size', 'wordpress_size', 'database_size', 'total_size' );
	$info = $this->get_data();
	foreach ( $info as $section => $details ) {
			if ( ! isset( $details['fields'] ) || empty( $details['fields'] ) ) {
				//echo 'skip ' . $details['label'] . '<br>';
				continue;
			}

			?>
			<h3 class="health-check-accordion-heading">
				<button aria-expanded="false" class="health-check-accordion-trigger" aria-controls="health-check-accordion-block-<?php echo esc_attr( $section ); ?>" type="button">
					<span class="title">
						<?php echo esc_html( $details['label'] ); ?>
						<?php

						if ( isset( $details['show_count'] ) && $details['show_count'] ) {
							printf( '(%d)', count( $details['fields'] ) );
						}

						?>
					</span>
					<?php

					if ( 'wp-paths-sizes' === $section ) {
						?>
						<span class="health-check-wp-paths-sizes spinner"></span>
						<?php
					}

					?>
					<span class="icon"></span>
				</button>
			</h3>

			<div id="health-check-accordion-block-<?php echo esc_attr( $section ); ?>" class="health-check-accordion-panel" hidden="hidden">
				<?php
				if ( isset( $details['description'] ) && ! empty( $details['description'] ) ) {
					printf( '<p>%s</p>', $details['description'] );
				}

				?>
				<table class="widefat striped health-check-table" role="presentation">
					<?php if (isset( $details['columns'] )) { ?>
					<thead>
						<tr valign="top">
							<?php
							foreach ( $details['columns'] as $column_name => $column ) {
								printf( '<th>%s</th>', esc_html( $column_name) );
							}
							?>
						</tr>
					</thead>
					<?php } ?>
					<tbody>
					<?php
					if ( isset( $details['multi_column'] ) && $details['multi_column'] ) {
						foreach ( $details['fields'] as $field_name => $field ) {
							if ( is_array( $field['value'] ) ) {
								$values = '';
								foreach ( $field['value'] as $name => $value ) {
									//$values .= sprintf( '<td>%s: %s</td>', esc_html( $name ), esc_html( $value ) );
									$values .= sprintf( '<td>%s</td>', esc_html( $value ) );
								}
							} else {
								$values = sprintf( '<td>%s</td>', esc_html( $field['value'] ));
							}

							printf( '<tr>%s</tr>', $values );
						}
					} else {
						foreach ( $details['fields'] as $field_name => $field ) {
							if ( is_array( $field['value'] ) ) {
								$values = '<ul>';

								foreach ( $field['value'] as $name => $value ) {
									$values .= sprintf( '<li>%s: %s</li>', esc_html( $name ), esc_html( $value ) );
								}

								$values .= '</ul>';
							} else {
								$values = esc_html( $field['value'] );
							}

							if ( in_array( $field_name, $sizes_fields, true ) ) {
								printf( '<tr><td>%s</td><td class="%s">%s</td></tr>', esc_html( $field['label'] ), esc_attr( $field_name ), $values );
							} else {
								printf( '<tr><td>%s</td><td>%s</td></tr>', esc_html( $field['label'] ), $values );
							}
						}
					}

					?>
					</tbody>
				</table>
			</div>
		<?php } ?>
	</div>

	<br/>
	<!-- Last updated -->
	<form method="post" id="monitor-status">
		<p class="description">
			<?php

			if ( false !== get_option( 'cv_monitor_last_checked' ) ) {
				echo __( 'Update last sent ', $this->main->plugin_name ) . $this->plugin->parse_timestamp( get_option( 'cv_monitor_last_checked' ) ) . ',';
			} else {
				echo __( 'No status has been sent yet,', $this->main->plugin_name );
			}
			echo " next update in " . $this->plugin->parse_timestamp( wp_next_scheduled( 'cv_update_monitor' ) ) . '. |  <a id="expand-data" href="javascript:;" >Expand Data &raquo;</a> &nbsp;' . get_submit_button( 'Send Status', 'button', 'cv-monitor-status', false );
			?>
		</p>
		<?php wp_nonce_field( 'status-site', 'status-site-nonce', false ); ?>

		<textarea id="json-data"><?php echo json_encode( $this->main->site->get_data() ); ?></textarea>
	</form>

	</div>
</div> <!-- End Wrap -->