<?php

/**
 * @var int $order_id
 * @var bool $disabled
 * @var mixed $package_created
 */

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

$order  = wc_get_order( $order_id );
$helper = \Ilabs\Inpost_International\Helpers\Helper::init();
?>

<p>
	<?php if ( $disabled ) { ?>
		<label disabled style="display: block" for="reference_number"
				class="graytext">
			<?php esc_html_e( 'Reference number: ', 'inpost-international' ); ?>
		</label>
	<?php } else { ?>
		<label disabled style="display: block" for="reference_number" class="">
			<?php esc_html_e( 'Reference number: ', 'inpost-international' ); ?>
		</label>
	<?php } ?>

	<?php
	if ( $disabled ) {
		$saved_referense = ! empty( $package_created['creating_args']['shipment']['parcel']['label']['comment'] ) ?
			$package_created['creating_args']['shipment']['parcel']['label']['comment']
			: '';
		?>
		<textarea disabled class="reference_number" rows="5" cols="27"
					style="max-width: 100%;"
					id="inpost_intl_reference_number"
					name="inpost_international_order_reference"><?php echo esc_html( $saved_referense ); ?></textarea>
		<?php
	} else {

		$default_ref_number    = $helper->get_maybe_custom_reference_number( $order_id );
		$is_order_note_exists  = false;
		$ref_number            = $default_ref_number;
		$plugin_order_settings = get_option( 'inpost_international_settings_sending' );
		if ( ! empty( $plugin_order_settings['copy_order_note'] ) && 'on' === $plugin_order_settings['copy_order_note'] ) {
			$order_note = $order->get_customer_note();
			if ( ! empty( $order_note ) ) {
				$is_order_note_exists = true;
				$ref_number           = $ref_number . ' ' . $order_note;
			}
		}

		?>
		<textarea class="reference_number inpost-intl-charcount" rows="5" cols="27"
					style="max-width: 100%;"
					id="inpost_intl_reference_number"
					data-id="<?php echo esc_attr( trim( $ref_number ) ); ?>"
					maxlength="100"
					name="inpost_international_order_reference"><?php echo esc_attr( trim( $ref_number ) ); ?></textarea>
		<span class="inpost-intl-charcount-label">
			<span class="label label-info" id="inp-intl-char-num">100 znaków do końca. Maksimum 100 znaków.</span><br>
			<span class="label inp-intl-charcount-limit-exceeded">Długość wiadomości przekracza już limit znaków.</span>
		</span>
	<?php } ?>
</p>
