<?php
/**
 * Settings screen markup.
 *
 * @package Harbor_EU_EInvoicing_For_WooCommerce
 *
 * @var array     $hein_settings Full settings array.
 * @var array     $hein_formats  Registered generators, slug => [ label, class ].
 * @var bool|null $hein_exposed  Whether the uploads mirror is publicly reachable; null = unknown.
 */

defined( 'ABSPATH' ) || exit;

$hein_text = static function ( string $path, string $label, string $description = '', string $type = 'text' ) use ( $hein_settings ) {
	$keys  = explode( '.', $path );
	$value = $hein_settings;

	foreach ( $keys as $key ) {
		$value = is_array( $value ) && isset( $value[ $key ] ) ? $value[ $key ] : '';
	}

	$id       = 'hein_' . implode( '_', $keys );
	$name     = 'hein_settings[' . implode( '][', $keys ) . ']';
	$help     = $id . '_help';
	$vat      = 'seller.vat_id' === $path;
	$feedback = $id . '_feedback';

	// The live validation region is useless to a screen reader unless the input points at it
	$described = trim( ( '' !== $description ? $help : '' ) . ( $vat ? ' ' . $feedback : '' ) );
	?>
	<tr>
		<th scope="row">
			<label for="<?php echo esc_attr( $id ); ?>"><?php echo esc_html( $label ); ?></label>
		</th>
		<td>
			<input
				type="<?php echo esc_attr( $type ); ?>"
				id="<?php echo esc_attr( $id ); ?>"
				name="<?php echo esc_attr( $name ); ?>"
				value="<?php echo esc_attr( (string) $value ); ?>"
				class="regular-text<?php echo $vat ? ' hein-vat-input' : ''; ?>"
				<?php echo $vat ? 'data-hein-vat-context="settings"' : ''; ?>
				<?php echo '' !== $described ? 'aria-describedby="' . esc_attr( $described ) . '"' : ''; ?>
			/>
			<?php if ( '' !== $description ) : ?>
				<p class="description" id="<?php echo esc_attr( $help ); ?>"><?php echo esc_html( $description ); ?></p>
			<?php endif; ?>
			<?php if ( $vat ) : ?>
				<p class="description hein-vat-feedback" id="<?php echo esc_attr( $feedback ); ?>" role="status" aria-live="polite"></p>
			<?php endif; ?>
		</td>
	</tr>
	<?php
};
?>
<div class="wrap hein-settings">
	<h1><?php esc_html_e( 'EU E-Invoicing', 'harbor-eu-einvoicing-for-woocommerce' ); ?></h1>

	<?php
	// Unfiltered: options.php registers its own "Settings saved." notice under the 'general' slug, so
	// filtering on 'hein_settings' would show this page's validation warnings but never a save confirmation
	settings_errors();
	?>

	<?php if ( false !== $hein_exposed ) : ?>
		<div class="notice notice-warning">
			<p>
				<?php if ( null === $hein_exposed ) : ?>
					<?php
					esc_html_e(
						'Could not confirm that the invoice folder in your uploads directory is protected. Invoices always download through WordPress, so the file copy below can be turned off.',
						'harbor-eu-einvoicing-for-woocommerce'
					);
					?>
				<?php else : ?>
					<?php
					esc_html_e(
						'The invoice folder in your uploads directory is publicly reachable. Turn off the file copy below: invoices always download through WordPress and do not need it.',
						'harbor-eu-einvoicing-for-woocommerce'
					);
					?>
				<?php endif; ?>
			</p>
		</div>
	<?php endif; ?>

	<form method="post" action="options.php">
		<?php settings_fields( 'hein_settings' ); ?>

		<h2><?php esc_html_e( 'Invoice profile', 'harbor-eu-einvoicing-for-woocommerce' ); ?></h2>
		<table class="form-table" role="presentation">
			<tr>
				<th scope="row">
					<label for="hein_profile"><?php esc_html_e( 'Profile', 'harbor-eu-einvoicing-for-woocommerce' ); ?></label>
				</th>
				<td>
					<select id="hein_profile" name="hein_settings[profile]" aria-describedby="hein_profile_help">
						<option value="peppol" <?php selected( 'peppol', $hein_settings['profile'] ); ?>>
							<?php esc_html_e( 'Peppol BIS Billing 3.0', 'harbor-eu-einvoicing-for-woocommerce' ); ?>
						</option>
						<option value="en16931" <?php selected( 'en16931', $hein_settings['profile'] ); ?>>
							<?php esc_html_e( 'EN 16931 (also for consumer sales)', 'harbor-eu-einvoicing-for-woocommerce' ); ?>
						</option>
					</select>
					<p class="description" id="hein_profile_help">
						<?php
						esc_html_e(
							'Peppol requires an electronic address for both parties, so the buyer must have a valid VAT number. Choose EN 16931 if you also invoice consumers.',
							'harbor-eu-einvoicing-for-woocommerce'
						);
						?>
					</p>
				</td>
			</tr>
			<tr>
				<th scope="row"><?php esc_html_e( 'Formats', 'harbor-eu-einvoicing-for-woocommerce' ); ?></th>
				<td>
					<fieldset>
						<legend class="screen-reader-text">
							<?php esc_html_e( 'Invoice formats', 'harbor-eu-einvoicing-for-woocommerce' ); ?>
						</legend>
						<?php foreach ( $hein_formats as $hein_slug => $hein_format ) : ?>
							<label for="<?php echo esc_attr( 'hein_format_' . $hein_slug ); ?>">
								<input
									type="checkbox"
									id="<?php echo esc_attr( 'hein_format_' . $hein_slug ); ?>"
									name="hein_settings[formats][]"
									value="<?php echo esc_attr( $hein_slug ); ?>"
									<?php checked( in_array( (string) $hein_slug, (array) $hein_settings['formats'], true ) ); ?>
								/>
								<?php echo esc_html( $hein_format['label'] ); ?>
							</label><br />
						<?php endforeach; ?>
					</fieldset>
					<?php do_action( 'hein_format_options', $hein_settings ); ?>
				</td>
			</tr>
			<tr>
				<th scope="row">
					<label for="hein_zero_rate_category">
						<?php esc_html_e( 'Zero-rated category', 'harbor-eu-einvoicing-for-woocommerce' ); ?>
					</label>
				</th>
				<td>
					<select
						id="hein_zero_rate_category"
						name="hein_settings[zero_rate_category]"
						aria-describedby="hein_zero_rate_category_help"
					>
						<option value="Z" <?php selected( 'Z', $hein_settings['zero_rate_category'] ); ?>>
							<?php esc_html_e( 'Zero rated', 'harbor-eu-einvoicing-for-woocommerce' ); ?>
						</option>
						<option value="E" <?php selected( 'E', $hein_settings['zero_rate_category'] ); ?>>
							<?php esc_html_e( 'Exempt from VAT', 'harbor-eu-einvoicing-for-woocommerce' ); ?>
						</option>
					</select>
					<p class="description" id="hein_zero_rate_category_help">
						<?php
						esc_html_e(
							'Used for a line with no VAT that is neither an export nor an intra-community supply. An exempt line needs an exemption code or reason.',
							'harbor-eu-einvoicing-for-woocommerce'
						);
						?>
					</p>
				</td>
			</tr>
			<?php
			$hein_text(
				'exemption_code',
				__( 'Exemption code', 'harbor-eu-einvoicing-for-woocommerce' ),
				__( 'A VATEX code, for example VATEX-EU-132-1A.', 'harbor-eu-einvoicing-for-woocommerce' )
			);
			$hein_text(
				'exemption_reason',
				__( 'Exemption reason', 'harbor-eu-einvoicing-for-woocommerce' ),
				__( 'Required when the exempt category is used and no code is set.', 'harbor-eu-einvoicing-for-woocommerce' )
			);
			?>
		</table>

		<h2><?php esc_html_e( 'Seller', 'harbor-eu-einvoicing-for-woocommerce' ); ?></h2>
		<table class="form-table" role="presentation">
			<?php
			$hein_text(
				'seller.name',
				__( 'Legal name', 'harbor-eu-einvoicing-for-woocommerce' ),
				__( 'The registered company name. Required.', 'harbor-eu-einvoicing-for-woocommerce' )
			);
			$hein_text(
				'seller.trading_name',
				__( 'Trading name', 'harbor-eu-einvoicing-for-woocommerce' )
			);
			$hein_text(
				'seller.vat_id',
				__( 'VAT number', 'harbor-eu-einvoicing-for-woocommerce' ),
				__( 'Include the country prefix. Without a VAT number, invoices are not subject to VAT.', 'harbor-eu-einvoicing-for-woocommerce' )
			);
			$hein_text(
				'seller.tax_registration',
				__( 'Tax registration', 'harbor-eu-einvoicing-for-woocommerce' ),
				__( 'A local tax number, where one is used alongside the VAT number.', 'harbor-eu-einvoicing-for-woocommerce' )
			);
			$hein_text(
				'seller.legal_id',
				__( 'Legal registration number', 'harbor-eu-einvoicing-for-woocommerce' ),
				__( 'Danish sellers must enter a CVR number. Dutch sellers may enter a KVK number.', 'harbor-eu-einvoicing-for-woocommerce' )
			);
			$hein_text(
				'seller.legal_id_scheme',
				__( 'Legal registration scheme', 'harbor-eu-einvoicing-for-woocommerce' ),
				__( 'Denmark: 0184. Netherlands: 0106 or 0190. Leave empty elsewhere.', 'harbor-eu-einvoicing-for-woocommerce' )
			);
			$hein_text(
				'seller.street',
				__( 'Street', 'harbor-eu-einvoicing-for-woocommerce' ),
				__( 'Required.', 'harbor-eu-einvoicing-for-woocommerce' )
			);
			$hein_text(
				'seller.street2',
				__( 'Street (line 2)', 'harbor-eu-einvoicing-for-woocommerce' )
			);
			$hein_text(
				'seller.city',
				__( 'City', 'harbor-eu-einvoicing-for-woocommerce' ),
				__( 'Required.', 'harbor-eu-einvoicing-for-woocommerce' )
			);
			$hein_text(
				'seller.postcode',
				__( 'Postcode', 'harbor-eu-einvoicing-for-woocommerce' ),
				__( 'Required.', 'harbor-eu-einvoicing-for-woocommerce' )
			);
			$hein_text(
				'seller.subentity',
				__( 'Region', 'harbor-eu-einvoicing-for-woocommerce' )
			);
			$hein_text(
				'seller.country',
				__( 'Country code', 'harbor-eu-einvoicing-for-woocommerce' ),
				__( 'Two letters, for example DE. Required.', 'harbor-eu-einvoicing-for-woocommerce' )
			);
			$hein_text(
				'seller.contact_name',
				__( 'Contact name', 'harbor-eu-einvoicing-for-woocommerce' ),
				__( 'Required: German invoices are rejected without it.', 'harbor-eu-einvoicing-for-woocommerce' )
			);
			$hein_text(
				'seller.contact_phone',
				__( 'Contact phone', 'harbor-eu-einvoicing-for-woocommerce' ),
				__( 'Required: German invoices are rejected without it.', 'harbor-eu-einvoicing-for-woocommerce' )
			);
			$hein_text(
				'seller.contact_email',
				__( 'Contact email', 'harbor-eu-einvoicing-for-woocommerce' ),
				__( 'Required: German invoices are rejected without it.', 'harbor-eu-einvoicing-for-woocommerce' ),
				'email'
			);
			$hein_text(
				'seller.endpoint_id',
				__( 'Electronic address', 'harbor-eu-einvoicing-for-woocommerce' ),
				__( 'Leave empty to derive it from the VAT number.', 'harbor-eu-einvoicing-for-woocommerce' )
			);
			$hein_text(
				'seller.endpoint_scheme',
				__( 'Electronic address scheme', 'harbor-eu-einvoicing-for-woocommerce' ),
				__( 'The Peppol EAS code, for example 9930 for Germany.', 'harbor-eu-einvoicing-for-woocommerce' )
			);
			?>
		</table>

		<h2><?php esc_html_e( 'Payment', 'harbor-eu-einvoicing-for-woocommerce' ); ?></h2>
		<table class="form-table" role="presentation">
			<?php
			$hein_text(
				'payment.iban',
				__( 'IBAN', 'harbor-eu-einvoicing-for-woocommerce' ),
				__( 'A bank transfer is only recorded as a credit transfer when an IBAN is set.', 'harbor-eu-einvoicing-for-woocommerce' )
			);
			$hein_text(
				'payment.bic',
				__( 'BIC', 'harbor-eu-einvoicing-for-woocommerce' )
			);
			$hein_text(
				'payment.account_name',
				__( 'Account holder', 'harbor-eu-einvoicing-for-woocommerce' )
			);
			$hein_text(
				'payment.terms_note',
				__( 'Payment terms', 'harbor-eu-einvoicing-for-woocommerce' ),
				__( 'Shown on the invoice. A default is used when this is empty.', 'harbor-eu-einvoicing-for-woocommerce' )
			);
			$hein_text(
				'payment.due_days',
				__( 'Payment due in days', 'harbor-eu-einvoicing-for-woocommerce' ),
				__( 'Zero means no due date. A paid order never carries one.', 'harbor-eu-einvoicing-for-woocommerce' ),
				'number'
			);
			?>
		</table>

		<h2><?php esc_html_e( 'Invoice numbers', 'harbor-eu-einvoicing-for-woocommerce' ); ?></h2>
		<table class="form-table" role="presentation">
			<?php
			$hein_text(
				'number.prefix',
				__( 'Number prefix', 'harbor-eu-einvoicing-for-woocommerce' )
			);
			$hein_text(
				'number.padding',
				__( 'Number padding', 'harbor-eu-einvoicing-for-woocommerce' ),
				__( 'How many digits the sequence is padded to.', 'harbor-eu-einvoicing-for-woocommerce' ),
				'number'
			);
			?>
			<tr>
				<th scope="row">
					<label for="hein_number_reset">
						<?php esc_html_e( 'Reset the sequence', 'harbor-eu-einvoicing-for-woocommerce' ); ?>
					</label>
				</th>
				<td>
					<select id="hein_number_reset" name="hein_settings[number][reset]" aria-describedby="hein_number_reset_help">
						<?php
						$hein_resets = [
							'none'    => __( 'Never', 'harbor-eu-einvoicing-for-woocommerce' ),
							'yearly'  => __( 'Every year', 'harbor-eu-einvoicing-for-woocommerce' ),
							'monthly' => __( 'Every month', 'harbor-eu-einvoicing-for-woocommerce' ),
						];

						foreach ( $hein_resets as $hein_value => $hein_label ) :
							?>
							<option value="<?php echo esc_attr( $hein_value ); ?>" <?php selected( $hein_value, $hein_settings['number']['reset'] ); ?>>
								<?php echo esc_html( $hein_label ); ?>
							</option>
						<?php endforeach; ?>
					</select>
					<p class="description" id="hein_number_reset_help">
						<?php
						esc_html_e(
							'Invoice numbers are issued in an unbroken sequence and never change once assigned.',
							'harbor-eu-einvoicing-for-woocommerce'
						);
						?>
					</p>
				</td>
			</tr>
		</table>

		<h2><?php esc_html_e( 'Storage', 'harbor-eu-einvoicing-for-woocommerce' ); ?></h2>
		<table class="form-table" role="presentation">
			<tr>
				<th scope="row"><?php esc_html_e( 'File copy', 'harbor-eu-einvoicing-for-woocommerce' ); ?></th>
				<td>
					<label for="hein_store_files">
						<input
							type="checkbox"
							id="hein_store_files"
							name="hein_settings[store_files]"
							value="1"
							<?php checked( 1, (int) $hein_settings['store_files'] ); ?>
							aria-describedby="hein_store_files_help"
						/>
						<?php esc_html_e( 'Also keep a copy of each invoice in the uploads folder', 'harbor-eu-einvoicing-for-woocommerce' ); ?>
					</label>
					<p class="description" id="hein_store_files_help">
						<?php
						esc_html_e(
							'Every invoice is stored in the database and downloaded through WordPress. This extra copy is optional.',
							'harbor-eu-einvoicing-for-woocommerce'
						);
						?>
					</p>
				</td>
			</tr>
			<tr>
				<th scope="row"><?php esc_html_e( 'Uninstall', 'harbor-eu-einvoicing-for-woocommerce' ); ?></th>
				<td>
					<label for="hein_delete_data_on_uninstall">
						<input
							type="checkbox"
							id="hein_delete_data_on_uninstall"
							name="hein_settings[delete_data_on_uninstall]"
							value="1"
							<?php checked( 1, (int) $hein_settings['delete_data_on_uninstall'] ); ?>
							aria-describedby="hein_delete_data_on_uninstall_help"
						/>
						<?php esc_html_e( 'Delete all invoices and settings when the plugin is deleted', 'harbor-eu-einvoicing-for-woocommerce' ); ?>
					</label>
					<p class="description" id="hein_delete_data_on_uninstall_help">
						<?php
						esc_html_e(
							'Issued invoices are records you are normally required to keep, so they are retained by default.',
							'harbor-eu-einvoicing-for-woocommerce'
						);
						?>
					</p>
				</td>
			</tr>
		</table>

		<?php submit_button(); ?>
	</form>
</div>
