<?php
/**
 * @var array $alsendo_connect_metabox_order_data
 * @var int $order_id
 * @var string $id
 */


if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}

use Inspire_Labs\Alsendo_Connect\Service_Structure_Helper;
use Inspire_Labs\Alsendo_Connect\Shipping_Method_Alsendo_Connect;
use Inspire_Labs\Alsendo_Connect\Alsendo_API;

$srv_list           = ( new \Inspire_Labs\Alsendo_Connect\Alsendo_API() )->get_service_structure_apaczka();
$choosed_carrier    = '';
$carrier_service_id = '';
$shipping_method    = '';

$order_meta = alsendo_connect()->get_order_meta_data( $order_id );
if ( ! empty( $order_meta['alsendo_order']['service_id'] ) ) {
	foreach ( $srv_list as $service_id => $service ) {
		if ( $service_id == $order_meta['alsendo_order']['service_id'] ) {
			$choosed_carrier = $service['name'];
		}
	}
}
$tracking_url     = '';
$waybill_number   = '';
$apaczka_order_id = '';
if ( ! empty( $order_meta['alsendo_order'] ) ) {
	if ( is_array( $order_meta['alsendo_order'] ) ) {
		$tracking_url       = ! empty( $order_meta['alsendo_order']['tracking_url'] ) ? $order_meta['alsendo_order']['tracking_url'] : '';
		$waybill_number     = ! empty( $order_meta['alsendo_order']['waybill_number'] ) ? $order_meta['alsendo_order']['waybill_number'] : esc_html__( 'Waybill number is not found', 'alsendo-connect' );
		$apaczka_order_id   = ! empty( $order_meta['alsendo_order']['id'] ) ? $order_meta['alsendo_order']['id'] : '';
		$carrier_service_id = ! empty( $order_meta['alsendo_order']['service_id'] ) ? $order_meta['alsendo_order']['service_id'] : '';
		$shipping_method    = ! empty( $order_meta['package_properties']['shipping_method'] ) ? $order_meta['package_properties']['shipping_method'] : '';

	}
}
?>
<h3>
	<?php esc_html_e( 'Your order has been placed on Apaczka.pl', 'alsendo-connect' ); ?>
</h3>
<h4><?php esc_html_e( 'The waybill number is:', 'alsendo-connect' ); ?>
	<strong>
		<a target="_blank" href="<?php echo esc_url( $tracking_url ); ?>"><?php echo esc_attr( $waybill_number ); ?></a>
	</strong>
</h4>
<?php if ( $choosed_carrier ) { ?>
	<h4><?php esc_html_e( 'Carrier:', 'alsendo-connect' ); ?>
		<strong id="apaczka_choosed_carrier_id" data-id="<?php echo esc_attr( $carrier_service_id ); ?>" style="color:#36aa27">
			<?php echo esc_attr( $choosed_carrier ); ?>
		</strong>
	</h4>
<?php } ?>
<br/>
<button
		class="button-primary"
		id="apaczka_get_waybill">
		<?php
		esc_html_e( 'Get waybill', 'alsendo-connect' );
		?>
</button>
<br/>
<br/>
<button class="button-primary apaczka_cancel" id="apaczka_cancel"  data-apaczka-id="<?php echo esc_attr( $apaczka_order_id ); ?>">
		<?php esc_html_e( 'Cancel parcel', 'alsendo-connect' ); ?>
</button>
<?php if ( ! empty( $alsendo_connect_metabox_order_data['package_properties']['shipping_method'] ) && 'COURIER' === $alsendo_connect_metabox_order_data['package_properties']['shipping_method'] ) { ?>
	<button class="button-primary apaczka_download_turn_in"
			id="apaczka_download_turn_in"
			data-apaczka-id="<?php echo esc_attr( $apaczka_order_id ); ?>">
			<?php
			esc_html_e(
				'Download turn in protocol',
				'alsendo-connect'
			);
			?>
	</button>
	<?php
}
$alsendo_connect_preloader = alsendo_connect()->get_plugin_img_url() . '/animation-round-small.gif'; ?>
<span id="alsendo_connect_preloader"><img src="<?php echo esc_url( $alsendo_connect_preloader ); ?>"></span>
