@model ProductDetailsModel

@inject Nop.Core.IWebHelper webHelper

@{ 
    var isAssociatedProduct = ViewData.ContainsKey("isAccessoryOrSparePartFor");
    var productUrl = isAssociatedProduct && ViewData.ContainsKey("parentURL")
        ? ViewData["parentURL"]
        : Url.RouteUrl("Product", new { SeName = Model.SeName }, webHelper.GetCurrentRequestProtocol()).ToLowerInvariant();
    var productPrice = Model.AssociatedProducts.Any()
        ? Model.AssociatedProducts.Min(associatedProduct => associatedProduct.ProductPrice.PriceValue)
        : Model.ProductPrice.PriceValue;
}

<!--Microdata-->
<div @if (isAssociatedProduct) { <text>itemprop="isAccessoryOrSparePartFor"</text> } itemscope itemtype="http://schema.org/Product">
    <meta itemprop="name" content="@Model.Name"/>
    <meta itemprop="sku" content="@Model.Sku"/>
    <meta itemprop="gtin" content="@Model.Gtin"/>
    <meta itemprop="mpn" content="@Model.ManufacturerPartNumber"/>
    <meta itemprop="description" content="@Model.ShortDescription"/>
    <meta itemprop="image" content="@Model.DefaultPictureModel.ImageUrl"/>
    @foreach (var manufacturer in Model.ProductManufacturers)
    {
        <meta itemprop="brand" content="@manufacturer.Name"/>
    }
    @if (Model.ProductReviewOverview.TotalReviews > 0)
    {
        <div itemprop="aggregateRating" itemscope itemtype="http://schema.org/AggregateRating">
            @{
                var ratingPercent = 0;
                if (Model.ProductReviewOverview.TotalReviews != 0)
                {
                    ratingPercent = ((Model.ProductReviewOverview.RatingSum * 100) / Model.ProductReviewOverview.TotalReviews) / 5;
                }
                var ratingValue = ratingPercent / (decimal)20;
            }
            <meta itemprop="ratingValue" content="@ratingValue.ToString("0.0", System.Globalization.CultureInfo.InvariantCulture)"/>
            <meta itemprop="reviewCount" content="@Model.ProductReviewOverview.TotalReviews"/>
        </div>
    }    
    <div itemprop="offers" itemscope itemtype="http://schema.org/Offer">
        <meta itemprop="url" content="@productUrl"/>
        <meta itemprop="price" content="@productPrice.ToString("0.00", System.Globalization.CultureInfo.InvariantCulture)"/>
        <meta itemprop="priceCurrency" content="@Model.ProductPrice.CurrencyCode"/>
        <meta itemprop="priceValidUntil" content="@Model.AvailableEndDate"/>
        <meta itemprop="availability" content="http://schema.org/@(Model.InStock ? "InStock" : "OutOfStock")" />
    </div>
    <div itemprop="review" itemscope itemtype="http://schema.org/Review">
        <meta itemprop="author" content="ALL"/>
        <meta itemprop="url" content="@Url.RouteUrl("ProductReviews", new { productId = Model.ProductReviewOverview.ProductId })"/>
    </div>
    @foreach (var product in Model.AssociatedProducts)
    {
        var viewDataDictionary = new ViewDataDictionary(ViewData);
        viewDataDictionary.Add("isAccessoryOrSparePartFor", "true");
        if (!product.VisibleIndividually)
        {
            viewDataDictionary.Add("parentURL", productUrl);
        }
        
        @await Html.PartialAsync("_Microdata", product, viewDataDictionary)        
    }
</div>
