<?php
header("content-type: application/json");
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

// connect to the mongo database
$m = new MongoDB\Driver\Manager("mongodb://root:oebb@mongo:27017");

$radsatz = $_GET['radsatz'];
$station = $_GET['station'];

#$radsatz = '004277AA#1';
#$station = 140;

$filter = ['overhauls.radsatznummer' => $radsatz];

$options_status = ['projection' => ['_id' => 0,  'overhauls.radsatznummer' => 1, 'overhauls.process.stationsnummer' => 1, 'overhauls.process.taetigkeiten.name' => 1, 'overhauls.process.taetigkeiten.status' => 1 ]];

$query_status = new MongoDB\Driver\Query($filter,$options_status);

$cursor_status = $m->executeQuery('oebb.process_instances',$query_status);

foreach ($cursor_status as $document) {
  foreach ($document as $overhauls) {
    foreach ($overhauls as $overhaul) {
      if($overhaul->radsatznummer == $radsatz) {
        foreach($overhaul->process as $process) {
          if($process->stationsnummer == $station) {
            foreach($process->taetigkeiten as $taetigkeit) {
             $result['status']=$taetigkeit->status;
            }
          }
        }
      }
    }
  }
}

echo json_encode($result);
