@using PantheonRA.BackOffice.Models
@model PantheonRA.BackOffice.Models.PostCreateViewModel
@{
    ViewBag.Title = string.IsNullOrEmpty(Model.Id) ? Loc.CreateNewPost : Loc.EditPost;
}

@using (Html.BeginForm(null, null, FormMethod.Post, new { id = "postForm" }))
{
    <div class="tabs-container">
        @Html.Partial("~/Views/Products/_CodeListTabNavigation.cshtml")
        <div class="tab-content">
            <div class="row buttons-tabbed">
                @Html.Partial("_FormButtons", new FormButtonsViewModel {
                    BackUrl = Url.Action("Index", "Post"),
                    SaveButtonGroup = new FormButtonGroup
                    {
                        Buttons = new List<FormButton>
                        {
                            new FormButton { Text = Loc.Save, Icon = "fa fa-save fa-lg", IsSubmit = true }
                        }
                    },
                    CreateButtonGroup = new FormButtonGroup
                    {
                        IsButtonName = true,
                        ButtonName = Loc.NewPostNumber,
                        Buttons = new List<FormButton>
                        {
                            new FormButton { Text = Loc.NewPostNumber, Icon = "fa fa-plus fa-lg", Url = @Url.Action("Create", "Products") }
                        }
                    }
                })

                <button id="invoiceDelete" type="button" onclick="removePost()" title="@Loc.Delete"
                        class="btn btn-danger btn-outline pull-right collapsable-form-button @(Model.IsEdit ? string.Empty : " hidden")">
                    <i class="far fa-trash-alt m-r-xs"></i>
                    <span class="visible-lg-inline visible-md-inline">@Loc.Delete</span>
                </button>
            </div>

            <div class="divided-content clearfix">
                <div class="divided-content-main divided-content-full">
                    <div class="form-horizontal form-md m-t-sm">
                        <div class="row">
                            <div class="col-xs-12 col-md-6 col-lg-4">
                                <div class="row">
                                    <div class="col-xs-12">
                                        <h3>@Loc.PostNumberBasicData</h3>
                                        <div class="form-group">
                                            @Html.LabelFor(model => model.Id, Loc.Code, new { @class = "col-md-3 control-label" })
                                            <div class="col-md-9">
                                                @Html.HiddenFor(model => model.IsEdit)
                                                @Html.TextBoxFor(model => model.Id,
                                                    HtmlAttributesHelper.CreateInputAttributes("form-control", "Id", Model.IsEdit ? true : false, Loc.Code, false, new { @autocomplete = "off" }))
                                                @Html.ValidationMessageFor(model => model.Id, "", new { @class = "text-danger" })
                                            </div>
                                        </div>
                                    </div>
                                </div>
                                <div class="row">
                                    <div class="col-xs-12">
                                        <div class="form-group">
                                            @Html.LabelFor(model => model.Name, Loc.Name, new { @class = "col-md-3 control-label" })
                                            <div class="col-md-9">
                                                @Html.TextBoxFor(model => model.Name, new { @id = "Name", @class = "form-control", @placeholder = Loc.Name, @autocomplete = "off" })
                                                @Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
}

<input type="hidden" id="RemovePostUrl" value="@Url.Action("Remove", "Post")" />
<input type="hidden" id="PostIndexUrl" value="@Url.Action("Index", "Post")" />
<input type="hidden" id="DeleteWasNotSuccessfulLoc" value="@Loc.PostAlreadyInUse" />
<input type="hidden" id="ConfirmDeleteLoc" value="@Loc.ConfirmDeleteMessage" />
<input type="hidden" id="GeneralErrorLoc" value="@Loc.GeneralError" />
<input type="hidden" id="ConfirmLoc" value="@Loc.Confirm" />
<input type="hidden" id="YesLoc" value="@Loc.Yes" />
<input type="hidden" id="NoLoc" value="@Loc.No" />

@section pageScripts{
    <script>
        $(document).ready(function () {
            $('#Id').focus();

            $('.input-validation-error').parents('.form-group').addClass('has-error');
            $('.field-validation-error').addClass('text-danger');
        });

        function removePost() {
            $().customModal({
                destroyOnClose: true,
                showCloseButton: true,
                type: "question",
                text: $("#ConfirmDeleteLoc").val(),
                buttons: [
                    {
                        icon: 'fa fa-times',
                        text: $("#NoLoc").val(),
                        closeOnAction: true,
                        type: 'ghost'
                    },
                    {
                        icon: 'fa fa-check',
                        text: $("#YesLoc").val(),
                        action: async function () {
                            var elementId = $("#Id").val();
                            const config = {
                                method: 'POST',
                                headers: {
                                    'Accept': 'application/json',
                                    'Content-Type': 'application/json'
                                },
                                body: JSON.stringify({ id: elementId })
                            };

                            const response = await fetch($("#RemovePostUrl").val(), config);
                            if (response.ok) {
                                const json = await response.json();
                                if (json.IsDeleted === true)
                                    window.location = $("#PostIndexUrl").val();
                                else {
                                    $().customModal({
                                        destroyOnClose: true,
                                        showCloseButton: true,
                                        type: "alert",
                                        text: $("#DeleteWasNotSuccessfulLoc").val(),
                                        buttons: [
                                            {
                                                icon: 'fa fa-check',
                                                text: $("#ConfirmLoc").val(),
                                                closeOnAction: true,
                                                type: 'primary'
                                            }
                                        ]
                                    });
                                }
                            }
                        },
                        closeOnAction: true,
                        type: 'primary'
                    }
                ]
            });
        }
    </script>
}
