{extends file=$smarty.const.SMARTY_LAYOUT_DASHBOARD_WIDGET_DEFAULT}

{block name="template-head" append}
	{$CSS->loadGroup("widgetTable")}
{/block}

{block name="localCSS" append}
	<style>
		
		span.red {
			font-weight: bold;
			color: #FF8080;
		}

		span.green {
			font-weight: bold;
			color: #7FC37F;
		}
		
		table.simpleList > tbody td.subservice div {
			margin-left: 16px;
		}

		table.simpleList > tbody td.passengers {
			width: 15%;
		}

		table.simpleList > tbody tr td.right {
			text-align:right;
		}

		table.simpleList > tbody tr td.left {
			text-align:left;
		}

	</style>

{/block}


{block name="content"}

	{$colsAssoc=$widget['data']['columnsList']}
	
	{if $widget['data']['servicesListWithIndex']}
			
		{$idOrder = \Entity\Service::select(key($widget['data']['servicesListWithIndex']))->idOrder}
		<table class="simpleList w100p">
			<thead>
				<tr>
					<th style="text-align:right;">#</th>
					{foreach $colsAssoc as $col => $nothing}
						<th>{$widgetLang['options']['columnsList_options'][$col]}</th>
					{/foreach}
				</tr>
			</thead>

			<tbody>
				{foreach $widget['data']['servicesListWithIndex'] as $idService => $serviceDetails}
					{$serviceEntity = \Entity\Service::select($idService)}
					<tr class="{cycle values="odd,even"} {if $serviceDetails['statusOp']=='XX'} disabled {/if}">
						{if $serviceDetails['idPackageService']}
							{$counter = "&nbsp; &raquo &nbsp;"}
						{else}
							{$counter = $serviceDetails['index']}
						{/if}

						<td>{$counter}</td>
						{foreach $colsAssoc as $col=>$nothing}

							{if $col=='serviceType'}
								<td class="left {if $serviceDetails['idPackageService']}subservice{/if}">
									<div>
										<b>{$serviceDetails['service']}</b>

										{* indicate package *}
										{if $serviceDetails['packageOfferMode']}
											<span class="red">{$widgetLang['services']['packageIndicator']}</span>
										{/if}

										{* indicate if subservice *}
										{if $serviceDetails['idPackageService']}
											<span class="green">{$widgetLang['services']['subserviceIndicator']}</span>
										{/if}
									</div>

									{* SERVICE ID *}
									<div class="secondaryInfo">[{serviceLabel ref=$idService}]</div>

									{* TICKETS NUMBERS *}
									{if $serviceEntity->passengersTicketsList}
										<div>
											{$widgetLang['services']['ticketsList']}: {$serviceEntity->passengersTicketsList|join:', '}
										</div>
									{/if}
								</td>
							{/if}

							{if $col=='destination'}
								<td class="left">
									<div>
										{if $serviceEntity->departureCityId}
											{cityLabel ref=$serviceEntity->departureCityId} &raquo {cityLabel ref=$serviceEntity->destinationCityId}
										{else}
											{cityLabel ref=$serviceEntity->destinationCityId}
										{/if}

									</div>
								</td>
							{/if}

							{if $col=='passengers'}
								<td class="left passengers">
									<div>
										{$widgetLang['services']['passengers']}:
										{if $serviceDetails['idCustomersList']}
											{$noRenderedCustomers = 0}
											{foreach $serviceDetails['idCustomersList']  as $idCustomer}
												{if !$idCustomer@first} , {/if}
												{customerLabel ref=$idCustomer}
												{$noRenderedCustomers = $noRenderedCustomers + 1}
												{if $idCustomer@iteration>=5}
													{break}
												{/if}
											{/foreach}
											{$restPassengers = $serviceDetails['totalPax'] - $noRenderedCustomers}
											{if $restPassengers}
												+ {$restPassengers}
											{/if}
										{else}
											{$serviceDetails['totalPax']}
										{/if}
									</div>


								</td>
							{/if}

							{if $col=='date'}
								<td class="center nowrap">
									<div>
										{$serviceDetails['startDate']|dateTimeFormat}
										{if $serviceEntity->duration}
											<span class="secondaryInfo"><b>({$serviceEntity->duration} {$serviceEntity->durationType})</b></span>
										{/if}
									</div>

									{if $serviceDetails['endDate'] != '0000-00-00'}
										<div>
											<i>{$widgetLang['services']['returnDateText']}: {$serviceDetails['endDate']|dateTimeFormat}</i>
										</div>
									{/if}
								</td>
							{/if}

							{if $col=='status'}
								<td class="left">
									<span class="{if $serviceDetails['statusOp']=="XX"}red{/if}" tooltip="{$widgetLang['services']['statusOpDescription'][$serviceDetails['statusOp']]}">
										{$widgetLang['services']['statusOpName'][$serviceDetails['statusOp']]}
									</span>
									/
									<span tooltip="{$widgetLang['services']['statusFinDescription'][$serviceDetails['statusFin']]}">
										{$widgetLang['services']['statusFinName'][$serviceDetails['statusFin']]}
									</span>
								</td>
							{/if}

							{if $col=='offer'}
								<td class="right nowrap">
									{if $serviceDetails['offerTotal']}
										{$serviceDetails['offerTotal']|priceFormat:$serviceDetails['offerTotalCurrency']}
										<div><span>({supplierLabel ref=$serviceEntity->chosenOffer->idSupplier})</span></div>
									{else}
										-
									{/if}
								</td>
							{/if}

							{if $col=='invoiced'}
								<td class="right nowrap">
									{$separator=""}
									{foreach $serviceDetails['invoicesValues'] as $currency => $values}
										{if $values['value'] > 0.0001 || $values['value'] < -0.0001}
											{$separator}
											{$values['value']|priceFormat:$currency}
											<div><span>
											(
												{foreach $values['invoices'] as $idInvoice => $invoiceDetails}
													{$invoiceEntity = \Entity\Invoice::select($idInvoice)}

													{if !$invoiceDetails@first}, {/if}
													<a href="#;" class="link" onclick="clientInvoiceView('{$idInvoice}');">
														{if $invoiceEntity->issueTime}
															{$invoiceEntity->invoiceNumber} / {$invoiceEntity->invoiceDate|dateTimeFormat}
														{else}
															#{$idInvoice}
														{/if}
													</a>
												{/foreach}
											)
											</span></div>
											{$separator=","}
										{/if}
									{/foreach}

									{if !$separator}
										-
									{/if}
								</td>
							{/if}

							{if $col=='cashed'}
								<td class="right nowrap">
									{$separator=""}
									{foreach $serviceDetails['invoicesValues'] as $currency => $values}
										{if $values['cashings']['cashedTotal'] > 0.0001 || $values['cashings']['cashedTotal'] < -0.0001}
											{$separator}
											{$values['cashings']['cashedTotal']|priceFormat:$currency}
											{$separator=","}
										{/if}
									{/foreach}

									{if !$separator}
										-
									{/if}
								</td>
							{/if}

							{if $col=='notCashed'}
								<td class="right nowrap">
									{$separator=""}
									{foreach $serviceDetails['invoicesValues'] as $currency => $values}
										{if $values['cashings']['restToCash'] > 0.0001 || $values['cashings']['restToCash'] < -0.0001}
											{$separator}
											{$values['cashings']['restToCash']|priceFormat:$currency}
											{$separator=","}
										{/if}
									{/foreach}

									{if !$separator}
										-
									{/if}
								</td>
							{/if}

							{if $col=='balanced'}
								<td class="left nowrap">
									<div>
										{if $serviceDetails['idBalance']}
											{$balanceEntity = \Entity\Balance::select($serviceDetails['idBalance'])}
											<span class="green">{$_LANG['yes']}</span>
											<div>
												<a href="#;" class="link" onclick="invoiceBalanceView('{$serviceDetails['idBalance']}');">
													<b>{$balanceEntity->balanceNumber}</b> / {$balanceEntity->balanceDate|dateTimeFormat}
												</a>
											</div>
										{else}
											<span class="red">{$_LANG['no']}</span>
										{/if}
									</div>

									{** refunded service **}
									<div>
										{if $serviceDetails['refunded']}
											{if $serviceDetails['idBalanceSpecial']}
												{$balanceEntity = \Entity\Balance::select($serviceDetails['idBalanceSpecial'])}
												<span class="green">{$_LANG['yes']}</span>
												<div>
												<a href="#;" class="link" onclick="invoiceBalanceView('{$serviceDetails['idBalanceSpecial']}');">
													<b>{$balanceEntity->balanceNumber}</b> / {$balanceEntity->balanceDate|dateTimeFormat}
												</a>
											</div>
											{else}
												<span class="red">{$_LANG['no']}</span>
											{/if}
										{/if}
									</div>
								</td>
							{/if}
							
							{if $col=='viewOption'}
									<td class="optionsBox sectionRow" style="width:8%;">
										<ul class="submenu Simple submenu-depth-0">
										
											{*ADD OFFER*}
											<li data-id="addServiceOffer" class="menuItem has-icon-true display-left" tooltip="<div class='has-description'><div class='description'>{$widgetLang['buttons']['addServiceOffer']}
											</div></div>">
												<var class="icon-offer" data-icon="offer"
													onclick="window.open(DCsConfigs.APP_URL+'reservations/serviceOffers/index.php?idService={$idService}', '_blank');">
												</var>
											</li>
											
											{*GENERATE DOCUMENTS*}
											{$generatedDocs = \Entity\Service::select($idService)->generatedDocumentsAssoc}
												
											{$tooltip = ""}
											{$isDisabled = false}
											{if $widget['data']['buttonGeneralStatusRestriction']['adminDocuments']}
												{$isDisabled = true}
												{$tooltip = "{$tooltip}{$_LANG['noAccessToOption']}"}
											{/if}
											{if $serviceDetails['disabledButtons']['adminDocuments']}
												{$isDisabled = true}
												{$tooltip = "{$tooltip}{$widget['data']['lang']['index']['options']['reasons']['manageDocuments']['noDocuments']}"}
											{/if}	
											
											
											{if count($generatedDocs) == 0}
												{if !$isDisabled}
													<li data-id="generateDocuments" class="menuItem has-icon-true" tooltip="<div class='has-description'><div class='description'>{$widgetLang['buttons']['adminDocuments']}
													</div></div>">
														<var class="icon-documents" data-icon="documents"
															onclick="
																loadPivotGenerateDoc(this, 'Generate documents', 'service', '{$idService}');"
														></var>	
													</li>
												{else}
													<li data-id="generateDocuments" class="menuItem has-icon-true" tooltip="<div class='has-description'><div class='description'>{$tooltip}													
														</div></div>">
														<var class="icon-documents-disabled" data-icon="documents"></var>	
													</li>
												{/if} 
											
												
											{else}
											
												{if !$isDisabled}
													<li data-id="generateDocuments" class="menuItem has-icon-true" tooltip="<div class='has-description'><div class='description'>{$widgetLang['buttons']['adminDocuments']}
													</div></div>">
														<var class="icon-issuedDoc" data-icon="issuedDoc"
															onclick="
																loadPivotGenerateDoc(this, 'Generate documents', 'service', '{$idService}');"
														></var>	
													</li>
												{else}
													<li data-id="generateDocuments" class="menuItem has-icon-true" tooltip="<div class='has-description'><div class='description'>{$tooltip}
														</div></div>">
														<var class="icon-issuedDoc-disabled" data-icon="issuedDoc"></var>	
													</li>
												{/if}
											
											{/if}
											
											
											{*ASSOCIATE PASSENGERS-TICKETS*}
											{if !$serviceDetails['disabledButtons']['manageTickets']} 
												<li data-id="addTicket" class="menuItem has-icon-true" tooltip="<div class='has-description'><div class='description'>{$widgetLang['buttons']['associatePassengersTickets']}
												</div></div>">
													<var class="icon-ticket" data-icon="ticket"
														onclick="Popup2.create().setId('importedServiceRefs').setUrl(MVC.appUrl('Orders/Services/ImportedServicesRefs', 'index', { 'idService': '{$idService}' })).setDimensions(600, 500).show();">
													</var>
												</li>
											{else}
												<li data-id="addTicket" class="menuItem has-icon-true" tooltip="<div class='has-description'><div class='description'>{$widget['data']['lang']['index']['options']['reasons']['tickets']['noManageTickets']}
												</div></div>">
													<var class="icon-ticket-disabled" data-icon="ticket"></var>
												</li>
											{/if}
										
										</ul>
									</td>
							{/if}
							
													
						{/foreach}
					</tr>

				{/foreach}
			</tbody>
		</table>
		
	
	{else}
		{if $widget['data']['errorMessage']}
			<div class="messageDivContainer layout-inline">
				{message type='warning' message=$widget['data']['errorMessage']}
			</div>
		{else}
			<div class="messageDivContainer layout-inline">
				{message type='info' message=$widgetLang['messages']['noResults']}
			</div>
		{/if}

	{/if}
{/block}
