//extern ESP8266WebServer httpServer;

static const char httpRoot[] PROGMEM =
  R"(<html><body>
  <h1>WiFi Thermostat</h1>
  
  <form id='minusBtnFrm'>
  <input type='hidden' name='minusBtn' value='1'>
  </form>
  <form id='plusBtnFrm'>
  <input type='hidden' name='plusBtn' value='1'>
  </form>
  <form id='modeBtnFrm'>
  <input type='hidden' name='modeBtn' value='1'>
  </form>
  <form id='onoffBtnFrm'>
  <input type='hidden' name='onoffBtn' value='1'>
  </form>  
  <span style="font-size:xx-large" id='setTemp'></span> &#8451;<br>
  <span id='mode'></span><br>
 <input type='button' onclick='return sendMinusBtn()' value='-'/>
  <input type='button' onclick='return sendPlusBtn()' value='+'/>
  <input type='button' onclick='return sendModeBtn()' value='MODE'/>
  <br>  
  <br>  
  <input type='button' onclick='return sendOnOffBtn()' value='EIN/AUS'/><br>
  <span id='onoff'></span><br>
  <br>
  <br>
  
  Aktuell: <span id='temp'></span> &#8451;&nbsp;&nbsp;&nbsp;<span id='hum'></span> %<br>
  Heizung <span id='heating'></span><br>
  <br>
  WiFi verbunden mit <i><span id='ssid'></span></i>.<br>
  <h6>Letztes Update vor
  <span id='ut'></span> Sekunden.
  <span id='status'></span>
  </h6>
  <br>
  <a href='/wifi.htm'>WiFi-Einstellungen</a><br>
  <a href='/config'>Einstellungen</a><br>
  <a href='/update'>Firmware Update</a>
  <script>
  function g(i) { return document.getElementById(i) };
  var xhttp, updateTime;
  
  function sendMinusBtn() {
  var form = document.getElementById('minusBtnFrm');
  return transmit(form);
  }
  function sendPlusBtn() {
  var form = document.getElementById('plusBtnFrm');
  return transmit(form);
  }
  function sendModeBtn() {
  var form = document.getElementById('modeBtnFrm');
  return transmit(form);
  }
  function sendOnOffBtn() {
  var form = document.getElementById('onoffBtnFrm');
  return transmit(form);
  }
  
  function transmit(f) {
    if (!xhttp) { 
      g('status').innerHTML = 'lädt...';
      xhttp = new XMLHttpRequest();
      xhttp.open('POST', 'api');
      xhttp.send(f ? (new FormData(f)) : '');
      xhttp.onreadystatechange = function () {
        if (xhttp.readyState === XMLHttpRequest.DONE && xhttp.status === 200) {
          var data = JSON.parse(xhttp.responseText);
          g('temp').innerHTML = data.temp.toFixed(1);
      g('hum').innerHTML = data.hum;
          g('setTemp').innerHTML = data.setTemp.toFixed(1);
          g('ssid').innerHTML = data.ssid;
      
      if(!data.heatingEnabled) {
      g('mode').innerHTML = '-';
      g('onoff').innerHTML = 'AUS';
      }
      else {
      g('onoff').innerHTML = 'EIN';
      if(data.mode == '1') {
        g('mode').innerHTML = 'Normalbetrieb';
      }
      else if(data.mode == '2') {
        g('mode').innerHTML = 'Nachtabsenkung';
      }
      }
      
      if(data.heating == '1') g('heating').innerHTML = 'aktiv';
      else if(data.heating == '0') g('heating').innerHTML = 'nicht aktiv';

          xhttp = null;
          g('status').innerHTML = '';
          updateTime = 0;
        }
      }
    }
    return false;
  }
  transmit();
  setInterval(function () { g('ut').innerHTML = ++updateTime; }, 1000);
  setInterval(transmit, 5000);
</script>
  </body></html>)";


static const char httpConfPage[] PROGMEM = 
R"(<html><body>
  <h1>WiFi Thermostat</h1>
  <h2>Configuration</h2>
  <a href='/'>Home</a>
  </body></html>)";

void httpServerHandleRoot() {
  httpServer.send_P(200, "text/html", httpRoot);
}

void httpServerHandleConfPage() {
  httpServer.send_P(200, "text/html", httpConfPage);
}

void httpServerHandleNotFound() {
  String message = "File Not Found\n\n";
  message += "URI: ";
  message += httpServer.uri();
  message += "\nMethod: ";
  message += (httpServer.method() == HTTP_GET) ? "GET" : "POST";
  message += "\nArguments: ";
  message += httpServer.args();
  message += "\n";
  for (uint8_t i = 0; i < httpServer.args(); i++) {
    message += " " + httpServer.argName(i) + ": " + httpServer.arg(i) + "\n";
  }
  httpServer.send(404, "text/plain", message);
}



