<?php
error_reporting(0);
header("Content-Type: text/html; charset=UTF-8");
$path="../../";
include("../../map.php");
include("../../scripts.php");
include("../../secure.php");
	//findDirectoryPath("deppo")
require_once "../../../deppo/tools/script/simple_xlsx.php";
if ($_GET["ne"] == "kayit"){
	$ilist = array( "Başlık" => "t1", "Kısa Açıklama" => "n2", "Açıklama" => "n1", "Keywords" => "n4", "Stok No" => "t4", "Resim" => "images", "Kategori[1]" => "s9", "Kategori[2]" => "s10", "Kategori[3]" => "s11", "Sıra" => "s12", "İşlem Sorumlusu" => "s19" );
	if($g8 == 1){ $ilist["Marka"] = "i1"; }
	if($a7 == 1){ $ilist["Varyasyon"] = "varyasyon"; }
	if($e5 == 1){ $newItems = array("Barkod No" => "t5", "Liste Fiyatı" => "pb2", "Satış Fiyatı" => "pb1", "Para Birimi" => "t2", "Kdv" => "s4"); $ilist = array_merge($ilist, $newItems);}
	if($a7 == 1){ $ilist["Dosya"] = "files"; }
	if($m16 == 1){ $ilist["Stok"] = "s5"; $ilist["Desi"] = "s6"; }

} elseif ($_GET["ne"] == "uye" && $_GET["durum"] == "exls"){
	$ilist = array( "Üye Kodu" => "t5", "Ad Soyad" => "t1", "Doğum Tarihi" => "t12", "IDN" => "t11", "Gsm No" => "t4", "Telefon" => "t7", "E-Mail" => "t2", "Şifre" => "t3", "Firma" => "ifirma", "Kategori" => "s2", "Şehir" => "sehir", "İlce" => "ilce", "Adres" => "n5", "Puan" => "puan", "İşlem Sorumlusu" => "s19" );

} elseif ($_GET["ne"] == "uye"){
	$ilist = array( "Üye Kodu" => "t5", "Ad Soyad" => "t1", "Doğum Tarihi" => "t12", "IDN" => "t11", "Gsm No" => "t4", "Telefon" => "t7", "E-Mail" => "t2", "Şifre" => "t3", "Kategori" => "s2", "Adres" => "n5", "İşlem Sorumlusu" => "s19", "Etiketler" => "n4" );

} elseif ($_GET["ne"] == "haber"){
	$ilist = array( "Başlık" => "t1", "Özet" => "n2", "Keywords" => "n4", "Detay" => "n1", "Başlama Tarihi" => "t4", "Bitiş Tarihi" => "t9", "Etkinlik Alanı" => "ievent_location", "Resim" => "images", "Kategori" => "s2", "İşlem Sorumlusu" => "s19" );
}
$q = ($_GET["ne"] == "kayit") ? "u" : (($_GET["ne"] == "uye") ? "k" : substr($_GET["ne"], 0, 1));
	// 1. Limiti belirle
if (!empty($_POST["rows"])) {
	$rowLimit = intval($_POST["rows"]);
	$maxLimit = !empty($module["limit_xls"]) ? intval($module["limit_xls"]) : 999;
		// Eğer post edilen değer limitten büyükse, kısıtla
	if ($rowLimit > $maxLimit) {
		$rowLimit = $maxLimit;
	}
	$_SESSION["limit_xls"] = $rowLimit;
} elseif (!isset($_SESSION["limit_xls"])) {
		// Eğer hiç belirlenmediyse fallback
	$_SESSION["limit_xls"] = !empty($module["limit_xls"]) ? intval($module["limit_xls"]) : 999;
}
	// 2. Sayfa numarası
$page = (isset($_GET["sayfa"]) && is_numeric($_GET["sayfa"])) ? intval($_GET["sayfa"]) : 1;
	// 3. flimit / llimit hesapla
$flimit = ($_SESSION["limit_xls"] * ($page - 1));
$llimit = $flimit + $_SESSION["limit_xls"];
$ucats = json_decode(GetArr($q,"categories"), true);
function getExcelFiles(){
	$files = glob('../../../deppo/excel/*.xlsx*');
	usort($files, function($a, $b){
		return filemtime($b) - filemtime($a);
	});
	return $files;
}
if($_GET["durum"]=="ixls"){$fID="ixls-form"; $btntext="Dosya Yükle";}elseif($_GET["durum"]=="view"){$fID="view-form"; $btntext="İçe Aktar";}elseif($_GET["durum"]=="match"){$fID="match-form"; $btntext="Göster";}elseif($_GET["durum"]=="exls"){$fID="exls-form"; $btntext="Dışa Aktar";}
?>
<!DOCTYPE html>
<html lang="en">

<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>.xlsx <?php echo $sdil[119]; //Yönetimi !?></title>
</head>

<body>
	<div class="qmodal">
		<div class="qmodal-content">
			<!-- Header with Action Buttons -->
			<div class="qmodal-header">
				<div class="header-title">
					<nav aria-label="breadcrumb">
						<ol class="breadcrumb">
							<li class="breadcrumb-item"><a href="#"><i class="fa fa-home"></i></a></li>
							<li class="breadcrumb-item"><a href="#">Excel</a></li>
							<li class="breadcrumb-item active" aria-current="page">.xlsx <?php echo $sdil[119]; //Yönetimi !?></li>
						</ol>
					</nav>
				</div>

				<?php if($_GET["durum"]=="view"){?>
					<input type="text" id="filtre" class="filter-input" placeholder="Tabloda ara..." style="margin-right: 90px;">
				<?php }elseif($_GET["durum"]=="import"){?>

					<a class="mb-1 mt-1 mr-1 btn btn-default" href="#"><i class="fa fa-pause"  onclick="pauseQueue()"></i></a>
					<a class="mb-1 mt-1 mr-1 btn btn-default" title="#"><i class="fa fa-play" onclick="resumeQueue()"></i></a>
				<?php }elseif($_GET["durum"]=="exls"){?>
					<a class="mb-1 mt-1 mr-1 btn btn-default" href="#c0"><input type="checkbox" id="c0" onclick="Checks(this);" style="float: left; width: 16px; height: 16px; margin-top: 1px; margin-right: 5px;"> Select All</i></a>
					<a class="mb-1 mt-1 mr-1 btn btn-default" href="#ebtn" title=""><i class="fa fa-file-excel-o" aria-hidden="true"></i> Export</a>
				<?php }?>
					<?php if($btntext<>""){?><button class="v3-btn-xs v3-btn-info" type="button" onclick="jQuery('#<?php echo $fID?>').submit();"><?php echo translate($btntext)?> <i class="fa fa-angle-right" aria-hidden="true"></i></button><?php }?>
			</div>

			<!-- Left Tab Menu -->
			<div class="v8-columns">
				<div class="v8-col200pxL-left">
					<ul class="v8-side-tabs v8-js-tabs v8-same-height">
						<li class="current"><a href="<?php if($_GET["durum"]<>"exls"){?>?ne=<?php echo $_GET["ne"]?>&islem=<?php echo $_GET["islem"]?>&durum=ixls<?php }else{?>?ne=<?php echo $_GET["ne"]?>&islem=<?php echo $_GET["islem"]?>&durum=exls<?php }?>" title=""><i class="fa fa-table"></i> <span> .xlsx <?php echo translate("Dosyası")?></span></a></li>
						<?php if($_GET["durum"]<>"exls"){?>
							<li class="<?php if (empty($_SESSION["cgiris"])) {echo "dn";}?>"><a href="?ne=<?php echo $_GET["ne"]?>&islem=<?php echo $_GET["islem"]?>&durum=xlsfiles" title=""><i class="fa fa-upload"></i>  <span><?php echo translate("Resimleri Klasörü")?> (.zip)</span></a></li>
							<li class="" style=""><a href="https://cdn.<?php echo $cdnurl?>/tools/ornek_<?php echo $_GET["ne"]?>.xlsx" title="Örnek Excel Dosyası"><i class="fa fa-file-excel-o"></i> <span><?php echo translate("Örnek Excel Dosyası")?></span></a></li>
						<?php }else{?>
							<!--<li class=""><a href="?ne=<?php echo $_GET["ne"]?>&islem=<?php echo $_GET["islem"]?>&durum=exls&py=pazaryeri" title=""><i class="fa fa-tag" aria-hidden="true"></i> <span> <?php echo translate("Pazaryeri")?></span></a></li>-->
						<?php }?>
					</ul>
					<div id="switchs">
						<div class="v3-alert v3-alert-warning ddn">
							<i class="fas fa-arrow-right" onclick="hide('switchs')" style="float: right; margin-bottom:10px;"></i>
						</div>

						<div class="grey-bg msa mdn" style="">
							<label>ID <span style="font-size:8px; color:#ccc;">(id)</span></label>
							<input class="form-control" type="text" name="id" value="<?php echo $rs["id"];?>" class="form-control">
						</div>
						
						<div class="grey-bg e <?php if (empty($_SESSION["cgiris"])) {echo "dn";}?>">
							<label><i class="fa fa-user-plus" aria-hidden="true"></i> <?php echo ctranslate("İşlem Sorumlusu")?> <span style="font-size:8px; color:#ccc;">(s19)</span></label>
							<select size="1" name="s19" class="form-control chosen-select">
								<option value="0" <?php if($rs["s19"]==0){echo "Selected";}?>>Admin</option>
								<?php $s19S = explode(',',$rs["s19"]); $sql=mysqli_query($conn, "select s2,id,t1 from t5 where (s9=1 or s1=1) $luye"); while($rsm=mysqli_fetch_assoc($sql)){?>
									<option class="" value="<?php echo $rsm["id"];?>" title="<?php echo $rsm["t1"];?>" alt="<?php echo $rsm["t1"];?>" <?php foreach($s19S as $s19){if($s19==$rsm["id"]){echo "Selected";}}?>><?php echo $rsm["t1"];?></option>
								<?php }?>
							</select>
						</div>

					</div>
				</div>

				<!-- Content Area -->
				<div class="v8-col200pxL-right">
					<div id="tab-detay" class="v8-tabs-content" style="height: 80vh !important; width: 100%; overflow-y: scroll; overflow-x: auto;">
						<?php if($_GET["durum"]=="ixls"){?>
							<form class="form" name="rall" method="post" action="?ne=<?php echo $_GET["ne"]?>&islem=<?php echo $_GET["islem"]?>&durum=match" enctype="multipart/form-data" id="<?php echo $_GET["durum"]?>-form">
								<fieldset class="grey-bg no-margin p-3 rounded">
								  <legend><?php echo translate("Excel Dosyası") ?></legend>
								  <div class="mb-3">
									<label for="xlsFile" class="form-label fw-semibold"><?php echo translate(".xlsx Dosyası") ?></label>
									<div class="input-group">
									  <input type="file" name="dosya" id="xlsFile" class="form-control" accept=".xlsx">
									  <button type="submit" class="btn btn-info" style="display:none;"><?php echo translate("Yükle") ?></button>
									</div>
								  </div>
								</fieldset>

								<fieldset class="grey-bg no-margin ">
									<label class="<?php if (empty($_SESSION["cgiris"])) {echo "dn";}?>">Önceki Yüklemeler</label>
									<div class="input-with-button <?php if (empty($_SESSION["cgiris"])) {echo "dn";}?>">
										<label for="simple-action">.xlsx file</label>
										<?php $excelFiles = getExcelFiles($directory);?>
										<select name="excel_files" onChange="self.location.href=this.options[this.selectedIndex].value;">
											<option value="">Dosya Seçin</option>
											<?php foreach ($excelFiles as $file): ?>
												<option value="?ne=<?php echo $_GET["ne"]?>&islem=<?php echo $_GET["islem"]?>&durum=match&file=<?= htmlspecialchars($file) ?>">
													<?= htmlspecialchars(basename($file)) ?>
												</option>
											<?php endforeach; ?>
										</select>
									</div>

									<p class="db">
										<h3>Lütfen Dikkat</h3>
										- <strong>Kayıt limiti: <?php echo $klimit ?></strong><br><br>
										- <strong>Yükleyeceğiniz dosya yalnızca ".xlsx" uzantılı olmalıdır.</strong><br><br>
										<span class="text-success">
											* <strong>Yeni Kayıt (Ekleme)</strong><br>
											- Görsel dosya adlarında boşluk ve özel karakter bulunmamalıdır.<br>
											&nbsp;&nbsp;&nbsp;Örnek: <del>ürün 1 görseli.jpg</del> → <code>urun1_gorseli.jpg</code><br>
											- .xlsx dosyasındaki görsellerin isimleri, klasördeki görsel dosyalarıyla birebir aynı olmalıdır.<br>
											- Resim Upload, kategori ve varyasyon işlemleri sadece ekleme esnasında yapılabilir.<br><br>
										</span>
										<span class="text-warning">
											* <strong>Güncelleme</strong><br>
											- Yalnızca renkle vurgulanmış hücreler güncellenebilir. Tüm alanların doldurulması zorunlu değildir.<br>
											- “Eşleştirme sütunu” belirtilirse, mevcut kayıt güncellenir; belirtilmezse yeni kayıt olarak eklenir.<br>
											- Eşleştirme sütunu belirtildiğinde, dosyada ilgili sütun mutlaka bulunmalıdır.<br><br>
										</span>
										- <strong>Veri kaybı yaşamamak için “Örnek Excel Dosyası”nı kullanmanız ve açıklamaları dikkatlice okumanız tavsiye edilir.</strong>
									</p>
								</div>
							</fieldset>

						</form>
					<?php }elseif($_GET["durum"]=="xlsfiles"){?>
						<form class="form" method="post" action="?ne=<?php echo $_GET["ne"]?>&islem=<?php echo $_GET["islem"]?>&durum=uxlsfiles" enctype="multipart/form-data">
							<fieldset class="grey-bg no-margin">
								<label><?php echo translate("Excel Resimleri")?></label>
								<div class="input-with-button">
									<label for="simple-action"><?php echo translate("Resim .Zip Dosyası")?></label>
									<input type="file" name="dosya" class="form-control fl" style="width:70%" accept=".zip">
									<button type="submit"  class="v3-btn v3-btn-info fr"><?php echo translate("Yükle")?></button>
								</div>
							</fieldset>
							<p class="db">
								- ".zip" Dosyası Olmalı<br>
								- klasör ismi özel karakter içermemeli; örnek : <del>ürün görselleri.zip</del> -> urun_gorselleri.zip<br>
								- dosyalar direk ziplenmeli ayrı klasör olmamalı
							</p>
						</form>
					<?php }elseif($_GET["durum"]=="read"){?>
						<form class="form" name="rall" method="post" action="../xls/exporter.php?ne=read&path=../../../xls/" target="_blank">
							<fieldset class="grey-bg no-margin">
								<label>.xls file</label>
								<div class="input-with-button">
									<label for="simple-action">.xls file</label>
									<input type="text" name="xls" id="t6" aria-label="" onclick="window.open('../../upload/upload_all.php?ne=excel&file=excel', 'Upload', 'width=450,height=300')">
									<button type="submit"  class="v3-btn v3-btn-info"><?php echo $sdil[147]; //İleri !?></button>
								</div>
							</fieldset>
						</form>
						<?php
					}elseif($_GET["durum"]=="match"){
						if($_GET["file"]==""){
							$kaydedilecekyer = "../../../deppo/excel";
							$dkaynak = $_FILES['dosya']['tmp_name'];
							$dtipi	 = $_FILES['dosya']['type'];
							$dadi	 = $_FILES["dosya"]["name"];
							$duzanti = explode('.',$_FILES['dosya']['name']);
							$dyeni   = "import-".date("Ymd").".".$duzanti[count($duzanti)-1]."";
							move_uploaded_file ($_FILES['dosya']['tmp_name'], $kaydedilecekyer . "/".$dyeni);
							$dosya="../../../deppo/excel/".$dyeni."";
						}else{
							$dosya=$_GET["file"];
						}
						?>
						<form action="?ne=<?php echo $_GET["ne"]?>&islem=<?php echo $_GET["islem"]?>&durum=view" method="post" id="<?php echo $_GET["durum"]?>-form">
							<input type="hidden" name="xls" value="<?php echo $dosya;?>">
							<?php
							if($xlsx = SimpleXLSX::parse($dosya)){ $result=$xlsx->rows(); } else { $result=SimpleXLSX::parseError();}
							$e = 0; foreach ($ilist as $key => $value) {$e++;
								if ($e > 20) {$e = 1;}
								?>
								<fieldset class="grey-bg v3-col-md-3 <?php echo $value;?>" style="">
									<label><?php echo $key;?> <span style="font-size:8px; color:#ccc;"> (<?php echo $value;?>)</span> 
										<?php if($value=="varyasyon"){?><i class="fa fa-question-circle" title="Adı|fiyat|stok, şeklinde alınacak"></i><?php }?>
										<?php if($value=="images"){?><i class="fa fa-question-circle" title="[Folder] için ixls, [Sql] Aktar, [Url] (https) çekme"></i><?php }?>
										<?php if($value=="files"){?><i class="fa fa-question-circle" title="[Folder] için ixls, [Sql] Aktar, [Url] (https) çekme"></i><?php }?>
									</label>
									<?php
									if($value<>"images" and $value<>"files"){
										echo "<select name=\"".$value."\" id=\"".$value."\" style=\"width:95%;\">";
									}else{
										echo "<select name=\"".$value."\" id=\"".$value."\" style=\"width:70%; float:left;\">";
									}
									echo "<option value=\"\">...</option>";
									foreach($result[0] as $option){
										$col = array_search($option, $result[0]);
										if($option<>""){?>
											<option value="<?php echo $option?>,<?php echo $col?>,<?php echo $key?>" <?php if($_SESSION[$option]==$option.",".$col.",".$key){echo "selected";}?>><?php echo ihb($option)?></option>
										<?php }
									}
									echo "</select>";
									if($value=="images"){
										echo "
										<select style=\"float:right;text-align: right; width:29%;\" name=\"iattr\">
										<option value=\"url\">Url</option>
										<option value=\"folder\">ixls</option>
										<option value=\"sql\">Sql</option>
										</select>
										";
									}
									if($value=="files"){
										echo "
										<select style=\"float:right;text-align: right; width:29%;\" name=\"fattr\">
										<option value=\"url\">Url</option>
										<option value=\"folder\">ixls</option>
										<option value=\"sql\">Sql Aktar</option>
										</select>
										";
									}
									?>
									<?php if($value=="t2" || $value=="s4"){?>
										<p style="right: 10px; bottom: -20px;"><?php if($value=="t2"){echo "Varsayılan <b>".$kpb."</b>";}elseif($value=="s4"){echo "Varsayılan <b>%".$s12."</b>";}?></p>
									<?php }elseif($value=="images" || $value=="files"){?>
										<p style="right: 10px; bottom: -20px;">[Folder] için ixls, [Sql] Aktar, [Url] (https) çekme</p>
									<?php }?>
								</fieldset>
							<?php }?>
							<?php if($_GET["ne"]=="uye" and $module["cinfo"]=="1"){?>
								<fieldset class="grey-bg v3-col-md-3" style="">
									<label><i class="<?php echo $rsi["n14"]?>"></i> <?php echo translate("Firma Adı");?> <span style="font-size:8px; color:#ccc;"> (ifirma)</span></label>
									<?php
									echo "<select name=\"ifirma\" style=\"width:95%;\">";
									echo "<option value=\"\">...</option>";
									foreach($result[0] as $value){
										$col = array_search($value, $result[0]);
										if($value<>""){
											echo "<option value=\"".$value.",".$col.",".translate("Firma Adı")."\">".ihb($value)."</option>";
										}
									}
									echo "</select>";
									?>
								</fieldset>
								<fieldset class="grey-bg v3-col-md-3" style="">
									<label><i class="<?php echo $rsi["n14"]?>"></i> <?php echo translate("Unvanı");?> <span style="font-size:8px; color:#ccc;"> (iunvan)</span></label>
									<?php
									echo "<select name=\"iunvan\" style=\"width:95%;\">";
									echo "<option value=\"\">...</option>";
									foreach($result[0] as $value){
										$col = array_search($value, $result[0]);
										if($value<>""){
											echo "<option value=\"".$value.",".$col.",".translate("Unvanı")."\">".ihb($value)."</option>";
										}
									}
									echo "</select>";
									?>
								</fieldset>
								<fieldset class="grey-bg v3-col-md-3" style="">
									<label><i class="<?php echo $rsi["n14"]?>"></i> <?php echo translate("Vergi Dairesi");?> <span style="font-size:8px; color:#ccc;"> (ivdaire)</span></label>
									<?php
									echo "<select name=\"ivdaire\" style=\"width:95%;\">";
									echo "<option value=\"\">...</option>";
									foreach($result[0] as $value){
										$col = array_search($value, $result[0]);
										if($value<>""){
											echo "<option value=\"".$value.",".$col.",".translate("Vergi Dairesi")."\">".ihb($value)."</option>";
										}
									}
									echo "</select>";
									?>
								</fieldset>
								<fieldset class="grey-bg v3-col-md-3" style="">
									<label><i class="<?php echo $rsi["n14"]?>"></i> <?php echo translate("Vergi Numarası");?> <span style="font-size:8px; color:#ccc;"> (ivnumara)</span></label>
									<?php
									echo "<select name=\"ivnumara\" style=\"width:95%;\">";
									echo "<option value=\"\">...</option>";
									foreach($result[0] as $value){
										$col = array_search($value, $result[0]);
										if($value<>""){
											echo "<option value=\"".$value.",".$col.",".translate("Vergi Numarası")."\">".ihb($value)."</option>";
										}
									}
									echo "</select>";
									?>
								</fieldset>
							<?php }?>
							<?php if($_GET["ne"]=="uye" and $module["uye_bankinfo"]=="1"){?>
								<fieldset class="grey-bg v3-col-md-3" style="">
									<label><?php echo translate("Hesap Adı");?> <span style="font-size:8px; color:#ccc;"> (ihesap1_adi)</span></label>
									<?php
									echo "<select name=\"ihesap1_adi\" style=\"width:95%;\">";
									echo "<option value=\"\">...</option>";
									foreach($result[0] as $value){
										$col = array_search($value, $result[0]);
										if($value<>""){
											echo "<option value=\"".$value.",".$col.",".translate("Hesap Adı")."\">".ihb($value)."</option>";
										}
									}
									echo "</select>";
									?>
								</fieldset>
								<fieldset class="grey-bg v3-col-md-3" style="">
									<label><?php echo translate("Hesap Şube");?> <span style="font-size:8px; color:#ccc;"> (ihesap1_sube)</span></label>
									<?php
									echo "<select name=\"ihesap1_sube\" style=\"width:95%;\">";
									echo "<option value=\"\">...</option>";
									foreach($result[0] as $value){
										$col = array_search($value, $result[0]);
										if($value<>""){
											echo "<option value=\"".$value.",".$col.",".translate("Hesap Şube")."\">".ihb($value)."</option>";
										}
									}
									echo "</select>";
									?>
								</fieldset>
								<fieldset class="grey-bg v3-col-md-3" style="">
									<label><i class="<?php echo $rsi["n14"]?>"></i> <?php echo translate("Hesap No");?> <span style="font-size:8px; color:#ccc;"> (ihesap1_no)</span></label>
									<?php
									echo "<select name=\"ihesap1_no\" style=\"width:95%;\">";
									echo "<option value=\"\">...</option>";
									foreach($result[0] as $value){
										$col = array_search($value, $result[0]);
										if($value<>""){
											echo "<option value=\"".$value.",".$col.",".translate("Hesap No")."\">".ihb($value)."</option>";
										}
									}
									echo "</select>";
									?>
								</fieldset>
								<fieldset class="grey-bg v3-col-md-3" style="">
									<label><i class="<?php echo $rsi["n14"]?>"></i> <?php echo translate("Hesap Pb");?> <span style="font-size:8px; color:#ccc;"> (ihesap1_pb)</span></label>
									<?php
									echo "<select name=\"ihesap1_pb\" style=\"width:95%;\">";
									echo "<option value=\"\">...</option>";
									foreach($result[0] as $value){
										$col = array_search($value, $result[0]);
										if($value<>""){
											echo "<option value=\"".$value.",".$col.",".translate("Hesap Pb")."\">".ihb($value)."</option>";
										}
									}
									echo "</select>";
									?>
								</fieldset>
								<fieldset class="grey-bg v3-col-md-3" style="">
									<label><i class="<?php echo $rsi["n14"]?>"></i> <?php echo translate("Hesap Iban");?> <span style="font-size:8px; color:#ccc;"> (ihesap1_iban)</span></label>
									<?php
									echo "<select name=\"ihesap1_iban\" style=\"width:95%;\">";
									echo "<option value=\"\">...</option>";
									foreach($result[0] as $value){
										$col = array_search($value, $result[0]);
										if($value<>""){
											echo "<option value=\"".$value.",".$col.",".translate("Hesap Iban")."\">".ihb($value)."</option>";
										}
									}
									echo "</select>";
									?>
								</fieldset>
							<?php }?>
							<?php
							$i=0;$sqli=mysqli_query($conn, "select * from genel where ne='inputs' and n3='".$_GET["ne"]."' and n4<>'switch' and s1=0 order by s10,id asc"); while($rsi=mysqli_fetch_assoc($sqli)){$i++;
								if($rsi["t3"]<>""){$ival=$rsi["t3"];}else{$ival = "i".$i."";}
								?>
								<fieldset class="grey-bg v3-col-md-3" style="">
									<label><i class="<?php echo $rsi["n14"]?>"></i> <?php echo $rsi["n1"];?> <span style="font-size:8px; color:#ccc;"> (i<?php echo $ival;?>)</span></label>
									<?php
									echo "<select name=\"".$ival."\" style=\"width:75%;\" id=\"".$ival."\">";
									echo "<option value=\"\">...</option>";
									foreach($result[0] as $value){
										$col = array_search($value, $result[0]);
										if($value<>""){ echo "<option value=\"".$value.",".$col.",".$rsi["n1"]."\" data-value=\"".$value.",".$col.",".$rsi["n1"]."\">".ihb($value)."</option>"; }
									}
									echo "</select>";
									echo "
									<select style=\"float:right;text-align: right;padding: 5px; font-size: 12px;\" id=\"t".$ival."\" onchange=\"setvalue('".$ival."')\">
									<option value=\"\">...</option>
									<option value=\"money\">Fiyat</option>
									<option value=\"select\">Seçenek</option>
									</select>
									";
									?>
								</fieldset>
							<?php }?>
							<fieldset class="grey-bg v3-col-md-3 lang" style="">
								<label>Dil <i class="fa fa-question-circle" title="Sadece Update için kullanılır."></i></label>
								<select name="lang" style="width:95%;">
									<option value="">...</option>
									<?php
									if(!empty($m1)){
										$i = 0;$diller = explode(',',$m1); foreach($diller as $dil) {$i++; if($i > 99) {$i = 1;}
										?>
										<option value="lang<?php echo $i?>" <?php if($_SESSION["lang"]=="lang".$i.""){echo "Selected";}?>><?php echo $dil;?></option>";
									<?php }}?>
								</select>
							</fieldset>
							<fieldset class="grey-bg v3-col-md-3 urow" style="">
								<label>Eşleştirme Sütunu <i class="fa fa-question-circle" title="Güncelleme işleminde Eşleştirme Sütunu"></i></label>
								<select name="urow" style="width:95%;">
									<option value="">...</option>
									<?php if($_GET["ne"]=="kayit"){?>
										<option value="t1" <?php if($_SESSION["urow"]=="t1"){echo "selected";}?>>Başlık</option>
										<option value="t4"  <?php if($_SESSION["urow"]=="t4"){echo "selected";}elseif($_SESSION["urow"]==""){echo "selected";}?>>Stok No</option>
										<option value="t5" <?php if($_SESSION["urow"]=="t5"){echo "selected";}?>>Barkod No</option>
									<?php }elseif($_GET["ne"]=="haber"){?>
										<option value="t1" <?php if($_SESSION["urow"]=="t1"){echo "selected";}?>>Başlık</option>
									<?php }else{?>
										<option value="t1" <?php if($_SESSION["urow"]=="t1"){echo "selected";}?>>Ad Soyad</option>
										<option value="t4"  <?php if($_SESSION["urow"]=="t4"){echo "selected";}elseif($_SESSION["urow"]==""){echo "selected";}?>>Gsm</option>
										<option value="t2" <?php if($_SESSION["urow"]=="t2"){echo "selected";}?>>Email</option>
										<option value="t5" <?php if($_SESSION["urow"]=="t5"){echo "selected";}?>>Üye Kod</option>
										<option value="t13" <?php if($_SESSION["urow"]=="t13"){echo "selected";}?>>Kimlik No</option>
									<?php }?>
								</select>
							</fieldset>
							<fieldset class="grey-bg v3-col-md-3 rows" style="">
								<label>Import Limit <i class="fa fa-question-circle" title="Boş olması durumunda tüm kayılar, min:2"></i></label>
								<input type="text" name="rows" class="form-control fl" style="width:95%" placeholder="<?php echo $_SESSION["limit_xls"]?>" value="<?php echo $_SESSION["limit_xls"];?>">
							</fieldset>
							<fieldset class="grey-bg v3-col-md-3 rows" style="">											
								<label>Detay resim URL <i class="fa fa-question-circle" title="detay resim URL"></i></label>
								<input type="text" name="xipath" class="form-control fl" style="width:95%" placeholder="detay resim URL" value="<?php echo $_SESSION["xipath"];?>" title="Eğer ürün açıklamasında kullanılan görseller varsa, her biri için tam URL girilmelidir. import_images/ klasörüne upload edilir">
								<p style="right: 10px; bottom: -20px;">*Ürün açıklamasındaki görselleri çekmek için URL girilmelidir </p>
							</fieldset>
							<fieldset class="grey-bg v3-col-md-3 dn" style="">
								<label>Kategorileri İşleme! <i class="fa fa-question-circle" title="Mevcut kategorilere ekleme yapma"></i></label>
								<input type="checkbox" value="1" name="kategorisiz">
							</fieldset>
							<button class="v3-btn-xs v3-btn-warning" style="display:none;" type="submit"><?php echo translate("Önizle")?> <i class="fa fa-angle-right" aria-hidden="true"></i></button>
						</form>
						<?php
					}elseif($_GET["durum"]=="import" or $_GET["durum"]=="view"){
						if($xlsx = SimpleXLSX::parse($_POST["xls"])){
							$result=$xlsx->rows();
						} else {
							$result=SimpleXLSX::parseError();
						}
						$available = 0; $unavailable = 0;

								// 1. Urow alanını yakala
								$urow = $_POST["urow"]; // örnek: t4
								$urowInfo = explode(",", $_POST[$urow]); // örnek: stockCode,0,Stok No
								$columnIndex = (int) $urowInfo[1];       // Excel'deki kolon indexi
								$fieldName = $urow;                      // SQL'deki alan adı (örnek: t4)

								// 2. Excel’den eşleşme için kullanılacak tüm değerleri topla
								$importedKeys = [];
								foreach ($result as $rowIndex => $value) {
									if ($rowIndex > $flimit && $rowIndex <= $llimit) {
										$cellValue = trim($value[$columnIndex]);
										if ($cellValue !== "") {
											$importedKeys[] = mysqli_real_escape_string($conn, $cellValue);
										}
									}
								}

								// 3. SQL'den topluca kontrol et
								if (count($importedKeys) > 0) {
									$inClause = "'" . implode("','", $importedKeys) . "'"; $existingKeys = [];
									$checkQuery = mysqli_query($conn, "SELECT `$fieldName` FROM `{$_SESSION['lang']}` WHERE ne='kayit' AND `$fieldName` IN ($inClause)"); while ($row = mysqli_fetch_assoc($checkQuery)) { $existingKeys[] = $row[$fieldName]; }
									$existingKeys = array_flip($existingKeys); // Hızlı kontrol için
								}

								// Kuyruk JS dizisi oluşturuluyor
								echo "<script>var formQueue = [];</script>";
								$row = -1;
								foreach ($result as $value) {
									$row++;
									if ($row > $flimit && $row <= $llimit) {
										echo "<script>formQueue.push('if".$row."');</script>";
									}
								}
								echo "<table style='width:100%; font-size:12px;' class=\"table table-hover filterable\">";
								echo "<tr>";
								echo "<td class=\"w1\" style=\"background-color:#ccc !important; color:#fff !important;\">Durum</td>";
								echo "<td style=\"background-color:#ccc !important; color:#fff !important;\">ID</td>";
								foreach ($_POST as $iname => $ival){
									$_SESSION[$iname] = $ival;
									if($_POST[$iname]<>"" and $iname<>"xls"){
										$iss = explode(',',$ival);
										echo "<td style=\"background-color:#ccc !important; color:#fff !important;\">".$iss[2]."</td>";
									}
								}
								//echo "<td style=\"background-color:#ccc !important; color:#fff !important;\">Lang</td>";
								echo "</tr>";
								$row=-1; $total = count($result);
								foreach($result as $value){$row++;
									if ($row > $flimit && $row <= $llimit){

										$cellValue = trim($value[$columnIndex]);
										$isExisting = isset($existingKeys[$cellValue]);

										if ($isExisting) {
											$available++;
											$icon = '<i class="fas fa-database text-success" title="Zaten mevcut" style="opacity:0.6;"></i>';
										} else {
											$unavailable++;
											$icon = '<i class="fas fa-database text-warning" title="Yeni eklenecek" style="opacity:0.7;"></i>';
										}

										echo "<form id=\"if".$row."\" enctype=\"multipart/form-data\">";
										echo "<tr style=\"cursor:pointer;\" id=\"row".$row."\" class=\"addrow\" onclick=\"AjaxPResult('../xls/xls_import.php?ne=".$_GET["ne"]."&islem=".$_GET["islem"]."&durum=kaydet&row=".$row."','#if".$row."','#r".$row."','0');\">";
										echo "<td><div id=\"r".$row."\" style=\"cursor:pointer;\">".$icon."</td>";
										echo "<td>".$row."</td>";
										echo "<input type='hidden' value='".$_POST["iattr"]."' name='iattr'>";
										echo "<input type='hidden' value='".$_POST["fattr"]."' name='fattr'>";
										echo "<input type='hidden' value='".$_POST["urow"]."' name='urow'>";
										echo "<input type='hidden' value='".$_POST["lang"]."' name='lang'>";
										echo "<input type='hidden' value='".$_POST["xipath"]."' name='xipath'>";
										foreach ($_POST as $iname => $ival){
											if($_POST[$iname]<>"" and $iname<>"xls"){

												$iss = explode(',',$_POST[$iname]);
												if($iname=="n1"){
													echo "<td>" . htmlspecialchars(kisalt(strip_tags($value[$iss[1]]), 50)) . "</td>";
													echo "<textarea name=\"".$iname."\" style=\"display:none;\">".$value[$iss[1]]."</textarea>";
												}elseif($iname=="t1" or $iname=="n2"){
													echo "<td title=\"".$title."\">".kisalt($deger,30)."</td><textarea name=\"".$iname."\" style=\"display:none;\">".$deger."</textarea>";
												}elseif($iname=="images"){
													$imgs = array_filter(explode(',', $deger));
													$thumbs = '';
													$maxShow = 3;
													$count = count($imgs);
													foreach(array_slice($imgs, 0, $maxShow) as $img){
														$thumbs .= "<a href='".trim($img)."' target='_blank'><img src='".trim($img)."' style='width:40px;height:40px;object-fit:cover;border-radius:4px;margin-right:3px;border:1px solid #ccc;'></a>";
													}
													if($count > $maxShow) $thumbs .= "<span style='font-size:10px;color:#999;'>+".($count-$maxShow)."</span>";
													echo "<td>".$thumbs."</td><textarea name=\"".$iname."\" style=\"display:none;\">".$deger."</textarea>";
												}elseif($iname=="t4" or $iname=="t7"){
													echo "<td title=\"".$value[$iss[1]]."\">".telefon($value[$iss[1]],10)."</td>";
													echo "<textarea name=\"".$iname."\" style=\"display:none;\">".$value[$iss[1]]."</textarea>";
												}elseif($iname=="pb1" or $iname=="pb2"){
													echo "<td>".wpara($value[$iss[1]])."</td>";
													echo "<textarea name=\"".$iname."\" style=\"display:none;\">".wprice($value[$iss[1]])."</textarea>";
												}else{
													if($iss[3]=="money"){
														echo "<td title=\"".$value[$iss[1]]."\">".wpara($value[$iss[1]])."</td>";
														echo "<textarea name=\"".$iname."\" style=\"display:none;\">".wprice($value[$iss[1]])."</textarea>";
													}elseif($iss[3]=="select"){
														echo "<td title=\"".$value[$iss[1]]."\">".$value[$iss[1]]."</td>";
														echo "<textarea name=\"".$iname."[]\" style=\"display:none;\">".$value[$iss[1]]."</textarea>";
													}else{
														echo "<td title=\"".$value[$iss[1]]."\">".$value[$iss[1]]."</td>";
														echo "<textarea name=\"".$iname."\" style=\"display:none;\">".$value[$iss[1]]."</textarea>";
													}
												}
											}
										}
										echo "</tr>";
										echo "</form>";
									}
								}
								echo "</table>";
								$totalpage = ceil($total / $_SESSION["limit_xls"]);
								$kalan_sayfa = $totalpage - $page;
								$start_row = ($page - 1) * $_SESSION["limit_xls"] + 1;
								$end_row = min($total, $page * $_SESSION["limit_xls"]);
								?>
								<script>
									let isPaused = false;
									let currentIndex = 0;

									function processQueue(index) {
										<?php if($page < $totalpage){ ?>
											if (index >= formQueue.length) {
											// liste tamamlandığında formu gönder
												document.getElementById('NextForm').submit();
												return;
											}
										<?php } ?>
										if (isPaused) return; // Eğer duraklatıldıysa çık
										currentIndex = index; // Şu anki index'i kaydet
										var formId = '#' + formQueue[index];
										var rowNum = formQueue[index].replace('if', '');
										var resultDiv = '#r' + rowNum;
										var url = '../xls/xls_import.php?ne=<?= $_GET["ne"] ?>&islem=<?= $_GET["islem"] ?>&durum=kaydet&row=' + rowNum;

										AjaxPResult(url, formId, resultDiv, 'nopl', '', function () {
											var rem1 = document.getElementById('remainder1'); // işlenen
											if (rem1) rem1.innerText = parseInt(rem1.innerText) + 1;
											processQueue(index + 1);
										});
									}

									function pauseQueue() { isPaused = true; }

									function resumeQueue() {
										if (!isPaused) return;
										isPaused = false;
										processQueue(currentIndex);
									}

									<?php if ($_GET["durum"] == "import"){ ?>
										jQuery(document).ready(function () { processQueue(0); });
									<?php } ?>
								</script>

								<?php if($_GET["durum"]=="view"){?>
									<form action="?ne=<?php echo $_GET["ne"]?>&islem=<?php echo $_GET["islem"]?>&durum=import&sayfa=<?php echo $_GET["sayfa"]?>" method="post" id="<?php echo $_GET["durum"]?>-form">
										<?php foreach ($_POST as $iname => $ival){ ?>
											<input type="hidden" name="<?php echo $iname?>" value="<?php echo $ival?>">
										<?php }?>
										<button class="v3-btn-xs v3-btn-danger" id="ibtn" style="display:none;" type="submit"><?php echo translate("İçe Aktar")?> <i class="fa fa-angle-right" aria-hidden="true"></i></button>
									</form>
								<?php }?>

								<div style="font-size: 12px; width: 183px; position: fixed; left: 10px; bottom: 30px; padding: 10px; background: #f9f9f94a;">
									<?php
									$limit = $_SESSION["limit_xls"]; $durumMetni = "";
									if ($available == $limit) {$durumMetni = "Liste <b>Ekli</b>";} elseif ($unavailable == $limit) {$durumMetni = "Liste <b>Eklenecek</b>";} else {$durumMetni = "Listede <b>$available</b> ekli, <b>$unavailable</b> eklenecek";}
									?>
									<?php echo "Toplam <b>$total</b> kayıttan <b>$start_row - $end_row</b> arasındaki kayıtlar, her sayfada <b>$limit</b> (<span id='remainder1'>0</span> kayıt aktarıldı)<br>Sayfa: <b>" . $page . "</b> / <b>$totalpage</b><br>$durumMetni<br><br>";?>
									
									<?php if($page <= $totalpage){ ?>
										<center>
											<form id="PrevForm" method="post" action="?ne=<?php echo $_GET["ne"]?>&islem=<?php echo $_GET["islem"]?>&durum=<?php echo $_GET["durum"]?>&sayfa=<?php echo $page-1;?>" style="float:left;">
												<?php foreach ($_POST as $iname => $ival){echo "<input type=\"hidden\" name=\"".$iname."\" value=\"".$ival."\">";}?>
												<button class="v3-btn v3-btn-info" style="" type="submit"><i class="fas fa-angle-left"></i> Geri </button>
											</form>

											<form id="NextForm" method="post" action="?ne=<?php echo $_GET["ne"]?>&islem=<?php echo $_GET["islem"]?>&durum=<?php echo $_GET["durum"]?>&sayfa=<?php echo $page+1;?>" style="float:right;">
												<?php foreach ($_POST as $iname => $ival){echo "<input type=\"hidden\" name=\"".$iname."\" value=\"".$ival."\">";}?>
												<button class="v3-btn v3-btn-warning" style="" type="submit"> İleri <span style="font-size:12px;"><?php echo $totalpage?> <i class="fas fa-angle-right"></i> <?php echo $page+1;?></span></button>
											</form>
										</center>
									<?php }?>
								</div>

							<?php }elseif($_GET["durum"]=="exls"){ ?>
								<form action="../xls/xls_export.php?ne=<?php echo $_GET["ne"]?>&islem=<?php echo $_GET["islem"]?>&durum=exls&py=<?php echo $_GET["py"]?>" target="_blank" method="post" id="exls-form">
									<div class="v3-col-md-12 v3-col-xs-12">
										<input type="text" class="v3-form-control" name="q" value="" style="" placeholder="filtre">
									</div>
									<div class="v3-col-md-4 v3-col-xs-12">
										<select name="c1" id="sc1" class="form-control">
											<option value="0">...</option>
											<?php
											foreach($ucats as $key=>$value){
												if(($value["ne"]=="".$q."kategori" or ($value["t1"]=="".$_GET["ne"]."" and $value["s1"]=="0"))){$value["id"]==$rs["s9"] ? $selected="selected" : $selected="";
												echo "<option value=\"".$value["id"]."\" id=\"".$value["id"]."\" ".$selected.">".$value["item"]."</option>";
											}
										}
										?>
									</select>
								</div>
								<div class="v3-col-md-4 v3-col-xs-12">
									<select name="c2" id="sc2" class="form-control">
										<option value="0">...</option>
									</select>
								</div>
								<div class="v3-col-md-4 v3-col-xs-12">
									<select name="c3" id="sc3" class="form-control">
										<option value="0">...</option>
									</select>
								</div>
								<script type="text/javascript">
									jQuery('#sc1').on('change', function(){
										var ID = jQuery('#sc1 option:selected').attr('ID');
										jQuery('#sc2').load('../ajax/categorie<?php if($module["ajax_categories"]<>""){echo "s";}?>_ajax.php?ne=<?php echo $_GET["ne"]?>&islem=kategori&islem=<?php echo $q?>alt_kategori&c='+ID);
									});
									jQuery('#sc2').on('change', function(){
										var ID = jQuery('#sc2 option:selected').attr('ID');
										jQuery('#sc3').load('../ajax/categorie<?php if($module["ajax_categories"]<>""){echo "s";}?>_ajax.php?ne=<?php echo $_GET["ne"]?>&islem=kategori&islem=<?php echo $q?>alt_kategori2&c='+ID);
									})
								</script>
								<?php $i = 0; foreach($ilist as $label => $value) { $i++; ?>
									<div class="v3-col-md-2 v3-col-xs-6">
										<label><?php echo $label; ?></label>
										<input type="checkbox" name="exp[]" class="dchck" value="<?php echo $label . ':' . $value; ?>" style="width: 24px; height: 24px;">
									</div>
								<?php } ?>
								<?php
								$i=0;$sqli=mysqli_query($conn, "select * from genel where ne='inputs' and t2='".$_SESSION["lang"]."' and n3='".$_GET["ne"]."' and n4<>'switch' and s1=0 order by s10 asc"); while($rsi=mysqli_fetch_assoc($sqli)){$i++;
									if($rsi["t3"]<>""){$ival=$rsi["t3"];}else{$ival = "i".$i."";}
									?>
									<div class="v3-col-md-2 v3-col-xs-6">
										<label><?php echo $rsi["n1"];?></label>
										<input type="checkbox" name="exp[]" class="dchck" value="<?php echo $rsi["n1"].":".$ival;?>" style="width: 24px; height: 24px;">
									</div>
								<?php }?>
								<div class="v3-col-md-12" style="clear:both;">
									<button type="submit" id="ebtn" class="v3-btn v3-btn-success" style="float:right;display:none">Export</button>
								</div>
							</form>
						<?php }?>
					</div>
				</div>
			</body>

		<script>
			function setvalue(id){
				var addval = jQuery("#t"+id+" option:selected").val();
				jQuery("#"+id+" option").each(function(){
					var getval = jQuery(this).attr("data-value");
					if(this.value!=""){
						jQuery(this).val(''+getval+','+addval+'');
					}
				});
				return false;
			}
			jQuery( ".continue" ).click(function() {
				jQuery('.addrow').each(function(i, obj) {
					jQuery('.addrow').trigger('click');
				});
			});
			jQuery(document).ready(function() {
				check_urow();
			});
			jQuery("[name='t1'], [name='t2'], [name='t4'], [name='t5'], [name='urow']").change(function() {
				check_urow();
			});
			function check_urow(){
				var urow = jQuery('select[name="urow"]').find(":selected").val();
				var t1 = jQuery('select[name="t1"]').find(":selected").val();
				var t2 = jQuery('select[name="t2"]').find(":selected").val();
				var t4 = jQuery('select[name="t4"]').find(":selected").val();
				var t5 = jQuery('select[name="t5"]').find(":selected").val();
				var pb1 = jQuery('select[name="pb1"]').find(":selected").val();
				var s5 = jQuery('select[name="s5"]').find(":selected").val();
				jQuery('.t1, .t2, .t4, .t5').removeClass('bg-danger');
				jQuery('.'+urow+'').addClass('bg-danger');
				<?php if($_GET["ne"]=="kayit"){?>
					if((urow=="t1" && t1=="Ürün Adı,0, Stok No") || (urow=="t4" && t4=="Stok No,4, Stok No") || (urow=="t5" && t5=="Barkod No,5, Barkod No")){
						//jQuery('.v3-btn-warning').removeAttr('disabled');
					}else{
						//jQuery('.v3-btn-warning').attr('disabled', 'disabled');
					}
				<?php }elseif($_GET["ne"]=="uye"){?>
					if((urow=="t2" && t2=="Email,6, Gsm No") || (urow=="t4" && t4=="Gsm,5, Gsm No")){
						//jQuery('.v3-btn-warning').removeAttr('disabled');
					}else{
						//jQuery('.v3-btn-warning').attr('disabled', 'disabled');
					}
				<?php }?>
				jQuery('.t1').addClass('bg-warning');
				return false;
			}
		</script>
		<?php
		if($_GET["durum"]=="uxlsfiles"){
			unlink("../../../ixls");
			$kaydedilecekyer = "../../../";
			$adi		= $_FILES["dosya"]["name"];
			move_uploaded_file ($_FILES['dosya']['tmp_name'], $kaydedilecekyer . "/".$_FILES["dosya"]["name"]);
			$zip = new ZipArchive;
			$res = $zip->open("../../../".$_FILES["dosya"]["name"]."");
			if ($res === TRUE) {
				$zip->extractTo('../../../ixls/');
				$zip->close();
				unlink("../../../".$_FILES["dosya"]["name"]."");
			}else{
				echo "Zip Err";
			}
			echo location("?ne=kayit&islem=xls&durum=ixls",self,500,1);
		}
		include("../public/log.php");
		?>
		</html>