@model List<Aberwyn.Controllers.AdminUserViewModel>

<h2>Användarhantering</h2>
<h3>Skapa ny roll</h3>
<form method="post" asp-action="CreateRole">
    <div class="form-group">
        <input type="text" name="roleName" class="form-control" placeholder="Namn på ny roll" required />
    </div>
    <button type="submit" class="btn btn-success mt-2">Skapa roll</button>
</form>
<h3>Skapa ny användare</h3>
 @if (TempData["Message"] != null)
{
    <div class="alert alert-success">@TempData["Message"]</div>
}

<form method="post" asp-action="CreateUser">
    <div class="form-group">
        <label for="email">E-post:</label>
        <input type="email" name="email" class="form-control" required />
    </div>
    <div class="form-group">
        <label for="password">Lösenord:</label>
        <input type="password" name="password" class="form-control" required />
    </div>
    <button type="submit" class="btn btn-success mt-2">Skapa användare</button>
</form>

<hr />
<h4>Alla roller</h4>
<ul>
@foreach (var role in ViewBag.AllRoles as List<string>)
{
    <li>@role</li>
}
</ul>

<table class="table">
    <thead>
        <tr>
            <th>E-post</th>
            <th>Roller</th>
            <th>Lägg till roll</th>
        </tr>
    </thead>
    <tbody>
@foreach (var user in Model)
{
    <tr>
        <td>@user.Email</td>
        <td>
            @foreach (var role in user.Roles)
            {
                <span>@role</span>
                <form method="post" asp-action="RemoveFromRole" style="display:inline;">
                    <input type="hidden" name="userId" value="@user.UserId" />
                    <input type="hidden" name="role" value="@role" />
                    <button type="submit" class="btn btn-sm btn-danger">Ta bort</button>
                </form>
            }
        </td>
        <td>
            <form method="post" asp-action="AddToRole" class="form-inline">
                <input type="hidden" name="userId" value="@user.UserId" />
                <input type="text" name="role" class="form-control" placeholder="Rollnamn" />
                <button type="submit" class="btn btn-sm btn-primary">Lägg till</button>
            </form>
        </td>
    </tr>
}
    </tbody>
</table>

<h3>Testa Pushnotis</h3>
<form onsubmit="sendPush(event)">
    <div class="form-group">
        <label for="title">Titel:</label>
        <input type="text" id="title" name="title" class="form-control" required />
    </div>
    <div class="form-group">
        <label for="body">Meddelande:</label>
        <input type="text" id="body" name="body" class="form-control" required />
    </div>
    <button type="submit" class="btn btn-warning mt-2">Skicka testnotis</button>
</form>

<hr />
<h3>Importera måltider från produktion</h3>
<form method="post" asp-action="ImportMealsFromProd">
    <button type="submit" class="btn btn-danger">Importera alla måltider</button>
</form>
<form method="post" asp-action="ImportMenusFromProd">
    <button type="submit" class="btn btn-danger mt-2">Importera veckomenyer</button>
</form>


<script>
  async function sendPush(event) {
    event.preventDefault(); // 🚫 stoppa formuläret från att göra vanlig POST

    const title = document.getElementById("title").value;
    const body = document.getElementById("body").value;

    const response = await fetch("/api/push/notify-all", {
      method: "POST",
      headers: { "Content-Type": "application/json" },
      body: JSON.stringify({ title, body })
    });

    if (response.ok) {
      alert("✅ Pushnotis skickad!");
    } else {
      alert("❌ Något gick fel.");
    }
  }
</script>
