import $ from 'jquery';
import { close_modal } from '../close_modal';

export const initialize_shipping_modal = ( order_id: string, form: JQuery<HTMLElement> ) => {
	$( '<br><br>' ).appendTo( form );
	$( `<div class="sender-and-pickup-form__buttons"><button type="submit" class="button button-primary alignright">${ window._wetail_shipping.i18n[ 'Print shipping label' ] }</button></div>` ).appendTo( form );

	const WETAIL_SHIPPING_ENGINE_SELECT_SENDER_FORM = 'wetail_shipping_engine_select_sender_form';

	let form_wrapper = $( '<div></div>' );

	form_wrapper.css( 'display', 'none' );

	$( '#' + WETAIL_SHIPPING_ENGINE_SELECT_SENDER_FORM ).remove();

	form_wrapper.attr( 'id', WETAIL_SHIPPING_ENGINE_SELECT_SENDER_FORM );

	form.appendTo( form_wrapper );

	$( 'body' ).prepend( form_wrapper );

	const width = 500,
		height = $( window ).height()! * 0.8;

	// @ts-ignore
	tb_show( '#' + order_id + ' - ' + window._wetail_shipping.i18n[ 'Select sender & pickup' ], '/?TB_inline&width=' + width + '&height=' + height + '&inlineId=' + WETAIL_SHIPPING_ENGINE_SELECT_SENDER_FORM );
	const TBWindow = $( '.sender-and-pickup-form' ).parents( '#TB_window' );
	TBWindow.attr( 'data-order_id', order_id );
	TBWindow.wrap( '<div class="tb-sender-and-pickup-form"></div>' );
	// @ts-ignore
	$( '#TB_closeWindowButton' ).off( 'click', tb_remove ).on( 'click', close_modal );

	$( '#TB_window' ).on( 'tb_unload', function() {
		$( '.tb-sender-and-pickup-form' ).remove();
	} );
};
