<?php
session_start(); 

if(!(($_SESSION['rights']=="A")||($_SESSION['rights']=="G")))
{
header("HTTP/1.1 301 Moved Permanently");
header("Location: login.php");
header("Connection: close");
exit();
}

require_once("core/dbase.php");

if($_GET["A"]=="Edit"){
  
 
  $query = "UPDATE 'lide' SET Jmeno='".$_GET['Name']."',Prijmeni='".$_GET['Surname']."',Skupina='".$_GET['Group']."',Opravneni='".$_GET['Rights']."',RFID='".$_GET['Rfid']."',PIN='".$_GET['Pin']."' WHERE Person_id=".$_GET['Id'];
  $result = $database->query($query);
  echo $query;
   exit();
  
}

if($_GET["A"]=="Remove"){
  
  $id = $_GET['Id'];
  $query = "UPDATE 'lide' SET 'Person_id'=".($id+1000).", 'Jmeno'=' ', 'Prijmeni'=' ','Opravneni'=' ','Skupina'=' ','RFID'=' ','PIN'=' ','Otisk'='X', 'Otiskupdate'=' '   WHERE Person_id = ".($id);
  $result = $database->query($query);
  
   exit();
  
}

if($_GET["A"]=="Add"){

  $query = "SELECT max(Person_id) FROM lide";
  $result = $database->query($query);
  $row = $result->fetchArray();
  $id = $row['max(Person_id)'];
  
  if($id>1000){
    $id = $id - 1000;
    $query = "UPDATE 'lide' SET 'Person_id'=".$id.", 'Jmeno'='".$_GET['Name']."', 'Prijmeni'='".$_GET['Surname']."','Opravneni'='U','Skupina'='".$_GET['Group']."','RFID'='".$_GET['Rfid']."','PIN'='".$_GET['Pin']."','Otisk'=' ' WHERE Person_id = ".($id+1000);
    
  }
  else {
    $id = $id + 1;
    $query = "INSERT INTO 'lide' ('Person_id','Jmeno','Prijmeni','Otisk','Opravneni','RFID','PIN','OtiskUpdate','Skupina','Heslo') VALUES (".$id.",'".$_GET['Name']."','".$_GET['Surname']."',' ','U','".$_GET['Rfid']."','".$_GET['Pin']."',NULL,'".$_GET['Group']."',NULL)";
  }
 
    echo $id;
   $result = $database->query($query);
   
   exit();
  
  
  
  }
  
?>  
<div class='left_panel'><h1><img src='adduser.png' /> Přidat uživatele</h1>
    <fieldset><legend>Základní údaje</legend>
    <label for="nameedit">Jméno:</label>
    <input class="text ui-widget-content ui-corner-all" id="nameedit" required />
    <label for="surnameedit">Příjmení:</label>
    <input type="text" class="text ui-widget-content ui-corner-all" id="surnameedit" required />
      
      
     <label for="groupedit">Skupina:</label>  
  <select id="groupedit" class="ui-widget-content ui-corner-all">
    <?php 
       
       $query = "SELECT ID, Nazev FROM Skupiny";
       if($_SESSION['rights']=="G")
       $query .= " WHERE ID =".$_SESSION['group'];
       $groupresult = $database->query($query);
       
        while($row = $groupresult->fetchArray()){
        echo "<option value ='",$row['ID'],"'>",$row['Nazev'],"</option>";
      
       }
       ?>
    </select>  
    </fieldset> 
    <fieldset><legend>Doplňková identifikace</legend>
    <label for="rfidedit">RFID karta:</label>
    <input class="text ui-widget-content ui-corner-all" id="rfidedit" placeholder="Nepovinný údaj" />
    <label for="pinedit">Pin:</label>
    <input class="text ui-widget-content ui-corner-all" id="pinedit" placeholder="Nepovinný údaj" />
   </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="Přidat" />
   
</div>

<div class='main_content'>
<table>
  <tr>
  <th>
    ID  
    </th>
    <th>
    Přijmení  
    </th>
    <th>
    Jméno
    </th>
    <th>
     Skupina
    </th>
    <th>
     Práva
    </th>
    <th>
     RFID
    </th>
    <th>
     Otisk
    </th> 
    <th>
     Editace
    </th> 
  </tr>
  <?php
  $query = "SELECT Person_id, Jmeno, Prijmeni, Opravneni, RFID, Skupiny.Nazev, OtiskUpdate FROM lide JOIN Skupiny ON lide.Skupina = Skupiny.ID";
  if(($_SESSION['rights']=="G")){
  $query .= " WHERE Skupina='".$_SESSION['group']."'";
  }
  //echo $query;
  //echo "<br />";
  $result = $database->query($query);
	

  while($row = $result->fetchArray())
  echo "<tr><td>",$row['Person_id'],"</td><td>",$row['Prijmeni'],"</td><td>",$row['Jmeno'],"</td><td>",$row['Nazev'],"</td><td>",$row['Opravneni'],"</td><td>",$row['RFID'],"</td><td>",$row['OtiskUpdate'],"</td><td class='edit'></td></tr>";
  ?>
   
</table>
</div>


<div id="edit-forma" title="Editace uživatele">
   <fieldset><legend>Základní údaje</legend>
    <label for="form_nameedit">Jméno:</label>
    <input class="text ui-widget-content ui-corner-all" id="form_nameedit" />
    <label for="form_surnameedit">Příjmení:</label>
    <input class="text ui-widget-content ui-corner-all" id="form_surnameedit" />
      
      
     <label for="groupedit">Skupina:</label>  
  <select id="form_groupedit" class="ui-widget-content ui-corner-all">
   <?php 
       
       
       
        while($row = $groupresult->fetchArray()){
        echo "<option value ='",$row['ID'],"'>",$row['Nazev'],"</option>";
      
       }
       ?>
    </select>  
         
     <label for="form_rightedit">Oprávnění:</label>  
  <select id="form_rightedit" class="ui-widget-content ui-corner-all">
      <option value ="U">Uživatel</option>
      <?php if($_SESSION['rights'] == "A"){
      echo "<option value =\"G\">Skupinový administrátor</option>\n<option value =\"A\">Hlavní administrátor</option>"; 
      } ?>
     
      
    <?php 
       
   //     while($row = $result->fetchArray()){
   //   echo "<option value ='",$row['Location'],"'>",$row['Name'],"</option>";
      
   //    }
       ?>
    </select>  
    </fieldset> 
    <fieldset><legend>Doplňková identifikace</legend>
    <label for="form_rfidedit">RFID karta:</label>
    <input class="text ui-widget-content ui-corner-all" id="form_rfidedit" placeholder="Není uveden" />
    <label for="form_pinedit">Pin:</label>
    <input class="text ui-widget-content ui-corner-all" id="form_pinedit" type="password" pattern="[0-9]{4}" title="Čtyřmístný číselný PIN" placeholder="Zobrazení stávajícího potlačeno" />
   <input type="hidden" id="form_row" value="" />
   <input type="hidden" id="form_person_id" value="" />
   </fieldset>
   
</div>


 <script type="text/javascript">

      $( "input[type=submit]" )
      .button()
      .click(function( event ) {
        event.preventDefault();
        var name = $("#nameedit").val();
        var surname = $("#surnameedit").val();
        var groupname = $("#groupedit" ).find('option:selected').text();
        var group = $("#groupedit" ).find('option:selected').val();
        var rfid =  $("#rfidedit").val();
        var pin =  $("#pinedit").val();
        var newrow =  '<td>'+surname+'</td><td>'+name+'</td><td>'+groupname+'</td><td>U</td><td>'+rfid+'</td><td></td><td class="edit"></td></tr>';
        
  
        
        $.get("users.php",{A: "Add" , Name: name, Surname: surname, Group: group, Rfid: rfid, Pin: pin}, 
        function(data){
       newrow = "<tr><td>" + data + "</td>" + newrow; 
        var lastrow =  $('.main_content table').append(newrow).children(':last').children(':last');          
          
           $(".main_content").animate({ scrollTop: $(document).height()}, 900, function(){
                 lastrow.effect("highlight", {color: 'green'}, 3000);
        
            });
          });
        });
        
    
 
       $( "#edit-forma" ).dialog({
      autoOpen: false,
      height: 470,
      width: 350,
      modal: true,
      buttons: {
        "Uložit změny": function() {
        var row = $("#form_row").val();
         $.get("users.php",{A: "Edit",Id: $("#form_person_id" ).val() , Name: $("#form_nameedit" ).val(), Surname: $("#form_surnameedit" ).val(), Group: $("#form_groupedit" ).find('option:selected').val(), Rights: $("#form_rightedit" ).find('option:selected').val(), Rfid: $("#form_rfidedit" ).val(),Pin: $("#form_pinedit" ).val()}, 
        function(data){          
        
        $(".main_content tr:eq("+row+")").find("td:eq(1)").html($("#form_surnameedit" ).val());
        $(".main_content tr:eq("+row+")").find("td:eq(2)").html($("#form_nameedit" ).val());
        $(".main_content tr:eq("+row+")").find("td:eq(3)").html($("#form_groupedit" ).find('option:selected').text());
        $(".main_content tr:eq("+row+")").find("td:eq(4)").html($("#form_rightedit" ).find('option:selected').val());
        $(".main_content tr:eq("+row+")").find("td:eq(5)").html($("#form_rfidedit" ).val());
          
          });
        
        $( this ).dialog( "close" );   
        },
        "Smazat uživatele": function() {
        var row = $("#form_row").val();  
        var rowa = $(".main_content tr:eq("+row+")");
        $.get("users.php",{A: "Remove",Id: $("#form_person_id" ).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 id = $(row).find("td:first").html();
        var name = $(row).find("td:eq(2)").html();
        var surname = $(row).find("td:eq(1)").html();
        var groupname =  $(row).find("td:eq(3)").html(); 
        var rowindex = $(this).parent().parent().children().index($(this).parent());
        var rfid = $(row).find("td:eq(5)").html();
        var rights = $(row).find("td:eq(4)").html();
        $("#form_person_id").val(id); 
        $("#form_nameedit").val(name);
        $("#form_row").val(rowindex);
       $("#form_surnameedit").val(surname);
       $("#form_rfidedit").val(rfid);
       $("#form_groupedit option").filter(function() {
      return $(this).text() == groupname; 
      }).prop('selected', true);
       $("#form_rightedit option").filter(function() {
      return $(this).val() == rights; 
      }).prop('selected', true);
       
       $( "#edit-forma" ).dialog( "open" );
    }); 
    
    

  </script>


                                     