{extends file=$smarty.const.SMARTY_LAYOUT_MAIN_POPUP}

{block name="template-head" append}
	{$JS->loadGroup('remoteLoad')}
	{$JS->load("{asset path="_js/offer.js"}")}
	{$CSS->load("_layout/{$smarty.const.THEME}/styles/orders/services/processBulk/index.css")}

	{$JS->loadGroup("optionsBox.icons")}
	{$CSS->loadGroup("optionsBox.icons")}
{/block}

{block name="content"}
	{***** because this template extend MAIN_POPUP, the form will be render if these 2 conditions will be fulfilled *****}
	{if $importedServicesList and !$closePopup}
		<form id="bulkProcessForm" class="form" method="post" action="">

		{showMessage}

			<table class="simpleList w100p" style="width: 99%">

				{***** HEADER FOR THE TABLE *****}
				<thead>
					<tr>
						<th class="counter">#</th>
						<th class="nowrap">{$lang['index']['thCols']['service']}</th>
						<th class="nowrap">{$lang['index']['thCols']['price']}</th>
						<th class="nowrap">{$lang['index']['thCols']['passengersSupplier']}</th>
						<th class="nowrap">{$lang['index']['thCols']['destination']}</th>
					</tr>
				</thead>

				<tbody>
					{foreach $importedServicesList as $idImportedService=>$importedService}

						<tr class="odd">
							<td >{counter}.</td>

							{******* DETAILS *******}
							<td>
								<div class="details">
									<span class="details label">{$lang['index']['service']['serviceCode']}:</span>
									<span class="details value">{serviceTypeLabel ref=$importedService['details']['serviceCode']}</span>
								</div>

								{if $importedService['details']['ticketNumbers']}
									<div class="details">
										<span class="details label">{$lang['index']['service']['ticketNo']}:</span>
										<span class="details value">
										{foreach $importedService['details']['ticketNumbers'] as $tkt}
											{$tkt['number']}{if !$tkt@last},{/if}
										{/foreach}
										</span>
									</div>
								{/if}
								{if $importedService['details']['ticketDate']}
									<div class="details">
										<span class="details label">{$lang['index']['service']['ticketDate']}:</span>
										<span class="details value">{$importedService['details']['ticketDate']|dateTimeFormat}</span>
									</div>
								{/if}
								{if $importedService['details']['confirmationNumber']}
									<div class="details">
										<span class="details label">{$lang['index']['service']['confirm']}:</span>
										<span class="details value">{$importedService['details']['confirmationNumber']}</span>
									</div>
								{/if}
								{if $importedService['details']['extResId']}
									<div class="details">
										<span class="details label">{$lang['index']['service']['extResId']}:</span>
										<span class="details value">{$importedService['details']['extResId']}</span>
									</div>
								{/if}
								{if $importedService['details']['extResDate']}
									<div class="details">
										<span class="details label">{$lang['index']['service']['extResDate']}:</span>
										<span class="details value">{$importedService['details']['extResDate']|dateTimeFormat}</span>
									</div>
								{/if}
								{if $importedService['details']['productType']}
									<div class="details">
										<span class="details label">{$lang['index']['service']['productType']}:</span>
										<span class="details value">{$importedService['details']['productType']}</span>
									</div>
								{/if}
								{if $importedService['details']['formOfPayment']}
									<div class="details">
										<span class="details label">{$lang['index']['service']['formOfPayment']}:</span>
										<span class="details value">{$_LANG['offers']['formOfPayment'][$importedService['details']['formOfPayment']]}</span>
									</div>
								{/if}
								{if $importedService['details']['className']}
									<div class="details">
										<span class="details label">{$lang['index']['service']['className']}:</span>
										<span class="details value">{$_LANG['flightClass'][$importedService['details']['className'][0]]}</span>
									</div>
								{/if}
								{if $importedService['details']['remarks']}
									<div class="details">
										<span class="details label">{$lang['index']['service']['remarks']}:</span>
										<span class="details value">
											{$importedService['details']['remarks']|nl2br|truncate:30}
											<a href="#;" class="imgIcon icon-infoBlue" tooltip="{$importedService['details']['remarks']}"></a>
										</span>

									</div>
								{/if}

								{***** EXTRA INFO = HIDDEN INPUTS *****}
								{$form[$idImportedService]['extraInfo']->render()}
							</td>

							<td>
								{******* RENDER THE FORM FOR PRICES *******}
								{if $form[$idImportedService]['priceContainer']}
									{$showCopy = true}
									{$form[$idImportedService]['priceContainer']->render()}
								{else}
									<div style="text-align:center; font-weight: bold">{$lang['index']['messages']['noOffer']}</div>
								{/if}

							</td>

							<td style="width:200px;">
								{******* RENDER PASSENGER(S) CONTAINER *********}
								{$form[$idImportedService]['passContainer']->render()}

								{******* RENDER SUPPLIER CONTAINER *********}
								{if $form[$idImportedService]['supplierContainer']}
									{$form[$idImportedService]['supplierContainer']->render()}
								{/if}
							</td>

							<td>
								{******* ROUTING *******}
								<div class="details">
									<span class="details label">{$lang['index']['destination']['departureDate']}:</span>
									<span class="details value">{$importedService['details']['departureDatetime']|dateTimeFormat}</span>
								</div>
								<div class="details">
									<span class="details label">{$lang['index']['destination']['returnDate']}:</span>
									<span class="details value">{$importedService['details']['endDatetime']|dateTimeFormat}</span>
								</div>
								<div class="details">
									<span class="details label">{$lang['index']['destination']['departure']}:</span>
									<span class="details value">
									{if $importedService['details']['departureStationCode']}
										{stationLabel ref=$importedService['details']['departureStationCode']} /
									{/if}

									{if $importedService['details']['departureCityId']}
										{cityLabel ref=$importedService['details']['departureCityId']} /
									{/if}

									{if $importedService['details']['departureCountryCode']}
										{countryLabel ref=$importedService['details']['departureCountryCode']}
									{/if}

									{if !$importedService['details']['departureStationCode'] and !$importedService['details']['departureCityId'] and !$importedService['details']['departureCountryCode']}
										-
									{/if}
									</span>
								</div>
								<div class="details">
									<span class="details label">{$lang['index']['destination']['destination']}:</span>
									<span class="details value">
									{if $importedService['details']['destinationStationCode']}
										{stationLabel ref=$importedService['details']['destinationStationCode']} /
									{/if}

									{if $importedService['details']['destinationCityId']}
										{cityLabel ref=$importedService['details']['destinationCityId']} /
									{/if}

									{if $importedService['details']['destinationCountryCode']}
										{countryLabel ref=$importedService['details']['destinationCountryCode']}
									{/if}

									{if !$importedService['details']['destinationStationCode'] and !$importedService['details']['destinationCityId'] and !$importedService['details']['destinationCountryCode']}
										-
									{/if}
									</span>
								</div>
								<div class="details">
									<span class="details label">{$lang['index']['destination']['itinerary']}:</span>
									<span class="details value">
									{if $importedService['details']['itinerary']}
										{$importedService['details']['itinerary']}
									{else}
										-
									{/if}
									</span>
								</div>
							</td>
						</tr>

					{/foreach}

				</tbody>
			</table>

			{* BULK REPLACE -------------------------------------------------------------------- *}
			<div class="copyPrices" {if !$showCopy}hidden=true{/if} >

				<span class="offerComponentBulkCopy"
					{if !$showCommission}
						hidden=true
					{/if}>
						{$lang['index']['copy']['commission']}:
						<input type="text" id="copyCommissionValue" class="copyPrice"/>
						{input type="App\Form\Input\Select_Currency" id="copyCommissionCurrency"}
				</span>

				<span class="offerComponentBulkCopy"
					{if !$showDiscount}
						hidden=true
					{/if} >
						{$lang['index']['copy']['discount']}:
						<input type="text" id="copyDiscountValue" class="copyPrice" />
						{input type="App\Form\Input\Select_Currency" id="copyDiscountCurrency"}
				</span>

				<span class="offerComponentBulkCopy"
					{if !$showServiceFee}
						hidden=true
					{/if} >
						{$lang['index']['copy']['serviceFee']}:
						<input type="text" id="copyServiceFeeValue" class="copyPrice" />
						{input type="App\Form\Input\Select_Currency" id="copyServiceFeeCurrency"}
				</span>

				<button type="button" id="copyOfferComponentsButton"
					{if !$showCommission && !$showDiscount && !$showServiceFee}
						hidden=true
					{else if !$showCopy}
						hidden=true
					{/if}
				>{$lang['index']['copy']['button']}</button>

				<div class="information">
					{$lang['index']['messages']['info']}
					<a href="#;" class="imgIcon icon-infoBlue" tooltip="{$lang['index']['messages']['tooltipInfo']}"></a>
				</div>
			</div>
		</form>
	{/if}

	{block name="buttons"}
		<div class="buttons layout-S-L">
			<button class="button-back" type="button"
				onclick="window.location = MVC.appUrl('Orders/ImportedOrders/ProcessServicesList', 'index', { 'idOrder': '{$idOrder}'});">{$_LANG['back']}
			</button>


			<button class="button-popupSubmit" type="button"
				{if $importedServicesList and !$closePopup}
					onclick="$('bulkProcessForm').submit()"
				{else}
					disabled="disabled"
				{/if}
			>{$_LANG['save']}</button>
		</div>
	{/block}
{/block}


{block name="js" append}
	<script type="text/javascript">

		{* call parent page for reloading stuff *}
		{if $closePopup}
			MVC.scheduleOnLoad(function(){
				setTimeout('window.close();', 1000);
				if (window.parent.opener && window.parent.opener.MVC) {
					MVC.notifyParent('document:pageRefresh', {
						'module': MVC.CURRENT_MODULE,
						'action': MVC.CURRENT_ACTION,
						'windowName': window.name
					});
				}
			});
		{/if}


		{* if there are customers or suppliers added using manual add or quick add, the page won't refresh *}
		MVC.pageRefresh = function (module, action, windowName, execOutput) {
			if (module=='Partners/Customers') {
				return;
			}

			if (module=='Partners/Suppliers'){
				return;
			}
		}

		{if !$closePopup}
			MVC.scheduleOnLoad(function(){

				{* calculate and refresh information about prices when you access the form *}
				if ($('bulkProcessForm')) {
					$('bulkProcessForm').select('input[id^=\'idImportedServiceStructure_\']').each(function(input){
						var currentIdImportedService = input.value;

						if ($('hasOffer_'+currentIdImportedService+'_').value==1) {
						// refresh totals
							refreshOfferTotal(currentIdImportedService);
						}
					});

					{* copy the offer components *}
					$('copyOfferComponentsButton').observe('click', function(){

						var copyCommissionValueField= $('copyCommissionValue');
						var copyCommissionCurrField	= $('copyCommissionCurrency');
						var copyDiscountValueField 	= $('copyDiscountValue');
						var copyDiscountCurrField 	= $('copyDiscountCurrency');
						var copySfValueField 		= $('copyServiceFeeValue');
						var copySfCurrField 		= $('copyServiceFeeCurrency');

						// go through each imported service
						$('bulkProcessForm').select('input[id^=\'idImportedServiceStructure_\']').each(function(input){
							var currentIdImportedService = input.value;

							if ($('hasOffer_'+currentIdImportedService+'_').value==1) {

								// replace commission
								if (copyCommissionCurrField.value && !isNaN(parseFloat(copyCommissionValueField.value)) && $('prices_commission__'+currentIdImportedService+'_')) {
									if ($('prices_commission__'+currentIdImportedService+'__checkboxProfile_').checked) {
										$('prices_commission__'+currentIdImportedService+'__checkboxProfile_').click();
									}
									$('prices_commission__'+currentIdImportedService+'__value_').value 		= parseFloat(copyCommissionValueField.value);
									$('prices_commission__'+currentIdImportedService+'__currency_').value 	= copyCommissionCurrField.value;
								}

								// replace discount
								if (copyDiscountCurrField.value && !isNaN(parseFloat(copyDiscountValueField.value)) && $('prices_discount__'+currentIdImportedService+'_')) {
									if ($('prices_discount__'+currentIdImportedService+'__checkboxProfile_').checked) {
										$('prices_discount__'+currentIdImportedService+'__checkboxProfile_').click();
									}
									$('prices_discount__'+currentIdImportedService+'__value_').value 	= parseFloat(copyDiscountValueField.value);
									$('prices_discount__'+currentIdImportedService+'__currency_').value = copyDiscountCurrField.value;
								}

								// replace service fee
								if (copySfCurrField.value && !isNaN(parseFloat(copySfValueField.value)) && $('prices_serviceFee__'+currentIdImportedService+'_')) {
									if ($('prices_serviceFee__'+currentIdImportedService+'__checkboxProfile_').checked) {
										$('prices_serviceFee__'+currentIdImportedService+'__checkboxProfile_').click();
									}
									$('prices_serviceFee__'+currentIdImportedService+'__value_').value 		= parseFloat(copySfValueField.value);
									$('prices_serviceFee__'+currentIdImportedService+'__currency_').value 	= copySfCurrField.value;
								}

								// refresh totals
								refreshOfferTotal(currentIdImportedService);
							}
						});

						// reset the correct values
						if (copySfCurrField.value && !isNaN(parseFloat(copySfValueField.value))) {
							copyCommissionValueField.value = '';
						}
						if (copyDiscountCurrField.value && !isNaN(parseFloat(copyDiscountValueField.value))) {
							copyDiscountValueField.value = '';
						}
						if (copySfCurrField.value && !isNaN(parseFloat(copySfValueField.value))) {
							copySfValueField.value = '';
						}

					});

					{* validate the inputs *}
					setTimeout(function () {
						_inputValidator.getRegisteredValidatorsForContainer($('bulkProcessForm')).each(function(v){
							v.validate(true);
						});
					}, 500);
				}

			});
		{/if}
	</script>
{/block}