<?php
/**
 * Tab: Logs (Free)
 * @package Panelra
 */
if ( ! defined( 'ABSPATH' ) ) { exit; }

if ( isset( $_POST['pnlrdb_clear_logs'] ) && check_admin_referer( 'pnlrdb_clear_logs' ) && current_user_can( 'manage_options' ) ) {
	Pnlrdb_Log_Manager::clear_logs();
}
$pnlrdb_logs = Pnlrdb_Log_Manager::get_logs();
?>
<h2 class="ap773-section-title"><?php esc_html_e( 'Logs', 'panelra-dashboard-builder' ); ?></h2>
<p class="ap773-section-desc"><?php esc_html_e( 'History of the last 50 settings changes.', 'panelra-dashboard-builder' ); ?></p>

<?php if ( ! empty( $pnlrdb_logs ) ) : ?>
<div style="display:flex;justify-content:flex-end;margin-bottom:16px;">
	<form method="post">
		<?php wp_nonce_field( 'pnlrdb_clear_logs' ); ?>
		<button type="submit" name="pnlrdb_clear_logs" value="1" class="ap773-btn ap773-btn--danger"
		        onclick="return confirm('<?php esc_attr_e( 'Clear all logs?', 'panelra-dashboard-builder' ); ?>')">
			<?php esc_html_e( 'Clear logs', 'panelra-dashboard-builder' ); ?>
		</button>
	</form>
</div>
<div class="ap773-card" style="padding:0;overflow:hidden;">
	<table class="ap773-table">
		<thead>
			<tr>
				<th><?php esc_html_e( 'Date / Time', 'panelra-dashboard-builder' ); ?></th>
				<th><?php esc_html_e( 'Option changed', 'panelra-dashboard-builder' ); ?></th>
				<th><?php esc_html_e( 'User', 'panelra-dashboard-builder' ); ?></th>
			</tr>
		</thead>
		<tbody>
		<?php foreach ( $pnlrdb_logs as $pnlrdb_log ) : ?>
			<tr>
				<td><?php echo esc_html( $pnlrdb_log['time'] ?? '-' ); ?></td>
				<td><code style="font-size:12px;color:#6366f1;"><?php echo esc_html( $pnlrdb_log['option'] ?? '-' ); ?></code></td>
				<td><?php
					$pnlrdb_user = get_user_by( 'id', $pnlrdb_log['user'] ?? 0 );
					echo $pnlrdb_user ? esc_html( $pnlrdb_user->display_name ) : '—';
				?></td>
			</tr>
		<?php endforeach; ?>
		</tbody>
	</table>
</div>
<?php else : ?>
<div class="ap773-empty">
	<svg width="40" height="40" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5"><path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z"/><polyline points="14 2 14 8 20 8"/></svg>
	<p><?php esc_html_e( 'No logs yet. Changes will appear here.', 'panelra-dashboard-builder' ); ?></p>
</div>
<?php endif; ?>
