@model <#= ViewDataTypeName #>
<#
    var selectedModelProperties = Options["SelectedModelProperties"];
#>
<script>
    function timeChanged(sender) {
        var date = sender.value;
        var hour = date.getHours();
        var min = date.getMinutes();
        var second = date.getSeconds();
        var inputDate = wijmo.Control.getControl("#inputDateFor" + sender.hostElement.id);
        inputDate.value.setHours(hour);
        inputDate.value.setMinutes(min);
        inputDate.value.setSeconds(second);
        inputDate.refresh();
    }
</script>

<form asp-action="<#if (isEdit){#>Edit<#}else{#>Create<#}#>">
    <div class="form-horizontal">
        <h4><#= ModelTypeName #></h4>
        <hr />
        <div asp-validation-summary="ValidationSummary.ModelOnly" class="text-danger"></div>
<#
var index = 0;
foreach (var prop in Options["Fields"])
{
    var propertyType = string.Empty;
    var propertyName = string.Empty;
    var headerName = string.Empty;
    var hasHeaderName = false;
    var needProcessDateTime = false;
    var control = string.Empty;
    var format = string.Empty;
    var controlFor = "for";
    foreach(var selectedProperty in selectedModelProperties)
    {
        if (selectedProperty["Name"].ToString() == prop["Binding"].ToString())
        {
            propertyType = selectedProperty["TypeName"].ToString();
            break;
        }
    }
    if (prop["Format"] != null)
    {
        format = prop["Format"].ToString();
    }
    hasHeaderName = prop["Header"] == null ? false : !string.IsNullOrEmpty(prop["Header"].ToString());
    headerName = hasHeaderName ? prop["Header"].ToString() : prop["Binding"].ToString();
    propertyName = prop["Binding"].ToString();
    propertyType = propertyType.Substring(propertyType.LastIndexOf('.') + 1);
    var required = false;
    var hasFormat = !string.IsNullOrEmpty(format);
    var formatAttr = "format";
    var dataMap = prop["DataMap"];
    var hasSetDataMap = dataMap["IsValid"];
    var modelExpression = string.Empty;
    index++;
    var isReadOnlyForEditView = isEdit && prop["IsReadOnly"];
    if(isReadOnlyForEditView)
    {
        hasSetDataMap = false;
        if (hasFormat && !string.IsNullOrEmpty(propertyType) && propertyType != "string" && propertyType != "bool" && propertyType != "bool?")
        {
            if (propertyType.Contains("?"))
            {
                modelExpression = "Model." + propertyName +".HasValue ? Model." + propertyName +".Value.ToString(\"" + format + "\") : \"\"";
            }
            else
            {
                modelExpression = "Model." + propertyName + ".ToString(\"" + format + "\")";
            }
#>
        @{var property<#= index #> = <#= modelExpression #>;}
<#
        }
        hasFormat = false;
    }
    else
    {
        if(hasSetDataMap)
        {
            control = "c1-combo-box";
            hasFormat = false;
        }
        else
        {
            switch (propertyType)
            {
                case "DateTime":
                case "DateTime?":
                    needProcessDateTime = !string.IsNullOrEmpty(format);
                    control = "c1-input-date";
                    required = true;
                    break;
                case "decimal":
                case "decimal?":
                case "double":
                case "double?":
                case "long":
                case "long?":
                case "float":
                case "float?":
                case "short":
                case "short?":
                case "int":
                case "int?":
                    control = "c1-input-number";
                    required = true;
                    break;
                case "bool":
                case "bool?":
                    control = "input";
                    controlFor = "asp-for";
                    hasFormat = false;
                    break;
                default:
                    control = "c1-input-mask";
                    formatAttr = "mask";
                    break;
            }
        }
    }
#>
        <div class="form-group">
<#
    if (hasHeaderName)
    {
#>
            <label class="control-label col-md-2"><#= headerName #></label>
<#
    }
    else
    {
#>
            <label asp-for="<#=propertyName #>" class="control-label col-md-2"></label>
<#
    }
#>
            <div class="col-md-10">
<#
    if (needProcessDateTime)
    {
        var containsDateFormat = false;
        var containsTimeFormat = false;
        var customFormat = false;
        switch (format)
        {
            case "f":
            case "F":
            case "g":
            case "G":
            case "s":
            case "o":
            case "O":
            case "u":
            case "U":
            case "r":
            case "R":
                containsDateFormat = true;
                containsTimeFormat = true;
                break;
            case "t":
            case "T":
                containsTimeFormat = true;
                break;
            case "d":
            case "D":
            case "m":
            case "M":
            case "y":
            case "Y":
                containsDateFormat = true;
                break;
            default:
                customFormat = true;
                break;
        }
        if (customFormat)
        {
            containsDateFormat = format.Contains('M') || format.Contains('d') || format.Contains('y');
            containsTimeFormat = format.Contains('H') || format.Contains('h') || format.Contains('m') || format.Contains('s');
        }
        if (containsDateFormat && containsTimeFormat)
        {
#>
                <c1-input-date for="<#= propertyName #>" id="inputDateFor<#= propertyName #>"<#if (required){#> required="false"<#}#><#if (hasFormat){#> <#= formatAttr #>="<#= format #>"<#}#>></c1-input-date>
                <c1-input-time<#if (isEdit){#> value="@Model.<#= propertyName #>"<#}#> id="<#= propertyName #>" value-changed="timeChanged"></c1-input-time>
<#
        }
        else if (containsTimeFormat)
        {
#>
                <c1-input-time for="<#= propertyName #>" step="30"<#if (required){#> required="false"<#}#><#if (hasFormat){#> <#= formatAttr #>="<#= format #>"<#}#>></c1-input-time>
<#
        }
        else
        {
#>
                <<#= control #> <#= controlFor #>="<#= propertyName #>"<#if (required){#> required="false"<#}#><#if (hasFormat){#> <#= formatAttr #>="<#= format #>"<#}#>></<#= control #>>
<#
        }
    }
    else
    {
        if(isReadOnlyForEditView)
        {
            if (string.IsNullOrEmpty(modelExpression))
            {
#>
                @Model.<#= propertyName #>
<#
            }
            else
            {
#>
                @property<#= index #>
<#
            }
#>
                <input type="hidden" asp-for="<#= propertyName #>" />
<#
        }
        else
        {
            var attrSelectValuePath = "";
            if (hasSetDataMap)
            {
                if(!string.IsNullOrEmpty(dataMap["SelectedValuePath"]))
                {
                    attrSelectValuePath = attrSelectValuePath + " selected-value-path=\"" + dataMap["SelectedValuePath"] + "\"";
                }
                if(!string.IsNullOrEmpty(dataMap["DisplayMemberPath"]))
                {
                    attrSelectValuePath = attrSelectValuePath + " display-member-path=\"" + dataMap["DisplayMemberPath"] + "\"";
                }
            }
#>
                <<#= control #> <#= controlFor #>="<#= propertyName #>"<#if (required){#> required="false"<#}#><#if (hasFormat){#> <#= formatAttr #>="<#= format #>"<#}#><#= attrSelectValuePath #>>
<#
            if (hasSetDataMap)
            {
                var attrSourceCollection = "";
                if(!dataMap["UseRemoteBinding"])
                {
                    attrSourceCollection = " source-collection=\"@ViewBag." + dataMap["ViewBagName"] + "\"";
                }
                else
                {
                    attrSourceCollection = " read-action-url=\"@Url.Action(\"" + dataMap["ReadActionUrl"]  + "\")" + "\"";
                } 
#>
                    <c1-items-source<#= attrSourceCollection #>></c1-items-source>
<#
            }
#>
                </<#= control #>>
                <span asp-validation-for="<#= propertyName #>" class="text-danger"></span>
<#
        }
    }
#>
            </div>
        </div>
<#
}
#>
        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="<#if (isEdit){#>Save<#}else{#>Create<#}#>" class="btn btn-default" />
            </div>
        </div>
    </div>
</form>
<div>
    <a asp-action="Index">Back to List</a>
</div>