<?php
include_once 'classes/TestProjektSmarty.class.php';
require_once("config.inc.php");
$templatename = substr(basename($_SERVER['PHP_SELF']),0,-3)."html";
$smarty = new Smarty();
require_once "language/german.inc.php";

$action = $_GET['action'];
if(isset($_GET['id'])){
  $_SESSION["status_id"] = $_GET['id'];
}
$status = $_SESSION["status_id"];

if($action == ''){

  if(isset($status)){
    $_SESSION["status_id"] = "$status";
  }
  
  $db = dbconnect();
  
  $result = $db->query("SELECT beschreibung FROM imt_rechte WHERE dateiname = 'status.php?id=$status'");
  $row = $result->fetch_array();
  $smarty->assign('status_titel', "$row[beschreibung]");
  $smarty->assign('status_id', "$status");
  

  
    
    $query = "SELECT a.vid, betreff, date_format(erfass_dat, '%d.%m.%Y (%H:%i)') erfass_dat_form, vorname, nachname
                FROM imt_vorschlag a, imt_verlauf b, imt_user c
               WHERE a.vid = b.vid
                 AND a.imtuid=c.imtuid
               GROUP BY a.vid
               ORDER BY erfass_dat ASC";
    
    $result = $db->query ($query)
              or die ("Cannot execute query");
    
    $table_data1 = array();
      
    while ($row = $result->fetch_array()){
      
      $result1 = $db->query("SELECT date_format(datum, '%d.%m.%Y (%H:%i)') datum_form
                                FROM imt_sitzungen a, imt_sitzungen_vorschlag b
                               WHERE a.isid = b.isid
                                 AND b.vid=$row[vid]");
      $row1 = $result1->fetch_array();

      if($status == 1){
        $result2 = $db->query("SELECT status
                                  FROM imt_verlauf
                                 WHERE vid = $row[vid]
                                   AND status < 100
                                 ORDER BY datum DESC, status DESC
                                 LIMIT 1");
        $row2 = $result2->fetch_array();
        if($row2['status'] == '1'){
          array_push($table_data1, array(  
           'vid' => $row['vid'],
           'betreff' => $row['betreff'],
           'erfass_dat_form' => $row['erfass_dat_form'],
           'sitzungsdat' => $row1['datum_form'],
           'vorname' => $row['vorname'],
           'dateiname' => 'bearbeiten.php',
           'nachname' => $row['nachname']
          )
          );        
        }
      }
      
      if($status == 7){
        $result2 = $db->query("SELECT status
                                  FROM imt_verlauf
                                 WHERE vid = $row[vid]
                                   AND status IN ('7')
                                 ORDER BY datum DESC, status DESC
                                 LIMIT 1");
        $row2 = $result2->fetch_array();
        if($row2['status'] == '7'){
          array_push($table_data1, array(  
           'vid' => $row['vid'],
           'betreff' => $row['betreff'],
           'erfass_dat_form' => $row['erfass_dat_form'],
           'sitzungsdat' => $row1['datum_form'],
           'vorname' => $row['vorname'],
           'dateiname' => 'ansicht_entschieden.php',
           'nachname' => $row['nachname']
          )
          );        
        }
      }
      
      if($status == 8){
        $result2 = $db->query("SELECT status
                                  FROM imt_verlauf
                                 WHERE vid = $row[vid]
                                   AND status IN ('8')
                                 ORDER BY datum DESC, status DESC
                                 LIMIT 1");
        $row2 = $result2->fetch_array();
        if($row2['status'] == '8'){
          array_push($table_data1, array(  
           'vid' => $row['vid'],
           'betreff' => $row['betreff'],
           'erfass_dat_form' => $row['erfass_dat_form'],
           'sitzungsdat' => $row1['datum_form'],
           'vorname' => $row['vorname'],
           'dateiname' => 'ansicht_entschieden.php',
           'nachname' => $row['nachname']
          )
          );        
        }
      }

      if($status == 100){
        $result2 = $db->query("SELECT status
                                  FROM imt_verlauf
                                 WHERE vid = $row[vid]
                                   AND status < 100 
                                 ORDER BY datum DESC, status DESC
                                 LIMIT 1");

        $row2 = $result2->fetch_array();
        if($row2['status'] != '1' AND $row2['status'] != '7' AND $row2['status'] != '8'){
          array_push($table_data1, array(  
           'vid' => $row['vid'],
           'betreff' => $row['betreff'],
           'erfass_dat_form' => $row['erfass_dat_form'],
           'sitzungsdat' => $row1['datum_form'],
           'vorname' => $row['vorname'],
           'dateiname' => 'bearbeiten.php',
           'nachname' => $row['nachname']
          )
          );        
        }
      }

    }
    
    $smarty->assign('table_data1', $table_data1);
    

}





$smarty->assign('action', "$action");
$smarty->display("$template/$templatename");

?>
