<?php
header('Content-Type: text/html; charset=utf-8');
$path="../../";
include("../../map.php");
$data = json_decode($_POST["data"], true);
//print_r($data);
if(!empty($_POST["s6"])){$ss6=$_POST["s6"];}else{$ss6="0";}
if(!empty($_POST["s7"])){$ss7=$_POST["s7"];}else{$ss7="0";}
$_SESSION["t7"] = $_POST["t7"];
if($_GET["durum"]==""){
	foreach($data as $key=>$value){
		echo "ID => ".$value["id"].", Sıra=>0, c1=>0, c2=>0, c3=>0, c4=>0";
		$sql = mysqli_query($conn, "update kategoriler set sira=".$key.", s1='0' where Cat_ID=".$value["id"]."");
		foreach($data[$key]["children"] as $key1=>$value1){
			echo "ID => ".$value1["id"].", Sıra=>".$value["id"].", c1=>".$value["id"].", c2=>0, c3=>0, c4=>0";
			$sql = mysqli_query($conn, "update kategoriler set sira='".$key1."', s1='".$value["id"]."', s2='0' where Cat_ID=".$value1["id"]."");
			foreach($data[$key]["children"][$key1]["children"] as $key2=>$value2){
				echo "ID => ".$value2["id"].", Sıra=>".$value1["id"].", c1=>".$value["id"].", c2=>".$value1["id"].", c3=>0, c4=>0";
				$sql = mysqli_query($conn, "update kategoriler set sira='".$key2."', s1='".$value["id"]."', s2='".$value1["id"]."', s3='0' where Cat_ID=".$value2["id"]."");
				foreach($data[$key]["children"][$key2]["children"][$key2]["children"] as $key3=>$value3){
					echo "ID => ".$value3["id"].", Sıra=>".$value2["id"].", c1=>".$value["id"].", c2=>".$value1["id"].", c3=>".$value2["id"].", c4=>0";
					$sql = mysqli_query($conn, "update kategoriler set sira='".$key3."', s1='".$value["id"]."', s2='".$value1["id"]."', s3='".$value2["id"]."', s4='0' where Cat_ID=".$value3["id"]."");
					foreach($data[$key]["children"][$key3]["children"][$key3]["children"][$key3]["children"] as $key4=>$value4){
						echo "ID => ".$value4["id"].", Sıra=>".$value3["id"].", c1=>".$value["id"].", c2=>".$value1["id"].", c3=>".$value2["id"].", c4=>".$value3["id"]."";
						$sql = mysqli_query($conn, "update kategoriler set sira='".$key4."', s1='".$value["id"]."', s2='".$value1["id"]."', s3='".$value2["id"]."', s4='".$value3["id"]."', s5='0' where Cat_ID=".$value4["id"]."");
					}
				}
			}
		}
	}
}elseif($_GET["durum"]=="kaydet"){
$larr = array();
foreach ($_POST as $key => $value){
	//if(substr($key,0,1)=="l"){
		$larr = $larr + array(trim($key) => trim($value));
	//}
}
$rjsl = json_encode($larr, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_NUMERIC_CHECK);
	$s3s = explode(',',$_POST["s3"]);
	if($_GET["id"]<>""){
		$sql = mysqli_query($conn, "update kategoriler set n1='".addslashes(stripslashes($rjsl))."', n2='".addslashes(stripslashes($_POST["n2"]))."', n3='".addslashes(stripslashes($_POST["n3"]))."', n4='".addslashes(stripslashes($_POST["n4"]))."', t3='".$_POST["t3"]."', t4='".$_POST["t4"]."', t5='".$_POST["t5"]."', t6='".$_POST["t6"]."', t7='".$_POST["t7"]."', t8='".$_POST["t8"]."', n5='".$_POST["n5"]."', s6='".$ss6."', s7='".$ss7."', s9='".$_POST["s19"]."' where Cat_ID=".$_GET["id"]."");	
	}else{
		$sql = $conn->query("insert into kategoriler (ne,n1,n2,n3,n4,t1,t3,t4,t6,t7,t8,n5,s6,s7,s19,ts1) values ('".$_GET["ne"]."', '".addslashes(stripslashes($rjsl))."', '".addslashes(stripslashes($_POST["n2"]))."', '".addslashes(stripslashes($_POST["n3"]))."', '".addslashes(stripslashes($_POST["n4"]))."', '".$_GET["ne"]."', '".$_POST["t3"]."', '".$_POST["t4"]."', '".$_POST["t6"]."', '".$_POST["t7"]."', '".$_POST["t8"]."', '".$_POST["n5"]."', '".$ss6."', '".$ss7."', '".$_POST["s9"]."', now())");
	}
	echo "url:?ne=".$_GET["ne"]."&islem=categories";
}elseif($_GET["durum"]=="clear"){
	$sql=mysqli_query($conn, "select * from kategoriler where t1='".$_GET["ne"]."'"); while($rs=mysqli_fetch_assoc($sql)){
		$Rs = mysqli_query($conn, "delete from kategoriler where Cat_ID=".$_GET["id"]);
	}
	echo "url:?ne=".$_GET["ne"]."&islem=categories";
}elseif($_GET["durum"]=="sil"){
	$Rs = mysqli_query($conn, "delete from kategoriler where t1='".$_GET["ne"]."' and Cat_ID=".$_GET["id"]);
	echo "url:?ne=".$_GET["ne"]."&islem=categories";
}
?>