<?php
$path="../../";
include("../../map.php");
include("../../secure.php");
$durum=isset($_GET["durum"]) ? $_GET["durum"] : "";
if(isset($_POST['position']) and is_array($_POST['position'])){
	$i=1;$case=array();$ids=array();
	foreach($_POST['position'] as $k=>$v){
		$v=(int)$v;
		if($v>0){
			$case[]="when ".$v." then ".$i;
			$ids[]=$v;
			$i++;
		}
	}
	if(count($ids)>0){
		$Rsq = mysqli_query($conn, "update t1 set s10=case id ".implode(" ", $case)." end where id in (".implode(",", $ids).")");
	}
}

if($durum=="extra_import"){
	$source_id=(int)$_POST["source_id"];
	$target_id=(int)$_GET["id"];
	$ne=mysqli_real_escape_string($conn, $_GET["ne"]);
	$extra_where="t1='".$ne."' and (ne='extra' or ne='' or ne is null)";
	if($source_id<=0 or $target_id<=0){
		echo ctranslate("Gecerli bir kaynak ID giriniz");
		exit;
	}
	if($source_id==$target_id){
		echo ctranslate("Kaynak ID ile hedef ID ayni olamaz");
		exit;
	}
	$check=mysqli_query($conn, "select count(*) as total from t1 where ".$extra_where." and s1=".$source_id);
	$row=mysqli_fetch_assoc($check);
	if((int)$row["total"]==0){
		echo ctranslate("Bu ID icin kopyalanacak kutu bulunamadi");
		exit;
	}
	$columns=array();
	$selects=array();
	$cols=mysqli_query($conn, "show columns from t1");
	while($col=mysqli_fetch_assoc($cols)){
		$field=$col["Field"];
		if($field=="id"){continue;}
		$columns[]="`".$field."`";
		if($field=="s1"){
			$selects[]="'".$target_id."'";
		}elseif($field=="ne"){
			$selects[]="'extra'";
		}else{
			$selects[]="`".$field."`";
		}
	}
	if(count($columns)>0){
		mysqli_autocommit($conn, false);
		$del=mysqli_query($conn, "delete from t1 where ".$extra_where." and s1=".$target_id);
		$ins=mysqli_query($conn, "insert into t1(".implode(",", $columns).") select ".implode(",", $selects)." from t1 where ".$extra_where." and s1=".$source_id);
		if($del and $ins){
			mysqli_commit($conn);
			mysqli_autocommit($conn, true);
		}else{
			mysqli_rollback($conn);
			mysqli_autocommit($conn, true);
			echo ctranslate("Ice aktarma sirasinda hata olustu");
			exit;
		}
	}
	echo "url:?ne=".$_GET["ne"]."&islem=".$_GET["islem"]."&id=".$_GET["id"]."&durum=ok";

}elseif($durum=="extra"){
	$xiarr = array();
	foreach ($_POST as $key => $value){
		if(substr($key,0,2)=="xi"){
			$xiarr = $xiarr + array($key => $value);
		}
	}
	$xrjsi = json_encode($xiarr, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_NUMERIC_CHECK);
	$xarr = array();
	foreach ($_POST as $key => $value){
		if(substr($key,0,2)=="xs" or substr($key,0,2)=="xc" or substr($key,0,2)=="xt"){
			$xarr = $xarr + array($key => $value);
		}
	}
	$xjs = json_encode($xarr, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_NUMERIC_CHECK);
	if($_POST["xs2"]==""){$xs2=0;}else{$xs2=$_POST["xs2"];}
	if($_POST["xs7"]==""){$xs7=0;}else{$xs7=$_POST["xs7"];}
	if($_POST["xs10"]==""){$xs10=0;}else{$xs10=$_POST["xs10"];}
	if($_POST["xs13"]==""){$xs13=0;}else{$xs13=$_POST["xs13"];}
	if(!empty($_GET["extra_id"])){
		$Rsq = mysqli_query($conn, "update t1 set t2='".$_POST["xt2"]."', t3='".$_POST["xt3"]."', n1='".addslashes(stripslashes($_POST["xn1"]))."', n2='".addslashes(stripslashes($_POST["xn2"]))."', n12='".addslashes(stripslashes($xjs))."', n17='".addslashes(stripslashes($xrjsi))."', s7='".$xs7."', s10='".$xs10."', s13='".$xs13."' Where t1='".$_GET["ne"]."' and id='".$_GET["extra_id"]."' ");
		echo ctranslate("Kayıt Güncellendi");
	}else{
		$conn->query("insert INTO t1(t1,t2,t3,ne,n1,n2,n12,n17,s1,s2,s7,s10,s13) VALUES ('".$_GET["ne"]."', '".$_POST["xt2"]."', '".$_POST["xt3"]."', 'extra', '".addslashes(stripslashes($_POST["xn1"]))."', '".addslashes(stripslashes($_POST["xn2"]))."', '".addslashes(stripslashes($xjs))."', '".addslashes(stripslashes($xrjsi))."', '".$_GET["id"]."', '".$xs2."', '".$xs7."', '".$xs10."', '".$xs13."');");
		echo "url:?ne=".$_GET["ne"]."&islem=".$_GET["islem"]."&id=".$_GET["id"]."&durum=ok&iq=".$_POST["xs2"]."";
	}

}elseif($durum=="extra_s"){
	$extra_id=(int)$_GET["extra_id"];
	$iq=(int)$_GET["sx2"];
	if($extra_id>0){
		$sql = mysqli_query($conn, "delete from t1 where id=".$extra_id);
	}
	echo "url:?ne=".$_GET["ne"]."&islem=".$_GET["islem"]."&id=".$_GET["id"]."&durum=ok&iq=".$iq."";
}
?>
