<?php
session_start();
$jahrgang = $_SESSION["jahrgang"];
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 "functions.php";
require_once "language/german.inc.php";
#require_once "func_lvs_exk_mehrtaegig.php";
#require_once "func_lvs_exk_eintaegig.php";

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


if ($action == '') {
  
  if ($jahrgang == "") {
    echo "Bitte zuerst <a href='jahrgang.php'>Jahrgang</a> w&auml;hlen!";
    exit;
  }
  

    $query_muschu = $db->query("SELECT count(*) Anz 
                                  FROM `dep_deputat_ermaessigung` 
                                 WHERE imtuid=$uid 
                                   AND jahr='$jahrgang' 
                                   AND art LIKE '%Mutterschutz%' 
                               ");
    $row_muschu   = $query_muschu->fetch_array();
    
    if($row_muschu['Anz'] > 0){

        $query_muschu1 = $db->query("SELECT date_format(created, '%d.%m.%Y') datum_form
                                  FROM `dep_deputat_ermaessigung` 
                                 WHERE imtuid=$uid 
                                   AND jahr='$jahrgang' 
                                   AND art LIKE '%Mutterschutz%'
                                   ");
        $row_muschu1   = $query_muschu1->fetch_array();
        # Button zum Löschen anzeigen
        $query_muschu2 = $db->query("SELECT count(*) Anz
                                      FROM dep_deputat_ermaessigung
                                     WHERE imtuid = '$uid'
                                       AND jahr = '$jahrgang'
                                       AND art like '%Mutterschutz%'
                                       AND lvs = 0
                                   ");
        $row_muschu2   = $query_muschu2->fetch_array();
        if($row_muschu2['Anz'] == 0){
          $del = 2;
        }else{
          $del = 0;
          $smarty->assign('muschu_erf_dat', "$row_muschu1[datum_form]");
        }
        
    }else{
       # Kein Löschbutton
       $del = 1;
    }
  $smarty->assign('muschu_erf', "$del");
  }


if ($action == 'save') {
  $query_muschu = $db->query("SELECT count(*) Anz 
                                FROM `dep_deputat_ermaessigung` 
                               WHERE imtuid=$uid 
                                 AND jahr='$jahrgang' 
                                 AND art LIKE '%Mutterschutz%' 
                             ");
  $row_muschu   = $query_muschu->fetch_array();
    
  if($row_muschu['Anz'] == 0){
    $datum = date("Y-m-d H:i:s");
    $sql1 = $db->query("INSERT INTO dep_deputat_ermaessigung ( imtuid
                                                             , jahr
                                                             , art
                                                             , beginn
                                                             , ende
                                                             , sws
                                                             , lvs
                                                             , sort_order
                                                             , created
                                                             )
                                VALUES
                                                             ( '$uid'
                                                             , '$jahrgang'
                                                             , 'Mutterschutz / Elternzeit'
                                                             , '$beginn'
                                                             , '$ende'
                                                             , '$sws'
                                                             , '$lvs'
                                                             , 5
                                                             , '$datum'
                                                             )
                              ");
   }else{
    $sql1 = $db->query("DELETE FROM dep_deputat_ermaessigung 
                         WHERE imtuid = '$uid' 
                           AND jahr='$jahrgang'
                           AND art LIKE '%Mutterschutz%' 
                              ");
   }  
  echo "<meta http-equiv=\"refresh\" content=\"0; URL=" . $_SERVER['PHP_SELF'] . "\">";


}

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

?>