@model (IList<Order>, string)
@inject Grand.Business.Core.Interfaces.Catalog.Products.IProductService _productService
@inject Grand.Business.Core.Interfaces.Common.Directory.ICountryService _countryService
@inject Grand.Business.Core.Interfaces.Common.Stores.IStoreService _storeService
@inject Grand.Business.Core.Interfaces.Storage.IPictureService _pictureService
@inject PdfSettings _pdfSettings
@inject Grand.Business.Core.Interfaces.Common.Localization.ITranslationService _translationService
@inject Grand.Business.Core.Interfaces.Checkout.Payments.IPaymentService _paymentService
@inject Grand.Business.Core.Interfaces.Common.Directory.ICurrencyService _currencyService
@inject Grand.Business.Core.Interfaces.Checkout.GiftVouchers.IGiftVoucherService _giftVoucherService
@{
    var logoPicture = await _pictureService.GetPictureById(_pdfSettings.LogoPictureId);
    var logoExists = logoPicture != null;
    var limitbyVendor = !string.IsNullOrEmpty(Model.Item2);
}
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <style>

        .orderItems {
            width: 100%;
            border: 1px solid black;
        }

            .orderItems tr th {
                background-color: #f5f5f5;
                text-align: left;
                padding: 0 5px 0 5px;
            }

            .orderItems tr,
            .orderItems tr td,
            .orderItems tr th {
                border: 1px solid black;
                padding: 0 5px 0 5px;
            }

        .summary {
            margin-top: 20px;
        }

        .footer {
            position: fixed;
            bottom: 0;
            width: 100%;
        }
    </style>
</head>
<body>
    @{
        var i = 0;
    }
    @foreach (var order in Model.Item1)
    {
        var priceIncludesTax = order.CustomerTaxDisplayTypeId == TaxDisplayType.IncludingTax;
        var store = await _storeService.GetStoreById(order.StoreId);
        if (!string.IsNullOrEmpty(_pdfSettings.InvoiceHeaderText))
        {
            <div>  @Html.Raw(@_pdfSettings.InvoiceHeaderText)</div>
        }
        <h2>@store.CompanyName</h2>
        if (logoExists)
        {
            var url = await _pictureService.GetPictureUrl(logoPicture, 0, false, store.Url);
            <div><img src="@url" alt="logo" title="logo" /></div>
        }
        <h3>@(string.Format(_translationService.GetResource("PDFInvoice.Order#", order.CustomerLanguageId), order.OrderNumber))</h3>
        <table>
            <tr>
                <td style="border-style: none; width:50%;">
                    @if (order.BillingAddress != null)
                    {
                        <ul style="list-style-type:none">
                            <li><b>@(_translationService.GetResource("PDFInvoice.BillingInformation", order.CustomerLanguageId))</b></li>
                            @if (!string.IsNullOrEmpty(order.BillingAddress.Company))
                            {
                                <li>@(string.Format(_translationService.GetResource("PDFInvoice.Company", order.CustomerLanguageId), order.BillingAddress.Company))</li>
                            }
                            <li>@(string.Format(_translationService.GetResource("PDFInvoice.Name", order.CustomerLanguageId), order.BillingAddress.FirstName + " " + order.BillingAddress.LastName))</li>
                            <li>@(string.Format(_translationService.GetResource("PDFInvoice.Phone", order.CustomerLanguageId), order.BillingAddress.PhoneNumber))</li>
                            <li>@(string.Format(_translationService.GetResource("PDFInvoice.Address", order.CustomerLanguageId), order.BillingAddress.Address1))</li>
                            <li>@(order.BillingAddress.City + ", " + order.BillingAddress.ZipPostalCode)</li>
                            <li>@((await _countryService.GetCountryById(order.BillingAddress.CountryId))?.Name)</li>
                            @{
                                var paymentMethod = _paymentService.LoadPaymentMethodBySystemName(order.PaymentMethodSystemName);
                                string paymentMethodStr = paymentMethod != null ? paymentMethod.FriendlyName : order.PaymentMethodSystemName;
                            }
                            <li>@(string.Format(_translationService.GetResource("PDFInvoice.PaymentMethod", order.CustomerLanguageId), paymentMethodStr))</li>
                            @await Component.InvokeAsync("Widget", new { widgetZone = "pdf_payment_method_below", additionalData = @order.Id })
                        </ul>
                    }
                </td>
                <td style="border-style: none; width:50%;">
                    @if (order.ShippingAddress != null)
                    {
                        <ul style="list-style-type:none">
                            <li><b>@(_translationService.GetResource("PDFInvoice.ShippingInformation", order.CustomerLanguageId))</b></li>
                            @if (!string.IsNullOrEmpty(order.ShippingAddress.Company))
                            {
                                <li>@(string.Format(_translationService.GetResource("PDFInvoice.Company", order.CustomerLanguageId), order.ShippingAddress.Company))</li>
                            }
                            <li>@(string.Format(_translationService.GetResource("PDFInvoice.Name", order.CustomerLanguageId), order.ShippingAddress.FirstName + " " + order.ShippingAddress.LastName))</li>
                            <li>@(string.Format(_translationService.GetResource("PDFInvoice.Phone", order.CustomerLanguageId), order.ShippingAddress.PhoneNumber))</li>
                            <li>@(string.Format(_translationService.GetResource("PDFInvoice.Address", order.CustomerLanguageId), order.ShippingAddress.Address1))</li>
                            <li>@(order.ShippingAddress.City + ", " + order.ShippingAddress.ZipPostalCode)</li>
                            <li>@((await _countryService.GetCountryById(order.ShippingAddress.CountryId))?.Name)</li>
                            <li>@(string.Format(_translationService.GetResource("PDFInvoice.ShippingMethod", order.CustomerLanguageId), order.ShippingMethod))</li>
                        </ul>
                    }
                </td>
            </tr>
        </table>

        <div style="display:block; clear:both;"></div>
        <div class="summary">
            <h3>@(_translationService.GetResource("PDFInvoice.Product(s)", order.CustomerLanguageId))</h3>
        </div>
        <table class="orderItems">
            <tr>
                <th>@(_translationService.GetResource("PDFInvoice.ProductName", order.CustomerLanguageId))</th>
                <th>@(_translationService.GetResource("PDFInvoice.ProductPrice", order.CustomerLanguageId))</th>
                <th>@(_translationService.GetResource("PDFInvoice.ProductQuantity", order.CustomerLanguageId))</th>
                <th>@(_translationService.GetResource("PDFInvoice.ProductTotal", order.CustomerLanguageId))</th>
            </tr>
            @foreach (var item in order.OrderItems.Where(x => x.VendorId == Model.Item2 || !limitbyVendor).ToList())
            {
                <tr>
                    <td>
                        @((await _productService.GetProductByIdIncludeArch(item.ProductId)).Name)
                        @if (!string.IsNullOrEmpty(item.AttributeDescription))
                        {
                            <br />
                            <small>@(Html.Raw(item.AttributeDescription))</small>
                        }
                    </td>
                    <td>@if(priceIncludesTax){@item.UnitPriceInclTax}else{@item.UnitPriceExclTax}</td>
                    <td>@item.Quantity</td>
                    <td>@if(priceIncludesTax){@item.PriceInclTax}else{@item.PriceExclTax}</td>
                </tr>
            }
        </table>
        @if (!limitbyVendor)
        {
            <div class="summary">
                <hr />
                <div class="right">
                    <ul style="list-style-type:none">
                        <li>@(_translationService.GetResource("PDFInvoice.Sub-Total", order.CustomerLanguageId))@if(priceIncludesTax){ @order.OrderSubtotalInclTax}else{ @order.OrderSubtotalExclTax} @order.CustomerCurrencyCode</li>
                        @if (order.OrderSubTotalDiscountInclTax > 0||order.OrderSubTotalDiscountExclTax > 0)
                        {
                            <li>@(_translationService.GetResource("PDFInvoice.Discount", order.CustomerLanguageId)) @if(priceIncludesTax){ @order.OrderSubTotalDiscountInclTax}else{  @order.OrderSubTotalDiscountExclTax} @order.CustomerCurrencyCode</li>
                        }
                        @if (order.PaymentMethodAdditionalFeeInclTax > 0 ||order.PaymentMethodAdditionalFeeExclTax > 0)
                        {
                            <li>@(_translationService.GetResource("PDFInvoice.PaymentmethodAdditionalFee", order.CustomerLanguageId)) @if(priceIncludesTax){@order.PaymentMethodAdditionalFeeInclTax}else{  @order.PaymentMethodAdditionalFeeExclTax} @order.CustomerCurrencyCode</li>
                        }
                        <li>@(_translationService.GetResource("PDFInvoice.Shipping", order.CustomerLanguageId)) @if(priceIncludesTax){@order.OrderShippingInclTax}else{  @order.PaymentMethodAdditionalFeeExclTax}  @order.OrderShippingExclTax @order.CustomerCurrencyCode</li>
                        @if (order.OrderDiscount > 0)
                        {
                            <li>@(_translationService.GetResource("PDFInvoice.Discount", order.CustomerLanguageId)) @order.OrderDiscount @order.CustomerCurrencyCode</li>
                        }
                        @foreach (var gcuh in await _giftVoucherService.GetAllGiftVoucherUsageHistory(order.Id))
                        {
                            if (gcuh.UsedValue > 0)
                            {
                                var giftvoucher = await _giftVoucherService.GetGiftVoucherById(gcuh.GiftVoucherId);
                                <li>@(string.Format(_translationService.GetResource("PDFInvoice.GiftVoucherInfo", order.CustomerLanguageId), giftvoucher.Code)) @gcuh.UsedValue</li>
                            }
                        }
                        @if (order.OrderTax > 0)
                        {
                            <li>@(_translationService.GetResource("PDFInvoice.Tax", order.CustomerLanguageId)) @order.OrderTax @order.CustomerCurrencyCode</li>
                        }
                        <li>@(_translationService.GetResource("PDFInvoice.OrderTotal", order.CustomerLanguageId)) @order.OrderTotal @order.CustomerCurrencyCode</li>
                    </ul>
                </div>
            </div>
        }
        if (!string.IsNullOrEmpty(_pdfSettings.InvoiceFooterText))
        {
            <footer class="footer">
                 @Html.Raw(@_pdfSettings.InvoiceFooterText)
            </footer>
        }
        if (Model.Item1.Count - 1 > i)
        {
            <div style="display:block; clear:both; page-break-after:always;"></div>
        }
        i++;
    }

</body>
</html>