<?php /* Template name: tabella mysql*/
get_header();
?>
<html>
<head>
    <title>Table Style</title>
    <meta charset="utf-8" />
    <meta name="viewport" content="initial-scale=1.0; maximum-scale=1.0; width=device-width;">
    <link rel="stylesheet" type="text/css" href="tab.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
</head>
<body>
<div class="contenitore_tabelle">
<div    class="tab_pres_responsive">
    <form action='' method='POST' > <!-- IL FORM SARÀ TUTTA LA TABELLA -->
        <table class="table-fill" class="scrollable">
            <thead>
            <tr>
                <th class="intest1" colspan="4">DATI</th>
                <th class="intest1" colspan="2">REGISTRO</th>
                <th class="intest" rowspan="2">
                    <button  class="bottone" name="bottone" title="conferma presenze" type="submit">
                        <img src="http://127.0.0.1/wordpress/wp-content/uploads/2016/07/tick-305245_640.png" width="30px" height="30px" alt="conferma">
                    </button>
               </th>
            </tr>
                <tr>
                    <th class="intest">ID</th>
                    <th class="intest">NOME</th>
                    <th class="intest">COGNOME</th>
                    <th class="intest">INFO</th>
                    <th class="intest">PRESENZA</th>
                    <th class="intest">ASSENZA</th>
                <span class="show">
                           <!--  <th class="int more hidden">DATA DI NASCITA</th>
                            <th class="int more hidden">CODICE FISCALE</th>
                            <th class="int more hidden">LUOGO DI NASCITA</th> -->
                            </span>
            </tr>
            </thead>
            <tbody  class="table-hover">
            <?php
            include('conn_selez_db.php');
            $query = "SELECT * FROM persone_inserite ORDER BY id";
            $result = mysqli_query($conn,$query) or die(mysqli_error($conn));
            while($row = mysqli_fetch_array($result)) {
                echo "
                                <tr class='tabr'>
                                    <td class='tab'>" . $row['0'] . "</td>
                                    <td class='tab'>" . $row['1'] . "</td>
                                    <td class='tab'>" . $row['2'] . "</td>
                                    
                                    <td class='tab'>
                                    <span class='main" . $row['0'] . " info'>
                                    <img id='info' src='http://127.0.0.1/wordpress/wp-content/uploads/2016/09/info.png' alt='info' title='info'>
                                    </span>
                                    </td>
                                    
                                    <td class='tab'>
                                        <input class='radio' id='ass$row[0]' type='radio'  name='registro[$row[0]]' value='assente'>
                                        <label for='ass$row[0]'><strong>Assente</strong></label>
                                    </td>
                                      <td class='tab'>
                                        <input class='radio' id='pre$row[0]' type='radio'  name='registro[$row[0]]' value='presente'>
                                        <label for='pre$row[0]'><strong>Presente</strong></label>
                                    </td>  
                                    <td class='tab' id='tab_bottoni'>
                                        <button  class='bottone' name='bottone' title='conferma presenza' type='submit'>
                                            <img src='http://127.0.0.1/wordpress/wp-content/uploads/2016/07/tick-305245_640.png' width='25px'
                                             height='30px' alt='conferma'>
                                             </button>
                                         <button class='bottone' name='elimina[$row[0]]' 
                                         title='elmina utente' type='submit'>
                                         <img src='http://127.0.0.1/wordpress/wp-content/uploads/2016/07/closeHover.png' width='25px'  alt='elimina'>
                                         </button>
                                        
                                    </td>
                                    
                                </tr>
                                    
                                    <td class='more" . $row['0'] . " hidden' colspan='2'><strong>Data di nascita:</strong></br>" . $row['3'] . "</td>
                                    <td class='more" . $row['0'] . " hidden' colspan='3'><strong>CF:</strong></br>" . $row['4'] . "</td>
                                    <td class='more" . $row['0'] . " hidden' colspan='2'><strong>Luogo di nascita:</strong></br>" . $row['5'] . "</td>
                                   
                            
                                    
                               
                       
           <script type='text/javascript'>
                    $('.main" . $row['0'] . "').click(function() {
                    $('.more" . $row['0'] . "').toggleClass('hidden');
                    $('.more').removeClass('hidden');
                    });
            </script>
                                ";
                // il nOme di ogni input sarà un array che avrà come chiavi
                // gli id degli utenti e come valori le rispettive presenze
            }
            if(isset($_POST['bottone']) && isset($_POST["registro"])) {
                $registro = $_POST["registro"]; // recupero l'array registro che contiente id e presenza degli utenti
                var_dump($registro);
                foreach ($registro as $id => $presenza) {// per ogni coppia chiave->valore all'interno dell'array $registro
                    $insert = "INSERT INTO presenze (ID,PRESENZA_ASSENZA) VALUES ($id,'$presenza');"; //definisco la query
                    /*
                    N.B.: all'interno della query $id non è racchiuso tra apici
                    poiché nella mia tabella è definito come intero.
                    */
                    if(!mysqli_query($conn,$insert)) { // se la query di inserimento fallisce...
                        echo "Errore".mysqli_error($conn)."<br>"; // e stampo il relativo errore mysql
                    }
                }
                echo  "<script type='text/javascript'>alert('Presenza/assenza registrata')</script>";
            }
            if(isset($_POST['elimina']))
            {                                       //----------problema recupero id
                $elimina= $_POST["elimina"];
                var_dump($elimina);
                    $delete = "DELETE FROM persone_inserite WHERE ID=".$elimina;
                    if (!mysqli_query($conn, $delete)) {
                        echo "<script type='text/javascript'> alert('Errore durante l eliminazione')";
                    }
            }
            ?>
            </tbody>
        </table>
    </form>
<div class="el_pres_resp" id="scroll1">
    <table class="table-fill" class="scrollable">
       <tr>
           <th class="int">ID</th>
           <th class="int">PRESENTE/ASSENTE</th>
           <th class="int" >DATA</th>
           <th class="int"> </th>
       </tr>
        <?php
        include('conn_selez_db.php');
        $query = "SELECT * FROM presenze ORDER BY DATA";
        $result = mysqli_query($conn,$query) or die(mysqli_error($conn));
        while($row = mysqli_fetch_array($result))
        {
            echo " 
<tbody class='table-hover'>
    <tr class='tabr'>
        <td class='tab' id='riga$row[0]'>".$row['0']."</td>
        <td class='tab'>".$row['1']."</td>
        <td class='tab'>".$row['2']."</td>
        <td class='tab'> </td>
</tbody>";
        }
        echo"
        </table>
        </div>
</div>
</body>
</html>";
get_footer();
?>