@inherits Dynamicweb.Rendering.RazorTemplateBase<Dynamicweb.Rendering.RazorTemplateModel<Dynamicweb.Rendering.Template>>
@using Dynamicweb.Ecommerce.Prices

@{
	var list = GetLoop("Ecom:IntegrationCustomerCenter.InvoiceLoop");
	string shopId = Pageview.Area.EcomShopId != null ? Pageview.Area.EcomShopId : string.Empty;
}

<header class="hstack gap-3 mb-4">
    <h1 class="h4 mb-0">@(Pageview.Page.GetDisplayName())</h1>
</header>

<div class="card overflow-hidden">
    <div class="table-responsive">
        <table class="table table-md table-hover mb-0 align-middle fs-7" style="--bs-table-color: var(--bs-body-color);" hx-swap-oob="true outerHTML:[data-swift-order-list]" data-swift-order-list>
            <thead style="--bs-table-bg: rgba(var(--bs-body-color-rgb), .0275); --bs-table-color: rgba(var(--bs-body-color-rgb), 0.5);">
				<tr>
					<th>@Translate("Order number")</th>
					<th>@Translate("Placed")</th>
					<th>@Translate("Total")</th>
					<th>&nbsp;</th>
				</tr>
			</thead>
			<tbody>
				@foreach (LoopItem item in list)
				{
					var currency = Dynamicweb.Ecommerce.Services.Currencies.GetCurrency(item.GetString("CurrencyCode")) ?? null;
					string price = currency != null ? new PriceInfo(currency) { PriceWithoutVAT = item.GetDouble("totalAmount") }.PriceWithoutVATFormatted : item.GetString("totalAmount");
					string orderDate = !string.IsNullOrEmpty(item.GetString("orderDate")) ? item.GetDate("orderDate").TimeOfDay.Ticks > 0 ? item.GetDate("orderDate").ToString(Dynamicweb.Core.Helpers.DateHelper.DateOnlyFormatStringMedium) : item.GetDate("orderDate").ToShortDateString() : string.Empty;
					string orderId = item.GetString("id");

					<tr class="position-relative">
						<td>
							<a href="@GetString("Ecom:IntegrationCustomerCenter.Invoice.Link")&itemID=@orderId" class="position-relative z-2 text-decoration-none text-decoration-underline-hover" data-dw-button="outline-secondary"  title="@Translate("View order")">@orderId</a>
						</td>
						<td>@orderDate</td>
						<td class="text-price">@price</td>
						<td>
							<div class="hstack gap-3">
								<button class="btn btn-outline-secondary text-body hstack border-0 p-2 position-relative z-2 ms-auto" data-dw-button="outline-secondary" type="button" data-bs-toggle="dropdown" aria-expanded="false">
									<span class="icon-2">
										@ReadFile("/Files/Images/Icons/ellipsis.svg")
									</span>
								</button>
								<ul class="dropdown-menu shadow-sm fs-7">
									<li>
										<a class="dropdown-item hstack gap-2" href="@GetString("Ecom:IntegrationCustomerCenter.OpenOrder.Link")&itemID=@orderId">
											<span class="icon-2">
												@ReadFile("/Files/Images/Icons/eye.svg")
											</span>
											@Translate("View")
										</a>
									</li>
									<li>
										<a class="dropdown-item hstack gap-2" href="/Admin/public/CustomerCenter/RequestExternalPdf.aspx?type=OpenOrder&id=@(orderId)&forceDownload=true&shopid=@(shopId)" title="@Translate("Download")">
											<span class="icon-2">
												@ReadFile("/Files/Images/Icons/download.svg")
											</span>
											@Translate("Download")
										</a>
									</li>
								</ul>
							</div>
						</td>
					</tr>
				}

				@if( list.Count() < 1 )
				{
					<tr class="position-relative">
						<td colspan="8">
							<div class="d-grid py-4">
								<span class="m-auto">
									<div class="d-grid rounded-circle m-auto" style="width:4rem;aspect-ratio:1;background-color:rgba(var(--bs-body-color-rgb), .0275);">
										<span class="icon-4 text-muted">
											@ReadFile("/Files/Images/Icons/file-invoice-dollar.svg")
										</span>
									</div>
									<div class="mt-3">@Translate("No invoices found")</div>
								</span>
							</div>
						</td>
					</tr>
				}
			</tbody>
		</table>
	</div>
</div>

<nav class="p-2 mt-3">
	<ul class="pagination m-0">
		@foreach (LoopItem nodePage in GetLoop("Ecom:IntegrationCustomerCenter.Paging.Pages"))
		{
			string isActive = nodePage.GetString("Ecom:IntegrationCustomerCenter.Paging.PageClass") == "currentPage" ? "active" : string.Empty;
			<li class="page-item @(isActive)">
				<a href="@nodePage.GetString("Ecom:IntegrationCustomerCenter.Paging.PageIndexUrl")" class="page-link">
					@nodePage.GetString("Ecom:IntegrationCustomerCenter.Paging.PageIndex")
				</a>
			</li>
		}
	</ul>
</nav>
