<?php
/**
 * @var array $calculate_order
 * @var array $calculate
 */


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


$skip_this_service = array( 27, 20, 81 );

use Inspire_Labs\Alsendo_Connect\Shipping_Method_Alsendo_Connect;

$single_offer = false;
$selected = '';
if( is_array( $calculate ) && 1 === count($calculate) ) {
    $single_offer = true;
}

?>

<fieldset id="apaczka_calculate_radio">
	<?php
	$first = true;
	$i     = 0;
	foreach ( $calculate as $k => $service ) {
        $checked = '';
		if ( in_array( $k, $skip_this_service ) ) {
			continue;
		}
		if ( $calculate_order['address']['sender']['is_residential'] === 1 && $k === 150 ) {
			continue; // exclude Geis for adres Prywatny.
		}

        if( $single_offer ) {
            $selected = ' selected';
            $checked = ' checked';
        }

		?>
		<label class="apaczka-calculate-item <?php echo esc_attr( $selected ); ?>"
				data-item="<?php echo esc_attr( $i ); ?>"
				data-pickup_courier="<?php echo esc_attr( $service['pickup_courier'] ); ?>"
				data-service_id="<?php echo esc_attr( $k ); ?>"
				data-supplier="<?php echo esc_attr( $service['supplier'] ); ?>">
			<p class="apaczka-logo-wrapper <?php echo 'service_id_' . esc_attr( $k ); ?>">
				<?php
				$logo     = alsendo_connect()->get_logo_apaczka( $k );
				$logo_src = '';
				if ( $logo ) {
					$logo_src = alsendo_connect()->get_plugin_img_url() . '/' . $logo . '.png';
				}
				?>
				<img class="apaczka-service-logo" src="<?php echo esc_url( $logo_src ); ?>" style="">
			</p>
			<p>
				<?php
				echo esc_html(
					str_replace(
						array(
							'Drzwi -',
							'Punkt -',
						),
						array(
							'<br>Drzwi -',
							'<br>Punkt -',
						),
						esc_attr( $service['name'] )
					)
				);
				?>
			</p>
			<div class="apaczka-calculate-item-price-wrapper">
				<p>
					<?php echo esc_html( Shipping_Method_Alsendo_Connect::format_calculate_price( $service['price'] ) ); ?>
					zł netto
				</p>
				<p>
					<?php echo esc_html( Shipping_Method_Alsendo_Connect::format_calculate_price( $service['price_gross'] ) ); ?>
					zł brutto
				</p>
				<input
						type="radio"
						value="<?php echo esc_attr( $k ); ?>"
						id="apaczka_calculate_radio_<?php echo esc_attr( $k ); ?>"
						class="apaczka_calculate_radio"
						name="apaczka_calculate_radio"
                    <?php echo esc_attr( $checked ); ?>
				>
				<?php ++$i; ?>
			</div>
		</label>
		<?php
		$first = false;
	}
	?>

</fieldset>
<?php