<?php
include './assets/functions.php';
include './settings/settings_redirector.php';
include './settings/config_redirector.php';

//GET BROWSER LANGUAGE
$lang = strtoupper(substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2));
$lang = in_array($lang, $supportedLanguages) ? $lang : 'US';

//INCLUDE THE TRANSLATION
include_once './settings/translations/translations_'.$lang.'.php';

// Default input values
$display ='';
$register = [
    'sn' => '',
    'organization' => '',
    'email' => '',
    'phone' => '',
    'city' => '',
    'country' => ''
];

if (isset($_POST['register'])){
  //Update register ARRAY
  $register = $_POST;
  //GET USERKEY
  $data = json_encode(array("username" => interface_user, "password" => interface_pw), JSON_UNESCAPED_UNICODE);
  //Secure data
  $payload = generate_payload($data);
  //API call
  $responses = ioServer('/v1/authorization', $payload);
  //Decode Payload
  if (!empty($responses)){$responses = decode_payload($responses);}else{$responses = '400';}
  
  if ($responses === 'NOK' || $responses === '400'){
    //Not allowed
    exit;
  } 
  else {
    // Start a new session
    session_start();    
    // Store data in session
    $_SESSION['userkey'] = $responses->userkey;
    // GET ALL POST DATA
    $data = json_encode($register, JSON_UNESCAPED_UNICODE);
    // Secure data
    $payload = generate_payload($data);
    // API call
    $message_return = ioServer('/v1/application/register', $payload);
    if (!empty($message_return)){
        $message_return = decode_payload($message_return);
        $display = 'display:none;';
      }
        else{
          $message_return = '400';
        }
  }
}
echo '
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,minimum-scale=1">
    <title>'.$register_title.'</title>
    <link rel="icon" type="image/png" href="'.icon_image.'">
    <style>
    * {
        box-sizing: border-box;
        font-family: -apple-system, BlinkMacSystemFont, "segoe ui", roboto, oxygen, ubuntu, cantarell, "fira sans", "droid sans", "helvetica neue", Arial, sans-serif;
        font-size: 16px;
      }
      
      body {
        position: relative;
        min-height: 100%;
        color: #555555;
        background-color: #FFFFFF;
        margin: 0;
        padding-bottom: 100px;
        /* Same height as footer */
      }
      
      h1, h2, h3, h4, h5 {
        color: #fff;
      }
      
      .content-wrapper {
        width: 1050px;
        margin: 0 auto;
      }
      
      header {
        position: relative;
        border-bottom: 1px solid #EEEEEE;
      }
      
      header .content-wrapper {
        display: flex;
      }
      
      
      main .myaccount h1 {
        display: block;
        font-weight: normal;
        color: #fff;
        margin: 0;
        padding: 40px 0;
        font-size: 50px;
        text-align: center;
        width: 100%;
      }
      
      main .myaccount {
        display: flex;
        flex-flow: wrap;
      }
      
      main .myaccount form {
        width: 100%;
      }
      
      main .myaccount h2 {
        width: 100%;
        font-weight: normal;
        font-size: 20px;
        padding: 30px 0 20px 0;
        margin: 0 0 10px 0;
        border-bottom: 1px solid #EEEEEE;
      }
      
      main .myaccount .login-register {
        display: flex;
        width: 100%;
        justify-content: space-between;
        margin-top: 40px;
      }
      
      main .myaccount .login-register h1 {
        text-align: left;
        padding-top: 15px;
      }
      
      main .myaccount .login-register .login {
        width: 100%;
        border-right: 1px solid #f6f6f6;
        padding-right: 45px;
      }
      
      main .myaccount .login-register .register {
        width: 100%;
        padding-left: 45px;
      }
      
      main .myaccount .login-register .btn {
        margin-top: 25px;
      }
      
      .btn {
        text-decoration: none;
        background: #03337a;
        border: 0;
        color: #FFFFFF;
        padding: 11px 16px;
        font-size: 14px;
        font-weight: 500;
        border-radius: 4px;
        cursor: pointer;
      }
      
      .btn:hover {
        background: #444954;
      }
      
      .btn:disabled {
        background: #ddd;
      }
      
      .form-label {
        display: block;
        padding: 20px 0 10px 0;
      }
      
      .form-field {
        width: 100%;
        padding: 10px;
        border: 1px solid #d5d5d5;
        border-radius: 4px;
        background-color: #fff;
      }
      
      footer {
        position: absolute;
        padding: 20px 0;
        width: 100%;
      }
      
      footer a {
        text-decoration: none;
        color: #555555;
      }
      
      footer a:hover {
        text-decoration: underline;
      }
      
      /* Responsive CSS below */
      @media screen and (max-width: 1050px) {
        .rhide {
          display: none;
        }
        .content-wrapper {
          width: 100%;
          padding: 0 10px;
        }
       
        main .checkout form, main .myaccount form {
          width: 100%;
        }
        main .myaccount .login-register {
          flex-flow: column;
        }
        main .myaccount .login-register .login {
          border-right: 0;
          padding: 10px;
        }
        main .myaccount .login-register .register {
          padding: 10px;
        }
        main .myaccount .menu {
          width: 100%;
          padding-right: 0;
        }
      }
      </style>
</head>
<body style="background-image:linear-gradient(rgba(0, 0, 0, 0.3), rgba(0, 0, 0, 0.3)), url(./assets/images/emergencyplug-auto-in-licht.jpg);
background-position: center center;background-size: 100% 100%; min-height: 100vh;background-repeat: no-repeat;">
<main style="color:#fff">       
<div class="myaccount content-wrapper">
<h1>'.$register_title.'</h1>
    <div class="login-register">
        <div class="login">
            <h4>'.$register_1_find.'</h4>
            <img style="width: 80%;
            margin-top: -75px;
            margin-bottom: -40px;" src="./assets/images/EP_register.gif" alt="">
            
            <h4>'.$register_2_enter.'</h4>
            <form action="" method="post">
                <label for="email" class="form-label">'.$register_2_label.' *</label>
                <input type="text" name="sn[]" id="sn" placeholder="'.$register_2_placeholder.'" style="margin-bottom:10px;" minlength="8" maxlength="8" required="" class="form-field">
                <button type="button" class="btn" onclick="addField(\'sn\',\'sn[]\');"> + </button><small>'.$register_2_add.'</small> 
            
        </div>
        <div class="register" style="'.$display.'">
            <h4>'.$register_3_register.'</h4>
                <label for="company" class="form-label">'.$register_3_name.' *</label>
                <input type="text" name="organization" placeholder="'.$register_3_name.'" value="'.$register['organization'].'" required="" class="form-field">
                <label for="email" class="form-label">'.$register_3_mail.' *</label>
                <input type="email" name="email" id="email" placeholder="info@example.com" value="'.$register['email'].'" required="" class="form-field">
                <label for="company" class="form-label">'.$register_3_phone.' *</label>
                <input type="text" name="phone" placeholder="'.$register_3_phone.'" value="'.$register['phone'].'" required="" class="form-field">
                <label for="city" class="form-label">'.$register_3_city.' *</label>
                <input type="text" name="city" placeholder="'.$register_3_city.'" value="'.$register['city'].'" required="" class="form-field">
                <label for="city" class="form-label">'.$register_3_country.' *</label>
                <input type="text" name="country" placeholder="'.$register_3_country.'" value="'.$register['country'].'" required="" class="form-field" style="margin-bottom: 20px;">
                <input type="checkbox" name="email_consent" checked required/>'.$register_3_email_consent.' <br>
                <input type="checkbox" name="terms_consent" required/>'.$register_3_term_consent_1.' <a href="" target="blank">'.$register_3_term_consent_2.'</a> *<br>
         
                <input name="register" type="submit" value="'.$register_button.'" class="btn">
            </form>
            <br>     
            <small>* '.$register_mandatory.'</small> 
        </div>';

        //IF MESSAGES ARE RETURN SHOW THEM
        if (!empty($message_return)){
          echo '
        <div class="register">
        <h4>'.$register_4_completed.'</h4>

        <p>'.$register_message_4.' '.software_update_user.'/'.software_update_pw.'</b></p>
        <h4>'.$register_5_details.'</h4>';
        foreach($message_return as $return_message){
          echo'
          <p>'.$return_message.'"</p>
          ';
        }

        echo'
        <h4>'.$register_6_return.'</h4>
        <p> <a href="register.php" class="btn alt mar-right-2">'.$register_6_button.'</a><p>
        ';

        }

echo'
    </div>  
</div>
</main>
</body>
<script>
function addField(id, name) {
  var x = document.createElement("INPUT");
  x.setAttribute("type", "text");
  x.setAttribute("name", name);
  x.setAttribute("value", "");
  x.setAttribute("placeholder", "'.$register_2_placeholder.'");
  x.setAttribute("class", "form-field");
  x.setAttribute("style", "margin-bottom:10px;");
  x.setAttribute("minlength", "8");
  x.setAttribute("maxlength", "8");
  x.setAttribute("required", "");
  var td_new = document.getElementById(id);
  td_new.parentNode.insertBefore(x, td_new.nextSibling);
  
}
</script>
</html>
';

