<div class="inner-wrapper">
	<?php
	$sayfa = $_GET['sayfa'];
	if($sayfa == "") {$sayfa = 0;}else{$sayfa = ($sayfa-1) * 10;}
	$sql = mysqli_query($conn, "select * from genel where ne='reklam' $lads order by id desc limit $sayfa,99"); 
	$sql2 = mysqli_query($conn, "select * from genel where ne='reklam' $lads");$rsay=mysqli_num_rows($sql2);
	$dir = '../deppo/reports/';
	$month = (isset($_GET['m']) && preg_match('/^\d{4}_\d{2}$/', $_GET['m'])) ? $_GET['m'] : date('Y_m');

	$file = $dir . 'ads_report_' . $month . '.json';
	$files = glob($dir . 'ads_report_*.json');
	$months = [];

	foreach ($files as $f) {
		if (preg_match('/ads_report_(\d{4}_\d{2})\.json$/', $f, $m)) { $months[] = $m[1]; }
	}

	$months = array_unique($months);
	rsort($months);
	?>
	<section role="main" class="content-body">
		<header class="page-header">
			<h2><?php echo $reklamlar; //Reklam Yönetimi?></h2>
			
			<div class="right-wrapper text-right">
				<ol class="breadcrumbs">
					<li>
						<a href=".">
							<i class="fa fa-home"></i>
						</a>
					</li>
					<li><span>Crm +Plus</span></li>
					<li><span><?php echo $reklamlar; //Reklam Yönetimi?></span></li>
				</ol>
				
				<a class="sidebar-right-toggle" data-open="sidebar-right"><i class="fa fa-chevron-left"></i></a>
			</div>
		</header>
		<?php if($_GET["durum"]=="ok"){?>
			<div class="alert alert-success">
				<?php echo $sdil[212]; //Islem Basariyla Gerçeklestirilmistir !?>
			</div>
		<?php }?>
		
		<div class="row">
			<div class="col">
				<section class="card">
					<header class="card-header v3-bg-info">
						<div class="card-actions" id="v-<?php echo $_GET['islem'];?>">
								<select name="m" onchange="self.location.href=this.options[this.selectedIndex].value;"  style="">
									<?php foreach ($months as $m): $ts = strtotime(str_replace('_', '-', $m) . '-01');?>
										<option value="?ne=<?php echo $_GET['ne'];?>&islem=<?php echo $_GET['islem'];?>&id=<?php echo $_GET['id'];?>&m=<?php echo $m; ?>" <?php echo ($m == $month ? 'selected' : ''); ?>>
											<?php echo date('F Y', $ts); ?>
										</option>
									<?php endforeach; ?>
								</select>
								<?php if (file_exists($file)): ?>
									<a href="<?php echo $file;?>" class="mb-1 mt-1 btn btn-default fr" style="margin-left:5px;" title="raporu indir" download>
										<i class="fa fa-download"></i>
									</a>
								<?php endif; ?>
<!--
								<a href="#" class="card-action card-action-dismiss" data-card-dismiss=""></a>
							-->
						</div>
						
						<h2 class="card-title"><?php echo $reklamlar; //Reklam Yönetimi?></h2>
					</header>
					<div class="card-body" style="">
						<table class="v3-table" style="">
							<thead>
								<tr class="mdn">
									<th class="w6" style="text-align:center;">ID</span></th>
									<th><?php echo $reklam[0]; //reklam?></th>
									<th class="w10"><?php echo $sdil[113]; //Türü?></th>
									<th class="w10"><?php echo $sdil[115]; //Yayın Tarihi?></th>
									<th class="w15"><?php echo $sdil[73]; //Konum?></th>
									<th class="w5">Hit</th>
									<th class="w5"><?php echo $sdil[213]; //İşlemler?></th>
								</tr>
							</thead>
							<tbody>
						<?php while($rs=mysqli_fetch_assoc($sql)){
								$totalClicks = 0;
								if ($rs["id"] && file_exists($file)) {
									$data = json_decode(file_get_contents($file), true);    
									if (isset($data[$rs["id"]])) {
										foreach ($data[$rs["id"]] as $date => $values) {
											$totalClicks += $values['total'];
										}
									}
								}
								if ($rs["s6"]==1 or $authority==0){
									$dauth=0;
								}else{
									$dauth=1;
								}
							?>
							<tr data-id="<?php echo $rs["id"];?>" class="<?php if($rs["t12"]<>"" and dt($rs["t12"])>5){?>alert alert-success<?php }elseif($rs["t12"]<>"" and dt($rs["t12"])<6 and dt($rs["t12"])>3){?>alert alert-warning<?php }elseif($rs["t12"]<>"" and dt($rs["t12"])<2){?>alert alert-danger<?php }?>">
								<th class="mdn cp v3-text-info mfrm" data-toggle="modal" data-width="90%" data-target="#myModal" data-url="modules/ads/ad.php?ne=<?php echo $_GET["ne"];?>&islem=dreklam&id=<?php echo $rs["id"];?>"><?php echo i2a($rs["id"],7);?></th>
								<td>
									<span title="<?php echo $rs["t1"];?>" class="with-tip"><i class="fas fa-power-off <?php if($rs["s13"]==1){echo "text-success";}else{echo "text-danger";}?>" aria-hidden="true"></i> <?php echo $rs["t1"];?></span>
								</td>
								<td class="mdn"><?php echo $rs["t3"];?></td>
								<td><?php echo dtarih($rs["ts1"],"gg ka yyyy");?><?php if($rs["t12"]<>""){?><br><?php echo $rs["t12"];?>(<?php //echo dl($rs["t12"]);?>)<?php }?></td>
								<td class="mdn">
									<?php
									$sqlc=mysqli_query($conn, "select * from kategoriler where Cat_ID=".$rs["s1"].""); $rsc=mysqli_fetch_assoc($sqlc); $jscx = json_decode($rsc["n1"],true);
									if(mysqli_num_rows($sqlc)>0) {
										echo "".$jscx[$_SESSION["lang"]]<>"" ? $jscx[$_SESSION["lang"]] : $rsc["Cat_".$_SESSION["lang"].""]." <span style='font-size:8px; color:#ccc;'>(".$rsc["Cat_ID"].")</span>";
									}elseif($rs["s1"]=="1000"){
										echo "Modal Ads <span style='font-size:8px; color:#ccc;'>(".$rs["s1"].")</span>";
									}elseif($rs["s1"]=="1001"){
										echo "Top Ads <span style='font-size:8px; color:#ccc;'>(".$rs["s1"].")</span>";
									}elseif($rs["s1"]=="1002"){
										echo "App Login Ads <span style='font-size:8px; color:#ccc;'>(".$rs["s1"].")</span>";
									}elseif($rs["s1"]=="1003"){
										echo "Login Ads <span style='font-size:8px; color:#ccc;'>(".$rs["s1"].")</span>";
									}else{
										echo $sdil[217]." <span style='font-size:8px; color:#ccc;'>(".$rs["s1"].")</span>";
									}
									?>
				                </td>
								<td class="mdn"><?php echo nice_number($totalClicks);?></td>
				                <td class="table-actions" style="text-align: right;">
				                	<div class="input-group-btn">
										<a class="btn btn-info mr2 mfrm" data-toggle="modal" data-width="90%" data-target="#myModal" data-url="modules/ads/ad.php?ne=<?php echo $_GET["ne"];?>&islem=dreklam&id=<?php echo $rs["id"];?>" title="<?php echo $sdil[199]; //Düzenle?>"><i class="fa fa-pencil"></i></a>
										<?php if ($dauth==0){?>
											<a title="<?php echo $sdil[216]; //Yetkili Değilsiniz ??>" class="btn btn-danger btn-ban-row" href="#"><i class="fa fa-lock" aria-hidden="true"></i></a>
										<?php }else{ ?>
											<a title="<?php echo $sdil[204]; //Kaldir?>" class="btn btn-danger btn-delete-row" data-href="?ne=<?php echo $_GET["ne"];?>&islem=<?php echo $_GET["islem"];?>&id=<?php echo $rs["id"];?>&durum=sreklam"><i class="fa fa-times" aria-hidden="true"></i></a>
										<?php } ?>
				                	</div>
				                </td>
				            </tr>
				        <?php }?>
				    </tbody>
				</table>
			</form>
		</div>
	</section>
</div>
</div>
<!-- end: page -->
</section>
</div>
<aside id="sidebar-right" class="sidebar-right">
	<div class="nano">
		<div class="nano-content">
			<a href="#" class="mobile-close d-md-none">
				Collapse <i class="fa fa-chevron-right"></i>
			</a>
			<div class="sidebar-right-wrapper">
				...
			</div>
		</div>
	</div>
</aside>
</section>
<?php
if($_GET["durum"] == "yreklam"){
	$rt1="".trim($sdil[200])." ".trim($nedir)."";
	$sql = $conn->query("insert into genel (ne,t1,t3,ts1,s12,s13,s19) values ('reklam', '".$rt1."', 'image', now(), '0', '1', '".$_SESSION["user_id"]."')");
	$id=$conn->insert_id;
	echo "<script>jQuery(document).ready(function () { jQuery('#myModal').css('width','90%'); jQuery('#myModal').find('iframe').attr('src','modules/ads/ad.php?ne=".$_GET["ne"]."&islem=d".$_GET["islem"]."&id=".$id."'); jQuery('#myModal').modal('show');});</script>";

}elseif($_GET["durum"] == "sreklam"){
	$sqlr=mysqli_query($conn, "select * from genel where ne='reklam' and id=".$_GET["id"].""); $rsr=mysqli_fetch_assoc($sqlr);
	$dosya1="../deppo/reklam/".$rsr["t6"].""; unlink($dosya1);
	$aql = mysqli_query($conn, "delete from genel where ne='reklam' and id=".$_GET["id"]."");	
	echo location("?ne=".$_GET["ne"]."&islem=".$_GET["islem"]."&durum=ok",self,500,1);
}
?>