@using System.Security.Claims;
@using Microsoft.AspNetCore.Identity
@inject UserManager<ApplicationUser> userManager;
@model CompositionFormModel
@{
    ViewData["Title"] = $"Edit {Model.Title}";

    string? userId = User.FindFirstValue(ClaimTypes.NameIdentifier);

    ApplicationUser? user = await userManager.FindByIdAsync(userId);

    bool isAuthenticated = User?.Identity?.IsAuthenticated ?? false;
    bool isAuthor = User.FindFirstValue(ClaimTypes.NameIdentifier) == Model.AuthorId;
    bool isAdmin = await userManager.IsInRoleAsync(user, "Admin");
}

<h1 class="text-center">Edit</h1>
<div class="text-center">
    <h2>@Model.Title</h2>
</div>
<hr />
<div class="row">
    <div class="col-sm-12 offset-lg-2 col-lg-8 offset-xl-3 col-xl-6">
        <form method="post">
            <partial name="_CompositionFormPartial" model="@Model"></partial>
            <div class="form-group">
                <input type="hidden" asp-for="Id" class="form-control" />
            </div>
            <div class="text-center">
                <input class="btn btn-primary mt-3" type="submit" value="Edit" />
            </div>
        </form>

        @if (isAuthenticated && isAuthor || isAdmin)
        {
            <hr />
            <h2 class="text-danger text-center" style="margin-top: 2rem">Delete your work</h2>
            <form onsubmit="return confirmAction();" method="post" asp-action="Delete" asp-route-compositionid="@Model.Id">
                <div class="text-center">
                    <input type="submit" value="Delete" class="btn btn-danger mt-3" />
                </div>
            </form>
        }
    </div>
</div>

@section Scripts{
    <partial name="_ValidationScriptsPartial" />

    <script>
        function confirmAction() {
            var confirmed = window.confirm("Are you sure you want to delete your work? Once deleted your work can only be recovered within a 30 day time period!");
            return confirmed;
        }
    </script>

    <script>
        var simplemde = new SimpleMDE({
            element: document.getElementById("content-textarea"),
            status: ["autosave", "lines", "words"]
        });
    </script>
}

