/* clang-format off */

//static const char html_confDevWiFi_script[] PROGMEM = R"=====(
//<script>
//  function g(i) { return document.getElementById(i) };
//  function sp(i){g(i).type=(g(i).type==='text'?'password':'text');}
//  var xhttp, reqTime, reqFin;
//  function setCbx(el, da) {
//    if(da == '1') {
//      el.checked = true;
//      el.style.visibility = 'visible';
//    }
//    else {
//      el.checked = false;
//      el.style.visibility = 'visible';
//    }
//  }
//  function updCbxVal(el) {
//    if (el.checked) el.value = '1';
//    else {
//      el.checked = true;
//    el.value = '0';
//      el.style.visibility = 'hidden';
//    }
//  }

static const char html_confDevWiFi_script[] PROGMEM = R"=====(
  function transmit(f) {
    if (!xhttp) {   
    reqTime = 0;
    reqFin = false;
    xhttp = new XMLHttpRequest();
    xhttp.timeout = 1000;
    xhttp.overrideMimeType('application/json');
    xhttp.open('POST', 'confDataDevWiFi');
    xhttp.send(f ? (new FormData(f)) : '');
    xhttp.onreadystatechange = function () {
      if (xhttp.readyState === XMLHttpRequest.DONE && xhttp.status === 200) {
        var data = JSON.parse(xhttp.responseText);
        g('devName').value = data.devName;
        g('hostName').value = data.hostName;
		    g('SSID1').value = data.SSID1;
        g('WPW1').value = data.WPW1;
        g('SSID2').value = data.SSID2;
        g('WPW2').value = data.WPW2;
        g('SSIDAP').innerHTML = data.SSIDAP;
		    g('WPWAP').value = data.WPWAP;
		    g('WAPtout').value = data.WAPtout;
        g('WConnCheck').value = data.WConnCheck;
		    g('Wretry').value = data.Wretry;
		    g('Wreboot').value = data.Wreboot;
        setCbx(g('ArdOTA'), data.ArdOTA);
        xhttp = null;
        reqFin = true;
       }
       else {
         if(!reqFin && reqTime > 10) {
           xhttp = null;
           reqFin = true;
         }
       }
     }
   }
    return false;
  }
  //transmit();
  function saveConf() {
    updCbxVal(g('ArdOTA'));
    g('frmConf').submit();
  }
  function init() {
    transmit();
    setCbx(g('WPW1Set'), 0);
    setCbx(g('WPW2Set'), 0);
    setCbx(g('WPWAPSet'), 0);
  }
)====="; // html_confDevWiFi_script
//  setInterval(function () { ++reqTime; }, 1000);
//</script>
//)====="; // html_confDevWiFi_script

static const char html_confDevWiFi_body[] PROGMEM = R"=====(
<b>Configuration - Device & WiFi</b>
<div class='config'><br>
<form id='frmConf' action='setConfDevWiFi' method='POST'>
<fieldset>
<legend>Device</legend>
<p><b>Device Name *</b><br><input type='text' name='devName' id='devName'></p>
<p><b>Hostname</b><br><input type='text' name='hostName' id='hostName'></p>
<p class='n'>if blank, random hostname will be generated</p>
</fieldset>
<br>
<p class='n'>After boot, firmware will first try to connect to Main Wifi-AP, <br>
then if unsuccessful Fallback-AP. <br>
If both fails it switches to Configuration AP-Mode. <br>
AP-Mode is disabled again after configured timeout and a reconnect to the set <br>
APs is tried every some minutes as configured below. <br>
If no connection can be established the module will reboot after another <br>
timeout if configured. <br>
Reconnect always tries Main-AP first, then Fallback-AP.</p>
<div></div>
<fieldset>
<legend>WiFi - Default-AP</legend>
<p><b>Set</b>&nbsp;<input type='checkbox' id='WPW1Set' name='WPW1Set'></p>
<p><b>SSID</b><br><input type='text' length=32 name='SSID1' id='SSID1'></p>
<p><b>Password</b>&nbsp;<input type='checkbox' onclick='sp("WPW1")'>&nbsp;show<br><input type='password' length=64 name='WPW1' id='WPW1'></p>
</fieldset>
<br>
<fieldset>
<legend>WiFi - Fallback-AP</legend>
<p><b>Set</b>&nbsp;<input type='checkbox' id='WPW2Set' name='WPW2Set'></p>
<p><b>SSID</b><br><input type='text' length=32 name='SSID2' id='SSID2'></p>
<p><b>Password</b>&nbsp;&nbsp;<input type='checkbox' onclick='sp("WPW2")'>&nbsp;show<br><input type='password' length=64 name='WPW2' id='WPW2'></p>
</fieldset>
<div></div><br>
<fieldset>
<legend>Configuration AP-Mode</legend>
<p><b>SSID</b>: <i><span id='SSIDAP'></span></i>
<p><b>Password *</b><input type='checkbox' id='WPWAPSet' name='WPWAPSet' onclick='sp("WPWAP")'><br><input type='password' name='WPWAP' id='WPWAP'></p>
<p class='n'>* min. 8 chars, empty password for open WiFi</p>
</fieldset>
<div></div><br>
<fieldset>
<legend>Connection Handling</legend>
<p><b>Switch off AP-Mode after [min] *</b><br><input type='number' name='WAPtout' id='WAPtout'></p>
<p><b>WiFi connection check interval [s]</b><input type='number' name='WConnCheck' id='WConnCheck'></p>
<p class='n'>min. 10s, max. 3600s</p><br>
<p><b>Retry connecting WiFi-1 every [min] *</b><input type='number' name='Wretry' id='Wretry'></p>
<p class='n'>Try WiFi-1 again when WiFi-2 is connected. <br>
(prefer WiFi-1 over WiFi-2)</p><br>
<p><b>Reboot if can´t connect for [min] *</b><input type='number' name='Wreboot' id='Wreboot'></p>
<p class='n'>* set to 0 to disable</p>
</fieldset>
<div></div><br>
<fieldset>
<legend>Additional Network Services</legend>
<p><b>ArduinoOTA</b>&nbsp;<input type='checkbox' id='ArdOTA' name='ArdOTA'></p>
<p class='n'>Enable ArduinoOTA service on port 8266.</p>
</fieldset>
<div></div><br>
</form>
<div></div>
<table style='width:100%'>
  <td style='width:50%'><button onclick='location="conf";' class='bgrey'>Cancel</button></td>
  <td style='width:50%'><button onclick='return saveConf()' class='bred'>Save</button></td>
</tr></table>
</div>
)====="; // html_confDevWiFi_body
