<style>
    button {height:36px}
    .fa {width:20px}
</style>
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<!-- FontAwesome -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@6.5.1/css/all.min.css">
<?php
error_reporting(0);
function tamamla($sayi, $uzunluk) {
    return str_pad($sayi, $uzunluk, "0", STR_PAD_LEFT);
}
// Türkçe dil ayarını belirle
setlocale(LC_COLLATE, 'tr_TR.UTF-8');
$locationjson = file_get_contents($_SERVER['DOCUMENT_ROOT'].'/deppo/tools/jsons/locations.json');
$locations = json_decode($locationjson, true);
if($_GET["ne"]=="" or $_GET["ne"]=="ulke"){
    $list="<li><a href=\"?\">Ülkeler</a></li>";
}elseif($_GET["ne"]=="il"){
    $list="<li><a href=\"?\">Ülkeler</a></li> <li><a href=\"?ne=il&id=".$_GET["id"]."&s1=".$_GET["s1"]."&s2=".$_GET["s2"]."&s3=".$_GET["s3"]."\">Şehirler</a></li>";
}elseif($_GET["ne"]=="ilce"){
    $list="<li><a href=\"?\">Ülkeler</a></li> <li><a href=\"?ne=il&id=".$_GET["id"]."&s1=".$_GET["s1"]."&s2=".$_GET["s2"]."&s3=".$_GET["s3"]."\">Şehirler</a></li> <li><a href=\"?ne=ilce&id=".$_GET["id"]."&s1=".$_GET["s1"]."&s2=".$_GET["s2"]."&s3=".$_GET["s3"]."\">İlçeler</a></li>";
}elseif($_GET["ne"]=="semt"){
    $list="<li><a href=\"?\">Ülkeler</a></li> <li><a href=\"?ne=il&id=".$_GET["id"]."&s1=".$_GET["s1"]."&s2=".$_GET["s2"]."&s3=".$_GET["s3"]."\">Şehirler</a></li> <li><a href=\"?ne=ilce&id=".$_GET["id"]."&s1=".$_GET["s1"]."&s2=".$_GET["s2"]."&s3=".$_GET["s3"]."\">İlçeler</a></li> <li><a href=\"?ne=semt&id=".$_GET["id"]."&s1=".$_GET["s1"]."&s2=".$_GET["s2"]."&s3=".$_GET["s3"]."\">Mahalleler</a></li>";
}
// Türkçe karakterleri doğru sıralamak için özel dizi
$turkish_char_map = [
    'Ç' => 'Cz', 'Ğ' => 'Gz', 'İ' => 'Iz', 'Ö' => 'Oz', 'Ş' => 'Sz', 'Ü' => 'Uz',
    'ç' => 'cz', 'ğ' => 'gz', 'ı' => 'iz', 'i' => 'iz', 'ö' => 'oz', 'ş' => 'sz', 'ü' => 'uz'
];
// Karşılaştırma fonksiyonu
function turkish_sort($a, $b) {
    global $turkish_char_map;
    // Türkçe harfleri İngilizce harf eşlemeleriyle değiştirerek karşılaştırma yap
    $a_transformed = strtr($a['t1'], $turkish_char_map);
    $b_transformed = strtr($b['t1'], $turkish_char_map);
    return strcmp($a_transformed, $b_transformed);
}
// "t1" alanına göre özel Türkçe sıralama uygula
usort($locations, "turkish_sort");
$json = "".$_SERVER['DOCUMENT_ROOT']."/deppo/tools/jsons/locations.json";
if($_SERVER['HTTP_HOST']=="cloud.crmplus.com.tr"){
	$update="false";
}
if ($_GET["ne"] == "il"){ $gID=$_GET["s1"]; }elseif ($_GET["ne"] == "ilce"){ $gID=$_GET["s2"]; }elseif ($_GET["ne"] == "semt"){ $gID=$_GET["s3"]; }
?>
<?php if($_GET["islem"]==""){?>
    <div style="">
        <ol class="breadcrumb" style="position: fixed; width: 100%; z-index: 9;top: 0px;">
            <li><a href="?"><i class="fa fa-home"></i></a></li>
            <?php echo $list?>
            <li style="float:right"><a href="https://cdn.crmplus.com.tr/konum/api" target="_blank"><i class="fa fa-search"></i></a></li>
            <li style="float:right"><a href="<?php if($update<>"false"){echo "?ne=".$_GET["ne"]."&islem=ulocation&s1=".$_GET["s1"]."&s2=".$_GET["s2"]."&s3=".$_GET["s3"]."&id=".$gID."";}else{echo "#";}?>" <?php if($update=="false"){echo "style=\"cursor: not-allowed;\"";}?>><i class="fa fa-download"></i></a></li>
            <li style="float:right"><a href="<?php echo "?ne=".$_GET["ne"]."&islem=clocation&s1=".$_GET["s1"]."&s2=".$_GET["s2"]."&s3=".$_GET["s3"]."&id=".$gID."";?>"><i class="fa fa-trash"></i></a></li>
            <input type="text" class="form-control input-sm w20" id="filtre" style="position: absolute; width: 20%; right: 140px; top: 3px;" name="q" placeholder="Key" autocomplete="off">
        </ol>
        <table style="margin-top: 40px;" class="table filterable">
            <?php if($_GET["ne"]=="" or $_GET["ne"]=="ulke"){?>
                <tr>
                    <td>
                        <form class="input-group" action="?ne=ulke&islem=yekle" method="post" style="margin:0px; padding:0px;">
                            <span class="input-group-addon" style="padding: 0px 5px; background-color: transparent; border: 0px solid #ccc;">
                                <button class="btn btn-default" type="button" onclick="toggle('#ijson')">..........</button>
                            </span>
                            <div class="col-xs-6">
                                <input type="text" class="form-control" placeholder="Yeni Ülke Ekle" name="t1">
                            </div>
                            <div class="col-xs-2">
                                <input type="text" class="form-control" placeholder="TR" name="t2">
                            </div>
                            <div class="col-xs-2">
                                <input type="text" class="form-control" placeholder="TUR" name="t3">
                            </div>
                            <div class="col-xs-2">
                                <input type="text" class="form-control" placeholder="90" name="t4">
                            </div>
                            <input type="hidden" value="0" name="s1">
                            <input type="hidden" value="0" name="s2">
                            <input type="hidden" value="0" name="s3">
                            <span class="input-group-addon" style="padding: 0px 5px; background-color: transparent; border: 0px solid #ccc;">
                                <button class="btn btn-default" type="submit"><i class="fa fa-plus"></i></button>
                                <button class="btn btn-default" type="button" title=""><i class="fa fa-ellipsis-h"></i></button>
                                <button class="btn btn-default" type="button" title=""><i class="fa fa-ellipsis-h"></i></button>
                            </span>
                        </form>
                    </td>
                </tr>
                <?php
                foreach($locations as $array){
                    if($array["ne"]=="ulke"){
							$c = 0;
							foreach ($locations as $count) {
								if ($count["ne"] == "il" && $count["s1"] == $array["id"]){$c++; }
							}
                        ?>
                        <tr>
                            <td>
                                <form class="input-group" action="?ne=ulke&islem=duzenle&id=<?php echo $array["id"]?>" method="post" style="margin:0px; padding:0px;">  
                                    <span style="display:none;"><?php echo str_replace($searchVal, $replaceVal, $array["t1"]);?></span>
                                    <span class="input-group-addon" style="padding: 0px 5px; background-color: transparent; border: 0px solid #ccc;">
                                        <button class="btn btn-default" type="button" title=""><?php echo tamamla($array["id"],5);?></button>
                                    </span>
                                    <div class="col-xs-6">
                                        <input type="text" class="form-control" placeholder="Ülke" value="<?php echo str_replace($searchVal, $replaceVal, $array["t1"]);?>" name="t1">
                                    </div>
                                    <div class="col-xs-2">
                                        <input type="text" class="form-control" placeholder="TR" name="t2" value="<?php echo $array["t2"];?>">
                                    </div>
                                    <div class="col-xs-2">
                                        <input type="text" class="form-control" placeholder="TUR" name="t3" value="<?php echo $array["t3"];?>">
                                    </div>
                                    <div class="col-xs-2">
                                        <input type="text" class="form-control" placeholder="90" name="t4" value="<?php echo $array["t4"];?>">
                                    </div>
                                    <input type="hidden" name="KID[]" value="<?php echo $array["id"]?>"/>
                                    <input type="hidden" value="0" name="s1">
                                    <input type="hidden" value="0" name="s2">
                                    <input type="hidden" value="0" name="s3">
                                    <span class="input-group-addon" style="padding: 0px 5px; background-color: transparent; border: 0px solid #ccc; position: relative;">
                                        <button class="btn btn-default" type="button" onclick="location.href='?ne=il&id=<?php echo $array["id"];?>&s1=<?php echo $array["id"];?>&s2=<?php echo $array["s2"];?>&s3=<?php echo $array["s3"];?>'" title=""><i class="fa fa-tasks" style="margin-top:0px;"></i><div style="position:absolute; margin-top:-25px; font-size:11px; width:24px; text-align:right;"><?php echo $c;?></div></button>
                                        <button class="btn btn-default" type="submit" title="Kayıt Düzenle"><i class="fa fa-check"></i></button>
                                        <button class="btn btn-default" type="button" onclick="location.href='?ne=ulke&islem=sil&id=<?php echo $array["id"];?>&s1=<?php echo $array["s1"];?>&s2=<?php echo $array["s2"];?>&s3=<?php echo $array["s3"];?>'" title="Kayıt Sil"><i class="fa fa-trash"></i></button>
                                    </span>
                                </form> 
                            </td>
                        </tr>   
                    <?php }}?>
                <?php }elseif($_GET["ne"]=="il"){?>
                    <tr>
                        <td>
                            <form class="input-group" action="?ne=<?php echo $_GET["ne"];?>&islem=yekle" method="post" style="margin:0px; padding:0px;">
                                <span class="input-group-addon" style="padding: 0px 5px; background-color: transparent; border: 0px solid #ccc;">
                                    <button class="btn btn-default" type="button" onclick="toggle('#ijson')">..........</button>
                                </span>
                                <div class="col-xs-4">
                                    <input type="text" class="form-control" placeholder="Yeni Şehir Ekle" name="t1">
                                </div>
                                <div class="col-xs-3">
                                    <input type="text" class="form-control" placeholder="Plaka" name="t2">
                                </div>
                                <div class="col-xs-4">
                                    <input type="text" class="form-control" placeholder="Bölge" name="t3">
                                </div>
                                <input type="hidden" value="<?php echo $_GET["s1"];?>" name="s1">
                                <input type="hidden" value="0" name="s2">
                                <input type="hidden" value="0" name="s3">
                                <span class="input-group-addon" style="padding: 0px 5px; background-color: transparent; border: 0px solid #ccc;">
                                    <button class="btn btn-default" type="submit"><i class="fa fa-plus"></i></button>
                                    <button class="btn btn-default" type="button" title=""><i class="fa fa-ellipsis-h"></i></button>
                                    <button class="btn btn-default" type="button" title=""><i class="fa fa-ellipsis-h"></i></button>
                                </span>
                            </form>
                        </td>
                    </tr>   
                    <?php
                    foreach($locations as $array){
                        if($array["ne"]=="il" and $array["s1"]==$_GET["s1"]){
							$c = 0;
							foreach ($locations as $count){
								if ($count["ne"] == "ilce" && $count["s2"] == $array["id"]){$c++; }
							}						
                            ?>
                            <tr>
                                <td>
                                    <form class="input-group" action="?ne=<?php echo $_GET["ne"];?>&islem=duzenle&id=<?php echo $array["id"]?>" method="post" style="margin:0px; padding:0px;">
                                        <span style="display:none;"><?php echo $array["t1"];?></span>   
                                        <span class="input-group-addon" style="padding: 0px 5px; background-color: transparent; border: 0px solid #ccc;">
                                            <button class="btn btn-default" type="button" title=""><?php echo tamamla($array["id"],5);?></button>
                                        </span>
                                        <div class="col-xs-4">
                                            <input type="text" class="form-control" value="<?php echo $array["t1"];?>" name="t1">
                                        </div>
                                        <div class="col-xs-3">
                                            <input type="text" class="form-control" value="<?php echo $array["t2"];?>" name="t2">
                                        </div>
                                        <div class="col-xs-4">
                                            <input type="text" class="form-control" value="<?php echo $array["t3"];?>" name="t3">
                                        </div>
                                        <input type="hidden" name="KID[]" value="<?php echo $array["id"]?>"/>
                                        <input type="hidden" value="<?php echo $array["s1"];?>" name="s1">
                                        <input type="hidden" value="<?php echo $array["s2"];?>" name="s2">
                                        <input type="hidden" value="<?php echo $array["s3"];?>" name="s3">
                                        <input type="hidden" value="<?php echo $array["t4"];?>" name="t4">
                                        <span class="input-group-addon" style="padding: 0px 5px; background-color: transparent; border: 0px solid #ccc;">
                                            <button class="btn btn-default" type="button" onclick="location.href='?ne=ilce&id=<?php echo $array["id"];?>&s1=<?php echo $array["s1"];?>&s2=<?php echo $array["id"];?>&s3=<?php echo $array["s3"];?>'" title="Alt List"><i class="fa fa-tasks" style="margin-top:0px;"></i><div style="position:absolute; margin-top:-25px; font-size:11px; width:24px; text-align:center;"><?php echo $c;?></div></button>
                                            <button class="btn btn-default" type="submit" title="Kayıt Düzenle"><i class="fa fa-check"></i></button>
                                            <button class="btn btn-default" type="button" onclick="location.href='?ne=<?php echo $_GET["ne"];?>&islem=sil&id=<?php echo $array["id"];?>&s1=<?php echo $array["s1"];?>&s2=<?php echo $array["s2"];?>&s3=<?php echo $array["s3"];?>'" title="Kayıt Sil"><i class="fa fa-trash"></i></button>
                                        </span>
                                    </form> 
                                </td>
                            </tr>           
                        <?php }}?>
                    <?php
						}elseif($_GET["ne"]=="ilce"){
						$c = 0; foreach ($locations as $count) {
							if ($count["ne"] == "ilce" && $count["s2"] == $_GET["s2"]){$c++; }
						}
					?>
                        <tr>
                            <td>
                                <form class="input-group" action="?ne=<?php echo $_GET["ne"];?>&islem=yekle" method="post" style="margin:0px; padding:0px;">
                                    <span class="input-group-addon" style="padding: 0px 5px; background-color: transparent; border: 0px solid #ccc;">
                                        <button class="btn btn-default" type="button" onclick="toggle('#ijson')">..........</button>
                                    </span>
                                    <input type="text" class="form-control" placeholder="Yeni İlçe Ekle (<?php echo $c?>)" name="t1">
                                    <input type="hidden" value="<?php echo $_GET["s1"];?>" name="s1">
                                    <input type="hidden" value="<?php echo $_GET["s2"];?>" name="s2">
                                    <input type="hidden" value="0" name="s3">
                                    <span class="input-group-addon" style="padding: 0px 5px; background-color: transparent; border: 0px solid #ccc;">
                                        <button class="btn btn-default" type="submit"><i class="fa fa-plus"></i></button>
                                    </span>
                                </form>
                            </td>
                        </tr>
                        <?php
                        foreach($locations as $array){
                            if($array["ne"]=="ilce" and $array["s2"]==$_GET["s2"]){
								$c = 0;
								foreach ($locations as $count) {
									if ($count["ne"] == "semt" && $count["s3"] == $array["id"]){$c++; }
								}
                                ?>
                                <tr>
                                    <td>
                                        <form action="?ne=<?php echo $_GET["ne"];?>&islem=duzenle&id=<?php echo $array["id"]?>" method="post" style="margin:0px; padding:0px;">
                                            <span style="display:none;"><?php echo $array["t1"];?></span>   
                                            <div class="input-group">
                                                <span class="input-group-addon" style="padding: 0px 5px; background-color: transparent; border: 0px solid #ccc;">
                                                    <button class="btn btn-default" type="button" title=""><?php echo tamamla($array["id"],5);?></button>
                                                </span>
                                                <input type="text" class="form-control" value="<?php echo $array["t1"];?>" name="t1">
                                                <input type="hidden" name="KID[]" value="<?php echo $rs["id"]?>"/>
                                                <input type="hidden" value="<?php echo $array["s1"];?>" name="s1">
                                                <input type="hidden" value="<?php echo $array["s2"];?>" name="s2">
                                                <input type="hidden" value="<?php echo $array["s3"];?>" name="s3">
                                                <input type="hidden" value="<?php echo $array["t2"];?>" name="t2">
                                                <input type="hidden" value="<?php echo $array["t3"];?>" name="t3">
                                                <input type="hidden" value="<?php echo $array["t4"];?>" name="t4">
                                                <span class="input-group-addon" style="padding: 0px 5px; background-color: transparent; border: 0px solid #ccc;">
                                                    <button class="btn btn-default" type="button" onclick="location.href='?ne=semt&id=<?php echo $array["id"];?>&s1=<?php echo $array["s1"];?>&s2=<?php echo $array["s2"];?>&s3=<?php echo $array["id"];?>'" title="Alt List (<?php echo $rsay[0]?>)"><i class="fa fa-tasks" style="margin-top:0px;"></i><div style="position:absolute; margin-top:-25px; font-size:11px; width:24px; text-align:center;"><?php echo $c;?></div></button>
                                                    <button class="btn btn-default" type="submit" title="Kayıt Düzenle"><i class="fa fa-check"></i></button>
                                                    <button class="btn btn-default" type="button" onclick="location.href='?ne=<?php echo $_GET["ne"];?>&islem=sil&id=<?php echo $array["id"];?>&s1=<?php echo $array["s1"];?>&s2=<?php echo $array["s2"];?>&s3=<?php echo $array["s3"];?>'" title="Kayıt Sil"><i class="fa fa-trash"></i></button>
                                                </span>
                                            </div>
                                        </form> 
                                    </td>
                                </tr>           
                            <?php }}?>
                        <?php
							}elseif($_GET["ne"]=="semt"){
								$c = 0; foreach ($locations as $count) {
									if ($count["ne"] == "semt" && $count["s3"] == $_GET["s3"]){$c++; }
								}
							?>
                            <tr>
                                <td>
                                    <form class="input-group" action="?ne=<?php echo $_GET["ne"];?>&islem=yekle&id=<?php echo $array["id"];?>" method="post" style="margin:0px; padding:0px;">
                                        <span class="input-group-addon" style="padding: 0px 5px; background-color: transparent; border: 0px solid #ccc;">
                                             <button class="btn btn-default" type="button" onclick="toggle('#ijson')">..........</button>
                                        </span>
                                        <input type="text" class="form-control" placeholder="Yeni Mahalle (<?php echo $c?>)" name="t1">
                                        <input type="hidden" value="<?php echo $_GET["s1"];?>" name="s1">
                                        <input type="hidden" value="<?php echo $_GET["s2"];?>" name="s2">
                                        <input type="hidden" value="<?php echo $_GET["s3"];?>" name="s3">
                                        <input type="hidden" value="<?php echo $array["t2"];?>" name="t2">
                                        <input type="hidden" value="<?php echo $array["t3"];?>" name="t3">
                                        <input type="hidden" value="<?php echo $array["t4"];?>" name="t4">
                                        <span class="input-group-addon" style="padding: 0px 5px; background-color: transparent; border: 0px solid #ccc;">
                                            <button class="btn btn-default" type="submit"><i class="fa fa-plus"></i></button>
                                        </span>
                                    </form>
                                </td>
                            </tr>
                            <?php
                            foreach($locations as $array){
                                if($array["ne"]=="semt" and $array["s3"]==$_GET["s3"]){
                                    ?>
                                    <tr>
                                        <td>
                                            <form  action="?ne=<?php echo $_GET["ne"];?>&islem=duzenle&id=<?php echo $array["id"]?>" method="post" style="margin:0px; padding:0px;">
                                                <span style="display:none;"><?php echo $array["t1"];?></span>   
                                                <div class="input-group">
                                                    <span class="input-group-addon" style="padding: 0px 5px; background-color: transparent; border: 0px solid #ccc;">
                                                        <button class="btn btn-default" type="button" title=""><?php echo tamamla($array["id"],5);?></button>
                                                    </span>
                                                    <input type="hidden" name="KID[]" value="<?php echo $array["id"]?>"/>
                                                    <input type="text" class="form-control" value="<?php echo $array["t1"];?>" name="t1">
                                                    <input type="hidden" value="<?php echo $array["s1"];?>" name="s1">
                                                    <input type="hidden" value="<?php echo $array["s2"];?>" name="s2">
                                                    <input type="hidden" value="<?php echo $array["s3"];?>" name="s3">
                                                    <input type="hidden" value="<?php echo $array["t2"];?>" name="t2">
                                                    <input type="hidden" value="<?php echo $array["t3"];?>" name="t3">
                                                    <input type="hidden" value="<?php echo $array["t4"];?>" name="t4">
                                                    <span class="input-group-addon" style="padding: 0px 5px; background-color: transparent; border: 0px solid #ccc;">
                                                        <button class="btn btn-default" type="submit" title="Kayıt Düzenle"><i class="fa fa-check"></i></button>
                                                        <button class="btn btn-default" type="button" onclick="location.href='?ne=<?php echo $_GET["ne"];?>&islem=sil&id=<?php echo $array["id"];?>&s1=<?php echo $array["s1"];?>&s2=<?php echo $array["s2"];?>&s3=<?php echo $array["s3"];?>'" title="Kayıt Sil"><i class="fa fa-trash"></i></button>
                                                    </span>
                                                </div>
                                            </form>
                                        </td>
                                    </tr>
                                <?php }}?>
                            <?php }?>
                        </table>
                    </div>
<?php
}elseif($_GET["islem"]=="duzenle"){
	$larr = array();
	foreach($locations as $key=>$value){
		if($value[id]==$_GET["id"]){    //update
			$larr[] = array('id' => $value[id], 'ne' => $value["ne"], 't1' => trim($_POST["t1"]), 't2' => $_POST["t2"], 't3' => $_POST["t3"], 't4' => $_POST["t4"], 's1' => $value["s1"], 's2' => $value["s2"], 's3' => $value["s3"]);
		}else{
			$larr = $larr + array( $key => $value);
		}
	}
    $loc = json_encode($larr, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_NUMERIC_CHECK);
    $fp = fopen($json, 'w');
    fwrite($fp, $loc);
    fclose($fp);
    echo "<SCRIPT LANGUAGE='JavaScript'>window.location='?ne=".$_GET["ne"]."&s1=".$_POST["s1"]."&s2=".$_POST["s2"]."&s3=".$_POST["s3"]."';</script>";
	}
elseif($_GET["islem"]=="yekle"){
	// Yeni diziyi tanımla
	$larr = [];
	// En büyük ID'yi bulmak için değişken oluştur
	$max_id = 0;
	foreach ($locations as $value) {
		$larr[] = $value; // Eski verileri ekle
		if ($value["id"] > $max_id) { // En büyük ID'yi bul
			$max_id = $value["id"];
		}
	}
	// Eğer $_POST["t1"] doluysa işlem yap
	if (!empty($_POST["t1"])) {
		// Eğer içinde virgül varsa parçala, yoksa tek değer olarak al
		$t1_list = strpos($_POST["t1"], ',') !== false ? explode(',', $_POST["t1"]) : [$_POST["t1"]];
		foreach ($t1_list as $t1) {
			if (!empty(trim($t1))) { // Boş değerleri ekleme
				$new_id = ++$max_id;
				$larr[] = [
					'id'  => $new_id,
					'ne'  => $_GET["ne"],
					't1'  => trim($t1),
					't2'  => $_POST["t2"],
					't3'  => $_POST["t3"],
					't4'  => $_POST["t4"],
					's1'  => $_POST["s1"],
					's2'  => $_POST["s2"],
					's3'  => $_POST["s3"]
				];
			}
		}
	}
	// JSON dosyasına kaydet
	$loc = json_encode($larr, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_NUMERIC_CHECK);
	file_put_contents($json, $loc);
	// Yönlendirme
	echo "<script>window.location='?ne={$_GET["ne"]}&s1={$_POST["s1"]}&s2={$_POST["s2"]}&s3={$_POST["s3"]}';</script>";
}elseif($_GET["islem"]=="sil"){
    $larr = array();
	foreach ($locations as $value) {
		// Eğer üst kategori siliniyorsa, alt kategorilere bağlı olanları da silelim
		if (
			$value["id"] == $_GET["id"] || // Direkt ID eşleşmesi (seçilen ID'nin kendisi)
			($_GET["ne"] == "ulke" && $value["s1"] == $_GET["id"]) || // İl siliniyorsa, o ile bağlı ilçeleri de sil
			($_GET["ne"] == "il" && $value["s2"] == $_GET["id"]) || // İl siliniyorsa, o ile bağlı ilçeleri de sil
			($_GET["ne"] == "ilce" && $value["s3"] == $_GET["id"]) || // İlçe siliniyorsa, o ilçeye bağlı semtleri de sil
			($_GET["ne"] == "semt" && $value["s4"] == $_GET["id"]) // Semt siliniyorsa, o semte bağlı alt verileri de sil
		) {
			continue; // Bu öğeyi atla (sil)
		}
		$larr[] = $value;
	}
	// Yeni JSON'u oluştur ve dosyaya yaz
	$loc = json_encode($larr, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_NUMERIC_CHECK);
	file_put_contents($json, $loc);
	// Sayfayı yönlendir
	echo "<script>window.location='?ne={$_GET["ne"]}&s1={$_GET["s1"]}&s2={$_GET["s2"]}&s3={$_GET["s3"]}';</script>";
}elseif($_GET["islem"]=="ulocation"){
	$jsonVeri = file_get_contents("https://cloud.crmplus.com.tr/deppo/tools/jsons/locations.json");
	$klasor = $_SERVER['DOCUMENT_ROOT'] . '/deppo/tools/jsons';
	$dosyaYolu = $klasor . '/locations.json';
	if (!is_dir($klasor)) { mkdir($klasor, 0777, true); }
	if (file_put_contents($dosyaYolu, $jsonVeri) !== false) {
		echo "<script>window.location='?';</script>";
	} else {
		echo "Hata";
	}
}elseif($_GET["islem"]=="clocation"){
	$larr = array();
	foreach ($locations as $value) {
		if (
			($_GET["ne"] == "il" && $value["s1"] == $_GET["id"]) ||
			($_GET["ne"] == "ilce" && $value["s2"] == $_GET["id"]) ||
			($_GET["ne"] == "semt" && $value["s3"] == $_GET["id"])
		) {
			continue; // Bu kaydı atla (sil)
		}
		$larr[] = $value;
	}
	// JSON'u güncelle
	$loc = json_encode($larr, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_NUMERIC_CHECK);
	file_put_contents($json, $loc);
	// Eğer geçerli bir işlem değilse JSON'u tamamen sil
	if (!in_array($_GET["ne"], ["il", "ilce", "semt"]) && $update != "false") {
		unlink($_SERVER['DOCUMENT_ROOT'].'/deppo/tools/jsons/locations.json');
	}
	// Sayfayı yönlendir
	echo "<script>window.location='?ne={$_GET["ne"]}&s1={$_GET["s1"]}&s2={$_GET["s2"]}&s3={$_GET["s3"]}';</script>";
}
?>