<!doctype html>
<html>
  <head>
    <title><%= "CC Points" + (locals.title ? ": " + title : "") %></title>
    <script>
      function load() {
        function redgreen(t) {
          var value;
          if (t.tagName === "INPUT") value = +t.value;
          else value = +t.innerText;
          if (!isFinite(value) || value === 0) {
            t.classList.remove("bg-red");
            t.classList.remove("bg-green");
          } else if (value > 0) {
            t.classList.remove("bg-red");
            t.classList.add("bg-green");
          } else if (value < 0) {
            t.classList.add("bg-red");
            t.classList.remove("bg-green");
          }
        }
        Array.from(document.querySelectorAll(".redgreen")).forEach(function (t) {
          redgreen(t);
          t.addEventListener('change', function () { redgreen(t); });
        });
      }
      function addSearch(key, value, replace) {
        // unfortunately caniuse URLSearchParams is only 95% at this moment
        // (plus it makes value-less entries ugly with a trailing =)
        // so let's do this manually
        key = encodeURIComponent(key);
        value = value === undefined ? null : encodeURIComponent(value);

        var entries = location.search.substr(1);
        if (entries.length === 0) entries = [];
        else entries = entries.split("&");
        entries = entries.filter(function (entry) {
          return !(entry === key || entry.startsWith(key + "="));
        });
        if (value === null) entries.push(key);
        else entries.push(key + "=" + value);

        window.location[!!replace ? "replace" : "assign"](window.location.pathname + "?" + entries.join("&"));
      }
    </script>
    <meta name="viewport" content="width=device-width, initial-scale=0.5">
    <link rel="stylesheet" href="styles.css">
  </head>
  <body onload="load();">
    <h1>CC Points Database [TEST, NOT OFFICIAL]</h1>
    <p>
      <a href="/">Home Page</a> |
      <% if (req.session.user) { %>
        <a href="/admin">Admin Menu</a> |
        Logged in as: <%= req.session.user.name %> (<a href="/logout" onclick="location='/logout?to=' + encodeURIComponent(location.pathname + location.search);return false;">Logout</a>)
      <% } else { %>
        <a href="/login" onclick="location='/login?to=' + encodeURIComponent(location.pathname + location.search);return false;">Admin Login</a>
      <% } %>
    </p>