@model PollModel
<div class="poll" id="poll-block-@(Model.Id)">
    <strong class="poll-display-text">@Model.Name</strong>
    @if (Model.AlreadyVoted)
    {
        <ul class="poll-results">
            @foreach (var item in Model.Answers)
            {
                <li class="answer">
                    @string.Format(T("Polls.VotesResultLine").Text, item.Name, item.NumberOfVotes, item.PercentOfTotalVotes.ToString("0.0"))
                </li>
            }
        </ul>
        <span class="poll-total-votes">@string.Format(T("Polls.TotalVotes").Text, Model.TotalVotes)</span>
    }
    else
    {
        <ul class="poll-options">
            @foreach (var item in Model.Answers)
            {
                <li class="answer">
                    <input id="pollanswers-@(item.Id)" type="radio" name="pollanswers-@(Model.Id)" value="@item.Id" />
                    <label for="pollanswers-@(item.Id)">@item.Name</label>
                </li>
            }
        </ul>
        <div class="buttons">
            <button type="button" id="vote-poll-@(Model.Id)" class="button-2 vote-poll-button">@T("Polls.Vote")</button>
            <span id="poll-voting-progress-@(Model.Id)" style="display: none;" class="please-wait">@T("Common.Wait")</span>
        </div>
        <div class="poll-vote-error" id="block-poll-vote-error-@(Model.Id)" style="display:none">
        </div>
        <nop-antiforgery-token />
        <script asp-location="Footer">
            $(document).ready(function () {
                $('#vote-poll-@(Model.Id)').on('click', function () {
                    var pollAnswerId = $("input:radio[name=pollanswers-@(Model.Id)]:checked").val();
                    if (typeof (pollAnswerId) == 'undefined') {
                        alert('@Html.Raw(JavaScriptEncoder.Default.Encode(T("Polls.SelectAnswer").Text))');
                    }
                    else {
                        var voteProgress = $("#poll-voting-progress-@(Model.Id)");
                        voteProgress.show();

                        var postData = {
                          "pollAnswerId": pollAnswerId
                        };
                        addAntiForgeryToken(postData);

                        $.ajax({
                            cache: false,
                            type: "POST",
                            url: "@(Url.RouteUrl("PollVote"))",
                            data: postData,
                            success: function (data, textStatus, jqXHR) {
                                if (data.error) {
                                    $("#block-poll-vote-error-@(Model.Id)").html(data.error);
                                    $('#block-poll-vote-error-@(Model.Id)').fadeIn("slow").delay(2000).fadeOut("slow");
                                }
                                if (data.html) {
                                    $("#poll-block-@(Model.Id)").replaceWith(data.html);
                                }
                            },
                            error: function (jqXHR, textStatus, errorThrown) {
                                alert('Failed to vote.');
                            },
                            complete: function (jqXHR, textStatus) {
                                voteProgress.hide();
                            }
                        });
                    }
                    return false;
                });
            });
        </script>
    }
</div>
