{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;
		}

        span.no-wrap {
            white-space: nowrap;
        }

        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>
										{* 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 format="%serviceName%"}</div>
                                    <div class="secondaryInfo"><i>#{$idService}</i></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']}
										<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['groupedInvoicesValues'] as $currency => $values}
                                        {$separator}
                                        {$values['value']|priceFormat:$currency}
                                        <div><span>
                                        (
                                            {foreach $values['invoices'] as $idInvoice => $invoiceDetails}
                                                {$invoiceEntity = \Entity\Invoice::select($idInvoice)}

                                                {if !$invoiceDetails@first}, {/if}
                                                    <span tooltip="{$invoiceDetails|priceFormat:$currency}">
                                                        {if $invoiceEntity->issueTime}
                                                            {invoiceLabel ref=$idInvoice format= "%invoiceNumber% / %invoiceDateStd%" tooltip="fdsfsdf"}
                                                        {else}
                                                            {invoiceLabel ref=$idInvoice}
                                                        {/if}
                                                    </span>
                                            {/foreach}
                                        )
                                        </span></div>
                                    {$separator=","}

                                    {/foreach}
                                    {if !$separator}
                                        -
                                    {/if}



                                    {if $serviceDetails['voidedInvoices']}
                                        <div><i>
                                        ( {$widgetLang['services']['invVoided']}:
                                            {foreach $serviceDetails['voidedInvoices'] as $idInvoice => $invoiceDetails}
                                                {$invoiceEntity = \Entity\Invoice::select($idInvoice)}

                                                {if !$invoiceDetails@first}, {/if}
                                                    {if $invoiceEntity->issueTime}
                                                        {invoiceLabel ref=$idInvoice format= "%invoiceNumber% / %invoiceDateStd%"}
                                                    {else}
                                                        {invoiceLabel ref=$idInvoice}
                                                    {/if}
                                                </a>
                                            {/foreach}
                                        )
                                        </i></div>
                                    {/if}

								</td>
							{/if}

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

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

							{if $col=='notCashed'}
								<td class="right nowrap">
									{$separator=""}
									{foreach $serviceDetails['groupedInvoicesValues'] as $currency => $values}
										{if $values['restToCash']}
											{$separator}
											{$values['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>
                                                {balanceLabel ref=$serviceDetails['idBalance'] format="%balanceNumber%"}
											</div>
										{else}
											<span class="red">{$_LANG['no']}</span>
										{/if}
									</div>

									{** refunded service **}
									<div>
										{if $serviceDetails['refunded']}
                                            <b>{$widgetLang['services']['refundBalance']}: </b>
											{if $serviceDetails['idBalanceRefund']}
												{$balanceEntity = \Entity\Balance::select($serviceDetails['idBalanceRefund'])}
												<span class="green">{$_LANG['yes']}</span>
												<div>
                                                    {balanceLabel ref=$serviceDetails['idBalanceRefund'] format="%balanceNumber%"}
											    </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}

                <tr>
                    <td colspan="6" class="left no-wrap" style="font-weight:bold;">{$_LANG['dashboard']['widgets']['_GENERAL_']['generalTotal']}</td>

                    {foreach $colsAssoc as $col=>$nothing}
                        {if $col=='offer'}
                            <td class="right nowrap" style="font-weight:bold;">
                                {$separator=""}
                                {foreach $widget['data']['groupedOfferTotals'] as $currency => $total}
                                    {if $total}
                                        {$separator}
                                        <span class="no-wrap">{$total|priceFormat:$currency}</span>
                                        {$separator=" + "}
                                    {/if}
                                {/foreach}
                            </td>
                        {/if}

                        {if $col=='invoiced'}
                            <td class="right nowrap" style="font-weight:bold;">
                                {$separator=""}
                                {foreach $widget['data']['groupedInvoicedTotals'] as $currency => $total}
                                    {if $total}
                                        {$separator}
                                        <span class="no-wrap">{$total|priceFormat:$currency}</span>
                                        {$separator=" + "}
                                    {/if}
                                {/foreach}
                            </td>
                        {/if}

                        {if $col=='cashed'}
                            <td class="right nowrap" style="font-weight:bold;">
                                {$separator=""}
                                {foreach $widget['data']['groupedCashedTotals'] as $currency => $total}
                                    {if $total}
                                        {$separator}
                                        <span class="no-wrap">{$total|priceFormat:$currency}</span>
                                        {$separator=" + "}
                                    {/if}
                                {/foreach}
                            </td>
                        {/if}

                        {if $col=='notCashed'}
                            <td class="right nowrap" style="font-weight:bold;">
                                {$separator=""}
                                {foreach $widget['data']['groupedNotCashedTotals'] as $currency => $total}
                                    {if $total}
                                        {$separator}
                                        <span class="no-wrap">{$total|priceFormat:$currency}</span>
                                        {$separator=" + "}
                                    {/if}
                                {/foreach}
                            </td>
                        {/if}

                    {/foreach}

                </tr>

			</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}
