@model List<MileageTraker.Web.Models.User>

@{
    ViewBag.Title = "Invite Uninitialized Users";
}

@{ Html.RenderPartial("BackToUsers"); }

@Html.Partial("_StatusMessage")

<h2 class="center-content">@ViewBag.Title</h2>

<div class="center-content well">
    <a href="#" id="invite-uninitialized-users" class="btn">Send Invitations</a>
    <h4>Inviting...</h4>
    <ul id="uninitialized-users">
        @foreach (var user in Model)
        {
            <li userId="@Html.Encode(user.UserId)">
                @Html.Encode(user.Username), @Html.Encode(user.Email)
            </li>
        }
    </ul>
</div>

@section Scripts
{
<script type="text/javascript">
    // send invites
    $(function () {
        $("#invite-uninitialized-users").click(function () {
            sendInvites();
            $(this).addClass("disabled").off("click");
            return false;
        });

        function sendInvites() {
            var $users = $("#uninitialized-users > li:not(.sent)");
            if ($users.length > 0) {
                var $li = $($users[0]);
                $.ajax({
                    url: "/User/SendInvite",
                    data: { userId: $li.attr("userId") },
                    success: function () {
                        $li.addClass("sent");
                        $li.append('<span class="badge badge-success">Sent</span>');
                        sendInvites();
                    }
                });
            }
        }
    });
</script>
}
