<?php
/**
 * @var \DhlPolska\Shipment\ShipmentData $shipment_data
 * @var string $id
 * @var string $tracking_number
 * @var string $tracking_url
 * @var string $label_url
 * @var string|null $return_label_url
 * @var bool $has_return_label
 * @var string $return_label_number
 * @var string $return_label_tracking_url
 *
 * @package DhlPolska\Shipment
 */

use DhlPolska\Shipment\ShipmentData;
use DhlPolska\Shipment\DhlServices;

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


$disabled = ! empty( $tracking_number );

$cod_status         = $shipment_data->get_cod_status();
$insurance_status   = $shipment_data->get_insurance_status();
$courier_booking_id = $shipment_data->get_courier_booking_id();

woocommerce_form_field(
	ShipmentData::OPTION_SERVICE,
	[
		'type'              => 'select',
		'label'             => __( 'Usługa', 'dhl-polska' ),
		'options'           => ( new DhlServices() )->get_all_services(),
		'input_class'       => [ 'js--dhl-service-select' ],
		'custom_attributes' => $disabled ? [ 'disabled' => 'disabled' ] : [],
	],
	(string) $shipment_data->get_service()
);

woocommerce_form_field(
	ShipmentData::OPTION_SHIPMENT_DATE,
	[
		'type'              => 'date',
		'label'             => __( 'Data nadania', 'dhl-polska' ),
		'custom_attributes' => $disabled ? [ 'disabled' => 'disabled' ] : [],
	],
	$shipment_data->get_shipment_date_for_metabox()
);

$parcelshop_data = $shipment_data->get_parcelshop_data();

foreach ( [
	ShipmentData::OPTION_PARCELSHOP_ID,
	ShipmentData::OPTION_PARCELSHOP_SAP,
	ShipmentData::OPTION_PARCELSHOP_NAME,
	ShipmentData::OPTION_PARCELSHOP_ZIP,
	ShipmentData::OPTION_PARCELSHOP_STREET,
	ShipmentData::OPTION_PARCELSHOP_CITY,
	ShipmentData::OPTION_PARCELSHOP_STREET_NO,
	ShipmentData::OPTION_PARCELSHOP_HOUSE_NO,
] as $field ) {
	$field_key = str_replace( 'parcelshop_', '', $field );
	echo '<input type="hidden" name="' . esc_attr( $field ) . '" class="js--parcelshop-' . esc_attr( $field_key ) . '" value="' . esc_attr( $parcelshop_data[ $field_key ] ?? '' ) . '">';
}

$service_point_class    = $shipment_data->get_service() === DhlServices::SERVICE_POINT ? '' : 'hidden';
$shipping_country       = strtoupper( (string) $shipment_data->get_shipping_country() );
$point_type             = $shipment_data->get_point_type();
$should_show_point_type = $shipment_data->get_service() === DhlServices::SERVICE_POINT && 'PL' !== $shipping_country;
$should_show_postnummer = $shipment_data->get_service() === DhlServices::SERVICE_POINT
	&& 'DE' === $shipping_country
	&& ShipmentData::POINT_TYPE_PACKSTATION === $point_type;
$point_type_label       = '';
if ( $should_show_point_type ) {
	$point_type_labels = [
		ShipmentData::POINT_TYPE_PARCELSHOP  => __( 'Punkt partnerski', 'dhl-polska' ),
		ShipmentData::POINT_TYPE_PACKSTATION => __( 'Automat paczkowy', 'dhl-polska' ),
	];
	$point_type_label  = $point_type_labels[ $point_type ] ?? $point_type;
}

if ( $should_show_point_type ) {
	echo '<input type="hidden" name="' . esc_attr( ShipmentData::OPTION_POINT_TYPE ) . '" class="js--parcelshop-point-type" value="' . esc_attr( $shipment_data->get_point_type() ) . '">';
}
?>
<div class="js--parcelshop-info-container <?php echo esc_attr( $service_point_class ); ?>">
	<div class="js--parcelshop-info" style="padding: 10px; background-color: #f7f7f7; border: 1px solid #ccd0d4; margin-bottom: 10px;">
		<?php if ( $parcelshop_data['sap'] ) : ?>
			<strong><?php echo esc_html( $parcelshop_data['name'] ); ?></strong><br/>
			<?php echo esc_html( $parcelshop_data['street'] ); ?> <?php echo esc_html( $parcelshop_data['streetNo'] ); ?><br/>
			<?php echo esc_html( $parcelshop_data['zip'] ); ?> <?php echo esc_html( $parcelshop_data['city'] ); ?><br/>
			(<?php echo esc_html( $parcelshop_data['sap'] ); ?>)
		<?php else : ?>
			<i><?php esc_html_e( 'Nie wybrano punktu odbioru', 'dhl-polska' ); ?></i>
		<?php endif; ?>
		<?php if ( $should_show_point_type ) : ?>
			<div style="margin-top: 8px;">
				<strong><?php esc_html_e( 'Typ punktu', 'dhl-polska' ); ?>:</strong>
				<span class="js--parcelshop-point-type-label"><?php echo esc_html( $point_type_label ); ?></span>
			</div>
		<?php endif; ?>
	</div>
</div>
<?php if ( $should_show_point_type ) : ?>
	<p class="form-row js--service-point-map-button-container <?php echo esc_attr( $service_point_class ); ?>">
		<div class="js--parcelshop-point-type-buttons">
			<label><?php esc_html_e( 'Wybierz z mapy:', 'dhl-polska' ); ?></label>
			<button type="button" class="button js--dhl-polska-map-button" data-country="<?php echo esc_attr( $shipment_data->get_shipping_country() ); ?>" data-point-type="<?php echo esc_attr( ShipmentData::POINT_TYPE_PARCELSHOP ); ?>" <?php echo $disabled ? 'disabled="disabled"' : ''; ?>>
				<?php esc_html_e( 'Punkt partnerski', 'dhl-polska' ); ?>
			</button>
			<button type="button" class="button js--dhl-polska-map-button" data-country="<?php echo esc_attr( $shipment_data->get_shipping_country() ); ?>" data-point-type="<?php echo esc_attr( ShipmentData::POINT_TYPE_PACKSTATION ); ?>" <?php echo $disabled ? 'disabled="disabled"' : ''; ?>>
				<?php esc_html_e( 'Automat paczkowy', 'dhl-polska' ); ?>
			</button>
		</div>
	</p>
<?php else : ?>
	<p class="form-row js--service-point-map-button-container <?php echo esc_attr( $service_point_class ); ?>">
		<button type="button" class="button js--dhl-polska-map-button" data-country="<?php echo esc_attr( $shipment_data->get_shipping_country() ); ?>" <?php echo $disabled ? 'disabled="disabled"' : ''; ?>>
			<?php esc_html_e( 'Wybierz punkt z mapy', 'dhl-polska' ); ?>
		</button>
	</p>
<?php endif; ?>
<?php if ( $should_show_postnummer ) : ?>
	<?php
	woocommerce_form_field(
		ShipmentData::OPTION_PARCELSHOP_POSTNUMMER,
		[
			'type'              => 'text',
			'label'             => __( 'Postnummer', 'dhl-polska' ),
			'custom_attributes' => $disabled ? [ 'disabled' => 'disabled' ] : [],
		],
		(string) ( $parcelshop_data['postnummer'] ?? '' )
	);
	?>
<?php endif; ?>
<?php
woocommerce_form_field(
	ShipmentData::OPTION_PACKAGES,
	[
		'type'     => 'dhl_polska_packages',
		'packages' => $shipment_data->get_packages(),
		'disabled' => $disabled,
	]
);

woocommerce_form_field(
	ShipmentData::OPTION_COD,
	[
		'type'              => 'checkbox',
		'label'             => __( 'Pobranie (COD)', 'dhl-polska' ),
		'default'           => 'no',
		'input_class'       => [ 'js--conditional-field' ],
		'custom_attributes' => array_merge(
			[ 'data-fields' => ShipmentData::OPTION_COD_AMOUNT ],
			$disabled ? [ 'disabled' => 'disabled' ] : []
		),
	],
	$cod_status ? '1' : '0'
);

woocommerce_form_field(
	ShipmentData::OPTION_COD_AMOUNT,
	[
		'type'              => 'number',
		'label'             => __( 'Kwota pobrania', 'dhl-polska' ),
		'class'             => $cod_status ? [] : [ 'hidden' ],
		'custom_attributes' => array_merge( [ 'step' => '0.01' ], $disabled ? [ 'disabled' => 'disabled' ] : [] ),
	],
	wc_format_decimal( $shipment_data->get_cod_amount(), wc_get_price_decimals() )
);

woocommerce_form_field(
	ShipmentData::OPTION_INSURANCE,
	[
		'type'              => 'checkbox',
		'label'             => __( 'Ubezpieczenie', 'dhl-polska' ),
		'default'           => 'no',
		'input_class'       => [ 'js--conditional-field' ],
		'custom_attributes' => array_merge(
			[ 'data-fields' => ShipmentData::OPTION_INSURANCE_AMOUNT ],
			$disabled ? [ 'disabled' => 'disabled' ] : []
		),
	],
	$insurance_status ? '1' : '0'
);

woocommerce_form_field(
	ShipmentData::OPTION_INSURANCE_AMOUNT,
	[
		'type'              => 'number',
		'label'             => __( 'Kwota ubezpieczenia', 'dhl-polska' ),
		'class'             => $insurance_status ? [] : [ 'hidden' ],
		'custom_attributes' => array_merge( [ 'step' => '0.01' ], $disabled ? [ 'disabled' => 'disabled' ] : [] ),
	],
	wc_format_decimal( $shipment_data->get_insurance_amount(), wc_get_price_decimals() )
);

woocommerce_form_field(
	ShipmentData::OPTION_REFERENCES,
	[
		'type'              => 'text',
		'label'             => __( 'Referencje', 'dhl-polska' ),
		'custom_attributes' => $disabled ? [ 'disabled' => 'disabled' ] : [],
	],
	$shipment_data->get_references()
);

woocommerce_form_field(
	ShipmentData::OPTION_NOTES,
	[
		'type'              => 'textarea',
		'label'             => __( 'Uwagi', 'dhl-polska' ),
		'custom_attributes' => $disabled ? [ 'disabled' => 'disabled' ] : [],
	],
	$shipment_data->get_notes()
);
?>

<?php if ( '' !== $courier_booking_id ) : ?>
	<p>
		<?php
		echo esc_html(
			sprintf(
				// Translators: courier booking identifier.
				__( 'Zamówienie kuriera: %s', 'dhl-polska' ),
				$courier_booking_id
			)
		);
		?>
	</p>
<?php endif; ?>

<?php if ( $tracking_number ) : ?>
	<div class="dhl-polska-return-label-section">
		<div class="dhl-polska-return-label-section-info">
			<span class="dhl-polska-return-label-section-title">
				<?php esc_html_e( 'Etykieta zwrotna', 'dhl-polska' ); ?>
			</span>
			<?php if ( '' !== $return_label_number && '' !== $return_label_tracking_url ) : ?>
				<span class="dhl-polska-return-label-section-tracking">
					<?php esc_html_e( 'Numer śledzenia: ', 'dhl-polska' ); ?>
					<a target="_blank"
						href="<?php echo esc_url( $return_label_tracking_url ); ?>">
						<?php echo esc_html( $return_label_number ); ?>
					</a>
				</span>
			<?php endif; ?>
		</div>
		<div class="dhl-polska-return-label-section-actions">
			<?php if ( $has_return_label && $return_label_url ) : ?>
				<a href="<?php echo esc_url( $return_label_url ); ?>"
					class="button button-shipping">
					<?php esc_html_e( 'Pobierz', 'dhl-polska' ); ?>
				</a>
				<button data-integration="dhl_polska" data-action="delete_return_label"
						data-id="<?php echo esc_attr( $id ); ?>"
						class="button button-shipping js--button">
					<?php esc_html_e( 'Usuń', 'dhl-polska' ); ?>
				</button>
			<?php else : ?>
				<button data-integration="dhl_polska" data-action="create_return_label"
						data-id="<?php echo esc_attr( $id ); ?>"
						class="button button-shipping js--button">
					<?php esc_html_e( 'Utwórz', 'dhl-polska' ); ?>
				</button>
			<?php endif; ?>
		</div>
	</div>

	<p>
		<?php esc_html_e( 'Numer nadania: ', 'dhl-polska' ); ?>
		<a target="_blank"
			href="<?php echo esc_url( $tracking_url ); ?>"><?php echo esc_html( $tracking_number ); ?></a>
	</p>
	<div class="dhl-polska-metabox-buttons">
		<span class="spinner shipping-spinner"></span>

		<button data-integration="dhl_polska" data-action="delete"
				data-id="<?php echo esc_attr( $id ); ?>"
				class="button button-shipping js--button">
			<?php esc_html_e( 'Usuń', 'dhl-polska' ); ?>
		</button>

		<a href="<?php echo esc_url( $label_url ); ?>"
			class="button button-primary">
			<?php esc_html_e( 'Pobierz etykietę', 'dhl-polska' ); ?>
		</a>
	</div>
<?php else : ?>
	<div class="dhl-polska-metabox-buttons">
		<span class="spinner shipping-spinner"></span>

		<button data-integration="dhl_polska" data-action="save"
				data-id="<?php echo esc_attr( $id ); ?>"
				class="button button-shipping js--button">
			<?php esc_html_e( 'Zapisz', 'dhl-polska' ); ?>
		</button>

		<button data-integration="dhl_polska" data-action="create"
				data-id="<?php echo esc_attr( $id ); ?>"
				class="button button-primary button-shipping js--button">
			<?php esc_html_e( 'Utwórz przesyłkę', 'dhl-polska' ); ?>
		</button>
	</div>
<?php endif; ?>

<script type="text/javascript">
	jQuery( document ).ready( function () {
		if ( typeof dhl_polska_init === 'function' ) {
			dhl_polska_init();
		}
	} )
</script>
