<?php
/**
 * @var array<string, string> $parcelshop_data            .
 * @var string                $customer_shipping_country .
 * @var string                $cart_contents_weight      .
 * @var string                $api_error                 .
 * @var string                $point_type                .
 * @var array<string, string> $point_types               .
 * @var array<string, string> $point_cods                .
 *
 * @package DhlPolska\Point
 */

use DhlPolska\Point\CheckoutChoosePointField;
use DhlPolska\Shipment\ShipmentData;

defined( 'ABSPATH' ) || exit;
?>
<tr class="woocommerce-shipping-totals shipping">
	<th><?php esc_html_e( 'Punkt odbioru DHL:', 'dhl-polska' ); ?></th>
	<td>
		<strong class="js--dhl-polska-point-name-text">
			<?php
			$street_line = trim( (string) ( $parcelshop_data['street'] ?? '' ) . ' ' . (string) ( $parcelshop_data['streetNo'] ?? '' ) );
			$city_line   = trim( (string) ( $parcelshop_data['zip'] ?? '' ) . ' ' . (string) ( $parcelshop_data['city'] ?? '' ) );
			$sap_line    = trim( (string) ( $parcelshop_data['sap'] ?? '' ) );
			$id_line     = trim( (string) ( $parcelshop_data['id'] ?? '' ) );

			$label_parts = [];
			if ( '' !== (string) ( $parcelshop_data['name'] ?? '' ) ) {
				$label_parts[] = (string) $parcelshop_data['name'];
			}
			if ( '' !== $street_line ) {
				$label_parts[] = $street_line;
			}
			if ( '' !== $city_line ) {
				$label_parts[] = $city_line;
			}

			if ( empty( $label_parts ) && '' === $sap_line && '' === $id_line ) :
				esc_html_e( 'Nie wybrano punktu odbioru', 'dhl-polska' );
			else :
				foreach ( $label_parts as $index => $line ) :
					echo esc_html( $line );
					if ( $index !== count( $label_parts ) - 1 ) :
						echo '<br/>';
					endif;
				endforeach;

				$identifier = '' !== $sap_line ? $sap_line : $id_line;
				if ( '' !== $identifier ) :
					if ( ! empty( $label_parts ) ) :
						echo '<br/>';
					endif;
					echo '(' . esc_html( $identifier ) . ')';
				endif;
			endif;
			?>
		</strong><br>
		[<a href="#0"
					data-country="<?php echo esc_attr( $customer_shipping_country ); ?>"
					data-parcel_weight="<?php echo esc_attr( $cart_contents_weight ); ?>"
					data-ptype="<?php echo esc_attr( $point_type ); ?>"
					<?php if ( ! empty( $point_types ) ) : ?>
						data-point-types="<?php echo esc_attr( wp_json_encode( $point_types ) ); ?>"
					<?php endif; ?>
					<?php if ( ! empty( $point_cods ) ) : ?>
						data-point-cod="<?php echo esc_attr( wp_json_encode( $point_cods ) ); ?>"
					<?php endif; ?>
					class="js--choose-point-dhl-polska"><?php esc_html_e( 'Wybierz punkt z mapy', 'dhl-polska' ); ?></a>]

		<?php
		$should_show_postnummer = 'DE' === strtoupper( (string) $customer_shipping_country )
			&& ShipmentData::POINT_TYPE_PACKSTATION === $point_type;
		?>
		<?php if ( $should_show_postnummer ) : ?>
			<p class="form-row form-row-wide">
				<label for="dhl_polska_postnummer">
					<?php esc_html_e( 'Postnummer', 'dhl-polska' ); ?>
					<abbr class="required" title="<?php esc_attr_e( 'wymagane', 'dhl-polska' ); ?>">*</abbr>
				</label>
				<input type="text"
						name="<?php echo esc_attr( CheckoutChoosePointField::FIELD_PARCELSHOP_POSTNUMMER ); ?>"
						id="dhl_polska_postnummer"
						value="<?php echo esc_attr( $parcelshop_data['postnummer'] ?? '' ); ?>"
						autocomplete="off"/>
				<span class="description">
					<?php esc_html_e( 'Wymagany dla odbioru w automacie paczkowym (Packstation) w Niemczech.', 'dhl-polska' ); ?>
				</span>
			</p>
		<?php endif; ?>

		<input type="hidden" name="<?php echo esc_attr( CheckoutChoosePointField::FIELD_PARCELSHOP_ID ); ?>"
				value="<?php echo esc_attr( $parcelshop_data['id'] ); ?>"
				class="js--parcelshop-id"/>
		<input type="hidden" name="<?php echo esc_attr( CheckoutChoosePointField::FIELD_PARCELSHOP_SAP ); ?>"
				value="<?php echo esc_attr( $parcelshop_data['sap'] ); ?>"
				class="js--parcelshop-sap"/>
		<input type="hidden" name="<?php echo esc_attr( CheckoutChoosePointField::FIELD_PARCELSHOP_NAME ); ?>"
				value="<?php echo esc_attr( $parcelshop_data['name'] ); ?>"
				class="js--parcelshop-name"/>
		<input type="hidden" name="<?php echo esc_attr( CheckoutChoosePointField::FIELD_PARCELSHOP_ZIP ); ?>"
				value="<?php echo esc_attr( $parcelshop_data['zip'] ); ?>"
				class="js--parcelshop-zip"/>
		<input type="hidden" name="<?php echo esc_attr( CheckoutChoosePointField::FIELD_PARCELSHOP_STREET ); ?>"
				value="<?php echo esc_attr( $parcelshop_data['street'] ); ?>"
				class="js--parcelshop-street"/>
		<input type="hidden" name="<?php echo esc_attr( CheckoutChoosePointField::FIELD_PARCELSHOP_CITY ); ?>"
				value="<?php echo esc_attr( $parcelshop_data['city'] ); ?>"
				class="js--parcelshop-city"/>
		<input type="hidden" name="<?php echo esc_attr( CheckoutChoosePointField::FIELD_PARCELSHOP_STREET_NO ); ?>"
				value="<?php echo esc_attr( $parcelshop_data['streetNo'] ); ?>"
				class="js--parcelshop-streetNo"/>
		<input type="hidden" name="<?php echo esc_attr( CheckoutChoosePointField::FIELD_PARCELSHOP_HOUSE_NO ); ?>"
				value="<?php echo esc_attr( $parcelshop_data['houseNo'] ); ?>"
				class="js--parcelshop-houseNo"/>
		<input type="hidden" name="<?php echo esc_attr( CheckoutChoosePointField::FIELD_API_ERROR ); ?>"
				value="<?php echo esc_attr( $api_error ); ?>"
				class="js--dhl-polska-api-error"/>

		<div class="js--dhl-polska-popup-container"></div>
	</td>
</tr>
