<?php
/**
 * @var array<string, mixed> $template
 * @var string              $weight_unit
 * @var string              $dimension_unit
 * @var array<string, string> $type_options
 * @var array<string, array{field: string, label: string}> $service_fields
 */

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


$weight       = $template['weight'] ?? '';
$package_type = $template['type'] ?? '';
$length       = $template['dimensions']['length'] ?? '';
$width        = $template['dimensions']['width'] ?? '';
$height       = $template['dimensions']['height'] ?? '';
$services     = is_array( $template['services'] ?? null ) ? $template['services'] : [];

?>
<table class="form-table">
	<tbody>
		<tr>
			<th scope="row">
				<label for="dhl-polska-template-type">
					<?php esc_html_e( 'Typ przesyłki', 'dhl-polska' ); ?>
				</label>
			</th>
			<td>
				<select
					id="dhl-polska-template-type"
					name="<?php echo esc_attr( \DhlPolska\ShipmentTemplates\ShipmentTemplateMetabox::FIELD_TYPE ); ?>"
				>
					<?php foreach ( $type_options as $value => $label ) : ?>
						<option value="<?php echo esc_attr( $value ); ?>" <?php selected( $package_type, $value ); ?>>
							<?php echo esc_html( $label ); ?>
						</option>
					<?php endforeach; ?>
				</select>
			</td>
		</tr>
		<tr>
			<th scope="row">
				<label for="dhl-polska-template-weight">
					<?php esc_html_e( 'Domyślna waga', 'dhl-polska' ); ?>
				</label>
				<span
					class="woocommerce-help-tip"
					data-tip="<?php echo esc_attr__( 'Domyślna waga zostanie użyta, gdy produkty w zamówieniu nie będą miały zdefiniowanej wagi i waga zamówienia będzie niemożliwa do obliczenia.', 'dhl-polska' ); ?>"
				></span>
			</th>
			<td>
				<input
					type="number"
					class="regular-text"
					id="dhl-polska-template-weight"
					name="<?php echo esc_attr( \DhlPolska\ShipmentTemplates\ShipmentTemplateMetabox::FIELD_WEIGHT ); ?>"
					value="<?php echo esc_attr( $weight ); ?>"
					min="0.01"
					step="0.01"
					required
				/>
				<span class="description"><?php echo esc_html( $weight_unit ); ?></span>
			</td>
		</tr>
		<tr>
			<th scope="row">
				<?php esc_html_e( 'Wymiary przesyłki', 'dhl-polska' ); ?>
			</th>
			<td>
				<label for="dhl-polska-template-length">
					<?php esc_html_e( 'Długość', 'dhl-polska' ); ?>
				</label>
				<input
					type="number"
					class="small-text"
					id="dhl-polska-template-length"
					name="<?php echo esc_attr( \DhlPolska\ShipmentTemplates\ShipmentTemplateMetabox::FIELD_LENGTH ); ?>"
					value="<?php echo esc_attr( $length ); ?>"
					min="1"
					step="1"
					required
				/>
				<label for="dhl-polska-template-width">
					<?php esc_html_e( 'Szerokość', 'dhl-polska' ); ?>
				</label>
				<input
					type="number"
					class="small-text"
					id="dhl-polska-template-width"
					name="<?php echo esc_attr( \DhlPolska\ShipmentTemplates\ShipmentTemplateMetabox::FIELD_WIDTH ); ?>"
					value="<?php echo esc_attr( $width ); ?>"
					min="1"
					step="1"
					required
				/>
				<label for="dhl-polska-template-height">
					<?php esc_html_e( 'Wysokość', 'dhl-polska' ); ?>
				</label>
				<input
					type="number"
					class="small-text"
					id="dhl-polska-template-height"
					name="<?php echo esc_attr( \DhlPolska\ShipmentTemplates\ShipmentTemplateMetabox::FIELD_HEIGHT ); ?>"
					value="<?php echo esc_attr( $height ); ?>"
					min="1"
					step="1"
					required
				/>
				<span class="description"><?php echo esc_html( $dimension_unit ); ?></span>
			</td>
		</tr>
		<tr>
			<th scope="row">
				<?php esc_html_e( 'Usługi dodatkowe', 'dhl-polska' ); ?>
			</th>
			<td>
				<?php foreach ( $service_fields as $service_key => $service_data ) : ?>
					<?php $is_enabled = (bool) ( $services[ $service_key ] ?? false ); ?>
					<p>
						<label for="<?php echo esc_attr( $service_data['field'] ); ?>">
							<input
								type="checkbox"
								id="<?php echo esc_attr( $service_data['field'] ); ?>"
								name="<?php echo esc_attr( $service_data['field'] ); ?>"
								<?php checked( $is_enabled ); ?>
							/>
							<?php echo esc_html( $service_data['label'] ); ?>
						</label>
					</p>
				<?php endforeach; ?>
			</td>
		</tr>
	</tbody>
</table>

<?php wp_nonce_field( \DhlPolska\ShipmentTemplates\ShipmentTemplateMetabox::NONCE_ACTION, \DhlPolska\ShipmentTemplates\ShipmentTemplateMetabox::NONCE_FIELD ); ?>
