<?php 
// VERSIONE PER PRE-LEAD (SELEZIONARE CAMPAGNA DI CUI SI VOGLIONO VEDERE I DETTAGLI)
if( $_SESSION['id_user'] == 5){
    $whereCond=" WHERE id=50 or id=51 or id=57";
    
}else{ $whereCond="";}

$campaignList = $connection->query("SELECT * FROM campaigns $whereCond 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>Dettagli</th>
											</tr>
										</thead>
										<tbody>
											
												<?php 
												for($c = 0; $c < count($campaigns); $c++)
												{
													$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;
													}

												?>
											<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><a href="?p=15&id=<?php echo $id; ?>"><button type="button" class="btn btn-info btn-custom">Lead raccolte</button></a></td>	
											
											</tr>
												<?php	
												}
												?>
												
											</tr>

										</tbody>
									</table>

									<!--end: Datatable -->
								</div>
							</div>
						</div>