<!--
Copyright 2020 Google LLC

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<html lang="en">
<head>
    <title>Tabs VS Spaces</title>
    <link rel="stylesheet"
          href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/css/materialize.min.css">
    <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/js/materialize.min.js" integrity="sha256-53f7070cc4c81c278c72f7a106fd71434e766cf49b26d6ee8b0e1003d7132b3d"></script>
</head>
<body>
<nav class="red lighten-1">
    <div class="nav-wrapper">
        <a href="#" class="brand-logo center">Tabs VS Spaces</a>
    </div>
</nav>
<div class="section">
    <div class="center">
        <h4>
            <%= if @tab_count == @space_count
                  "TABS and SPACES are evenly matched!"
                elsif @tab_count > @space_count
                  "TABS are winning by #{@tab_count - @space_count} vote#{@tab_count - @space_count > 1 ? "s" : ""}!"
                else
                  "SPACES are winning by #{@space_count - @tab_count} vote#{@space_count - @tab_count > 1 ? "s" : ""}!"
                end
            %>
        </h4>
    </div>
    <div class="row center">
        <div class="col s6 m5 offset-m1">
            <div class="card-panel <%= @tab_count > @space_count ? "green lighten-3" : "" %>">
                <i class="material-icons large">keyboard_tab</i>
                <h3><%= @tab_count %> votes</h3>
                <button id="voteTabs" class="btn green">Vote for TABS</button>
            </div>
        </div>
        <div class="col s6 m5">
            <div class="card-panel <%= @tab_count < @space_count ? "blue lighten-3" : "" %>">
                <i class="material-icons large">space_bar</i>
                <h3><%= @space_count %> votes</h3>
                <button id="voteSpaces" class="btn blue">Vote for SPACES</button>
            </div>
        </div>
    </div>
    <h4 class="header center">Recent Votes</h4>
    <ul class="container collection center">
      <% @recent_votes.each do |vote| %>
      <%= render partial: "vote", locals: { vote: vote } %>
      <% end %>
    </ul>
</div>
<script>
    function vote(candidate) {
        var xhr = new XMLHttpRequest();
        xhr.onreadystatechange = function () {
            if (this.readyState == 4) {
                if (!window.alert(this.responseText)) {
                    window.location.reload();
                }
            }
        };
        xhr.open("POST", "/", true);
        xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        xhr.send("candidate=" + candidate);
    }

    document.getElementById("voteTabs").addEventListener("click", function () {
        vote("TABS");
    });
    document.getElementById("voteSpaces").addEventListener("click", function () {
        vote("SPACES");
    });
</script>
</body>
</html>
