@using MrCMS.Helpers
@using MrCMS.Web.Apps.Ecommerce.Helpers
@using MrCMS.Web.Apps.Ecommerce.Models
@using MrCMS.Web.Apps.Ecommerce.Settings
@model MrCMS.Web.Apps.Ecommerce.Pages.Product
<table class="table table-condensed table-striped" id="product-pricing-info">
    <thead>
        <tr>
            <th>
                Name
            </th>
            @foreach (var option in Model.Options)
            {
                <th>
                    @option.Name
                </th>
            }
            <th>
                Product Type
            </th>
            <th>
                SKU
            </th>
            <th>
                Price
            </th>
            <th>
                Stock Remaining
            </th>
            <th>
                Weight (kg)
            </th>
            <th>
                Actions
            </th>
        </tr>
    </thead>
    <tbody>
        @foreach (var variant in Model.Variants)
        {
            <tr>
                <td>
                    @if (!String.IsNullOrWhiteSpace(variant.Name))
                    {
                        <span>@variant.Name</span>
                    }
                    else
                    {
                        <span>@Model.Name</span>
                    }
                </td>
                @foreach (var option in Model.Options)
                {
                    var productAttributeValue = variant.OptionValues.FirstOrDefault(value => value.ProductOption.Name == option.Name);
                    <td>
                        @(productAttributeValue != null ? productAttributeValue.Value : string.Empty)
                    </td>
                }
                <td>
                    @variant.VariantType.GetDescription().BreakUpString()
                </td>
                <td>
                    @variant.SKU
                </td>
                <td>
                    @variant.BasePrice.ToCurrencyFormat() @Html.ActionLink("Add price break", "Add", "PriceBreak", new { id = variant.Id, type = "ProductVariant" }, new { @class = "btn btn-xs btn-default", data_toggle = "fb-modal" })
                    @if (variant.PriceBreaks.Any())
                    {
                        <h5>Price Breaks</h5>
                        <table class="table table-striped table-condensed">
                            <tr>
                                <td>Qty</td>
                                <td>Price</td>
                                <td></td>
                            </tr>
                            @foreach (var priceBreak in variant.PriceBreaks.OrderBy(@break => @break.Quantity))
                            {
                                <tr>
                                    <td>@priceBreak.Quantity</td>
                                    <td>@priceBreak.Price.ToCurrencyFormat()</td>
                                    <td>@Html.ActionLink("Delete", "Delete", "PriceBreak", new { id = priceBreak.Id }, new { @class = "btn btn-danger btn-xs pull-right", data_toggle = "fb-modal" })</td>
                                </tr>
                            }
                        </table>
                    }
                </td>
                <td>
                    @if (variant.TrackingPolicy == TrackingPolicy.DontTrack)
                    {
                        <text>Not tracked</text>
                    }
                    else if (SiteSettings<EcommerceSettings>().WarehouseStockEnabled)
                    {
                        @variant.GetStockRemaining() <text> </text> @Html.ActionLink("Manage Stock", "Index", "WarehouseStock", new { sku = variant.SKU }, new { @class = "btn btn-xs btn-default" })
                    }
                    else
                    {
                        @variant.StockRemaining.ToString()
                    }
                </td>

                <td>
                    @variant.Weight.ToString("0.0000")
                </td>
                <td>
                    <div class="btn-group">
                        @Html.ActionLink("Edit", "Edit", "ProductVariant", new { id = variant.Id }, new { @class = "btn btn-xs btn-default", data_toggle = "fb-modal" })
                        @if (Model.Variants.Count > 1)
                        {
                            @Html.ActionLink("Delete", "Delete", "ProductVariant", new { id = variant.Id }, new { @class = "btn btn-xs btn-danger", data_toggle = "fb-modal" })
                        }
                    </div>
                </td>
            </tr>
        }
    </tbody>
</table>
