<?php
/**
 * Carteasy – Admin: Widerrufsliste
 *
 * Simple Liste aller Widerrufs-Datensätze mit Filter + Action "processed".
 * Zugriff: /index.php?admin_modul=admin_revocation_list
 *
 * @copyright  Wlanium / Thomas Bartelt
 * @since      2026-04-19
 */

include_once './core/revocation.class.php';

class admin_revocation_list {

	private $base_object;
	private $layout_object;
	private $revocation;

	public function __construct($base_object, $layout_object) {
		$this->base_object   = $base_object;
		$this->layout_object = $layout_object;
		$this->revocation    = new Revocation($base_object);
	}

	public function run() {

		// Action: als bearbeitet markieren
		if ($_SERVER['REQUEST_METHOD'] === 'POST'
			&& isset($_POST['action'])
			&& $_POST['action'] === 'mark_processed'
			&& !empty($_POST['id'])
		) {
			$this->revocation->mark_processed(
				(int)$_POST['id'],
				(string)($_POST['note'] ?? '')
			);
			// PRG
			header('Location: /index.php?admin_modul=admin_revocation_list&done=1');
			exit;
		}

		$filter_status = isset($_GET['status']) ? (string)$_GET['status'] : '';
		$rows = $this->revocation->list_all($filter_status ?: null, 500);

		return $this->render($rows, $filter_status, !empty($_GET['done']));
	}

	private function render($rows, $filter, $just_done) {
		$h = function($s) { return htmlspecialchars((string)$s, ENT_QUOTES, 'UTF-8'); };

		$html  = '<div style="padding:20px;font-family:Arial,sans-serif;">';
		$html .= '<h1 style="color:#c00;">Widerrufsliste</h1>';
		$html .= '<p style="color:#666;">Elektronische Widerrufe nach EU-RL 2026/2673. '
			   . 'Alle B2C-Bestellungen seit Aktivierung des Features.</p>';

		if ($just_done) {
			$html .= '<div style="background:#e7f7e7;border-left:4px solid #2a7;'
				   . 'padding:10px 14px;margin:12px 0;">Als bearbeitet markiert.</div>';
		}

		// Filter
		$html .= '<form method="get" style="margin:16px 0;">'
			   . '<input type="hidden" name="admin_modul" value="admin_revocation_list">'
			   . '<label>Status: '
			   . '<select name="status" onchange="this.form.submit()">'
			   .   '<option value="">alle</option>';
		foreach (['pending','submitted','processed','expired','revoked_by_admin'] as $st) {
			$sel = ($st === $filter) ? ' selected' : '';
			$html .= '<option value="'.$st.'"'.$sel.'>'.$st.'</option>';
		}
		$html .= '</select></label></form>';

		if (empty($rows)) {
			$html .= '<p><em>Keine Einträge gefunden.</em></p></div>';
			return $html;
		}

		$html .= '<table style="width:100%;border-collapse:collapse;font-size:.9em;">'
			   . '<thead><tr style="background:#f0f0f0;text-align:left;">'
			   . '<th style="padding:8px;">ID</th>'
			   . '<th style="padding:8px;">Bestellnr.</th>'
			   . '<th style="padding:8px;">Kunde (E-Mail)</th>'
			   . '<th style="padding:8px;">Status</th>'
			   . '<th style="padding:8px;">Bestelldatum</th>'
			   . '<th style="padding:8px;">Widerruf am</th>'
			   . '<th style="padding:8px;">Owner-Mail</th>'
			   . '<th style="padding:8px;">Aktion</th>'
			   . '</tr></thead><tbody>';

		foreach ($rows as $r) {
			$badge_color = [
				'pending'         => '#999',
				'submitted'       => '#c00',
				'processed'       => '#2a7',
				'expired'         => '#888',
				'revoked_by_admin'=> '#555',
			][$r->status] ?? '#555';

			$html .= '<tr style="border-bottom:1px solid #eee;">'
				   . '<td style="padding:8px;">'.(int)$r->id.'</td>'
				   . '<td style="padding:8px;">'.$h($r->order_number ?: '#'.$r->order_id).'</td>'
				   . '<td style="padding:8px;">'.$h($r->customer_email).'</td>'
				   . '<td style="padding:8px;"><span style="background:'.$badge_color
				   .  ';color:#fff;padding:2px 8px;border-radius:3px;font-size:.85em;">'
				   . $h($r->status).'</span></td>'
				   . '<td style="padding:8px;">'.$h($r->order_date).'</td>'
				   . '<td style="padding:8px;">'.$h($r->submitted_at ?? '–').'</td>'
				   . '<td style="padding:8px;">'.$h($r->owner_notified_at ? '✓' : '–').'</td>'
				   . '<td style="padding:8px;">';

			if ($r->status === 'submitted') {
				$html .= '<form method="post" style="display:inline;" '
					   . 'onsubmit="return confirm(\'Als bearbeitet markieren?\')">'
					   . '<input type="hidden" name="admin_modul" value="admin_revocation_list">'
					   . '<input type="hidden" name="action" value="mark_processed">'
					   . '<input type="hidden" name="id" value="'.(int)$r->id.'">'
					   . '<button type="submit" style="background:#2a7;color:#fff;'
					   . 'border:0;padding:6px 12px;border-radius:3px;cursor:pointer;">'
					   . 'processed</button></form>';
			} elseif ($r->status === 'processed') {
				$html .= '<span style="color:#2a7;">abgeschlossen</span>';
			} else {
				$html .= '–';
			}

			$html .= '</td></tr>';
		}

		$html .= '</tbody></table></div>';
		return $html;
	}
}
