@inherits Microsoft.VisualStudio.Web.CodeGeneration.Templating.RazorTemplateBase
@using Microsoft.VisualStudio.Web.CodeGeneration.EntityFrameworkCore
@using System.Collections.Generic
@using System.Linq
@@model @Model.ViewDataTypeName

@{
    if (Model.IsPartialView)
    {
    }
    else if (Model.IsLayoutPageSelected)
    {
@:@@{
    @:ViewData["Title"] = "@Model.ViewName";
        if (!string.IsNullOrEmpty(Model.LayoutPageFile))
        {
    @:Layout = "@Model.LayoutPageFile";
        }
@:}
@:
@:<h1>@Model.ViewName</h1>
@:
    }
    else
    {
@:@@{
    @:Layout = null;
@:}
@:
@:<!DOCTYPE html>
@:
@:<html>
@:<head>
    @:<meta name="viewport" content="width=device-width" />
    @:<title>@Model.ViewName</title>
@:</head>
@:<body>
    @:
    //    PushIndent("    ");
}
@:<h4>@Model.ViewDataTypeShortName</h4>
@:<hr />
@:<div class="row">
    @:<div class="col-md-4">
        string formId = Model.ViewDataTypeShortName + "Form";
        @:<form asp-action="@Model.ViewName" id="@formId">
            @:<div asp-validation-summary="ModelOnly" class="text-danger"></div>
            foreach (var property in Model.ModelMetadata.Properties)
            {
                if (property.Scaffold && !property.IsAutoGenerated && !property.IsReadOnly)
                {
                    if (property.IsForeignKey)
                    {
                        @:<div class="form-group">
                            @:<label asp-for="@property.PropertyName"></label>
                            @:<select asp-for="@property.PropertyName" asp-items="ViewBag.@property.PropertyName"></select>
                        @:</div>
                        continue;
                    }

                    bool isCheckbox = property.TypeName.Equals("System.Boolean");
                    bool isDatetime = property.TypeName.Contains("System.DateTime");

                    if (isCheckbox)
                    {
                        @:<div class="form-group">
                            @:@@Html.Kendo().CheckBoxFor(m => m.@property.PropertyName)
                            @:<label asp-for="@property.PropertyName"></label>
                            @:<span asp-validation-for="@property.PropertyName" class="text-danger k-invalid-msg" data-for="@property.PropertyName"></span>
                        @:</div>
                    }
                    else if (isDatetime)
                    {
                        @:<div class="form-group">
                            @:<label asp-for="@property.PropertyName"></label>
                            @:<kendo-datetimepicker for="@property.PropertyName" />
                            @:<span asp-validation-for="@property.PropertyName" class="text-danger k-invalid-msg" data-for="@property.PropertyName"></span>
                        @:</div>
                    }
                    else if (IsNumericType(property))
                    {
                        @:<div class="form-group">
                            @:<label asp-for="@property.PropertyName"></label>
                            @:<kendo-numerictextbox for="@property.PropertyName" />
                            @:<span asp-validation-for="@property.PropertyName" class="text-danger k-invalid-msg" data-for="@property.PropertyName"></span>
                        @:</div>
                    }
                    else if (property.IsEnum && !property.IsEnumFlags)
                    {
                        @:<div class="form-group">
                            @:<label asp-for="@property.PropertyName"></label>
                            @:<select asp-for="@property.PropertyName"></select>
                            @:<span asp-validation-for="@property.PropertyName" class="text-danger k-invalid-msg" data-for="@property.PropertyName"></span>
                        @:</div>
                    }
                    else if (property.IsMultilineText)
                    {
                        @:<div class="form-group">
                            @:<label asp-for="@property.PropertyName"></label>
                            @:<textarea asp-for="@property.PropertyName" class="k-textbox"></textarea>
                            @:<span asp-validation-for="@property.PropertyName" class="text-danger k-invalid-msg" data-for="@property.PropertyName"></span>
                        @:</div>
                    }
                    else
                    {
                        @:<div class="form-group">
                            @:<label asp-for="@property.PropertyName"></label>
                            @:<input asp-for="@property.PropertyName" class="k-textbox" />
                            @:<span asp-validation-for="@property.PropertyName" class="text-danger k-invalid-msg" data-for="@property.PropertyName"></span>
                        @:</div>
                    }
                }
            }
}
<div class="form-group">
    <input type="submit" value="Create" class="k-button k-primary" />
</div>
</form>
</div>
</div>

<div>
    <a asp-action="Index" class="k-button">Back to List</a>
</div>

@{
    if (Model.ReferenceScriptLibraries)
    {
    @:<script>
        @:$(document).ready(function () {
            @:$("#@formId").kendoValidator({
                @:// You can define your custom rules here:
                @://rules: {}
            @:});
        @:})
    @:</script>
    }
    // The following code closes the tag used in the case of a view using a layout page and the body and html tags in the case of a regular view page
    if (!Model.IsPartialView && !Model.IsLayoutPageSelected)
    {
    @:</body>
    @:</html>
        }
    }
    @functions
    {
        // Do we need to use this in conjunction with the PrimaryKey check?
        bool IsPropertyGuid(IPropertyMetadata property)
        {
            return string.Equals("System.Guid", property.TypeName, StringComparison.OrdinalIgnoreCase);
        }

        string GetValueExpression(IPropertyMetadata property)
        {
            return property.PropertyName;
        }

        bool IsNumericType(IPropertyMetadata property)
        {
            var types = new[] {
                "System.Byte",
                "System.SByte",
                "System.UInt16",
                "System.UInt32",
                "System.UInt64",
                "System.Int16",
                "System.Int32",
                "System.Int64",
                "System.Decimal",
                "System.Double",
                "System.Single"
            };

            return types.Any(property.TypeName.Contains);
        }
    }
