<?php
error_reporting(0);
$path="../../";
include("../../map.php");
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
	$data = json_decode(file_get_contents('php://input'), true);
	function deleteJsonFiles(string $directory): void {
		$files = glob("$directory/*.json");		
		foreach ($files as $file) { unlink($file); }
		$subDirs = glob("$directory/*", GLOB_ONLYDIR);
		foreach ($subDirs as $subDir) {
			deleteJsonFiles($subDir);
		}
	}
	
	if (!empty($data['modules']) || !empty($data['folders'])) {
		if ($conn->connect_error) { die("Bağlantı hatası: " . $conn->connect_error); }
		if (!empty($data['modules'])) {
			$modules = $data['modules'];
			$queries = [
				"sayfa" => "DELETE FROM " . $_SESSION["lang"] . " WHERE ne='sayfa'",
				"haber" => "DELETE FROM " . $_SESSION["lang"] . " WHERE ne='haber'",
				"kayit" => "DELETE FROM " . $_SESSION["lang"] . " WHERE ne='kayit'",
				"uye" => "DELETE FROM t5",
				"hizmet" => "DELETE FROM t4 WHERE ne = 'hizmet'",
				"cari" => "DELETE FROM t4 WHERE ne = 'cari'",
				"eticaret" => "DELETE FROM eticaret",
				"service" => "DELETE FROM t4 WHERE ne = 'service'",
				"relations" => "DELETE FROM t1 WHERE ne = 'relations'",
				"request" => "DELETE FROM t2 WHERE ne = 'request'",
				"fmesaj" => "DELETE FROM t2 WHERE ne = 'fmesaj'",
				"not" => "DELETE FROM genel WHERE ne = 'not'",
				"xuye" => "DELETE FROM t1 WHERE t1 = 'uye'"
			];
			foreach ($modules as $module) {
				if (isset($queries[$module])) {
					$conn->query($queries[$module]);
				}
			}
		}
		if (!empty($data['folders'])) {
			$folders = $data['folders'];
			foreach ($folders as $folder) {
				if($folder=="json"){
					deleteJsonFiles('../../../deppo');
				}else{
					$path = "../../../deppo/$folder";
					if (is_dir($path)) {
						array_map('unlink', glob("$path/*"));
					}
				}
			}
		}
		
		echo "Temizleme işlemi tamamlandı.";
		$conn->close();
		exit;
	}
} else {
	include("../../scripts.php");
	?>
<style>
	.module-item {position.relative;}
	.module-item span{margin-top: 5px; position: absolute; margin-left: 15px;}
	.list-group-item strong{margin-top: 5px; position: absolute; margin-left: 15px;}
</style>
	<div class="container mt-4">
		<ul class="nav nav-tabs" id="cleanTab" role="tablist">
			<li class="nav-item active">
				<a class="nav-link active" id="veri-tab" data-toggle="tab" href="#veri" role="tab"><i class="fas fa-database"></i> Veri Temizle</a>
			</li>
			<li class="nav-item">
				<a class="nav-link" id="icerik-tab" data-toggle="tab" href="#icerik" role="tab"><i class="fas fa-photo-video"></i> İçerik Temizle</a>
			</li>
		</ul>
		<div class="tab-content mt-3">
			<div class="tab-pane fade active in" id="veri" role="tabpanel">
				<div class="card shadow-lg">
					<div class="card-header bg-danger text-white" style="padding:15px;">
						<h5 class="mb-0">Veri Temizle</h5>
					</div>
					<div class="card-body">
						<form id="deleteForm">
							<ul class="list-group">
								<li class="list-group-item font-weight-bold bg-light">
									<input type="checkbox" id="selectAllModules"> <strong>Tümünü Seç</strong>
								</li>
								<?php
									$modules = [ "sayfa" => "Sayfalar", "haber" => "Haberler", "kayit" => "Kayitlar", "uye" => "Üyeler", "hizmet" => "Hesap Planları", "cari" => "Cari Kartlar", "eticaret" => "Eticaret Sepet,Sipariş", "service" => "Teklifler", "relations" => "Müşteri İlişkileri", "request" => "Talepler", "fmesaj" => "Form Mesajları", "not" => "Notlar", "xuye" => "Üye Extra", ];
									foreach ($modules as $key => $label) { echo "<li class='list-group-item module-item'><input type='checkbox' name='modules' value='$key'> <span>$label</span></li>"; }
								?>
							</ul>
							<div class="text-center mt-3">
								<button type="submit" class="btn btn-danger w-100">Seçili Verileri Temizle</button>
							</div>
						</form>
					</div>
				</div>
			</div>
			<div class="tab-pane fade" id="icerik" role="tabpanel">
				<div class="card shadow-lg">
					<div class="card-header bg-warning text-white" style="padding:15px;">
						<h5 class="mb-0">İçerik Temizle</h5>
					</div>
					<div class="card-body">
						<form id="deleteContentForm">
							<ul class="list-group">
								<?php
									$folders = ["images" => "Resimler", "dosya" => "Dosyalar", "video" => "Video Dosyaları", "excel" => "Excel Dosyaları", "kategori" => "Kategori", "rek-lam" => "Reklamlar", "slide" => "Slidelar", "drive" => "Drive Dosyaları", "json" => "Json Dosyaları"];
									foreach ($folders as $key => $label) { echo "<li class='list-group-item module-item'><input type='checkbox' name='modules' value='$key'> <span>$label</span></li>"; }
								?>
							</ul>
							<div class="text-center mt-3">
								<button type="submit" class="btn btn-warning w-100">Seçili İçerikleri Temizle</button>
							</div>
						</form>
					</div>
				</div>
			</div>
		</div>
	</div>
	<script>
		document.getElementById("deleteContentForm").addEventListener("submit", function(event) {
			event.preventDefault();
			let selectedFolders = [];
			document.querySelectorAll("input[name='folders']:checked").forEach(el => {
				selectedFolders.push(el.value);
			});
			if (selectedFolders.length === 0) {
				alert("Lütfen en az bir içerik türü seçin!");
				return;
			}
			if (!confirm("Seçili içerik klasörleri temizlenecek! Emin misiniz?")) {
				return; }
				fetch("", {
					method: "POST",
					headers: { "Content-Type": "application/json" },
					body: JSON.stringify({ folders: selectedFolders })
				})
				.then(response => response.text())
				.then(data => alert(data));
			});
		</script>
	<?php } ?>
	<?php echo $footer;?>