<?php
/**
 * Wave Access – PDF-Prüfbericht (Pro)
 *
 * Erzeugt eine druckoptimierte Berichtsansicht (Nachweis-Dokumentation) mit
 * automatischem Druckdialog – Speichern als PDF über den Browser.
 * Bewusst ohne PHP-PDF-Bibliothek: kein Speicher-/Kompatibilitätsrisiko,
 * pixelgenaue Ausgabe über die Print-Engine des Browsers.
 */

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

class Wave_Report_PDF {

	private static $instance = null;

	public static function instance() {
		if ( null === self::$instance ) self::$instance = new self();
		return self::$instance;
	}

	private function __construct() {
		add_action( 'admin_post_wave_report_pdf', array( $this, 'render' ) );
	}

	public function render() {
		if ( ! current_user_can( 'manage_options' ) ) wp_die( 'forbidden' );
		check_admin_referer( 'wave_report_pdf' );
		if ( ! Wave_License::is_pro() ) wp_die( esc_html__( 'Der PDF-Prüfbericht ist Teil des Pro-Tarifs.', 'wave-access' ) );

		$plugin  = Wave_Access::get_instance();
		$data    = $plugin->get_report_data();
		$options = get_option( 'wave_options', array() );
		$lic     = Wave_License::get();

		$active_fixes = array();
		$fix_labels = array(
			'enable_skip_links'       => __( 'Sprungmarken (Skip-Links)', 'wave-access' ),
			'enable_focus_indicator'  => __( 'Verbesserter Fokus-Indikator', 'wave-access' ),
			'enable_aria_labels'      => __( 'Automatische ARIA-Landmarks', 'wave-access' ),
			'enable_lang_attributes'  => __( 'Sprach- und Richtungsattribute', 'wave-access' ),
			'enable_tab_order'        => __( 'Optimierte Tab-Reihenfolge', 'wave-access' ),
			'enable_heading_checker'  => __( 'Überschriften-Hierarchie-Prüfung', 'wave-access' ),
			'enable_image_checker'    => __( 'Alt-Text-Prüfung', 'wave-access' ),
			'enable_floating_widget'  => __( 'Eingabehilfen-Widget für Besucher', 'wave-access' ),
		);
		foreach ( $fix_labels as $key => $label ) {
			if ( ! empty( $options[ $key ] ) ) $active_fixes[] = $label;
		}

		header( 'Content-Type: text/html; charset=utf-8' );
		?>
<!DOCTYPE html>
<html lang="<?php echo esc_attr( substr( get_locale(), 0, 2 ) ); ?>">
<head>
<meta charset="utf-8">
<title><?php esc_html_e( 'Barrierefreiheits-Prüfbericht', 'wave-access' ); ?> – <?php echo esc_html( get_bloginfo( 'name' ) ); ?></title>
<style>
	:root{--fg:#0f172a;--muted:#64748b;--border:#e2e8f0;--brand:#0178b6;--ok:#16a34a;--warn:#d97706;--bad:#dc2626}
	*{box-sizing:border-box;margin:0;padding:0}
	body{font-family:-apple-system,'Segoe UI',Helvetica,Arial,sans-serif;color:var(--fg);font-size:13px;line-height:1.55;padding:48px 56px;max-width:860px;margin:0 auto}
	header{display:flex;justify-content:space-between;align-items:flex-start;border-bottom:3px solid var(--brand);padding-bottom:18px;margin-bottom:28px}
	h1{font-size:22px;letter-spacing:-.01em}
	.sub{color:var(--muted);margin-top:4px}
	.brand{text-align:right;color:var(--muted);font-size:11px}
	.brand strong{color:var(--brand);font-size:14px}
	h2{font-size:15px;margin:26px 0 10px;padding-bottom:6px;border-bottom:1px solid var(--border)}
	table{width:100%;border-collapse:collapse;font-size:12.5px}
	th,td{text-align:left;padding:7px 10px;border-bottom:1px solid var(--border);vertical-align:top}
	th{background:#f8fafc;font-weight:600}
	.kpis{display:flex;gap:14px;margin:14px 0}
	.kpi{flex:1;border:1px solid var(--border);border-radius:8px;padding:12px 14px}
	.kpi .n{font-size:22px;font-weight:700}
	.kpi .l{color:var(--muted);font-size:11px}
	.ok{color:var(--ok)}.warn{color:var(--warn)}.bad{color:var(--bad)}
	ul{padding-left:18px}
	li{margin:3px 0}
	.foot{margin-top:36px;padding-top:14px;border-top:1px solid var(--border);color:var(--muted);font-size:10.5px}
	.noprint{position:fixed;top:14px;right:14px;background:var(--brand);color:#fff;border:0;border-radius:8px;padding:10px 18px;font-size:13px;cursor:pointer}
	@media print{.noprint{display:none}body{padding:0}}
	@page{margin:18mm 16mm}
</style>
</head>
<body>
	<button class="noprint" onclick="window.print()"><?php esc_html_e( 'Als PDF speichern / drucken', 'wave-access' ); ?></button>

	<header>
		<div>
			<h1><?php esc_html_e( 'Barrierefreiheits-Prüfbericht', 'wave-access' ); ?></h1>
			<div class="sub"><?php echo esc_html( get_bloginfo( 'name' ) ); ?> · <?php echo esc_html( home_url() ); ?></div>
			<div class="sub"><?php printf( esc_html__( 'Erstellt am %s', 'wave-access' ), esc_html( date_i18n( get_option( 'date_format' ) . ' H:i' ) ) ); ?></div>
		</div>
		<div class="brand">
			<strong>wave access</strong><br>
			<?php esc_html_e( 'Nachweis-Dokumentation', 'wave-access' ); ?><br>
			<?php printf( 'Lizenz: %s', esc_html( Wave_License::plan_label() ) ); ?>
		</div>
	</header>

	<h2><?php esc_html_e( '1. Zusammenfassung', 'wave-access' ); ?></h2>
	<div class="kpis">
		<div class="kpi"><div class="n <?php echo $data['images_missing'] > 0 ? 'warn' : 'ok'; ?>"><?php echo (int) $data['images_missing']; ?></div><div class="l"><?php esc_html_e( 'Bilder ohne Alt-Text', 'wave-access' ); ?></div></div>
		<div class="kpi"><div class="n"><?php echo (int) $data['checklist_progress']; ?>%</div><div class="l"><?php esc_html_e( 'BFSG/WCAG-Checkliste erfüllt', 'wave-access' ); ?></div></div>
		<div class="kpi"><div class="n"><?php echo count( $active_fixes ); ?></div><div class="l"><?php esc_html_e( 'Aktive automatische Maßnahmen', 'wave-access' ); ?></div></div>
	</div>
	<p><?php esc_html_e( 'Dieser Bericht dokumentiert den Stand der Barrierefreiheits-Maßnahmen dieser Website zum oben genannten Zeitpunkt. Er basiert auf automatisierten Prüfungen und der Selbstbewertung anhand der BFSG/WCAG-2.1-AA-Checkliste und dient als Nachweis der fortlaufenden Bemühungen im Sinne von BFSG und European Accessibility Act.', 'wave-access' ); ?></p>

	<h2><?php esc_html_e( '2. Aktive automatische Maßnahmen', 'wave-access' ); ?></h2>
	<ul>
		<?php foreach ( $active_fixes as $fix ) : ?><li><?php echo esc_html( $fix ); ?></li><?php endforeach; ?>
		<?php if ( empty( $active_fixes ) ) : ?><li><?php esc_html_e( 'Keine Maßnahmen aktiviert.', 'wave-access' ); ?></li><?php endif; ?>
	</ul>

	<h2><?php esc_html_e( '3. Checkliste (Selbstbewertung)', 'wave-access' ); ?></h2>
	<table>
		<thead><tr><th style="width:55%"><?php esc_html_e( 'Kriterium', 'wave-access' ); ?></th><th><?php esc_html_e( 'Kategorie', 'wave-access' ); ?></th><th style="width:14%"><?php esc_html_e( 'Status', 'wave-access' ); ?></th></tr></thead>
		<tbody>
		<?php foreach ( $data['checklist'] as $row ) : ?>
			<tr>
				<td><?php echo esc_html( $row['title'] ); ?></td>
				<td><?php echo esc_html( $row['category'] ); ?></td>
				<td><?php echo $row['done'] ? '<span class="ok">&#10003; ' . esc_html__( 'erfüllt', 'wave-access' ) . '</span>' : '<span class="warn">&#9675; ' . esc_html__( 'offen', 'wave-access' ) . '</span>'; ?></td>
			</tr>
		<?php endforeach; ?>
		</tbody>
	</table>

	<?php if ( ! empty( $data['images'] ) ) : ?>
	<h2><?php printf( esc_html__( '4. Bilder ohne Alt-Text (%d)', 'wave-access' ), (int) $data['images_missing'] ); ?></h2>
	<table>
		<thead><tr><th><?php esc_html_e( 'Datei', 'wave-access' ); ?></th><th><?php esc_html_e( 'Hochgeladen', 'wave-access' ); ?></th></tr></thead>
		<tbody>
		<?php foreach ( array_slice( $data['images'], 0, 50 ) as $img ) : ?>
			<tr><td><?php echo esc_html( basename( $img->guid ) ); ?></td><td><?php echo esc_html( date_i18n( get_option( 'date_format' ), strtotime( $img->post_date ) ) ); ?></td></tr>
		<?php endforeach; ?>
		</tbody>
	</table>
	<?php if ( $data['images_missing'] > 50 ) : ?><p class="sub"><?php printf( esc_html__( '… und %d weitere (vollständige Liste als CSV-Export verfügbar).', 'wave-access' ), (int) $data['images_missing'] - 50 ); ?></p><?php endif; ?>
	<?php endif; ?>

	<div class="foot">
		<?php printf(
			esc_html__( 'Erstellt mit Wave Access %1$s · wavetools.io · WordPress %2$s · Hinweis: Dieser Bericht dokumentiert automatisierte Prüfungen und eine Selbstbewertung. Er ersetzt keine manuelle Prüfung durch Experten und stellt keine Rechtsberatung oder Konformitätsgarantie dar.', 'wave-access' ),
			esc_html( WAVE_VERSION ),
			esc_html( get_bloginfo( 'version' ) )
		); ?>
	</div>

	<script>window.addEventListener('load', function(){ setTimeout(function(){ window.print(); }, 400); });</script>
</body>
</html>
		<?php
		exit;
	}
}
