<?php 
$setList = $connection->query("SELECT * FROM comments_sets ORDER BY set_name ASC");
$sets = $setList->fetchAll();

$languages = $connection->query("SELECT DISTINCT id,name FROM languages");
$languageData = $languages->fetchAll(PDO::FETCH_KEY_PAIR);

$setLanguage = isset($languageData[0]['name']) ? $languageData[0]['name'] : "N.D.";
?>
<link href="assets/plugins/custom/datatables/datatables.bundle.css" rel="stylesheet" type="text/css" />
<div class="kt-container  kt-container--fluid  kt-grid__item kt-grid__item--fluid">
							<div class="kt-portlet kt-portlet--mobile">
								<div class="kt-portlet__head kt-portlet__head--lg">
									<div class="kt-portlet__head-label">
										<span class="kt-portlet__head-icon">
											<i class="kt-font-brand flaticon2-line-chart"></i>
										</span>
										<h3 class="kt-portlet__head-title">
											Lista set di commenti
										</h3>
									</div>
									
								</div>
								<div class="kt-portlet__body">

									<!--begin: Datatable -->
									<table class="table table-striped- table-bordered table-hover table-checkable" id="kt_table_1">
										<thead>
											<tr>
												<th>Nome set</th>
												<th>Lingua</th>
												<th>Commenti</th>
												<th></th>
												<th></th>
											</tr>
										</thead>
										<tbody>
											
												<?php 
												for($c = 0; $c < count($sets); $c++)
												{
													$id = $sets[$c]['id'];
													$langId = $sets[$c]['set_language'];
													$setName = $sets[$c]['set_name'];

													$qNumber = $connection->query("SELECT * FROM comments WHERE sets_in = '$id'");
													$qCount = $qNumber->fetchAll();
													
												?>
											<tr>
											<td><?php echo $setName ?></td>	
											<td><?php echo $languageData[$langId]; ?></td>	
											<td><?php echo count($qCount); ?></td>	
											<td><a href="?p=12&id=<?php echo $id; ?>"><button type="button" class="btn btn-info btn-custom">Modifica</button></a></td>
											<td><button type="button" class="btn btn-danger btn-custom elimina" data-id="<?php echo $id ?>">Elimina</button></a></td>
											</tr>
												<?php	
												}
												?>
												
											</tr>

										</tbody>
									</table>

									<!--end: Datatable -->
								</div>
							</div>
						</div>
<script>
$(document).ready(function() {	

$('.elimina').click(function(e) {
	var idSet = $(this).data("id");
	var riga = $(this).parents('tr'); 
            swal.fire({
                title: 'Sei sicuro?',
                text: "Non potrai più ripristinare il set di domande!",
                type: 'warning',
                showCancelButton: true,
                confirmButtonText: 'Sì, eliminalo!'
            }).then(function(result) {
            	console.log(idSet);
                if (result.value) {
                	console.log("Confermato");
                	$.post("be_services/delete.php", {
            values: {"what" : "commentSet","who" : idSet}
        }, function(res) {
            if (res.success)
            {
            	table.row( riga ).remove().draw();
            	 swal.fire(
                        'Fatto!',
                        'Set di commenti eliminato con successo',
                        'success'
                    )
            }
                else {
                    // eliminazione fallita per qualche ragione
                } 
        }, "json")

                   
                }
            });
        });

var table = $('#kt_table_1').DataTable();
})
</script>						