<?php
	$path="../../";
	include("../../map.php");
	include("../../scripts.php");
?>
<div class="v3-container" style="margin-top:10%; text-align:center;">
  <div class="v3-row"><div>
  <!-- Nav tabs -->
  <ul class="nav nav-tabs" role="tablist">
    <li role="presentation" class="active"><a href="#uniq" aria-controls="uniq" role="tab" data-toggle="tab">UniqCodes</a></li>
    <li role="presentation"><a href="#files" aria-controls="files" role="tab" data-toggle="tab">Files</a></li>
  </ul>
  <!-- Tab panes -->
  <div class="tab-content">
    <div role="tabpanel" class="tab-pane active" id="uniq">
    <form action="?islem=create&type=<?php echo $_GET["type"]?>" method="post"> 
       <input type="hidden" name="olusturulma" value="<?php echo date("Y-m-d")?>">
      <div class="v3-col-md-6 mmb5" style="margin-bottom:5px;">
        <input class="form-control" type="text" name="json" value="uniq_codes_<?php echo date("dmy")?>" placeholder="Dosya adı">
      </div>
<?php if($_GET["type"]=="discount"){?>
      <div class="v3-col-xs-6 mb5 mmb5" style="margin-bottom:5px;">
        <input class="form-control" type="text" name="min_tutar" value="" placeholder="Minimum Tutar">
      </div>
      <div class="v3-col-xs-3 mb5 mmb5" style="margin-bottom:5px;">
        <input class="form-control" type="text" name="indirim_tl" value="" placeholder="İndirim Tutar">
      </div>
      <div class="v3-col-xs-3 mb5" style="margin-bottom:5px;">
        <input class="form-control" type="text" name="indirim_yuzde" value="" placeholder="İndirim Yüzde">
      </div>
      <div class="v3-col-xs-6 mb5 mmb5" style="margin-bottom:5px;">
        <input class="form-control" type="text" name="haricIDs" value="" placeholder="Hariç Kategori ID'leri">
      </div>
<?php }else{?>
		<?php
		$i=0;$sqli=mysqli_query($conn, "select * from genel where ne='inputs' and (t2='All' or t2='".$_SESSION["lang"]."') and n3='uniq' and n4<>'switch' and (n2='0' or n2='".$rs["Cat_ID"]."') and s1=0 order by s10 asc");
		if(mysqli_num_rows($sqli)>0){
			?>
				<div class="" style="display: flex;">
					<?php while($rsi=mysqli_fetch_assoc($sqli)){$i++;?>
						<div class="col-xs-4 col-md-4" style="">
							<?php if($rsi["n4"]=="input"){?>
								<input class="form-control <?php echo $rsi["n13"]?>" type="<?php echo $rsi["n5"]?>" name="<?php if($rsi["n2"]<>""){echo $rsi["n12"];}else{echo cevir($rsi["n1"]);};?>" value="<?php echo $jsi["i".$i.""];?>" placeholder="<?php echo $rsi["n1"]?>" style="" <?php echo $rsi["n8"]?> <?php echo $rsi["n6"]?>>
							<?php }elseif($rsi["n4"]=="textarea"){?>
								<textarea class="form-control <?php echo $rsi["n13"]?>" name="<?php if($rsi["n2"]<>""){echo $rsi["n12"];}else{echo cevir($rsi["n1"]);};?>" placeholder="<?php echo $rsi["n11"]?>" <?php echo $rsi["n8"]?> <?php echo $rsi["n6"]?>><?php echo $jsi["i".$i.""];?></textarea>
							<?php }?>
							<p style="font-size:10px;"><?php echo $rsi["n9"];?></p>
						</div>
					<?php }?>
				</div>
		<?php }?>
<?php }?>
      <div class="v3-col-md-12 mb5 mmb5" style="margin-bottom:5px; clear:both;">
        <input class="form-control" type="text" name="aciklama" value="" placeholder="<?php echo ctranslate("Açıklama")?>">
      </div>
      <div class="v3-col-xs-3 mb5 mmb5" style="margin-bottom:5px;">
        <input class="form-control" type="date" name="gecerlilik" value="" placeholder="<?php echo ctranslate("Geçerlilik Süresi")?>">
      </div>
      <div class="v3-col-xs-3 mb5 mmb5" style="margin-bottom:5px;">
        <input class="form-control" type="number" name="count" value="" placeholder="<?php echo ctranslate("Kod Sayısı")?>">
      </div>
      <button type="submit" class="v3-btn v3-btn-warning">Oluştur</button>
	<?php if($_POST["json"]<>""){?>
		<a href="?islem=create&type=<?php echo $_GET["type"]?>&file=<?php echo cevir($_POST["json"])."_".$_GET["type"];?>&op=xls"  class="v3-btn v3-btn-warning"><i class="fa fa-file-excel-o" aria-hidden="true" style="margin-bottom: 5px; float:right"> İndir</i></a>
	<?php }?>
    </form>
<?php
	if($_POST["json"]<>""){
		$dosya  = "../../../deppo/dosya/".cevir($_POST["json"])."_".$_GET["type"].".json";
		if(!file_exists("../../../deppo/dosya/".cevir($_POST["json"])."_".$_GET["type"].".json")){
			$create = fopen($dosya, 'w') or die("Bir Hata Oluştu.!");
			fclose($create);
		}
		$url		 = "".$weburl."/deppo/dosya/".cevir($_POST["json"])."_".$_GET["type"].".json";
		$result	 = file_get_contents($url);
		$arrays = json_decode($result, true);
		//Print_r($result);
		$iarr = array();
if($_POST["type"]=="discount"){
		if($_POST["indirim_yuzde"]==""){$indirim_yuzde=0;}else{$indirim_yuzde=$_POST["indirim_yuzde"];}
		if($_POST["indirim_tl"]==""){$indirim_tl=0;}else{$indirim_tl=$_POST["indirim_tl"];}
		for ($i = 0; $i <= $_POST["count"]-1; $i++) {$id++;
			$iarr[] = array('id' => $id, 'indirim_kodu' => generateSPassword(7), 'aciklama' => $_POST["aciklama"], 'haricIDs' => $_POST["haricIDs"], 'min_tutar' => $_POST["min_tutar"], 'indirim_tl' => $indirim_tl, 'indirim_yuzde' => $indirim_yuzde, 'olusturulma' => date("Y-m-d"), 'gecerlilik' => $_POST["gecerlilik"]);
		}
}else{
		for ($i = 0; $i <= $_POST["count"]-1; $i++) {$id++;
			$arr_new[id] = $id;
			$arr_new[uniq] = generateSPassword(7);
			foreach($_POST as $key=>$value){
				if($key<>"count" and $key<>"json"){
					$arr_new[$key] = $value;
				}
			}
			array_push($iarr,$arr_new);
		}
}
		$data = json_encode($iarr, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_NUMERIC_CHECK);
		$fp	  = fopen($dosya, 'w');
		fwrite($fp, $data);
		fclose($fp);
	}
	if($_GET["op"]=="xls"){
		$json	=	"../../../deppo/dosya/".$_GET["file"].".json";
		$csv		=	"../../../deppo/excel/".$_GET["file"].".csv";
		header('Content-Type: text/html; charset=utf-8');
		jsonToCSV($json, $csv);
		echo '<center><a href="'.$csv.'" target="_blank">.csv dosyası hazır, indir</a></center>';
	}
	if($_GET["op"]=="del"){
		unlink('../../../deppo/dosya/'.$_GET["file"].'');		
	}
//-- İndirim Kodu -->
?>
</div>
    <div role="tabpanel" class="tab-pane" id="files">
<table class="v3-table">
<?php
if ($handle = opendir('../../../deppo/dosya/')) {
    while (false !== ($entry = readdir($handle))) {
		$file=explode('.',$entry);
        if ($entry != "." && $entry != ".." && $file[1] == "json") {
echo "<tr>";
echo "<td>".$entry."</td>";
echo "<td style='width:10%'><a href=\"?islem=create&type=".$_GET["type"]."&file=".$file[0]."&op=xls\">İndir</a></td>";
echo "<td style='width:10%'><a href=\"?islem=create&type=".$_GET["type"]."&file=".$entry."&op=del\">Kaldır</a></td>";
echo "</tr>";
        }
    }
echo "</table>";
    closedir($handle);
}
?>
</div>
  </div>
  </div>
</div>
</div>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>