<?php
session_start(); 
if(!isset($_SESSION['username'])){
  header("HTTP/1.1 301 Moved Permanently");
  header("Location: login.php");
  header("Connection: close");

  exit();
  }
  
  
  require_once("core/dbase.php");
  
  if($_GET["A"]=="Delete"){
  
  $query = "SELECT Person_id FROM lide WHERE"; 
  $query .= " Jmeno='".$_GET['Name']."'";
  $query .= "AND Prijmeni='".$_GET['Surname']."'";
  $result = $database->query($query);
  $row = $result->fetchArray();    
  $id = $row['Person_id'];
  echo $query;
  echo $id;
  $query = "ATTACH DATABASE '/var/tmp/daily' AS daily";
  $result = $database->query($query);
  $query = "DELETE FROM daily.dochazka WHERE Person_id =".$id." AND Date_time='".$_GET['Oldtime']."'";
  $result = $database->query($query);
  echo $query;
  exit();
  
  
  
  }
  
  if($_GET["A"]=="Edit"){
  
  $query = "SELECT Person_id FROM lide WHERE"; 
  $query .= " Jmeno='".$_GET['Name']."'";
  $query .= "AND Prijmeni='".$_GET['Surname']."'";
  $result = $database->query($query);
  $row = $result->fetchArray();    
  $id = $row['Person_id'];
  $query = "ATTACH DATABASE '/var/tmp/daily' AS daily";
  $result = $database->query($query);
  $query = "UPDATE daily.dochazka SET Location='".$_GET['Leaving']."',Date_time='".$_GET['Newtime']."' WHERE Person_id=".$id." AND Date_time='".$_GET['Oldtime']."'";
  $result = $database->query($query);
   echo $query;
   exit();
  
  
  
  }
  if($_GET["A"]=="Show"){
    if(($_SESSION['rights']=="U") && ($_GET['Name']!=$_SESSION['username'] )){
      echo "Uživatel neexistuje, nebo nemáte oprávnění zobrazit jeho docházku.";
      exit();
    }
    if(($_SESSION['rights']=="G")){
      $query = "SELECT Skupina FROM lide WHERE";
      $name = explode(" ",$_GET['Name']);
      $query .= " Jmeno='".$name[0]."'";
      $query .= "AND Prijmeni='".$name[1]."'";
      $result = $database->query($query);
      $row = $result->fetchArray();
      if($row['Skupina']!=$_SESSION['group']){
        echo "Uživatel neexistuje, nebo nespadá do Vaší skupiny.";
        exit();
      }
    }   
   echo "<table><tr><th>Jméno</th><th>Přijmení</th><th>Typ</th><th>Datum</th><th>Editace</th></tr>";
   if((date("Y-m-d"))==($_GET['To'])){
  $query = "ATTACH DATABASE '/var/tmp/daily' AS daily";
  $database->query($query);
  $query = "SELECT Jmeno,Prijmeni,Date_time,Name FROM daily.dochazka JOIN lide, terminaly ON daily.dochazka.Person_id = lide.Person_id AND terminaly.location=daily.dochazka.location WHERE 1 ";
  if ($_GET['Leaving']!="Vše"){
  $query .= "AND Name='".$_GET['Leaving']."'";
  }
  if ($_GET['Name']!=""){
  $name = explode(" ",$_GET['Name']);
  $query .= "AND Jmeno='".$name[0]."'";
  $query .= "AND Prijmeni='".$name[1]."'";
  }
  $result = $database->query($query);
  while($row = $result->fetchArray())
  echo "<tr><td>",$row['Jmeno'],"</td><td>",$row['Prijmeni'],"</td><td>",$row['Name'],"</td><td>",$row['Date_time'],"</td><td class='edit'></td></tr>";
  }
  if((date("Y-m-d"))!=($_GET['From'])){
  $query = "SELECT Jmeno, Prijmeni, Date_time, Name FROM stdvypis WHERE Date_Time >='".$_GET['From']."' AND Date_Time <='".$_GET['To']."'";
  if ($_GET['Leaving']!="Vše"){
  $query .= "AND Name='".$_GET['Leaving']."'";
  }
  if ($_GET['Name']!=""){
  $name = explode(" ",$_GET['Name']);
  $query .= "AND Jmeno='".$name[0]."'";
  $query .= "AND Prijmeni='".$name[1]."'";
   
  }
  //echo $query;
  //echo "<br />";
   echo $query;
  $result = $database->query($query);
	
  
  while($row = $result->fetchArray())
  echo "<tr><td>",$row['Jmeno'],"</td><td>",$row['Prijmeni'],"</td><td>",$row['Name'],"</td><td>",$row['Date_time'],"</td><td>&nbsp;&nbsp;N/A</td></tr>";
  }
  echo "</table>"; 
exit();
}
?>
<div class='left_panel'><h1><img src='magnifier.png' /> Filtrace záznamů</h1>
<fieldset><legend>Podle jména</legend> <label for="search">Jméno:</label>
  <input class="text ui-widget-content ui-corner-all" id="search" <?php
//if($_SESSION['rights']=="U")
echo "Value='".$_SESSION['username']."'";
?>/></fieldset>
<fieldset><legend>Podle typu</legend>
<select id="anim" class="ui-widget-content ui-corner-all">
    <option value="">Vše</option>
   <?php  
     $query = "SELECT Location, Name FROM terminaly WHERE Location LIKE 'A%' ORDER BY Location";
     $result = $database->query($query);
	   $option = 0;
      while($row = $result->fetchArray()){
       echo "<option value ='",$option,"'>",$row['Name'],"</option>";
       $option++;
      }
      
    ?>
  </select>
</fieldset>
<fieldset><legend>Podle data a času</legend><div id="radio">
    <input type="radio" id="radio1" name="radio" checked="checked" /><label for="radio1">Dnes</label>
    <input type="radio" id="radio2" name="radio" /><label for="radio2">Týden</label>
    <input type="radio" id="radio3" name="radio" /><label for="radio3">Měsíc</label>
  </div>
  <p>Od: <input type="text" class="ui-widget-content ui-corner-all" id="dateFrom" /></p>
  <p>Do: <input type="text" class="ui-widget-content ui-corner-all" id="dateTo" /></p>
  </fieldset>
<input class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only submit" role="button" aria-disabled="false" type="submit" value="Filtrovat" />

</div>

<div class='main_content'>

</div>


<div id="edit-form" title="Editace záznamu">
  <form>
  <fieldset>
    <legend><span id="edit_name"></span>
    <span id="edit_surname"></span></legend>
    Řádek číslo : <span id="edit_row"></span><br /> <br />
    Změnit datum a čas :
    <input type="text" name="edit_datetime" id="edit_datetime" value="" class="text ui-widget-content ui-corner-all" />
    Změnit typ : <div id="edit_leaving"></div>
    <div id="edit_name"></div>
    <select id="edit_leaving" class="ui-widget-content ui-corner-all">
    <?php 
       
        while($row = $result->fetchArray()){
      echo "<option value ='",$row['Location'],"'>",$row['Name'],"</option>";
      
       }
       ?>
  </select>
      <input type="hidden" name="old_datetime" id="old_datetime" />
  </fieldset>
  </form>
</div>


 <script type="text/javascript"
        src="js/jquery.ui.datepicker-cs.js"></script>
<script>
  $(function() {
    var availableTags = [    
    <?php  
    
     if($_SESSION['rights']=="U") echo "\"".$_SESSION['username']."\"";
     else {
        $query = "SELECT Jmeno, Prijmeni FROM lide WHERE Person_id < 1000 ";
        if($_SESSION['rights']=="G")
          $query .= "AND Skupina='".$_SESSION['group']."'";
        $result = $database->query($query);
	
        while($row = $result->fetchArray())
        echo "\"",$row['Jmeno']," ",$row['Prijmeni'],"\",";
      }
    ?>
      
    ];
    
    $( "#search" ).autocomplete({
      source: availableTags
    });
    $( "#radio" ).buttonset();
    $.datepicker.setDefaults( {dateFormat: "yy-mm-dd", firstDay: 1});
     $( "#dateFrom" ).datepicker();
     $( "#dateTo" ).datepicker();
     $( "#radio1" ).button().click(function( event ) {$( "#dateFrom" ).datepicker('setDate', new Date()); $( "#dateTo" ).datepicker('setDate', new Date());});
     $( "#radio2" ).button().click(function( event ) {
      var weekdate=new Date(); 
      weekdate.setDate(weekdate.getDate() - (((weekdate.getDay())+6)%7));
      $( "#dateFrom" ).datepicker('setDate', weekdate); 
      $( "#dateTo" ).datepicker('setDate', new Date());
     }); 
     
     $( "#radio3" ).button().click(function( event ) {
      var monthdate=new Date(); 
      monthdate.setDate(1);
      $( "#dateFrom" ).datepicker('setDate',monthdate); 
      $( "#dateTo" ).datepicker('setDate', new Date());
     });   
     
     $( "input[type=submit]" )
      .button()
      .click(function( event ) {
        $('.main_content').hide();
        event.preventDefault();
        $.get("attend.php",{A: "Show", Name: $('#search').val(), Leaving: $('#anim').find('option:selected').text(), From: $('#dateFrom').val(), To: $('#dateTo').val()}, 
        function(data){          
         $('.main_content').html(data);
         $('.main_content').show('fade');
        });
        
      });
       $( "#dateFrom" ).datepicker('setDate', new Date());
       $( "#dateTo" ).datepicker('setDate', new Date());
       
       $( "#edit-form" ).dialog({
      autoOpen: false,
      height: 275,
      width: 350,
      modal: true,
      buttons: {
        "Uložit změny": function() {
          var row = $("#edit_row").html();
          $.get("attend.php",{A: "Edit", Name: $("#edit_name").html(), Surname: $("#edit_surname").html(), Leaving: $("#edit_leaving " ).find('option:selected').val(), Oldtime: $("#old_datetime" ).val(), Newtime: $("#edit_datetime" ).val()}, 
        function(data){          
            $('.main_content').html(data);
          $(".main_content tr:eq("+row+")").find("td:eq(3)").html($("#edit_datetime" ).val());
          $(".main_content tr:eq("+row+")").find("td:eq(2)").html($("#edit_leaving " ).find('option:selected').text());
          
          });
          $( this ).dialog( "close" );
        },
        "Smazat záznam": function() {
            var row = $("#edit_row").html();  
            var rowa = $(".main_content tr:eq("+row+")");
             $.get("attend.php",{A: "Delete", Name: $("#edit_name").html(), Surname: $("#edit_surname").html(), Oldtime: $("#old_datetime" ).val()}, 
        function(data){          
         
          rowa.fadeOut('slow',function(){$rowa.remove();});
          
          });
          $( this ).dialog( "close" );
          
        },
        Cancel: function() {
          $( this ).dialog( "close" );
        }
      },
    });
    
   
    
   $('.main_content').delegate('.edit', 'click', function() {
       var row = $(this).parent();
       var name = $(row).find("td:first").html();
       var surname = $(row).find("td:eq(1)").html();
       var leaving = $(row).find("td:eq(2)").html(); 
       var rowindex = $(this).parent().parent().children().index($(this).parent());
       var datetime = $(row).find("td:eq(3)").html();
       $("#edit_name").html(name);
       $("#edit_surname").html(surname);
      $("#edit_row").html(rowindex);
       $("#edit_datetime").val(datetime );
       $("#old_datetime").val(datetime );
       $("#edit_leaving option").filter(function() {
      return $(this).text() == leaving; 
      }).prop('selected', true);
      
       $( "#edit-form" ).dialog( "open" );
    }); 
    
    
  });
  </script>


