<?php

namespace Kitgenix\PDF_Invoicing\Modules\Admin;

use Kitgenix\PDF_Invoicing\Modules\Invoicing\DocumentTypes;
use Kitgenix\PDF_Invoicing\Modules\Invoicing\PdfGenerator;
use Kitgenix\PDF_Invoicing\Modules\Invoicing\TemplateRenderer;
use Kitgenix\PDF_Invoicing\Modules\Settings\Settings;

defined( 'ABSPATH' ) || exit;

final class OrderMetaBox {

	private static ?PdfGenerator $pdf = null;

	/**
	 * Initialise order meta box. Accept an injected PdfGenerator for shared
	 * configuration; if none is provided a fallback instance is created.
	 */
	public static function init( ?PdfGenerator $pdf = null ): void {
		if ( $pdf ) {
			self::$pdf = $pdf;
		} else {
			// Fallback for backward compatibility – avoid duplicate creation
			// in normal bootstrap where AdminModule injects the shared instance.
			$renderer = new TemplateRenderer();
			self::$pdf = new PdfGenerator( $renderer );
		}

		add_action( 'add_meta_boxes', [ self::class, 'add_meta_box' ] );
		add_action( 'admin_enqueue_scripts', [ self::class, 'enqueue_assets' ] );
		add_action( 'admin_footer', [ self::class, 'render_history_modals' ] );

		// Admin-post endpoints for meta box actions (downloads).
		add_action( 'admin_post_kitgenix_admin_stream_invoice', [ self::class, 'handle_admin_stream_invoice' ] );
		// Receipt admin endpoint (download).
		add_action( 'admin_post_kitgenix_admin_stream_receipt', [ self::class, 'handle_admin_stream_receipt' ] );
		// Packing slip admin endpoint (download).
		add_action( 'admin_post_kitgenix_admin_stream_packing_slip', [ self::class, 'handle_admin_stream_packing_slip' ] );
		// Credit note admin endpoint (download).
		add_action( 'admin_post_kitgenix_admin_stream_credit_note', [ self::class, 'handle_admin_stream_credit_note' ] );
		add_action( 'admin_post_kitgenix_admin_stream_pro_forma_invoice', [ self::class, 'handle_admin_stream_pro_forma_invoice' ] );
		add_action( 'admin_post_kitgenix_admin_stream_delivery_note', [ self::class, 'handle_admin_stream_delivery_note' ] );
		add_action( 'admin_post_kitgenix_admin_stream_statement', [ self::class, 'handle_admin_stream_statement' ] );

		// Deliberate document-integrity actions: regenerate (re-render an
		// already-issued PDF, keeping the prior revision) and void/unvoid
		// (status flag only – never touches the archived file). Both are
		// gated on `manage_woocommerce` rather than `edit_shop_orders`
		// because they alter the audit trail of an issued financial
		// document, not just fetch a copy of it.
		add_action( 'admin_post_kitgenix_admin_regenerate_document', [ self::class, 'handle_regenerate_document' ] );
		add_action( 'admin_post_kitgenix_admin_void_document', [ self::class, 'handle_void_document' ] );
	}

	public static function add_meta_box(): void {
		$screen_id = 'shop_order';

		if ( function_exists( 'wc_get_container' ) ) {
			try {
				$container  = \wc_get_container();
				$controller = $container->get(\Automattic\WooCommerce\Internal\DataStores\Orders\CustomOrdersTableController::class);

				if ( $controller && $controller->custom_orders_table_usage_is_enabled() ) {
					if ( function_exists( 'wc_get_page_screen_id' ) ) {
						$screen_id = \wc_get_page_screen_id( 'shop-order' );
					}
				}
			} catch ( \Throwable $e ) {
				// noop - fall back to legacy screen id.
			}
		}

		add_meta_box(
			'kitgenix-pdf-invoicing-meta-box',
			__( 'Kitgenix PDF Invoicing', 'kitgenix-pdf-invoicing-for-woocommerce' ),
			[ self::class, 'render_meta_box' ],
			$screen_id,
			'side',
			'default'
		);
	}

	public static function enqueue_assets( string $hook ): void {
		$screen = get_current_screen();
		if ( ! $screen ) {
			return;
		}

		$allowed_screens = [ 'shop_order' ];
		if ( function_exists( 'wc_get_page_screen_id' ) ) {
			$allowed_screens[] = \wc_get_page_screen_id( 'shop-order' );
		}

		if ( ! in_array( $screen->id, $allowed_screens, true ) ) {
			return;
		}

		if ( defined( 'KITGENIX_PDF_INVOICING_FOR_WOOCOMMERCE_URL' ) ) {
			$base_url = KITGENIX_PDF_INVOICING_FOR_WOOCOMMERCE_URL;
		} elseif ( defined( 'KITGENIX_PDF_INVOICING_FOR_WOOCOMMERCE_FILE' ) ) {
			$base_url = plugin_dir_url( KITGENIX_PDF_INVOICING_FOR_WOOCOMMERCE_FILE );
		} else {
			$base_url = plugin_dir_url( __FILE__ );
		}

		if ( defined( 'KITGENIX_PDF_INVOICING_FOR_WOOCOMMERCE_PATH' ) ) {
			$base_dir = KITGENIX_PDF_INVOICING_FOR_WOOCOMMERCE_PATH;
		} elseif ( defined( 'KITGENIX_PDF_INVOICING_FOR_WOOCOMMERCE_FILE' ) ) {
			$base_dir = plugin_dir_path( KITGENIX_PDF_INVOICING_FOR_WOOCOMMERCE_FILE );
		} else {
			$base_dir = plugin_dir_path( __FILE__ );
		}

		// Enqueue admin order meta styles.
		$css_path = trailingslashit( $base_dir ) . 'assets/css/admin-order-meta.css';
		$css_ver  = file_exists( $css_path ) ? (string) filemtime( $css_path ) : KITGENIX_PDF_INVOICING_FOR_WOOCOMMERCE_VERSION;

		wp_enqueue_style(
			'kitgenix-pdf-invoicing-admin-order-meta',
			$base_url . 'assets/css/admin-order-meta.css',
			[],
			$css_ver
		);

		wp_enqueue_style( 'kitgenix-pdf-invoicing-for-woocommerce-admin-ui' );

		// Shared modal/collapsible library (same file every Kitgenix plugin
		// ships) – powers the per-document "History" modal below via plain
		// data-kitgenix-modal-target markup, no page-specific JS needed.
		$components_js_path = trailingslashit( $base_dir ) . 'assets/js/kitgenix-admin-components.js';
		$components_js_ver  = file_exists( $components_js_path ) ? (string) filemtime( $components_js_path ) : $css_ver;

		wp_enqueue_script(
			'kitgenix-admin-components',
			$base_url . 'assets/js/kitgenix-admin-components.js',
			[],
			$components_js_ver,
			true
		);
	}

	/**
	 * Resolve the order currently being edited from the URL (classic
	 * post.php?post=ID and HPOS admin.php?page=wc-orders&id=ID). Used by the
	 * admin_footer history-modal renderer, which runs outside render_meta_box()
	 * and so doesn't receive the order object WordPress passes to callbacks.
	 */
	private static function resolve_current_order(): ?\WC_Order {
		$order_id = 0;

		$get_post_id = filter_input( INPUT_GET, 'post', FILTER_VALIDATE_INT );
		if ( $get_post_id ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
			$order_id = absint( $get_post_id );
		} else {
			$get_hpos_id = filter_input( INPUT_GET, 'id', FILTER_VALIDATE_INT );
			if ( $get_hpos_id ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
				$order_id = absint( $get_hpos_id );
			}
		}

		if ( ! $order_id || ! current_user_can( 'edit_shop_order', $order_id ) ) {
			return null;
		}

		$order = wc_get_order( $order_id );
		return $order instanceof \WC_Order ? $order : null;
	}

	/**
	 * Render the meta box contents.
	 *
	 * @param mixed $post_or_order WP_Post|WC_Order|int
	 */
	public static function render_meta_box( $post_or_order ): void {
		$order = null;

		if ( $post_or_order instanceof \WC_Order ) {
			$order = $post_or_order;
		} elseif ( $post_or_order instanceof \WP_Post ) {
			$order = wc_get_order( $post_or_order->ID );
		} elseif ( is_numeric( $post_or_order ) ) {
			$order = wc_get_order( (int) $post_or_order );
		}

		if ( ! $order instanceof \WC_Order ) {
			echo esc_html__( 'Order not found.', 'kitgenix-pdf-invoicing-for-woocommerce' );
			return;
		}

		if ( ! self::$pdf ) {
			$renderer = new TemplateRenderer();
			self::$pdf = new PdfGenerator( $renderer );
		}

		$settings          = Settings::get_all();
		$can_manage_status = current_user_can( 'manage_woocommerce' );

		?>
		<div class="kitgenix-metabox kitgenix-pdf-invoicing-for-woocommerce-order-meta">
			<div class="kitgenix-doc-list">
				<?php foreach ( DocumentTypes::all() as $type ) :
					$status = self::$pdf->get_document_status( $order, $type );
					$available = self::$pdf->is_document_available_for_order( $order, $type );

					// Keep a row visible for anything already issued even if
					// current generation rules would now hide it – status/void
					// history must never disappear just because settings changed.
					if ( ! $available && 'not_generated' === $status ) {
						continue;
					}

					$identifier = 'not_generated' !== $status
						? DocumentTypes::get_identifier( $order, $settings, $type )
						: '';

					$badge_class = 'neutral';
					$status_label = __( 'Not generated', 'kitgenix-pdf-invoicing-for-woocommerce' );
					if ( 'issued' === $status ) {
						$badge_class  = 'success';
						$status_label = __( 'Issued', 'kitgenix-pdf-invoicing-for-woocommerce' );
					} elseif ( 'voided' === $status ) {
						$badge_class  = 'warning';
						$status_label = __( 'Voided', 'kitgenix-pdf-invoicing-for-woocommerce' );
					}

					$stream_action = DocumentTypes::get_admin_stream_action( $type );
					$stream_url    = admin_url( 'admin-post.php?action=' . $stream_action . '&order_id=' . $order->get_id() . '&nonce=' . wp_create_nonce( 'kitgenix_admin_pdf' ) );
					$history       = self::$pdf->get_document_history( $order, $type );
					?>
					<?php
					// Nothing to generate yet and nothing to manage – a "Not
					// generated" type only ever has a single possible action
					// (Download, which generates on first click), so it fits
					// entirely on one line instead of a second row that would
					// otherwise just hold an em dash.
					$is_ungenerated = ( 'not_generated' === $status );
					?>
					<div class="kitgenix-doc-row" data-kitgenix-doc-type="<?php echo esc_attr( $type ); ?>">
						<div class="kitgenix-doc-row__line">
							<span class="kitgenix-doc-row__label"><?php echo esc_html( DocumentTypes::get_label( $type ) ); ?></span>
							<div class="kitgenix-doc-row__end">
								<span class="kitgenix-badge <?php echo esc_attr( $badge_class ); ?> kitgenix-badge-dot"><?php echo esc_html( $status_label ); ?></span>
								<?php if ( $is_ungenerated && $available ) : ?>
									<div class="kitgenix-icon-actions">
										<a href="<?php echo esc_url( $stream_url ); ?>" class="kitgenix-icon-button" target="_blank" title="<?php esc_attr_e( 'Download (PDF)', 'kitgenix-pdf-invoicing-for-woocommerce' ); ?>" aria-label="<?php esc_attr_e( 'Download (PDF)', 'kitgenix-pdf-invoicing-for-woocommerce' ); ?>">
											<span class="dashicons dashicons-download" aria-hidden="true"></span>
										</a>
									</div>
								<?php endif; ?>
							</div>
						</div>
						<?php if ( ! $is_ungenerated ) : ?>
							<div class="kitgenix-doc-row__line">
								<span class="kitgenix-doc-row__identifier"><?php echo esc_html( $identifier ); ?></span>

								<div class="kitgenix-icon-actions">
									<?php if ( $available ) : ?>
										<a href="<?php echo esc_url( $stream_url ); ?>" class="kitgenix-icon-button" target="_blank" title="<?php esc_attr_e( 'Download (PDF)', 'kitgenix-pdf-invoicing-for-woocommerce' ); ?>" aria-label="<?php esc_attr_e( 'Download (PDF)', 'kitgenix-pdf-invoicing-for-woocommerce' ); ?>">
											<span class="dashicons dashicons-download" aria-hidden="true"></span>
										</a>
									<?php endif; ?>

									<?php if ( $can_manage_status ) :
										$regenerate_url = wp_nonce_url(
											admin_url( 'admin-post.php?action=kitgenix_admin_regenerate_document&order_id=' . $order->get_id() . '&doc_type=' . rawurlencode( $type ) ),
											'kitgenix_admin_document_status_' . $type . '_' . $order->get_id()
										);
										$void_url = wp_nonce_url(
											admin_url( 'admin-post.php?action=kitgenix_admin_void_document&order_id=' . $order->get_id() . '&doc_type=' . rawurlencode( $type ) . '&void=' . ( 'voided' === $status ? '0' : '1' ) ),
											'kitgenix_admin_document_status_' . $type . '_' . $order->get_id()
										);
										$void_label = 'voided' === $status
											? __( 'Unvoid', 'kitgenix-pdf-invoicing-for-woocommerce' )
											: __( 'Void', 'kitgenix-pdf-invoicing-for-woocommerce' );
										?>
										<a href="<?php echo esc_url( $regenerate_url ); ?>" class="kitgenix-icon-button" onclick="if(!confirm('<?php echo esc_js( __( 'Regenerate this PDF from the order\'s current data? The previously issued file is kept (not deleted) and this is recorded in the document history.', 'kitgenix-pdf-invoicing-for-woocommerce' ) ); ?>')){return false;} this.classList.add('is-loading'); return true;" title="<?php esc_attr_e( 'Regenerate', 'kitgenix-pdf-invoicing-for-woocommerce' ); ?>" aria-label="<?php esc_attr_e( 'Regenerate', 'kitgenix-pdf-invoicing-for-woocommerce' ); ?>">
											<span class="dashicons dashicons-update" aria-hidden="true"></span>
										</a>
										<a href="<?php echo esc_url( $void_url ); ?>" class="kitgenix-icon-button" onclick="this.classList.add('is-loading');" title="<?php echo esc_attr( $void_label ); ?>" aria-label="<?php echo esc_attr( $void_label ); ?>">
											<span class="dashicons <?php echo 'voided' === $status ? 'dashicons-visibility' : 'dashicons-hidden'; ?>" aria-hidden="true"></span>
										</a>
									<?php endif; ?>

									<?php if ( ! empty( $history ) ) : ?>
										<button type="button" class="kitgenix-icon-button" data-kitgenix-modal-target="#kitgenix-pdf-invoicing-history-modal-<?php echo esc_attr( $type ); ?>" title="<?php esc_attr_e( 'History', 'kitgenix-pdf-invoicing-for-woocommerce' ); ?>" aria-label="<?php esc_attr_e( 'History', 'kitgenix-pdf-invoicing-for-woocommerce' ); ?>">
											<span class="dashicons dashicons-backup" aria-hidden="true"></span>
										</button>
									<?php endif; ?>
								</div>
							</div>
						<?php endif; ?>
					</div>
				<?php endforeach; ?>
			</div>
			<?php if ( $can_manage_status ) : ?>
				<p class="description kitgenix-document-status-note">
					<?php esc_html_e( 'Regenerating keeps the previously issued file and logs the change. Voiding only flags a document – it never deletes or alters the archived PDF. Neither action alone guarantees compliance with your local invoicing/record-keeping law.', 'kitgenix-pdf-invoicing-for-woocommerce' ); ?>
				</p>
			<?php endif; ?>
		</div>
		<?php
	}

	/**
	 * Render one hidden modal per document type that has history, into the
	 * admin footer (kept out of the metabox itself, consistent with the
	 * other Kitgenix order-screen modals). Opened via the plain
	 * data-kitgenix-modal-target markup above – kitgenix-admin-components.js
	 * (enqueued in enqueue_assets()) handles the open/close/escape behavior,
	 * no page-specific JS required.
	 */
	public static function render_history_modals(): void {
		$screen = get_current_screen();
		if ( ! $screen ) {
			return;
		}

		$allowed_screens = [ 'shop_order' ];
		if ( function_exists( 'wc_get_page_screen_id' ) ) {
			$allowed_screens[] = \wc_get_page_screen_id( 'shop-order' );
		}

		if ( ! in_array( $screen->id, $allowed_screens, true ) ) {
			return;
		}

		$order = self::resolve_current_order();
		if ( ! $order instanceof \WC_Order ) {
			return;
		}

		if ( ! self::$pdf ) {
			$renderer  = new TemplateRenderer();
			self::$pdf = new PdfGenerator( $renderer );
		}

		foreach ( DocumentTypes::all() as $type ) {
			$status    = self::$pdf->get_document_status( $order, $type );
			$available = self::$pdf->is_document_available_for_order( $order, $type );
			if ( ! $available && 'not_generated' === $status ) {
				continue;
			}

			$history = self::$pdf->get_document_history( $order, $type );
			if ( empty( $history ) ) {
				continue;
			}

			$modal_id = 'kitgenix-pdf-invoicing-history-modal-' . $type;
			$title_id = $modal_id . '-title';
			?>
			<div id="<?php echo esc_attr( $modal_id ); ?>" class="kitgenix-modal" hidden role="dialog" aria-modal="true" aria-labelledby="<?php echo esc_attr( $title_id ); ?>">
				<div class="kitgenix-modal-backdrop" data-kitgenix-modal-close="1"></div>
				<div class="kitgenix-modal-dialog kitgenix-modal-dialog-lg" role="document">
					<div class="kitgenix-modal-header">
						<h2 class="kitgenix-modal-title" id="<?php echo esc_attr( $title_id ); ?>">
							<?php
							/* translators: %s: document type label, e.g. "Invoice" */
							printf( esc_html__( '%s history', 'kitgenix-pdf-invoicing-for-woocommerce' ), esc_html( DocumentTypes::get_label( $type ) ) );
							?>
						</h2>
						<button type="button" class="kitgenix-modal-close" data-kitgenix-modal-close="1" aria-label="<?php esc_attr_e( 'Close', 'kitgenix-pdf-invoicing-for-woocommerce' ); ?>">&times;</button>
					</div>
					<div class="kitgenix-modal-body">
						<table class="kitgenix-table kitgenix-document-history-table">
							<thead>
								<tr>
									<th><?php esc_html_e( 'Event', 'kitgenix-pdf-invoicing-for-woocommerce' ); ?></th>
									<th><?php esc_html_e( 'Date', 'kitgenix-pdf-invoicing-for-woocommerce' ); ?></th>
									<th><?php esc_html_e( 'Number', 'kitgenix-pdf-invoicing-for-woocommerce' ); ?></th>
									<th><?php esc_html_e( 'By', 'kitgenix-pdf-invoicing-for-woocommerce' ); ?></th>
									<th><?php esc_html_e( 'Note', 'kitgenix-pdf-invoicing-for-woocommerce' ); ?></th>
								</tr>
							</thead>
							<tbody>
								<?php foreach ( $history as $event ) :
									$by_id   = isset( $event['by'] ) ? (int) $event['by'] : 0;
									$by_user = $by_id > 0 ? get_userdata( $by_id ) : false;
									$by_name = $by_user ? $by_user->display_name : __( 'System', 'kitgenix-pdf-invoicing-for-woocommerce' );
									?>
									<tr>
										<td><?php echo esc_html( ucfirst( (string) ( $event['event'] ?? '' ) ) ); ?></td>
										<td><?php echo esc_html( (string) ( $event['at'] ?? '' ) ); ?></td>
										<td><?php echo esc_html( (string) ( $event['identifier'] ?? '' ) ); ?></td>
										<td><?php echo esc_html( $by_name ); ?></td>
										<td><?php echo esc_html( (string) ( $event['note'] ?? '' ) ); ?></td>
									</tr>
								<?php endforeach; ?>
							</tbody>
						</table>
					</div>
				</div>
			</div>
			<?php
		}
	}

	private static function stream_requested_document( string $type ): void {
		if ( ! isset( $_GET['order_id'] ) ) {
			wp_die( esc_html__( 'Missing order ID.', 'kitgenix-pdf-invoicing-for-woocommerce' ) );
		}

		$order_id = absint( wp_unslash( $_GET['order_id'] ) );
		if ( ! current_user_can( 'edit_shop_order', $order_id ) ) {
			wp_die( esc_html__( 'Insufficient permissions.', 'kitgenix-pdf-invoicing-for-woocommerce' ) );
		}

		$nonce = isset( $_GET['nonce'] ) ? sanitize_text_field( wp_unslash( $_GET['nonce'] ) ) : '';
		if ( '' === $nonce ) {
			wp_die( esc_html__( 'Invalid nonce.', 'kitgenix-pdf-invoicing-for-woocommerce' ) );
		}
		if ( ! wp_verify_nonce( $nonce, 'kitgenix_admin_pdf' ) ) {
			wp_die( esc_html__( 'Invalid nonce.', 'kitgenix-pdf-invoicing-for-woocommerce' ) );
		}

		if ( ! self::$pdf ) {
			$renderer = new TemplateRenderer();
			self::$pdf = new PdfGenerator( $renderer );
		}

		self::$pdf->stream_document( $order_id, $type );
		exit;
	}

	public static function handle_admin_stream_invoice(): void {
		self::stream_requested_document( DocumentTypes::INVOICE );
	}

	public static function handle_admin_stream_credit_note(): void {
		self::stream_requested_document( DocumentTypes::CREDIT_NOTE );
	}

	public static function handle_admin_stream_receipt(): void {
		self::stream_requested_document( DocumentTypes::RECEIPT );
	}

	public static function handle_admin_stream_packing_slip(): void {
		self::stream_requested_document( DocumentTypes::PACKING_SLIP );
	}

	public static function handle_admin_stream_pro_forma_invoice(): void {
		self::stream_requested_document( DocumentTypes::PRO_FORMA_INVOICE );
	}

	public static function handle_admin_stream_delivery_note(): void {
		self::stream_requested_document( DocumentTypes::DELIVERY_NOTE );
	}

	public static function handle_admin_stream_statement(): void {
		self::stream_requested_document( DocumentTypes::STATEMENT );
	}

	/**
	 * Shared guard for the regenerate/void admin-post actions: validates
	 * order id + document type + capability + a type-and-order-scoped
	 * nonce, and returns the resolved order or dies.
	 */
	private static function guard_document_status_action(): array {
		$order_id = isset( $_GET['order_id'] ) ? absint( wp_unslash( $_GET['order_id'] ) ) : 0;
		$doc_type = isset( $_GET['doc_type'] ) ? sanitize_key( wp_unslash( $_GET['doc_type'] ) ) : '';

		if ( $order_id < 1 || ! in_array( $doc_type, DocumentTypes::all(), true ) ) {
			wp_die( esc_html__( 'Invalid request.', 'kitgenix-pdf-invoicing-for-woocommerce' ) );
		}

		// Deliberately a higher bar than plain document download: this
		// changes the audit trail of an issued financial document.
		if ( ! current_user_can( 'manage_woocommerce' ) ) {
			wp_die( esc_html__( 'Insufficient permissions.', 'kitgenix-pdf-invoicing-for-woocommerce' ) );
		}

		check_admin_referer( 'kitgenix_admin_document_status_' . $doc_type . '_' . $order_id );

		$order = wc_get_order( $order_id );
		if ( ! $order instanceof \WC_Order ) {
			wp_die( esc_html__( 'Order not found.', 'kitgenix-pdf-invoicing-for-woocommerce' ) );
		}

		if ( ! self::$pdf ) {
			$renderer  = new TemplateRenderer();
			self::$pdf = new PdfGenerator( $renderer );
		}

		return [ $order, $doc_type ];
	}

	private static function redirect_back_to_order( \WC_Order $order ): void {
		$edit_url = $order->get_edit_order_url();
		wp_safe_redirect( $edit_url ? $edit_url : admin_url() );
		exit;
	}

	public static function handle_regenerate_document(): void {
		[ $order, $doc_type ] = self::guard_document_status_action();

		// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- verified above via check_admin_referer() inside guard_document_status_action().
		$reason = isset( $_GET['reason'] ) ? sanitize_text_field( wp_unslash( $_GET['reason'] ) ) : '';
		self::$pdf->regenerate_document( $order, $doc_type, $reason );

		self::redirect_back_to_order( $order );
	}

	public static function handle_void_document(): void {
		[ $order, $doc_type ] = self::guard_document_status_action();

		// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- verified above via check_admin_referer() inside guard_document_status_action().
		$voided = isset( $_GET['void'] ) ? '1' === sanitize_text_field( wp_unslash( $_GET['void'] ) ) : true;
		self::$pdf->set_document_voided( $order, $doc_type, $voided );

		self::redirect_back_to_order( $order );
	}
}
