<?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

	// Delete error log
	if ( isset( $_POST["cv-monitor-errors-clear"] ) && wp_verify_nonce( $_POST['error-log-nonce'], 'error-log' ) ) {
		$result = $this->main->site->delete_error_log( $_POST['logs'] );
		$path = realpath( $_POST['logs'] );
		if ( false === $path ) $path = $_POST['logs'];
		$path = esc_html( $path );

		if ( function_exists("SimpleLogger") ) {
			$current_user = wp_get_current_user();
			SimpleLogger()->info("User '{$current_user->user_login}' " . ($result === true ? 'successfully deleted' : 'failed to delete') . " error log file '{$path}'.");
		}

		if ( $result === true ) {
			echo '<div class="notice is-dismissible updated"><p>' . $path . __( " deleted successfully.", $this->main->plugin_name );
		} else {
			//echo '<div class="notice is-dismissible error"><p>' . __('There was a problem clearing the Error Log:<br>', $this->main->plugin_name) . error_get_last();
			echo '<div class="notice is-dismissible error"><p>' . __( "There was a problem deleting ", $this->main->plugin_name ) . $path . ':<br>' . $result;
		}
		 echo '</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
	}

	// Display only if error logs found
	// ABSPATH = dirname(__FILE__) . '/'
	// So remove last /
	$arrLogs = $this->main->site->get_error_logs( substr(ABSPATH, 0, -1) );
	/*echo '<pre>';
	var_dump($arrLogs);
	echo '</pre>';*/
	if ( count( $arrLogs ) > 0 ) {
		// Enable only if file name is error_log
		$is_disabled = ( substr( $arrLogs[0][0], -strlen( 'error_log' ) ) === 'error_log' ) ? '' : '-disabled';
	?>
		<form method="post" id="cv-monitor-error-logs">
			<div class="top-nav">
				<select name="logs" id="logs">
					<?php
					$is_first = true;
					foreach ($arrLogs as $log) {
						printf('<option value="%s">%s</option>', $log[0], $log[0] . ' (' . $this->plugin->parse_file_size( $log[1] ) . ')');
					}
					?>
				</select>
				<?php submit_button('Delete Error Log', 'button' . $is_disabled, 'cv-monitor-errors-clear', false ); ?>
			</div>
			<?php wp_nonce_field( 'error-log', 'error-log-nonce', false ); ?>
			<div id="log-display" class="large-text code" rows="10"><?php _e( 'Loading...', $this->main->plugin_name ); ?></div>
			<div class="tablenav bottom">
				<div class="alignleft actions bulkactions"></div>
				<div class="tablenav-pages one-page">
					<span class="displaying-num" id="log-size">0 kB</span>
				</div>
				<br class="clear">
			</div>
		</form>
	<?php } else { ?>
		<form method="post" id="cv-monitor-error-logs">
			<div id="log-display" class="large-text code" rows="10"><?php _e( 'No errors found... yet.', $this->main->plugin_name ); ?></div>
		</form>
	<?php }	?>

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