<?php
$path="../../";
include("../../map.php");
include("../../scripts.php");
$sqlu=mysqli_query($conn, "select * from ".$_SESSION["lang"]." where id=".$_GET["id"]." order by id desc"); $rs=mysqli_fetch_assoc($sqlu); $varyasyonlar = json_decode($rs["n19"], true) ?: [];
if($_GET["optionGrupID"]<>""){$_SESSION["optionGrupID"]=$_GET["optionGrupID"];}
?>
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title><?php echo $vyonlar?></title>
</head>
<body>
	<div class="qmodal">
		<div class="qmodal-content">
			<!-- Header with Action Buttons -->
			<div class="qmodal-header">
				<select class="form-control" style="width:200px;" onchange="self.location.href=this.options[this.selectedIndex].value;">
					<option value="">Seçenek Grubu</option>
					<?php $sqlg=mysqli_query($conn, "select * from genel where ne='optionGrup' order by n1 asc"); while($rsg=mysqli_fetch_assoc($sqlg)){?>
					<option value="?ne=<?php echo $_GET["ne"];?>&islem=<?php echo $_GET["islem"];?>&id=<?php echo $_GET["id"];?>&optionGrupID=<?php echo $rsg["id"]?>"><?php echo $rsg["n1"]?></option>
					<?php }?>
				</select>
				<button type="button" class="btn btn-info" onclick="location.href='?ne=<?php echo $_GET["ne"];?>&islem=<?php echo $_GET["islem"];?>&id=<?php echo $_GET["id"];?>'"><i class="fas fa-home"></i></button>
			</div>
			<!-- Left Tab Menu -->
			<div class="v8-columns">
				<!-- Content Area -->
				<div ts1="<?php echo dtarih($rs["ts1"],"gg ka yy time");?>" ts2="<?php echo dtarih($rs["ts2"],"gg ka yy time");?>" ts3="<?php echo dtarih($rs["ts3"],"gg ka yy time");?>">
					<div class="alert alert-warning" id="err" style="display:none;"><?php echo $sdil[212]; //islem Basariyla Gerçeklestirilmistir !?></div>

					<form method="POST" action="?ne=<?php echo $_GET["ne"];?>&islem=<?php echo $_GET["islem"];?>&id=<?php echo $_GET["id"];?>&rb=ekle" style="min-width: 800px; overflow:auto;">
						<table class="table">
							<tr class="" style="height: 34px;">
								<th>
									<?php echo $vyon[0]?>
									<span style="float:right;">...</span><!--/Varyasyon kopyala eklenebilir/-->
								</th>
								<th style="width:50%">
									<table class="var" style="width:100%;">
										<tr style="background-color: transparent;">
											<?php if($e5==1){?>
												<td style="width:20%; text-align:right;"><?php echo ctranslate("Liste Fiyatı")?></td>
												<td style="width:20%; text-align:right;"><?php echo ctranslate("Satış Fiyat")?></td>
											<?php }?>
											<?php if($f3==1){?>
												<td style="width:20%; text-align:right;"><?php echo ctranslate("Desi")?></td>
											<?php }?>
											<?php if($f7==1){?>
												<td style="width:20%; text-align:right;"><?php if($f7==1){ echo ctranslate("Stok");} //Stok?></td>
											<?php }?>
										</tr>
									</table>
								</th>
								<td style="width:6%; text-align:center;">
									<?php echo ctranslate("Sıra")?>
								</td>
								<td style="width:8%">
									<?php echo ctranslate("İşlem")?>
								</td>
							</tr>
							<tr class="addtr">
								<td>
									<input type="text" name="vt1" list="lvt1" title="<?php echo ctranslate("Yeni Ekle")?>" class="form-control with-tip" placeholder="<?php echo $vyon[0]?>" autocomplete="off">
									<datalist id="lvt1">
									<?php $sqlg=mysqli_query($conn, "select * from genel where ne='option' and s1='".$_SESSION["optionGrupID"]."' order by n1 asc"); while($rsg=mysqli_fetch_assoc($sqlg)){?>
										<option value="<?php echo $rsg["n1"]?>"><?php echo $rsg["n1"]?></option>
									<?php }?>
									</datalist>
								</td>
								<td style="width:50%">
									<table class="var" style="width:100%;">
										<tr>
											<?php if($e5==1){?>
												<td style="width:20%">
													<div style="position: absolute; color: silver; margin-top: 7px;margin-left: 5px;"><i class="fa fa-<?php echo strtolower($rs["t2"]);?>"></i></div>
													<input type="text" name="vpb2" class="form-control tar" style="text-decoration: line-through" value="<?php echo sprice($rs["pb2"]);?>" title="<?php echo ctranslate("Eski Fiyat")?>" class="form-control with-tip" onblur="if(this.value == '') { this.value='<?php echo wprice($rs["pb2"]);?>';};" onfocus="if(this.value == '<?php echo wprice($rs["pb2"]);?>') { this.value = ''}">
												</td>
												<td style="width:20%">
													<div style="position: absolute; color: silver; margin-top: 7px;margin-left: 5px;"><i class="fa fa-<?php echo strtolower($rs["t2"]);?>"></i></div>
													<input type="text" name="vpb1" class="form-control tar" style="" value="<?php echo sprice($rs["pb1"]);?>" title="<?php echo ctranslate("Fiyat")?>" class="form-control with-tip" onblur="if(this.value == '') { this.value='<?php echo wprice($rs["pb1"]);?>';};" onfocus="if(this.value == '<?php echo wprice($rs["pb1"]);?>') { this.value = ''}">
												</td>
											<?php }?>
											<?php if($f3==1){?>
												<td style="width:20%"><input type="number" name="vs6" class="form-control tar" style="" value="<?php echo $rs["s6"];?>" title="<?php echo ctranslate("Desi")?>" class="form-control with-tip" onkeypress="return SadeceRakam(event);" onblur="if(this.value == '') { this.value='0';};SadeceRakam(event,false);" onfocus="if(this.value == '0') { this.value = ''}"></td>
											<?php }?>
											<?php if($f7==1){?>
												<td style="width:20%">
													<div style="position: absolute; color: silver; margin-top: 7px;margin-left: 5px;"><?php echo $rs["t10"];?></div>
													<input type="number" name="vs5" class="form-control tar" style="" value="0" title="<?php echo ctranslate("Stok (Adet)")?>" class="form-control with-tip" onkeypress="return SadeceRakam(event);" onblur="if(this.value == '') { this.value='0';};SadeceRakam(event,false);" onfocus="if(this.value == '0') { this.value = ''}">
												</td>
											<?php }?>
										</tr>
									</table>
								</td>
								<td style="width:6%">
									<input type="number" name="vs10" value="0" title="Sira (s10)" class="form-control with-tip tac" onkeypress="return SadeceRakam(event);" onblur="if(this.value == '') { this.value='0';};SadeceRakam(event,false);" onfocus="if(this.value == '0') { this.value = ''}">
								</td>
								<td  style="width:8%">
									<button type="submit" class="btn btn-warning"  style="padding: 4px 7px;"><i class="fa fa-plus"></i></button>
								</td>
							</tr>
						</form>

						<?php foreach ($varyasyonlar as $varyasyon) { ?>
							<form method="POST" action="?ne=<?php echo $_GET["ne"];?>&islem=<?php echo $_GET["islem"];?>&id=<?php echo $_GET["id"];?>&rb=duzenle&rb_id=<?php echo $varyasyon["id"];?>" style="margin-bottom:5px;" id="xf<?php echo $varyasyon["id"];?>" name="f<?php echo $varyasyon["id"];?>">	
								<tr data-id="<?php echo $varyasyon["id"];?>" class="edittr btr">
									<td>
										<input type="text" name="vt1" value="<?php echo $varyasyon["t1"];?>" class="form-control with-tip" placeholder="<?php echo ctranslate("Varyasyon")?>">
									</td>
									<td>
										<table class="var" style="width:100%;">
											<tr>
												<?php if($e5==1){?>
													<td class="tar w20 p0">
														<div style="position: absolute; color: silver; margin-top: 7px;margin-left: 5px;"><i class="fa fa-<?php echo strtolower($rs["t2"]);?>"></i></div>
														<input type="text" name="vpb2" class="form-control tar" style="text-decoration: line-through" value="<?php echo sprice($varyasyon["pb2"]);?>" title="Liste Fiyat" class="form-control with-tip">
													</td>
												<?php }?>
												<?php if($e5==1){?>
													<td class="tar w20 p0">
														<div style="position: absolute; color: silver; margin-top: 7px;margin-left: 5px;"><i class="fa fa-<?php echo strtolower($rs["t2"]);?>"></i></div>
														<input type="text" name="vpb1" class="form-control tar" style="" value="<?php echo sprice($varyasyon["pb1"]);?>" title="Fiyat" class="form-control with-tip">
													</td>
												<?php }?>
												<?php if($f3==1){?>
													<td class="tar w20 p0"><input type="number" name="vs6" class="form-control tar" style="" value="<?php echo $varyasyon["s6"];?>" title="Desi" class="form-control with-tip" onkeypress="return SadeceRakam(event);" onblur="if(this.value == '') { this.value='0';};SadeceRakam(event,false);" onfocus="if(this.value == '0') { this.value = ''}"></td>
												<?php }?>
												<?php if($f7==1){?>
													<td class="tar w20 p0">
														<div style="position: absolute; color: silver; margin-top: 7px;margin-left: 5px;"><?php echo $rs["t10"];?></div>
														<input type="number" name="vs5" class="form-control tar" style="" value="<?php echo $varyasyon["s5"];?>" title="Stok" class="form-control with-tip" onkeypress="return SadeceRakam(event);" onblur="if(this.value == '') { this.value='0';};SadeceRakam(event,false);" onfocus="if(this.value == '0') { this.value = ''}">
													</td>
												<?php }?>
											</tr>
										</table>
									</td>
									<td>
										<input type="number" name="vs10" style="" value="<?php echo $varyasyon["s10"];?>" title="Sira (s10)" class="form-control with-tip tac" onkeypress="return SadeceRakam(event);" onblur="if(this.value == '') { this.value='0';};SadeceRakam(event,false);" onfocus="if(this.value == '0') { this.value = ''}">
									</td>
									<td>
										<button type="button" onclick="Ajaxs('<?php echo $varyasyon["id"];?>','#err','AjaxPost');" class="btn btn-warning" style="padding: 4px 7px;"><i class="fa fa-refresh"></i></button>
										<button type="button" class="btn btn-danger btn-delete-row" data-href="?ne=<?php echo $_GET["ne"];?>&islem=<?php echo $_GET["islem"];?>&id=<?php echo $_GET["id"];?>&rb=sil&rb_id=<?php echo $varyasyon["id"];?>&rs5=<?php echo $varyasyon["s5"];?>"><i class="fa fa-times"></i></button>		
									</td>
								</tr>
							</form>
						<?php }?>
					</table>
				</div>
			</div>
		</body>
		<?php echo $footer?>
		</html>
		<?php
if ($_GET["rb"] == "duzenle") {
	$vs5  = !empty($_POST["vs5"])  ? (float)$_POST["vs5"]  : 0;
	$vs6  = !empty($_POST["vs6"])  ? (float)$_POST["vs6"]  : 0;
	$vs10 = !empty($_POST["vs10"]) ? (float)$_POST["vs10"] : 0;
	$vpb1 = !empty($_POST["vpb1"]) ? sprice($_POST["vpb1"]) : 0;
	$vpb2 = !empty($_POST["vpb2"]) ? sprice($_POST["vpb2"]) : 0;
	$id    = (int)$_GET["id"];
	$rb_id = (int)$_GET["rb_id"];

	foreach ($varyasyonlar as &$v) {
		if ($v["id"] == $rb_id) {
			// stok sadece değiştiyse güncelle
			if ($f7 == 1 && $vs5 != (float)$v["s5"]) {
				$diff = $vs5 - (float)$v["s5"];
				if ($diff != 0) {
					$new_s5 = $rs["s5"] + $diff;
					for ($l = 1; $l <= $lx; $l++) {
						mysqli_query($conn, "UPDATE lang{$l} SET s5='$new_s5' WHERE id=$id");
					}
				}
			}

			// varyasyon güncelle
			$v["t1"]  = $_POST["vt1"];
			$v["t2"]  = $_POST["vt2"];
			$v["pb1"] = $vpb1;
			$v["pb2"] = $vpb2;
			$v["s5"]  = $vs5;
			$v["s6"]  = $vs6;
			$v["s10"] = $vs10;
			$v["ts1"] = date("Y-m-d H:i:s");
			break;
		}
	}

	$json = json_encode($varyasyonlar, JSON_UNESCAPED_UNICODE);
	mysqli_query($conn, "UPDATE lang1 SET n19='" . mysqli_real_escape_string($conn, $json) . "' WHERE id=$id");
	go("?ne={$_GET["ne"]}&islem={$_GET["islem"]}&id=$id&nedir=ok#&tab-rb");

} elseif ($_GET["rb"] == "sil") {
	$id    = (int)$_GET["id"];
	$rb_id = (int)$_GET["rb_id"];
	$rs5   = (float)$_GET["rs5"];
	$yeni_varyasyonlar = [];

	foreach ($varyasyonlar as $v) {
		if ($v["id"] != $rb_id) $yeni_varyasyonlar[] = $v;
	}

	// stok güncelle
	$vks5 = $rs["s5"] - $rs5;
	for ($l = 1; $l <= $lx; $l++) {
		mysqli_query($conn, "UPDATE lang{$l} SET s5='$vks5' WHERE id=$id");
	}

	// JSON’u geri kaydet
	$json = json_encode($yeni_varyasyonlar, JSON_UNESCAPED_UNICODE);
	mysqli_query($conn, "UPDATE lang1 SET n19='" . mysqli_real_escape_string($conn, $json) . "' WHERE id=$id");
	go("?ne={$_GET["ne"]}&islem={$_GET["islem"]}&id=$id&nedir=ok#&tab-rb");

} elseif ($_GET["rb"] == "ekle") {
	$vs5  = !empty($_POST["vs5"])  ? (float)$_POST["vs5"]  : 0;
	$vs6  = !empty($_POST["vs6"])  ? (float)$_POST["vs6"]  : 0;
	$vs10 = !empty($_POST["vs10"]) ? (float)$_POST["vs10"] : 0;
	$vpb1 = !empty($_POST["vpb1"]) ? sprice($_POST["vpb1"]) : 0;
	$vpb2 = !empty($_POST["vpb2"]) ? sprice($_POST["vpb2"]) : 0;

	$id = (int)$_GET["id"];
	$vks5 = $rs["s5"] + $vs5;

	// yeni id üret
	$yeni_id = (count($varyasyonlar) > 0) ? max(array_column($varyasyonlar, 'id')) + 1 : 1;

	// yeni varyasyon
	$yeni_varyant = [ "id"     => $yeni_id, "t1"     => $_POST["vt1"], "t2"  => $_POST["vt2"], "pb1"    => $vpb1, "pb2"    => $vpb2, "s5"   => $vs5, "s6"     => $vs6, "s10"    => $vs10, "kur"    => "TRY", "kdv"    => 20, "ts1" => date("Y-m-d H:i:s") ];

	$varyasyonlar[] = $yeni_varyant;
	// JSON’u geri kaydet
	$json = json_encode($varyasyonlar, JSON_UNESCAPED_UNICODE);
	mysqli_query($conn, "UPDATE lang1 SET n19='" . mysqli_real_escape_string($conn, $json) . "' WHERE id=$id");

	for ($l = 1; $l <= $lx; $l++) {
		mysqli_query($conn, "UPDATE lang{$l} SET s5='$vks5' WHERE id=$id");
	}
	go("?ne={$_GET["ne"]}&islem={$_GET["islem"]}&id=$id&nedir=ok#&tab-rb");
}
?>