@using Grand.Domain.Shipping
@model IList<Shipment>
@inject Grand.Business.Core.Interfaces.Checkout.Orders.IOrderService _orderService
@inject Grand.Business.Core.Interfaces.Checkout.Shipping.IShipmentService _shipmentService
@inject Grand.Business.Core.Interfaces.Common.Directory.ICountryService _cournyService
@inject Grand.Business.Core.Interfaces.Catalog.Products.IProductService _productService
@inject Grand.Business.Core.Interfaces.Common.Directory.ICountryService _countryService
@inject Grand.Business.Core.Interfaces.Common.Configuration.ISettingService _settingService
@inject Grand.Business.Core.Interfaces.Common.Stores.IStoreService _storeService
@inject Grand.Infrastructure.IWorkContext _workContext
@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 companyName = _workContext.CurrentStore.CompanyName;
    var limitbyVendor = _workContext.CurrentVendor != null;
}



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

        .right {
            float: right;
        }
    </style>
</head>
<body>
    @for (int i = 0; i < Model.Count; i++)
    {
        var order = await _orderService.GetOrderById(Model[i].OrderId);
        var shipment = Model[i];
        <div>
            <h2>@(string.Format(_translationService.GetResource("PDFPackagingSlip.Shipment", order.CustomerLanguageId), shipment.ShipmentNumber))</h2>
            <div>@companyName</div>
        </div>
        @if (order.ShippingAddress != null)
        {
            <div>
                <h3>@(_translationService.GetResource("PDFInvoice.ShippingInformation", order.CustomerLanguageId))</h3>
                <ul style="list-style-type:none">
                    @if (!string.IsNullOrEmpty(order.ShippingAddress.Company))
                    {
                        <li>@(string.Format(_translationService.GetResource("PDFPackagingSlip.Company", order.CustomerLanguageId), order.ShippingAddress.Company))</li>
                    }
                    <li>@(string.Format(_translationService.GetResource("PDFPackagingSlip.Name", order.CustomerLanguageId), order.ShippingAddress.FirstName + " " + order.ShippingAddress.LastName))</li>
                    <li>@(string.Format(_translationService.GetResource("PDFPackagingSlip.Phone", order.CustomerLanguageId), order.ShippingAddress.PhoneNumber))</li>
                    <li>@(string.Format(_translationService.GetResource("PDFPackagingSlip.Address", order.CustomerLanguageId), order.ShippingAddress.Address1))</li>
                    <li>@(order.ShippingAddress.City + ", " + order.ShippingAddress.ZipPostalCode)</li>
                    <li>@((await _countryService.GetCountryById(order.ShippingAddress.CountryId))?.Name)</li>
                </ul>
            </div>
        }
        <div style="display:block; clear:both;"></div>
        <h5>@(string.Format(_translationService.GetResource("PDFPackagingSlip.ShippingMethod", order.CustomerLanguageId), order.ShippingMethod))</h5>
        <div class="summary">
            <h3>@(_translationService.GetResource("PDFInvoice.Product(s)", order.CustomerLanguageId))</h3>
        </div>
        <table class="orderItems">
            <tr>
                <th>@(_translationService.GetResource("PDFPackagingSlip.ProductName", order.CustomerLanguageId))</th>
                <th>@(_translationService.GetResource("PDFPackagingSlip.SKU", order.CustomerLanguageId))</th>
                <th>@(_translationService.GetResource("PDFPackagingSlip.QTY", order.CustomerLanguageId))</th>
            </tr>
            @foreach (var item in shipment.ShipmentItems)
            {
                var orderItem = order.OrderItems.Where(x => x.Id == item.OrderItemId).FirstOrDefault();
                if (orderItem != null && (orderItem.VendorId == _workContext.CurrentVendor?.Id || !limitbyVendor))
                {
                    <tr>
                        <td>
                            @((await _productService.GetProductByIdIncludeArch(item.ProductId)).Name)
                            @if (!string.IsNullOrEmpty(orderItem.AttributeDescription))
                            {
                                <br />
                                <small>@(Html.Raw(orderItem.AttributeDescription))</small>
                            }
                        </td>

                        <td>@((await _productService.GetProductByIdIncludeArch(item.ProductId)).Sku)</td>
                        <td>@item.Quantity</td>
                    </tr>
                }
            }
        </table>

        if (Model.Count - 1 > i)
        {

            <div style="display:block; clear:both; page-break-after:always;"></div>
        }
    }
</body>
</html>