<?php
//ini_set('display_errors', '1');
//ini_set('display_startup_errors', '1');
//error_reporting(E_ALL);

include './assets/functions.php';
include './settings/settings.php';
include './settings/config.php';
include_once './settings/translations/translations_US.php';


// Check if the user is already logged in, if yes then redirect him to welcome page
if(isset($_SESSION["loggedin"]) && $_SESSION["loggedin"] === true){
	header("location: index.php");
	exit();
}

// Define variables and initialize with empty values
$username = $password = '';
$username_err = $password_err = '';
$retry = 0;

// Process submitted form data
if ($_SERVER['REQUEST_METHOD'] === 'POST') {

  // Check if username is empty
  if(empty(trim($_POST['username']))){
    $username_err = 'Please enter username.';
  } else{
    $username = trim($_POST['username']);
  }

  // Check if password is empty
  if(empty(trim($_POST['password']))){
    $password_err = 'Please enter your password.';
  } else{
    $password = trim($_POST['password']);
  }

  // Validate credentials
  if (empty($username_err) && empty($password_err) && $retry < 3) {
    $data = json_encode(array("username" => $username, "password" => $password), 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'){
          $retry++;
          $password_err = 'Not authorized, please retry';
        } elseif ($responses == '1'){
          $password_err = 'Too many login attempts. User blocked, please contact your administrator';
        } else {
        // Start a new session
        session_start();
      
        // Store data in sessions
        $_SESSION['loggedin'] = true;
        $_SESSION['id'] = $responses->id;
        $_SESSION['username'] = $responses->username;
        $_SESSION['email'] = $responses->email;
        $_SESSION['salesID'] = $responses->salesID;
        $_SESSION['partnerhierarchy'] = $responses->partnerhierarchy; //clean;
        $_SESSION['permission'] = $responses->permission;
        $_SESSION['profile'] = $responses->profile;
        $_SESSION['userkey'] = $responses->userkey;
        $_SESSION['language'] = $responses->language;

        $language_user = trim($responses->language) ?? 'US';
        if($responses->profile == 'firmwaretool,application'){
          header('location: index.php?page=firmwaretool');
          exit();

        } else {
          header('location: index.php?language='.$language_user.'');
          exit();
        }
      }
  }
  else {
    // Display an error for passord mismatch
    $password_err = 'Not authorized';
  }
} 
echo' 
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=UTF-8" />
<title>'.site_title.'</title>
<link rel="shortcut icon" href="'.icon_image.'" type="image/x-icon" />
<style>
* {
  padding: 0;
  margin: 0;
  box-sizing: border-box;
  font-family: "Open Sans", Helvetica, sans-serif;
  accent-color: '.color.';
}

body {
  background-color: #fff;
  font-family: sans-serif;
}

.logon {
  margin: 0;
  position: absolute;
  top: 40%;
  left: 50%;
  transform: translate(-50%, -50%);
}

#imagelogon {
background-image: url('.icon_image.');
background-repeat: no-repeat;
opacity: inherit;
width: 85px;
height: 150px;
margin: 0 auto;
position: relative;
top: 87px;
-webkit-filter: drop-shadow(5px 5px 5px #222);
filter: drop-shadow(5px 5px 5px #222);
}

.login-box{
    width: 350px;
    height: 330px;
    background-color:'.color.';
    padding: 55px 30px;
    border-radius: 10px;
    text-align: center;
    -webkit-box-shadow: 10px 10px 5px -4px rgba(0,0,0,0.75);
    -moz-box-shadow: 10px 10px 5px -4px rgba(0,0,0,0.75);
    box-shadow: 10px 10px 5px -4px rgba(0,0,0,0.75);
  
  }
  
  .login-box h1{
    font-weight: 500;
    color: #fff;
  }
  
  .login-box input[type="text"],
  .login-box input[type="password"]{
    width: 75%;
    margin-bottom: 10px;
  }
  
  .login-box input[type="text"],
  .login-box input[type="password"]{
    border-radius: 10px;
    //background: transparent;
    border: none;
    outline: none;
    border: 2px solid '.color_accent.';
    font-size: 13px;
    height: 40px;
    text-align: center;
    transition: 0.27s;
  }
  .login-box input[type="text"]:focus,
  .login-box input[type="password"]:focus{
  width: 80%;
  border-color:#6fd653;
  }
  
  .login-box input[type="submit"]{
    width: 60%;
    border: none;
    outline: none;
    background: '.color_accent.';
    padding: 10px 0;
    border-radius: 10px;
    color: #ffffff;
    font-weight: 500;
    text-transform: uppercase;
    cursor: pointer;
    margin-bottom: 20px;
    position: relative;
    bottom: -12px;
    font-style: italic;
  }
  
  .login-box input[type="submit"]:hover{
    background:#6FD653;
   transition: .2s;
  }

  .login-box input[type="submit"]:focus{
    transform: scaleX(1.1);
  }
  
  .login-box a {
    text-decoration: none;
    font-size: 12px;
    color: #00bcd4;
  }
  
  .login-box a:hover{
    color: #6FD653;
  }

  #reset a {
    text-decoration: none;
    font-size: 10px;
    color: #00bcd4;
  }

  #reset {
  margin-top: -10px;
  }

  #message p {
    font-size: 15px;
    color: #ffffff;;
  }

  /* Slide 5 */
.slider-checkbox {
	width: 3em;
	height: 1.5em;
	margin: 20px auto;
}

.slider-checkbox * {
	transition: 250ms;
	box-sizing: border-box;
}

.slider-checkbox input[type="checkbox"] {
	display: none;
}

.slider-checkbox label {
	display: inline-block;
	width: 100%;
	height: 95%;
	background: '.color.';
	border-radius: 0.7em;
	padding-top: 0.2em;
	padding-left: 0.2em;
}

.slider-checkbox span {
	display: inline-block;
	width: 1em;
	height: 1em;
	background: #FFF;
	border: none;
	border-radius: 2em;
}

.slider-checkbox input[type="checkbox"]:checked ~ label {
	background: #93ed49;
	padding-left: 1.7em;
}

</style>
</head>
<body style="background-color:'.color_accent.';background-image: url(./assets/images/emergencyplug-auto-in-licht.jpg);
background-position: center center;background-size: 100% 100%; min-height: 100vh;background-repeat: no-repeat;">
';

//Maintenance mode notification message
if (maintenance_mode_communication){
echo'
<p style="text-align: center;">
    <small>'.maintenance_mode_notification.'</small> 
</p>';
}

//Logon view
echo'
<div class="logon">
    <div class="logon_center" style="position: static;">
      <div id="imagelogon"></div>
      <div class="login-box">   
        <small style="color:#fff;">'.site_title.'</small>  
        <h1>Customer Portal</h1>
        ';

        if (maintenance_mode)
        {
          //Maintenance mode is on => Show maintenance mode text
          echo '
          <p style="margin-top: 10%;color: white;">'.maintenance_mode_text.'</p>
          ';
        } else {
          //Maintenance mode is off => Show login
          echo '
        <form style="margin-top:15px;" action="'.$_SERVER['PHP_SELF'].'" method="POST">
        <input type="text" id="username" name="username" placeholder="Username" value="'; echo $username; echo '" autofocus>
        <input type="password" id="password" name="password" placeholder="Password" value="'; echo $password; echo '">
        <div id="reset"><a href="reset.php">'.$button2.'</a></div>
        <input type="submit" value="'.$button1.'">
        </form>
        <div id="message"><p>'.$username_err.''.$password_err.'<p></div>';
        }
        echo '
          
      </div>';
      // -----------------------
      //DISPLAY REDIRECT SLIDER
      // -----------------------
        echo '
        <section title="">
          <div class="slider-checkbox">
            <input id="my-check-box" type="checkbox" onchange="location.href=\'https://legacy.veliti.nl\'"/>
            <label for="my-check-box">
                <span id="ball"></span>
            </label>
          </div>
          <p style="text-align:center;margin-top:-20px;color:#ffffff7a;font-size:12px;">use legacy portal</p>
        </section>
        ';
      // -----------------------
      // END - REDIRECT SLIDER
      // -----------------------

      echo '
    </div>
</div>
</body>
</html>
';
?>
