<script src="//code.jquery.com/jquery-latest.js" type="text/javascript"></script>
<link rel="stylesheet" href="//cdn.v3medya.com/css/watolye.css" type="text/css" /> 
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
<style>
	.whatsapp, .v3statusac, .kvkk, .call, .soldbox {display:none !important;}
	label { display: block; }
	fieldset { margin: 8px !important; width: 23% !important; border: 0px !important;}
	.lang, .s12, .s19, .urow, .rows {display:none;}
</style>
<?php
$path="../../../../";
if(file_exists("".$path."config/map.php" )){$path2="config/"; include("".$path."config/map.php");}else{include("".$path."map.php");}
include("../fonksiyon".$ver.".php");
include("../cevir.php");
include("../../inc.php");
header("Content-Type: text/html; charset=UTF-8");
require_once "../simple_xlsx.php";
if($_GET["ne"]=="kayit"){
	if($g8==1){ $xg8="Marka:i1,"; }
	if($a7==1){ $xa7="Varyasyon:varyasyon,"; }
	if($e5==1){ $xe5="Barkod No:t5,Liste Fiyatı:pb2,Satış Fiyatı:pb1,Para Birimi:t2,Kdv:s4,";}
	if($m16==1){ $xm16="Stok:s5,Desi:s6,";}
	if($a3>0){ $xa3="Dosya:files,";}
	$ilist="Başlık:t1, Kısa Açıklama: n2,Açıklama:n1, Keywords:n4, Stok No:t4, ".$xg8." ".$xe5." ".$xm16." ".$xa3." Resim:images, Kategori[1]:s9, Kategori[2]:s10, Kategori[3]:s11, ".$xa7." Sıra:s12, İşlem Sorumlusu:s19";
}elseif($_GET["ne"]=="uye"){
	$ilist="Ad Soyad:t1, Doğum Tarihi:t12, IDN:t11, Gsm No:t4, Telefon:t7, E-Mail:t2, Şifre:t3, Adres:n5, İşlem Sorumlusu:s19";
}
if ($_GET["ne"] == "kayit"){
	$q="u";
}elseif ($_GET["ne"] == "uye"){
	$q="k";
}else{
	$q=substr($_GET["ne"],0,1);
}
if($module["limit_xls"]<>""){$klimit = $module["limit_xls"];}else{$klimit = "999";}
if($_GET["sayfa"] == "" or $_GET["sayfa"] == "1") {
	$page = 1; $flimit=0; $llimit= $klimit;
}else{
	$page = $_GET["sayfa"]; $flimit=(($_GET["sayfa"] * $klimit) - $klimit); $llimit= $flimit+$klimit;
}
$dosya	= realpath("../../../excel/".$_POST["xls"]."");
echo $header;
?>
<?php if($_GET["durum"]=="ixls"){?>
	<div class="alert-warning mb20 p3 mmb20 mp3"  style="margin-bottom:10px;"><i class="fa fa-info-circle" aria-hidden="true"></i> <?php echo translate("Lütfen .xslx dosyanızı gözat butonuna tıklayarak upload edin ve ileri butonuna basınız")?> <a href="//cdn.<?php echo $cdnurl?>/tools/ornek_kayit.xlsx" class="v3-btn v3-btn-xs v3-btn-warning" style="float:right;">Örnek .xslx İndir</a></div>
	<form class="form" name="rall" method="post" action="?ne=<?php echo $_GET["ne"]?>&islem=<?php echo $_GET["islem"]?>&durum=esle">
		<input type="text" name="xls" id="t6" class="v3-form-control" aria-label="" style="width:90%; float:left; margin-right:10px;">
		<button type="button"  class="v3-btn v3-btn-warning"  onclick="window.open('../../../../<?php echo $crmpath?>/modules/upload/upload_all.php?ne=excel&file=excel', 'Upload', 'width=450,height=300')"><?php echo translate("Gözat")?></button>
		<button type="submit"  class="v3-btn v3-btn-info"><?php echo translate("İleri")?></button>
	</form>
	<p class="db">
		<h3 style="font-size:24px;">Lütfen Dikkat</h3>
		- <strong>Kayıt Limiti <?php echo $klimit?></strong><br><br>
		- <strong>import edeceğiniz dosya ".xlsx" Uzantılı Olmalı</strong><br><br>
		<span class="text-success">
			* <strong>Ekleme</strong><br>
			- Resimler boşluk ve özel karakterler içermemeli; örnek : <del>ürün 1 görseli.jpg</del> -> urun1_gorseli.jpg<br>
			- .xlsx dosyasındaki resimler ile klasör içindeki resimler aynı isimde olmalı<br><br>
		</span>
		<span class="text-warning">
			* <strong>Güncelleme</strong><br>
			- Sadece renkli kutu alanları güncellenebilir, hepsi girilmesi zorunlu değildir.<br>
			- Eşleştirme sütunu belirtilirse, mevcut kayıt güncellenir, belirtilmezse kayıt kontrolü yapmadan yenisi eklenir.<br>
			- Eşleştirme sütunu belirtildiğinde, mutlaka ilgili sütunda gönderilmelidir.<br><br>
		</span>
		- <strong>Sorun yaşamamak için lütfen "Örnek Excel Dosyası"nı kullanınız, ve açıklamaları okuyunuz</strong>
	</p>
<?php }elseif($_GET["durum"]=="esle"){?>
	<form action="?ne=<?php echo $_GET["ne"]?>&islem=<?php echo $_GET["islem"]?>&durum=goster" method="post">
		<input type="hidden" name="xls" value="<?php echo $dosya;?>">
		<?php
		if($xlsx = SimpleXLSX::parse($dosya)){
			$result=$xlsx->rows();
		} else {
			$result=SimpleXLSX::parseError();
		}
		$extras = explode(',',$ilist); foreach($extras as $ibaslik) { $e++; if($e > 20) {$e = 1;} //sayı arttırma
		$extra2 = explode(':',$ibaslik);
		?>
		<fieldset class="grey-bg v3-col-md-3 <?php echo $extra2[1];?>" style="<?php if($extra2[1]=="t1" or $extra2[1]=="t2" or $extra2[1]=="s4" or $extra2[1]=="s5" or $extra2[1]=="pb1" or $extra2[1]=="pb2"){echo "color:red;";}?>">
			<label style="display: block;"><?php echo $extra2[0];?> <span style="font-size:8px; color:#ccc;"> (<?php echo $extra2[1];?>)</span> 
				<?php if($extra2[1]=="varyasyon"){?><i class="fa fa-question-circle" title="Adı|fiyat|stok, şeklinde alınacak"></i><?php }?>
				<?php if($extra2[1]=="images"){?><i class="fa fa-question-circle" title="[Folder] için ixls, [Sql] Aktar, [Url] (https) çekme"></i><?php }?>
				<?php if($extra2[1]=="files"){?><i class="fa fa-question-circle" title="[Folder] için ixls, [Sql] Aktar, [Url] (https) çekme"></i><?php }?>
			</label>
			<?php
			if($extra2[1]<>"images" and $extra2[1]<>"files"){
				echo "<select name=\"".$extra2[1]."\" style=\"width:95%;\">";
			}else{
				echo "<select name=\"".$extra2[1]."\" style=\"width:70%; float:left;\">";
			}
			echo "<option value=\"\">...</option>";
			foreach($result[0] as $value){
				$col = array_search($value, $result[0]);
				if($value<>""){
					echo "<option value=\"".$value.",".$col.",".$extra2[0]."\">".ihb($value)."</option>";
				}
			}
			echo "</select>";
			if($extra2[1]=="images"){
				echo "
				<select style=\"float:right;text-align: right;width: 29%;\" name=\"iattr\">
				<option value=\"folder\">ixls</option>
				<option value=\"sql\">Sql</option>
				<option value=\"url\">Url</option>
				</select>
				";
			}
			if($extra2[1]=="files"){
				echo "
				<select style=\"float:right;text-align: right;width: 29%;\" name=\"fattr\">
				<option value=\"folder\">Folder</option>
				<option value=\"sql\">Sql Aktar</option>
				</select>
				";
			}
			?>
		</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("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++;?>
		<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 $i;?>)</span></label>
			<?php
			echo "<select name=\"i".$i."\" style=\"width:75%;\" id=\"i".$i."\">";
			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;\" id=\"ti".$i."\" onchange=\"setvalue('i".$i."')\">
			<option value=\"\">...</option>
			<option value=\"money\">Price</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">Başlık</option>
				<option value="t4" selected>Stok No</option>
				<option value="t5">Barkod No</option>
			<?php }else{?>
				<option value="t1">Ad Soyad</option>
				<option value="t4" selected>Gsm</option>
				<option value="t2">Email</option>
				<option value="t13">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="0,10">
	</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 v3-btn-warning" style="float: right;margin-right: 24px; margin-top: 10px; clear:both;" type="submit">İleri</button>
</form>
<?php
}elseif($_GET["durum"]=="goster"){
	if($xlsx = SimpleXLSX::parse($_POST["xls"])){
		$result=$xlsx->rows();
	} else {
		$result=SimpleXLSX::parseError();
	}
	?>
	<form action="?ne=<?php echo $_GET["ne"]?>&islem=<?php echo $_GET["islem"]?>&durum=import" method="post">
		<?php foreach ($_POST as $iname => $ival){ ?>
			<input type="hidden" name="<?php echo $iname?>" value="<?php echo $ival?>">
		<?php }?>
		<?php
		echo "<table style='width:100%;' class=\"v3-table v3-table-hover\">";
		echo "<tr>";
		echo "<td style=\"background-color:#ccc !important; color:#fff !important;\" class=\"w5\">ID</td>";
		foreach ($_POST as $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 "</tr>";
		$row	=	-1;
		$total	=	count($result);
		if($_POST["rows"]==""){$flimit=0; $llimit=$total;}else{$srows=explode(",",$_POST["rows"]); $flimit=($srows[0]); $llimit=($srows[1]);}
		foreach($result as $value){$row++;
			if($row > $flimit and $row <= $llimit){
				echo "<tr>";
				echo "<td>".$row."</td>";
				foreach ($_POST as $iname => $ival){
					if(!empty($_POST[$iname]) and $iname<>"xls"){
						$iss = explode(',',$_POST[$iname]);
						if($iname=="t1" or $iname=="n1" or $iname=="n2" or $iname=="n4"){
							echo "<td title=\"".$iname."->".$ival."\">".kisalt($value[$iss[1]],3)."</td>";
						}elseif($iname=="images"){
							echo "<td title=\"".$iname."->".$ival."\">".substr($value[$iss[1]],0,30)."...</td>";
						}elseif($iname=="pb1" or $iname=="pb2"){
							echo "<td title=\"".$iname."->".$ival."\">".wprice($value[$iss[1]])."</td>";
						}else{
							if($iss[3]=="money"){
								echo "<td title=\"".$iname."->".$ival."\">".wprice($value[$iss[1]])."</td>";
							}else{
								echo "<td title=\"".$iname."->".$ival."\">".$value[$iss[1]]."</td>";
							}
						}
					}
					$format="";
				}
				echo "</tr>";
			}
		}
		echo "</table>";
		?>
		<button class="v3-btn v3-btn-danger" id="ibtn" style="float:right; clear:both;" type="submit"><?php echo translate("Import")?></button>
	</form>
	<?php
}elseif($_GET["durum"]=="import"){
	if($xlsx = SimpleXLSX::parse($_POST["xls"])){
		$result=$xlsx->rows();
	} else {
		$result=SimpleXLSX::parseError();
	}
	echo "<table style='width:100%;' class=\"v3-table v3-table-hover\">";
	echo "<tr>";
	echo "<td class=\"w1\" style=\"background-color:#ccc !important; color:#fff !important;\">İşlem</td>";
	echo "<td style=\"background-color:#ccc !important; color:#fff !important;\">No</td>";
	foreach ($_POST as $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);
	if($_POST["rows"]==""){$flimit=0; $llimit=$total;}else{$srows=explode(",",$_POST["rows"]); $flimit=($srows[0]); $llimit=($srows[1]);}
	foreach($result as $value){$row++;
		if($row > $flimit and $row <= $llimit){
			echo "<tr style=\"cursor:pointer;\" onclick=\"AjaxPResult('../../../../".$crmpath."/modules/xls/xls_import.php?ne=".$_GET["ne"]."&islem=".$_GET["islem"]."&s19=".$_SESSION["User_ID"]."&durum=kaydet','#f".$row."','#r".$row."','0');\">";
			echo "<td><div id=\"r".$row."\" style=\"cursor:pointer;\"><i class=\"fa fa-refresh\"></i></td>";
			echo "<td>".$row."</td>";
			echo "<form id=\"f".$row."\">";
			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'>";
			foreach ($_POST as $iname => $ival){
				if($_POST[$iname]<>"" and $iname<>"xls"){
					$iss = explode(',',$_POST[$iname]);
					if($iname=="n1"){
						echo "<td>".kisalt($value[$iss[1]],5)."</td>";
						echo "<textarea name=\"".$iname."\" style=\"display:none;\">".$value[$iss[1]]."</textarea>";
					}elseif($iname=="t1" or $iname=="n2"){
						echo "<td title=\"".$value[$iss[1]]."\">".kisalt($value[$iss[1]],10)."...</td>";
						echo "<textarea name=\"".$iname."\" style=\"display:none;\">".$value[$iss[1]]."</textarea>";
					}elseif($iname=="images"){
						echo "<td title=\"".$value[$iss[1]]."\">".substr($value[$iss[1]],0,30)."...</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>";
						}else{
							echo "<td title=\"".$value[$iss[1]]."\">".$value[$iss[1]]."</td>";
							echo "<textarea name=\"".$iname."\" style=\"display:none;\">".$value[$iss[1]]."</textarea>";
						}
					}
				}
			}
			echo "<input type='hidden' value='".$_POST["lang"]."' name='lang'>";
			echo "</form>";
			echo "<script> jQuery(document).ready(function(){ setTimeout(function(){ AjaxPResult('../../../../".$crmpath."/modules/xls/xls_import.php?ne=".$_GET["ne"]."&islem=".$_GET["islem"]."&s19=".$_SESSION["User_ID"]."&durum=kaydet&row=".$row."','#if".$row."','#r".$row."','nopl'); }, ".($row*1000).");});</script>";
			echo "</tr>";
			}
		}
		echo "</table>";
	}								
	?>