@using PantheonRA.BackOffice.Helpers;
@model PantheonRA.Contracts.DataContracts.DocumentCreateData
@{
    var isPreviewOnly = ViewBag.IsPreviewOnly != null ? (bool)ViewBag.IsPreviewOnly : false;
}

<div class="form-horizontal form-md">
    @Html.HiddenFor(model => model.Id)
    @Html.HiddenFor(model => model.AcKey)
    @Html.HiddenFor(model => model.PreviewImage)
    @Html.HiddenFor(model => model.IsWorkflowRun)
    @Html.HiddenFor(model => model.IsTaskCompleted)

    @if (!Model.IsWorkflowRun)
    {
        <div class="form-group">
            @Html.LabelFor(model => model.Date, Loc.DocumentDate, new { @class = "col-lg-3 control-label" })
            <div class="col-lg-9">
                @Html.TextBoxFor(model => model.Date, Model.Date.ToString("dd.MM.yyyy"),
                    HtmlAttributesHelper.CreateInputAttributes("form-control datepicker", "Date", isPreviewOnly || Model.IsTaskCompleted, Loc.DocumentDate))
                @Html.ValidationMessageFor(model => model.Date, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Date, Loc.DocumentDueDate, new { @class = "col-lg-3 control-label" })
            <div class="col-lg-9">
                @Html.TextBoxFor(model => model.DateDue, Model.Date.ToString("dd.MM.yyyy"),
                    HtmlAttributesHelper.CreateInputAttributes("form-control datepicker", "DateDue", isPreviewOnly || Model.IsTaskCompleted, Loc.DocumentDueDate))
                @Html.ValidationMessageFor(model => model.DateDue, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Note, Loc.Note, new { @class = "col-lg-3 control-label" })
            <div class="col-lg-9">
                @Html.TextAreaFor(model => model.Note,
                    HtmlAttributesHelper.CreateInputAttributes("form-control", "Note", isPreviewOnly || Model.IsTaskCompleted, Loc.Note, true, new { rows = 4}))
                @Html.ValidationMessageFor(model => model.Note, "", new { @class = "text-danger" })
            </div>
        </div>
    }

    @if (Model.Metadata != null && Model.Metadata.Any())
    {
        @Html.HiddenFor(model => model.Date)

        for (var i = 0; i < Model.Metadata.Count; i++)
        {
            @MetadataInput(Model.Metadata[i], Html.NameFor(model => Model.Metadata[i]),
                Html.ValidationMessageFor(model => Model.Metadata[i], null, new { @class="text-danger"}),
                @ViewData.ModelState.IsValidField("Document.Metadata[" + i + "]"))
        }
        <div class="form-group has-error">
            <div class="col-lg-offset-3 col-lg-9">
                <span class="field-validation-valid text-danger" data-valmsg-for="Document.Metadata[2].Value" data-valmsg-replace="true">
                    @Html.ValidationMessageFor(x => x.Metadata)
                </span>
            </div>
        </div>
    }

    <div class="form-group">
        @Html.Label("DocConnections", Loc.ConnectDocument, new { @class = "col-lg-3 control-label" })
        <div class="col-lg-9">
            <input class="form-control typeahead_3 typeahead_wide obligatory-input addable-input-btn-connection" placeholder="@Loc.ConnectDocument" id="addDocConnection" dataUrl="@Url.Action("GetAvailableDocumentLinks", "Documents")" autocomplete="off" />
            <div class="display-inline-block">
                <button type="button" class="btn btn-success createNewButton" id="add-connection-btn" onclick="addConnectionToDocument()" title="@Loc.AddConnection">
                    <i id="add-position-icon reverse-color" class="fa fa-plus"></i>
                </button>
            </div>
        </div>
    </div>

    <div class="form-group">
        <div class="col-xs-12">
            @Html.ValidationSummary(true)
        </div>
    </div>

    <input type="hidden" value="false" name="Redirect" id="Redirect" />
    @if (Model.IsTaskCompleted)
    {
        <button type="button" onclick="(function () { $('#Redirect').val('true'); $('#documentForm').submit(); })();" class="btn btn-outline btn-default btn-full">
            <i class="fas fa-external-link m-r-xs"></i> @Loc.OtherDocumentData
        </button>
    }
</div>

<input type="hidden" value="@Loc.Address" id="LocAddress" />
<input type="hidden" value="@Loc.ZipCode" id="LocZipCode" />
<input type="hidden" value="@Loc.VAT" id="LocVAT" />
<input type="hidden" value="@Loc.Company" id="LocCompany" />
<input type="hidden" value="@Loc.Name" id="LocName" />
<input type="hidden" value="@Loc.Post" id="LocPost" />
<input type="hidden" value="@Loc.RegisterVatNumber" id="LocRegisterVatNumber" />
<input type="hidden" value="@Loc.Department" id="LocDepartment" />
<input type="hidden" value="@Loc.DateFormat" id="LocDateFormat" />

@helper MetadataInput(PantheonRA.Contracts.DataContracts.TaskMetadata metadata, MvcHtmlString componentName, MvcHtmlString validationError, bool isValid)
{
    var inputClass = "";
    if (metadata.Obligatory && !Model.IsTaskCompleted)
    {
        inputClass += "obligatory-input ";
    }
    if (metadata.Type == PantheonRA.BackOffice.Enums.MetadataType.Date)
    {
        inputClass += "datepicker ";
    }
    if (metadata.Type == PantheonRA.BackOffice.Enums.MetadataType.Decimal)
    {
        inputClass += "numberInput ";
    }

    <div class="form-group @(!isValid ? "has-error" : string.Empty)">
        <label class="col-lg-3 control-label" for="@Html.NameFor(model => metadata.Value)" title="@metadata.OutputName">@metadata.OutputName</label>
        <div class="col-lg-9">
            @if (string.IsNullOrEmpty(metadata.Setter))
            {
                <input autocomplete="off" class="form-control @inputClass" name="@(componentName + "." + Html.DisplayNameFor(model => metadata.Value))"
                       placeholder="@metadata.OutputName" type="text" value="@metadata.Value" @(Model.IsTaskCompleted ? "readonly" : string.Empty)>
            }
            else
            {
                <input autocomplete="off" class="form-control @inputClass typeahead_3 typeahead_wide @metadata.Setter"
                       value="@(string.IsNullOrEmpty(metadata.Display) ? metadata.Value : metadata.Display)"
                       placeholder="@metadata.OutputName" type="text" @(Model.IsTaskCompleted ? "readonly" : string.Empty)>
                <input value="@metadata.Value" name="@(componentName + "." + Html.DisplayNameFor(model => metadata.Value))" hidden />
            }

            @validationError
        </div>
        <input type="hidden" value="@metadata.MetadataId" name="@(componentName + "." + Html.DisplayNameFor(model => metadata.MetadataId))" />
        <input type="hidden" value="@metadata.Setter" name="@(componentName + "." + Html.DisplayNameFor(model => metadata.Setter))" />
        <input type="hidden" value="@metadata.Type" name="@(componentName + "." + Html.DisplayNameFor(model => metadata.Type))" />
        <input type="hidden" value="@metadata.OutputName" name="@(componentName + "." + Html.DisplayNameFor(model => metadata.OutputName))" />
        <input type="hidden" value="@metadata.Message" name="@(componentName + "." + Html.DisplayNameFor(model => metadata.Message))" />
        <input type="hidden" value="@metadata.Obligatory.ToString()" name="@(componentName + "." + Html.DisplayNameFor(model => metadata.Obligatory))" />
    </div>
}