<#@ template language="C#" HostSpecific="True" #>
<#@ output extension=".cshtml" #>
<#@ include file="..\..\ViewHead.cshtml.include.t4" #>
@using C1.Web.Mvc.Fluent

@model IEnumerable<<#= ViewDataTypeName #>>
<p>
    @Html.ActionLink("<#= Resources["InputsCreateNew"] #>", "Create")
</p>
<#
    var param = "{";
    var paramName = "key";
    int i = 1;
    foreach (var primaryKey in PrimaryKeys)
    {
        param = param + paramName + i + "=item." + primaryKey.Key + ",";
        i++;
    }
    param = param.Substring(0, param.Length - 1) + "}";
    var selectedModelProperties = Component["SelectedModelProperties"];
    var fields = Component["Fields"];
#>
<table class="table">
    <tr>
<#
        foreach (var field in fields)
        {
            var hasHeader = !string.IsNullOrEmpty(field["Header"]);
            var fieldName = hasHeader ? field["Header"] : field["Binding"];
            if (hasHeader)
            {
#>
        <th>
            <#= fieldName#>
        </th>
<#
            }
            else
            {
#>
        <th>
            @Html.DisplayNameFor(model => model.<#= fieldName#>)
        </th>
<#
            }
        }
#>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
<#
        var index = 0;
        foreach (var field in fields)
        {
            var fieldName = field["Binding"];
            var fieldType = string.Empty;
            var modelExpression = string.Empty;
            foreach (var property in selectedModelProperties)
            {
                if (field["Binding"] == property["Name"])
                {
                    fieldType = property["TypeName"];
                    break;
                }
            }
            if (!string.IsNullOrEmpty(field["Format"]) && !string.IsNullOrEmpty(fieldType) && fieldType != "string" && fieldType != "bool" && fieldType != "bool?")
            {
                if (fieldType.Contains("?"))
                {
                    modelExpression = "item." + fieldName +".HasValue ? item." + fieldName +".Value.ToString(\"" + field["Format"] + "\") : \"\"";
                }
                else
                {
                    modelExpression = "item." + fieldName + ".ToString(\"" + field["Format"] + "\")";
                }
                index++;
#>
            @{var property<#= index#> = <#= modelExpression#>;}
<#
            }
#>
        <td>
<#
            if (string.IsNullOrEmpty(modelExpression))
            {
#>
            @Html.DisplayFor(model => item.<#= field["Binding"]#>)
<#
            }
            else
            {
#>
            @Html.DisplayFor(model => property<#= index#>)
<#
            }
#>
        </td>
<#
        }
#>
        <td>
            @Html.ActionLink("<#= Resources["InputsEdit"] #>", "Edit", new <#= param#>) |
            @Html.ActionLink("<#= Resources["InputsDetails"] #>", "Details", new <#= param#>) |
            @Html.ActionLink("<#= Resources["InputsDelete"] #>", "Delete", new <#= param#>)
        </td>
    </tr>
}
</table>