<?php
/**
 * Order metabox markup.
 *
 * @package Harbor_EU_EInvoicing_For_WooCommerce
 *
 * @var WC_Order   $hein_order    The order.
 * @var int        $hein_id       The order id.
 * @var array      $hein_formats  Enabled generators, slug => [ label, class ].
 * @var array      $hein_stored   Stored invoice rows for this order.
 * @var array|null $hein_notice   The outcome of the last generate attempt.
 * @var string     $hein_number   The invoice number already assigned to this order.
 * @var float      $hein_refunded The total refunded on this order.
 * @var string     $hein_profile  The configured profile.
 */

defined( 'ABSPATH' ) || exit;

$hein_vat = HEIN_Checkout_Fields::vat( $hein_order );
$hein_po  = HEIN_Checkout_Fields::po( $hein_order );
?>
<div class="hein-metabox">
	<?php if ( null !== $hein_notice && 'error' === $hein_notice['type'] ) : ?>
		<div class="notice notice-error inline" role="alert" data-hein-error="<?php echo esc_attr( $hein_notice['code'] ); ?>">
			<p><?php echo esc_html( $hein_notice['message'] ); ?></p>
		</div>
	<?php elseif ( null !== $hein_notice ) : ?>
		<div class="notice notice-success inline" role="status">
			<p>
				<?php
				printf(
					/* translators: %s: the invoice number */
					esc_html__( 'Invoice %s generated.', 'harbor-eu-einvoicing-for-woocommerce' ),
					esc_html( $hein_notice['message'] )
				);
				?>
			</p>
		</div>
	<?php endif; ?>

	<?php if ( $hein_refunded > 0 ) : ?>
		<div class="notice notice-warning inline">
			<p>
				<?php
				esc_html_e(
					'This order has been refunded. The invoice documents the original supply; a refund is documented by a separate credit note.',
					'harbor-eu-einvoicing-for-woocommerce'
				);
				?>
			</p>
		</div>
	<?php endif; ?>

	<?php wp_nonce_field( 'hein_order_meta_' . $hein_id, 'hein_order_meta_nonce' ); ?>

	<p class="form-field hein-field">
		<label for="hein_vat_number"><?php esc_html_e( 'VAT number', 'harbor-eu-einvoicing-for-woocommerce' ); ?></label>
		<input
			type="text"
			id="hein_vat_number"
			name="hein_vat_number"
			class="hein-vat-input"
			data-hein-vat-context="order"
			value="<?php echo esc_attr( $hein_vat ); ?>"
			maxlength="20"
			aria-describedby="hein_vat_number_help"
		/>
		<span class="description hein-vat-feedback" id="hein_vat_number_help" role="status" aria-live="polite"></span>
	</p>

	<p class="form-field hein-field">
		<label for="hein_po_reference"><?php esc_html_e( 'Purchase order reference', 'harbor-eu-einvoicing-for-woocommerce' ); ?></label>
		<input
			type="text"
			id="hein_po_reference"
			name="hein_po_reference"
			value="<?php echo esc_attr( $hein_po ); ?>"
			maxlength="60"
		/>
	</p>

	<?php do_action( 'hein_order_metabox_fields', $hein_order ); ?>

	<?php if ( '' !== $hein_number ) : ?>
		<p class="hein-number">
			<strong><?php esc_html_e( 'Invoice number', 'harbor-eu-einvoicing-for-woocommerce' ); ?>:</strong>
			<?php echo esc_html( $hein_number ); ?>
		</p>
	<?php endif; ?>

	<?php if ( 'peppol' === $hein_profile && ! HEIN_Checkout_Fields::is_vat_valid( $hein_order ) ) : ?>
		<p class="description">
			<?php esc_html_e( 'The Peppol profile needs a valid buyer VAT number. Without one, use the EN 16931 profile in the settings.', 'harbor-eu-einvoicing-for-woocommerce' ); ?>
		</p>
	<?php endif; ?>

	<?php if ( $hein_formats ) : ?>
		<p class="hein-actions">
			<?php foreach ( $hein_formats as $hein_slug => $hein_format ) : ?>
				<a
					class="button"
					href="<?php echo esc_url( HEIN_Order_Actions::generate_url( $hein_id, (string) $hein_slug ) ); ?>"
				>
					<?php esc_html_e( 'Generate', 'harbor-eu-einvoicing-for-woocommerce' ); ?>
					<span class="screen-reader-text"><?php echo esc_html( $hein_format['label'] ); ?></span>
					<span aria-hidden="true"><?php echo esc_html( $hein_format['label'] ); ?></span>
				</a>
			<?php endforeach; ?>
		</p>
	<?php endif; ?>

	<?php if ( $hein_stored ) : ?>
		<ul class="hein-downloads">
			<?php foreach ( $hein_stored as $hein_row ) : ?>
				<li>
					<a href="<?php echo esc_url( HEIN_Order_Actions::download_url( $hein_id, (string) $hein_row['format'] ) ); ?>">
						<?php esc_html_e( 'Download', 'harbor-eu-einvoicing-for-woocommerce' ); ?>
						<span class="screen-reader-text"><?php echo esc_html( (string) $hein_row['format'] ); ?></span>
						<span aria-hidden="true"><?php echo esc_html( (string) $hein_row['format'] ); ?></span>
					</a>
					<?php if ( 'sent' === $hein_row['status'] ) : ?>
						<span class="hein-sent"><?php esc_html_e( 'sent', 'harbor-eu-einvoicing-for-woocommerce' ); ?></span>
					<?php endif; ?>
				</li>
			<?php endforeach; ?>
		</ul>
	<?php endif; ?>
</div>
