@inherits Microsoft.Extensions.CodeGeneration.Templating.RazorTemplateBase
@using Microsoft.Extensions.CodeGeneration.EntityFramework
@@model @GetEnumerableTypeExpression(Model.ViewDataTypeName)

@{
    if (Model.IsPartialView)
    {
    }
    else if (Model.IsLayoutPageSelected)
    {
@:@@{
    @:ViewData["Title"] = @@SR["@Model.ViewName"];
        if (!string.IsNullOrEmpty(Model.LayoutPageFile))
        {
    @:Layout = "@Model.LayoutPageFile";
        }
@:}
@:
@:<h2>@@SR["@Model.ViewName"]</h2>
@:
    }
    else
    {
@:@@{
    @:Layout = null;
@:}
@:
@:<!DOCTYPE html>
@:
@:<html>
@:<head>
    @:<meta name="viewport" content="width=device-width" />
    @:<title>@@SR["@Model.ViewName"]</title>
@:</head>
@:<body>
        //    PushIndent("    ");
    }
@:<p>
    @:<a asp-action="Create">@@SR["Create New"]</a>
@:</p>
@:<table class="table">
    @:<tr>
    IEnumerable<PropertyMetadata> properties = Model.ModelMetadata.Properties;
    foreach (var property in properties)
    {
        if (property.Scaffold && !property.IsPrimaryKey && !property.IsForeignKey)
        {
        <th>
            @@Html.DisplayNameFor(model => model.@GetValueExpression(property))
        </th>
        }
    }
        @:<th></th>
    @:</tr>
    @:
@:@@foreach (var item in Model) {
    @:<tr>
    foreach (PropertyMetadata property in properties)
    {
        if (property.Scaffold && !property.IsPrimaryKey && !property.IsForeignKey)
        {
        <td>
            @@Html.DisplayFor(modelItem => item.@GetValueExpression(property))
        </td>
        }
    }

    string pkName = GetPrimaryKeyName();
    if (pkName != null)
    {
        @:<td>
            @:<a asp-action="Edit" asp-route-id="@@item.@pkName">@@SR["Edit"]</a> |
            @:<a asp-action="Details" asp-route-id="@@item.@pkName">@@SR["Details"]</a> |
            @:<a asp-action="Delete" asp-route-id="@@item.@pkName">@@SR["Delete"]</a>
        @:</td>
    }
    else
    {
        <td>
            @@Html.ActionLink(SR["Edit"], "Edit",new { /* id=item.PrimaryKey */ }) |
            @@Html.ActionLink(SR["Details"], "Details", new { /* id=item.PrimaryKey */ }) |
            @@Html.ActionLink(SR["Delete"], "Delete", new { /* id=item.PrimaryKey */ })
        </td>
    }
    @:</tr>
@:}

@:</table>
    if(!Model.IsPartialView && !Model.IsLayoutPageSelected)
    {
        //ClearIndent();
@:</body>
@:</html>
    }
}
@functions
{
    string GetPrimaryKeyName()
    {
        return (Model.ModelMetadata.PrimaryKeys != null && Model.ModelMetadata.PrimaryKeys.Length == 1)
        ? Model.ModelMetadata.PrimaryKeys[0].PropertyName
        : null;
    }

    string GetValueExpression(PropertyMetadata property)
    {
        //Todo: Get the association for the property and use that.
        return property.PropertyName;
    }

    string GetEnumerableTypeExpression(string typeName)
    {
        return "IEnumerable<" + typeName + ">";
    }
}