<?php
if(!isset($_SESSION)) { session_start(); }
include_once '../classes/TestProjektSmarty.class_subdir.php';
require_once("../config.inc.php");
$templatename = substr(basename($_SERVER['PHP_SELF']),0,-3)."html";
$smarty = new SmartyAdmin();
require_once "../language/german.inc.php";
require_once("../func_get_parameter.php");

if (isset($_GET['action'])) {
  $action = $_GET['action'];
} else {
  $action = '';
}

$termin = $_GET['tid'];
if ($action == '') {

   $query1 = "SELECT lfd_haushalt, count(*) Anz
                FROM gd_buchung
               WHERE reihe = ''
                 AND tid = $termin
               GROUP BY lfd_haushalt";

   $result1 = $db->query($query1) or die("Cannot execute query1");
   
   
   while ($row1 = $result1->fetch_array()) {
    $value2 = '';
    $mehrfachbelegung = trim(get_parameter(6));
    if($mehrfachbelegung == 0){
    $query2 = "SELECT reihe, platz
                 FROM gd_verfplaetze
                WHERE concat(reihe,platz) not in (select concat(reihe,platz) from gd_buchung where tid=$termin and lfd_haushalt!=$row1[lfd_haushalt])
                  AND reihe not in ('Sonntagschule', 'Vorsonntagschule')
                  order by reihe, platz
              ";
     }else{
    $query2 = "SELECT reihe, platz
                 FROM gd_verfplaetze
                WHERE reihe not in ('Sonntagschule', 'Vorsonntagschule')
                  order by reihe, platz
              ";
     }
    $result2 = $db->query( $query2)
              or die ("Cannot execute query2");

    $ln2 = 0;
    while ($row2 = $result2->fetch_array()){   
      $inner1[$ln2]['reihe'] = $row2['reihe'];
      $inner1[$ln2]['platz'] = $row2['platz'];
      $value2 = $inner1;
      $ln2++;
     }

   
     $query_name = $db->query("SELECT bid, vorname, nachname
                                 FROM gd_buchung
                                WHERE reihe = ''
                                  AND lfd_haushalt= $row1[lfd_haushalt]
                                  AND tid = $termin
                                ORDER BY bid
                                LIMIT 1");
     $row_name = $query_name->fetch_array();

     $row1['inner']           = $value2;
     $row1['tid']             = $termin;
     $row1['vorname']         = $row_name['vorname'];
     $row1['nachname']        = $row_name['nachname'];
     $table_data1[]           = $row1;
   }
  $smarty->assign('table_data1', $table_data1);
}
$smarty->assign('action', "$action");
$smarty->display("$template/admin/$templatename");

?>