<!DOCTYPE HTML>  
<html>
  <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="shortcut icon" href="favicon.png" type="image/x-icon">
    <link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css"> 
  </head>

  <body>  
    <?php
    require_once 'phpscripts/functions.php';
    require_once 'phpscripts/params.php';
    require_once 'phpscripts/payment_server.php';
    require_once 'phpscripts/database.php';
    require_once 'phpscripts/translations.php';

    // Get parameters
    $params = getRequestParams();

    $device_id = getMandatoryParam($params, "device_id", "Bad link");

    // Check if client is registered
    $client_id = getClientCookie();
    if ($client_id === null) {
      header('location: register.php?device_id=' . $device_id);
      exit;
    }

    $user = Database::getUserByClient($client_id);

    // Localization
    $browser_language = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
    $language = $user["language"] ?? $browser_language;
    $msg = getTranslations("index.php", $language);

    if ($msg === false) {
      $error = PaymentServer::$lastError;
      if (isNullOrEmpty($error))
        $error = "Server not responding";
      header('location: error.php?message=' . $error);
      exit;
    }

    // TODO: Update user browser details ?
    // Check device
    $device = PaymentServer::getDeviceInfo($device_id, true, $user["phone"], $language);

    if ($device === false) {
      $error = PaymentServer::$lastError;
      if (isNullOrEmpty($error))
        $error = $msg->get(3, "Unknown device");
      header('location: error.php?message=' . $error);
      exit;
    }

    if (!DeviceInfo::validate($device)) {
      header('location: error.php?message=' . $msg->get(4, "Device is not configured"));
      exit;
    }

    // Next workflow step:
    // GD/GS - select ONE product 
    // VM/VS - wait item is selected on the machine
    // RE/RL - Refrigerator with lock 
    // LO - Door lock

    $device_type = $device->{"deviceType"};
    $success = 1;
    if ($device_type === "RE" || $device_type === "RL")
      $next_page = "select_items.php";
    else if ($device_type === "GD" || $device_type === "GS")
      $next_page = "select_item.php";
    else if ($device_type === "VM" || $device_type === "VS") {
      $terminal_info = Database::getTerminalInfo($device_id);
      $machine_name = "<b>" . $terminal_info['machine_name'] . "</b>";
      $vipps_activated = $terminal_info['vipps_activated'];
      $vipps_supported = $terminal_info['vipps_supported'];
      $qr_verified = $terminal_info['qr_verified'];
      if ($vipps_supported == "false") {
        $success = 0;
        $message = ($msg->language === 'no') ? 'Vipps støttes ikke ennå. Vennligst bruk brikke eller kort.<br/><b>' . $device_id . "</b><br/>" . $machine_name .
                $qr_verified . $vipps_activated : 'Vipps is not yet supported. Please use tag or card.<br/><b>' . $device_id . "</b><br/>" . $machine_name;
        Database::registerQRRequests($success,$device_id, $device);
        header("location: error.php?message=$message");
      } else {
        Database::registerQRRequests($success,$device_id, $device);
        header("location: wait_item_selected.php?device_id=$device_id&client_id=$client_id");
      }
      exit;
    }
    //else if ($device_type === "LO")
    //    $next_page = "open_lock.php"; // TODO: Can we reuse "wait item dispensed" logic?

    if (!isset($next_page)) {
      $success = 0;
      Database::registerQRRequests($success,$device_id, $device);
      header('location: error.php?message=' . $msg->get(8, "Unsupported device type"));
      exit;
    }

    //header("refresh:2;url=$next_page?device_id=$device_id&client_id=$client_id");
    ?>

    <div class="w3-container w3-center" style="padding-top: 60px">
      <img src="img/nopayn_logo_standard.svg" height="50">

      <div style="padding-top: 50px">
        <p class="w3-large w3-text-teal"><?php echo $msg->get(1, "Welcome to mobile payment!"); ?></p>
      </div> 

      <div style="padding-top: 20px">
        <label for="merchant"><?php echo $msg->get(5, "Merchant"); ?>:</label>
        <label id="merchant"><?php echo $device->{"org_name"}; ?></label><br>

        <label for="location"><?php echo $msg->get(6, "Location"); ?>:</label>
        <label id="location"><?php echo $device->{"site_name"}; ?></label><br>

        <label for="machine"><?php echo $msg->get(7, "Machine"); ?>:</label>
        <label id="machine"><?php echo $device->{"device_name"}; ?></label><br>
      </div> 

      <form method="get" action="<?php echo $next_page; ?>" style="padding-top: 30px">
        <input type="hidden" name="device_id" value="<?php echo $device_id; ?>">
        <input type="hidden" name="client_id" value="<?php echo $client_id; ?>">

        <input type="submit" name="submit" value="<?php echo $msg->get(9, "Make purchase"); ?>" class="w3-button w3-teal w3-text-white" style="width: 180px">
      </form>
    </div>

  </body>

</html>
