<?php // phpcs:disable ?>
<?php if ( ! defined( 'ABSPATH' ) ) {
	exit;
} // Exit if accessed directly ?>
<input type="hidden" id="paczkomat_order_id" name="paczkomat_order_id" value="<?php echo esc_attr( $args['post_id'] ); ?>">

<?php

require_once WC()->plugin_path() . '/includes/wc-template-functions.php';

// phpcs:disable
/** @var WC_Paczkomaty_Shipping_Method $paczkomaty_shipping_method */

$params = array(
	'type'              => 'select',
	'options'           => array(
		'paczkomaty' => __( 'Paczkomat', 'woocommerce-paczkomaty-inpost' ),
		'polecony'   => __( 'Paczka E-Commerce (wyłączona)', 'woocommerce-paczkomaty-inpost' ),
		'kurier'     => __( 'Kurier', 'woocommerce-paczkomaty-inpost' ),
	),
	'class'             => array(
		'first',
		'kurier',
		'polecony',
		'paczkomaty',
		'allegro_kurier',
		'allegro_polecony',
		'allegro_paczkomaty',
	),
	'input_class'       => array( 'paczkomat_usluga' ),
	'custom_attributes' => array( 'style' => 'width:100%' ),
	'label'             => __( 'Usługa', 'woocommerce-paczkomaty-inpost' ),
	'id'                => "paczkomat_usluga_{$id}",
);

if ( $allegro ) {
	$params['options']['allegro_paczkomaty'] = __( 'Allegro Paczkomat', 'woocommerce-paczkomaty-inpost' );
	$params['options']['allegro_polecony']   = __( 'Allegro miniKurier24', 'woocommerce-paczkomaty-inpost' );
	$params['options']['allegro_kurier']     = __( 'Allegro Kurier', 'woocommerce-paczkomaty-inpost' );
}
if ( 'polecony' != $args['paczkomaty_usluga'] ) {
	unset( $params['options']['polecony'] );
}

if ( ! $editable ) {
	$params['custom_attributes']['disabled'] = 'disabled';
}
woocommerce_form_field( 'paczkomat_usluga', $params, $args['paczkomaty_usluga'] );


$params = array(
	'type'              => 'select',
	'options'           => apply_filters( 'paczkomaty_shipping_method_admin_machines', array( $args['paczkomat_id'] => $args['paczkomat_id'] . ' ' . __( '(Błąd ładowania listy paczkomatów!)', 'woocommerce-paczkomaty-inpost' ) ), false ),
	'class'             => array( 'first', 'paczkomaty', 'allegro_paczkomaty' ),
	'input_class'       => array( 'select2_ajax_paczkomaty' ),
	'custom_attributes' => array( 'style' => 'width:100%' ),
	'label'             => __( 'Wybrany paczkomat', 'woocommerce-paczkomaty-inpost' ),
	'id'                => "paczkomat_id_{$id}",
);

if ( ! $editable ) {
	$params['custom_attributes']['disabled'] = 'disabled';
}

woocommerce_form_field( 'paczkomat_id', $params, $args['paczkomat_id'] );

$params = array(
	'type'    => 'select',
	// phpcs:disable
	'options' => $paczkomaty_shipping_method->packageSizes,
	'class'   => array( 'first', 'paczkomaty', 'allegro_paczkomaty' ),
	'label'   => __( 'Wybrany rozmiar paczki', 'woocommerce-paczkomaty-inpost' ),
	'id'      => "paczkomat_size_{$id}",
);

if ( ! $editable ) {
	$params['custom_attributes']['disabled'] = 'disabled';
}

woocommerce_form_field( 'paczkomat_size', $params, $args['paczkomat_size'] );

$params = array(
	'type'  => 'number',
	'class' => array( 'first', 'kurier', 'allegro_kurier' ),
	'label' => __( 'Długość paczki [mm]', 'woocommerce-paczkomaty-inpost' ),
	'id'    => "paczkomat_length_{$id}",
);
if ( ! $editable ) {
	$params['custom_attributes']['disabled'] = 'disabled';
}
woocommerce_form_field( 'paczkomat_length', $params, $args['paczkomat_length'] );

$params = array(
	'type'  => 'number',
	'class' => array( 'first', 'kurier', 'allegro_kurier' ),
	'label' => __( 'Szerokość paczki [mm]', 'woocommerce-paczkomaty-inpost' ),
	'id'    => "paczkomat_width_{$id}",
);
if ( ! $editable ) {
	$params['custom_attributes']['disabled'] = 'disabled';
}
woocommerce_form_field( 'paczkomat_width', $params, $args['paczkomat_width'] );

$params = array(
	'type'  => 'number',
	'class' => array( 'first', 'kurier', 'allegro_kurier' ),
	'label' => __( 'Wysokość paczki [mm]', 'woocommerce-paczkomaty-inpost' ),
	'id'    => "paczkomat_height_{$id}",
);
if ( ! $editable ) {
	$params['custom_attributes']['disabled'] = 'disabled';
}
woocommerce_form_field( 'paczkomat_height', $params, $args['paczkomat_height'] );

$params = array(
	'type'  => 'number',
	'class' => array( 'first', 'kurier', 'allegro_kurier' ),
	'label' => __( 'Waga paczki [kg]', 'woocommerce-paczkomaty-inpost' ),
	'id'    => "paczkomat_weight_{$id}",
);
if ( ! $editable ) {
	$params['custom_attributes']['disabled'] = 'disabled';
}
woocommerce_form_field( 'paczkomat_weight', $params, $args['paczkomat_weight'] );

if ( $args['customer_ref_available'] ) {
	$params = array(
			'type'    => 'text',
			'class'   => array( 'first', 'paczkomaty', 'kurier', 'allegro_paczkomaty', 'allegro_kurier' ),
			'default' => $args['paczkomat_customer_ref'],
			'label'   => __( 'Numer referencyjny', 'woocommerce-paczkomaty-inpost' ),
			'id'      => "paczkomat_customer_ref_{$id}",
	);
	if ( ! $editable ) {
		$params['custom_attributes']['disabled'] = 'disabled';
	}
	woocommerce_form_field( 'paczkomat_customer_ref', $params, $args['paczkomat_customer_ref'] );
}

$sending_methods         = $paczkomaty_shipping_method->get_sending_methods();
$sending_methods_options = apply_filters( 'woocommerce_paczkomaty_inpost_shipx_sending_methods_options', $paczkomaty_shipping_method->get_sending_method_options( $sending_methods ) );
$params                  = array(
	'type'    => 'select',
	'options' => $sending_methods_options,
	'class'   => array(
		'first',
		'paczkomaty',
		'kurier',
		'polecony',
		'allegro_paczkomaty',
		'allegro_kurier',
		'allegro_polecony',
	),
	'label'   => __( 'Sposób nadania', 'woocommerce-paczkomaty-inpost' ),
	'id'      => "paczkomat_sending_method_{$id}",
);

if ( ! $editable ) {
	$params['custom_attributes']['disabled'] = 'disabled';
}
woocommerce_form_field( 'paczkomat_sending_method', $params, $args['paczkomat_sending_method'] );

$params = array(
	'type'        => 'checkbox',
	'class'       => array(
		'first',
		'paczkomaty',
		'kurier',
		'allegro_paczkomaty',
		'allegro_kurier',
		'paczkomat_ubezpieczenie',
		'allegro_polecony'
	),
	'input_class' => array(),
	'label'       => __( 'Ubezpieczenie', 'woocommerce-paczkomaty-inpost' ),
	'id'          => "paczkomat_ubezpieczenie_{$id}",
);

if ( ! $editable ) {
	$params['custom_attributes']['disabled'] = 'disabled';
	$params['input_class'][]                 = 'disabled';
}
woocommerce_form_field( 'paczkomat_ubezpieczenie', $params, $args['paczkomat_ubezpieczenie'] );

$key    = 'paczkomat_ubezpieczenie_amount';
$params = array(
	'label'             => __( 'Kwota ubezpieczenia', 'woocommerce-paczkomaty-inpost' ),
	'type'              => 'number',
	'class'             => array( 'first', 'paczkomaty', 'kurier', 'allegro_paczkomaty', 'allegro_kurier', 'allegro_polecony' ),
	'custom_attributes' => array(
		'min'  => 0,
		'step' => '0.01',
	),
	'id'                => "paczkomat_ubezpieczenie_amount_{$id}",
);
if ( ! $editable ) {
	$params['custom_attributes']['disabled'] = 'disabled';
}
woocommerce_form_field( $key, $params, $args['paczkomat_ubezpieczenie_amount'] );


$params = array(
	'type'        => 'checkbox',
	'class'       => array( 'first', 'paczkomaty', 'kurier', 'allegro_paczkomaty', 'allegro_kurier', 'paczkomat_cod', 'allegro_polecony' ),
	'input_class' => array(),
	'label'       => __( 'Pobranie', 'woocommerce-paczkomaty-inpost' ),
	'id'          => "paczkomat_cod_{$id}",
);

if ( ! $editable ) {
	$params['custom_attributes']['disabled'] = 'disabled';
	$params['input_class'][]                 = 'disabled';
}
woocommerce_form_field( 'paczkomat_cod', $params, $args['paczkomat_cod'] );

$key    = 'paczkomat_cod_amount';
$params = array(
	'label'             => __( 'Kwota pobrania', 'woocommerce-paczkomaty-inpost' ),
	'type'              => 'number',
	'class'             => array( 'first', 'paczkomaty', 'kurier', 'allegro_paczkomaty', 'allegro_kurier', 'allegro_polecony' ),
	'custom_attributes' => array(
		'min'  => 0,
		'step' => '0.01',
	),
	'id'                => "paczkomat_cod_amount_{$id}",
);
if ( ! $editable ) {
	$params['custom_attributes']['disabled'] = 'disabled';
}
woocommerce_form_field( $key, $params, $args['paczkomat_cod_amount'] );

$params = array(
	'type'        => 'checkbox',
	'class'       => array( 'first', 'paczkomaty', 'paczkomat_cod' ),
	'input_class' => array(),
	'label'       => __( 'Paczka w Weekend', 'woocommerce-paczkomaty-inpost' ),
	'id'          => WPDesk_Paczkomaty_End_Of_Week_Collection::FIELD_PACZKOMATY_END_OF_WEEK_COLLECTION . "_{$id}",
);

if ( ! $editable ) {
	$params['custom_attributes']['disabled'] = 'disabled';
	$params['input_class'][]                 = 'disabled';
}
woocommerce_form_field(
	WPDesk_Paczkomaty_End_Of_Week_Collection::FIELD_PACZKOMATY_END_OF_WEEK_COLLECTION,
        $params,
        $args[ WPDesk_Paczkomaty_End_Of_Week_Collection::FIELD_PACZKOMATY_END_OF_WEEK_COLLECTION ]
);

$params = array(
	'type'        => 'checkbox',
	'class'       => array( 'first', 'kurier', 'allegro_kurier' ),
	'input_class' => array(),
	'label'       => __( 'Powiadomienia SMS', 'woocommerce-paczkomaty-inpost' ),
	'id'          => WPDesk_Paczkomaty_Additional_Service_SMS_Notification::FIELD_PACZKOMATY_SMS_NOTIFICATION . "_{$id}",
);

if ( ! $editable ) {
	$params['custom_attributes']['disabled'] = 'disabled';
	$params['input_class'][]                 = 'disabled';
}
woocommerce_form_field(
	WPDesk_Paczkomaty_Additional_Service_SMS_Notification::FIELD_PACZKOMATY_SMS_NOTIFICATION,
	$params,
	$args[ WPDesk_Paczkomaty_Additional_Service_SMS_Notification::FIELD_PACZKOMATY_SMS_NOTIFICATION ]
);

$params = array(
	'type'        => 'checkbox',
	'class'       => array( 'first', 'kurier', 'allegro_kurier' ),
	'input_class' => array(),
	'label'       => __( 'Powiadomienia E-mail', 'woocommerce-paczkomaty-inpost' ),
	'id'          => WPDesk_Paczkomaty_Additional_Service_Email_Notification::FIELD_PACZKOMATY_EMAIL_NOTIFICATION . "_{$id}",
);

if ( ! $editable ) {
	$params['custom_attributes']['disabled'] = 'disabled';
	$params['input_class'][]                 = 'disabled';
}
woocommerce_form_field(
	WPDesk_Paczkomaty_Additional_Service_Email_Notification::FIELD_PACZKOMATY_EMAIL_NOTIFICATION,
	$params,
	$args[ WPDesk_Paczkomaty_Additional_Service_Email_Notification::FIELD_PACZKOMATY_EMAIL_NOTIFICATION ]
);

$params = array(
	'type'        => 'checkbox',
	'class'       => array( 'first', 'kurier', 'allegro_kurier' ),
	'input_class' => array(),
	'label'       => __( 'Doręczenie w sobotę', 'woocommerce-paczkomaty-inpost' ),
	'id'          => WPDesk_Paczkomaty_Additional_Service_Saturday::FIELD_SATURDAY . "_{$id}",
);

if ( ! $editable ) {
	$params['custom_attributes']['disabled'] = 'disabled';
	$params['input_class'][]                 = 'disabled';
}
woocommerce_form_field(
	WPDesk_Paczkomaty_Additional_Service_Saturday::FIELD_SATURDAY,
	$params,
	$args[ WPDesk_Paczkomaty_Additional_Service_Saturday::FIELD_SATURDAY ]
);
?>
<?php if ( ! empty( $args['paczkomat_packcode'] ) ) : ?>
	<p class="paczkomaty polecony kurier allegro_paczkomaty allegro_polecony allegro_kurier"><strong>Numer
			przesyłki:</strong> <?php echo esc_attr( $args['paczkomat_packcode'] ); ?></p>
<?php endif; ?>

<?php if ( $args['shipX'] && ! empty( $args['paczkomat_shipment_status'] ) ) : ?>
	<p class="kurier paczkomaty allegro_kurier allegro_paczkomaty">
		<strong><?php esc_html( 'Status InPost:', 'woocommerce-paczkomaty-inpost' ); ?></strong> <?php echo esc_attr( $args['paczkomat_shipment_status_translated'] ); ?>
	</p>
<?php endif; ?>

<?php if ( ! empty( $args['paczkomat_offers_message'] ) ) : ?>
	<p class="kurier allegro_kurier flexible_shipping_shipment_message_error">
		<strong><?php esc_html( 'Dodatkowe informacje:' ); ?></strong> <?php echo esc_attr( $args['paczkomat_offers_message'] ); ?></p>
<?php endif; ?>

<?php if ( $args['create_shipment_allowed'] && ( 'fs-new' == $args['status'] || 'fs-failed' == $args['status'] ) ) : ?>
	<p class="paczkomaty polecony kurier allegro_paczkomaty allegro_polecony allegro_kurier">
		<button
			class="button-shipping-send button-paczkomaty button button-shipping button-primary"><?php echo esc_html( 'Utwórz przesyłkę', 'woocommerce-paczkomaty-inpost' ); ?></button>
		<span class="spinner shipping-spinner shipping-send-spinner"></span>
	</p>
<?php endif; ?>

<?php if ( ! empty( $args['label_url'] ) ) : ?>
	<p class="paczkomaty polecony kurier allegro_paczkomaty allegro_polecony allegro_kurier">
		<a target="_blank" class="button-paczkomaty button-shipping button button-primary"
		   href="<?php echo esc_url( $args['label_url'] ); ?>"><?php echo esc_html( 'Pobierz etykietę', 'woocommerce-paczkomaty-inpost' ); ?></a>
	</p>
<?php endif; ?>
<?php if ( $args['shipX'] && 'fs-new' != $args['status'] && 'fs-failed' != $args['status'] && 'fs-confirmed' != $args['status'] ) : ?>
	<p class="kurier paczkomaty allegro_kurier allegro_paczkomaty allegro_polecony">
		<button class="button-paczkomaty button-shipping button-shipping-refresh button button-primary"
		        id="paczkomaty_refresh_package_status_<?php echo esc_attr( $id ); ?>"><?php echo esc_html( 'Odśwież status', 'woocommerce-paczkomaty-inpost' ); ?></button>
		<?php if ( 'fs-offers_prepared' != $args['status'] ) : ?>
			<button class="button-paczkomaty button-shipping button button-shipping-cancel"
			        id="paczkomaty_cancel_package_<?php echo esc_attr( $id ); ?>"><?php echo esc_html( 'Anuluj', 'woocommerce-paczkomaty-inpost' ); ?></button>
		<?php endif; ?>
		<span class="spinner shipping-spinner shipping-refresh-spinner"></span>
		<?php if ( ! empty( $args['paczkomat_shipment_status'] ) && ( 'created' == $args['paczkomat_shipment_status'] || 'offer_selected' == $args['paczkomat_shipment_status'] ) ) : ?>
			<script type="text/javascript">
                function paczkomaty_auto_refresh_package_status_<?php echo esc_attr( $id ); ?>() {
                	if (!jQuery('#paczkomaty_refresh_package_status_<?php echo esc_attr( $id ); ?>').is(":disabled")) {
						jQuery('#paczkomaty_refresh_package_status_<?php echo esc_attr( $id ); ?>').trigger('click');
					}
                }
                jQuery(document).ready(function () {
                    setTimeout(paczkomaty_auto_refresh_package_status_<?php echo esc_attr( $id ); ?>, 10000);
                })
			</script>
		<?php endif; ?>
	</p>
<?php endif; ?>

<?php if ( 'fs-new' == $args['status'] || 'fs-failed' == $args['status'] ) : ?>
	<p class="paczkomaty polecony kurier allegro_paczkomaty allegro_polecony allegro_kurier">
		<button class="button-paczkomaty button-shipping button button-shipping-save"
		        href="#"><?php echo esc_html( 'Zapisz', 'woocommerce-paczkomaty-inpost' ); ?></button>
		<span class="spinner shipping-spinner shipping-save-spinner"></span>
	</p>
<?php endif; ?>
<?php if ( $label_avaliable ) : ?>
	<?php if ( apply_filters( 'flexible_printing', false ) ) : ?>
		<div>
			<?php
			echo apply_filters(
				'flexible_printing_print_button',
				'',
				'paczkomaty_shipping_method',
				array(
					'content' => 'print',
					'id'      => esc_attr( $args['paczkomat_packcode'] ),
					'icon'    => true,
					'label'   => __( 'Drukuj na: %s', 'woocommerce-paczkomaty-inpost' ),
					'tip'     => __( 'Drukuj na: %s', 'woocommerce-paczkomaty-inpost' ),
					'data'    => array(
						'shippment_id'       => esc_attr( $id ),
						'paczkomat_packcode' => esc_attr( $args['paczkomat_packcode'] ),
					),
				)
			);
			?>
		</div>
	<?php endif; ?>
<?php endif; ?>
<div class="flexible_shipping_shipment_error_exploration" style="display: none;">
    <?php echo sprintf(
        __( 'Sprawdź w %1$snaszej dokumentacji%2$s co oznacza poniższy błąd, dlaczego wystąpił i jak go naprawić →' ),
        '<a target="_blank" href="https://www.wpdesk.pl/docs/inpost-najczestsze-problemy/#Bledy_walidacji">',
        '</a>'
    ); ?>
</div>

<!--
<?php print_r( $args['shipment_data_send'] ); ?>
-->
<!--
<?php print_r( $args['paczkomat_shipment'] ); ?>
-->

<script type="text/javascript">
    jQuery(document).ready(function($){
        jQuery('#shipment_meta_box_<?php echo $id; ?> input[type=checkbox].disabled').attr('disabled',true);
        fs_paczkomaty_fields(<?php echo $id; ?>, '<?php echo $args['paczkomaty_usluga']; ?>' );
        if ( '<?php echo $args['paczkomaty_usluga']; ?>' != 'polecony' ) {
            fs_paczkomaty_checkboxes(<?php echo $id; ?>,'<?php echo $args['paczkomaty_usluga']; ?>');
        }

        function flexible_shipping_shipment_error_exploration_toggle() {
        	let error_content = $('#flexible_shipping_shipment_<?php echo esc_attr( $id ); ?> .flexible_shipping_shipment_message_error').html();
        	let visibile = error_content !== undefined && error_content.trim() !== '';
			$('#flexible_shipping_shipment_<?php echo esc_attr( $id ); ?> .flexible_shipping_shipment_error_exploration').toggle(visibile);
        }

		$('#flexible_shipping_shipment_<?php echo esc_attr( $id ); ?>').on('flexible_shipping_ajax_fs_action_after',function(){
			flexible_shipping_shipment_error_exploration_toggle();
        });

		flexible_shipping_shipment_error_exploration_toggle();
    });
</script>
