<?php 
if($_SESSION['id_user']==5){
    $whereCondi="where id=50 or id=51";
    $campaignList = $connection->query("SELECT * FROM campaigns $whereCondi ORDER BY campaign_creation DESC");
}else{
    $campaignList = $connection->query("SELECT * FROM campaigns ORDER BY campaign_creation DESC");
}
$campaigns = $campaignList->fetchAll();

?>
<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 campagne
										</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 campagna</th>
												<th>Cliente</th>
												<th>Inizio campagna</th>
												<th>Fine campagna</th>
												<th>Lead raccolti</th>
												<th>Stato campagna</th>
												<th>Visualizza landing</th>
												<th></th>
												<th></th>
											</tr>
										</thead>
										<tbody>
											
												<?php 
												
												for($c = 0; $c < count($campaigns); $c++)
												{
												    echo $campaigns[$c]['campaign_owner'];
													$id = $campaigns[$c]['id'];
													$ownerId = $campaigns[$c]['campaign_owner'];
													$meta = $connection->query("SELECT meta_key,meta_value FROM campaigns_meta WHERE campaign_id = '$id'");
													$campaignMeta = $meta->fetchAll(PDO::FETCH_KEY_PAIR);

													$customer = $connection->query("SELECT name FROM customers WHERE id = '$ownerId'");
													$customerData = $customer->fetchAll();
													$campaignCustomer = isset($customerData[0]['name']) ? $customerData[0]['name'] : "N.D.";

													$tabellaLeads = "campaign" . $id . "_leads";
													
													$leadCount = $connection->query("select count(*) from $tabellaLeads"); 
													if($leadCount)
													{
														$leadCount = $leadCount->fetchColumn();
													}else
													{
														$leadCount = 0;
													}

													if($campaignMeta['start_ts'] < time() && $campaignMeta['end_ts'] > time())
													{
														$attiva = "Attiva";	
													}
													else if($campaignMeta['start_ts'] > time())
													{
														$attiva = "Non ancora iniziata";
													}
													else
													{
														$attiva = "Conclusa";
													}
													


													$cFolder = $campaignMeta['campaign_folder'];

												?>
											<tr>
											<td><?php echo $campaigns[$c]['campaign_name']; ?></td>	
											<td><?php echo $campaignCustomer; ?></td>	
											<td><?php echo $campaignMeta['start']; ?></td>	
											<td><?php echo $campaignMeta['end']; ?></td>	
											<td><?php echo $leadCount; ?> </td>	
											<td><?php echo $attiva; ?></td>	
											<td><a href="../<?php echo $cFolder; ?>/" target="_blank">Apri landing</a></td>
											<td><a href="?p=3&id=<?php echo $id; ?>">Modifica</a></td>
											<td><button type="button" class="btn btn-danger btn-custom elimina" data-id="<?php echo $id ?>" data-folder="<?php echo $cFolder ?>">Elimina</button></td>
											</tr>
												<?php	
												}
												?>
												
											</tr>

										</tbody>
									</table>

									<!--end: Datatable -->
								</div>
							</div>
						</div>
<script type="text/javascript">
$(document).ready(function() {	

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

                   
                }
            });
        });
var table = $('#kt_table_1').DataTable();

})
</script>		
				