<?php
$path="../../";
include("../../map.php");
include("../../scripts.php");
include("../../secure.php");
?>
<script>
	function readURL(input) {
		if (input.files && input.files[0]) {
			var reader = new FileReader();
			reader.onload = function (e) {
				jQuery('#img_prev')
				.attr('src', e.target.result)
				.height(200);
			};
			reader.readAsDataURL(input.files[0]);
		}
	}
	jQuery(document).ready(function(){
		jQuery('#img_prev').click(function(){ jQuery('#imgupload').trigger('click'); });
	});
	function readURL2(input) {
		if (input.files && input.files[0]) {
			var reader = new FileReader();
			reader.onload = function (e) {
				jQuery('#img_prev2')
				.attr('src', e.target.result)
				.height(200);
			};
			reader.readAsDataURL(input.files[0]);
		}
	}
	jQuery(document).ready(function(){
		jQuery('#img_prev2').click(function(){ jQuery('#imgupload2').trigger('click'); });
	});
</script>
<?php
if ($_GET["id"]<>""){
	$sql=mysqli_query($conn, "select * from medya where ne='slide' and id='".$_GET['id']."'"); $rs=mysqli_fetch_assoc($sql); $jsi = json_decode($rs["n6"], true); $jsm = json_decode($rs["n7"], true);
	$n1=$rs["n1"];
	$n2=$rs["n2"];
	$n3=$rs["n3"];
	$n4=$rs["n4"];
	$s1=$rs["s1"];
	$t2=$rs["t2"];
	$t4=$rs["t4"];
	$t6=$rs["t6"];
}else{
	$t6="";
	$s1="0";
}
?>
<form enctype="multipart/form-data" name="frm" method="post" action="?ne=<?php echo $_GET["ne"];?>&islem=<?php echo $_GET["islem"];?>&durum=slide&id=<?php echo $_GET["id"];?>">
	<div class="panel panel-default">
		<div class="panel-heading bg-<?php if($_GET["durum"]=="ok"){echo "success";}else{echo "warning";}?>">
			<?php echo $sdil[75] //Resim Ekle?>
			<div id="v-image" style="float:right"></div>
		</div>
		<div class="panel-body">
			<div class="col-xs-12 col-md-8" style="float:right; border:1px solid #ccc; background: #ccc; border-radius:3px; padding: 10px; text-align: center;">
				<div class="row" style="margin:0;">
					<div class="col-xs-<?php echo ($b6 != 2 ? '12' : '6'); ?> col-md-<?php echo ($b6 != 2 ? '12' : '6'); ?> p-0" style="">
						<?php if ($n1 != "") { ?>
							<img src="../../../deppo/slide/<?php echo $n1;?>" style="width:100%; height:300px; object-fit:cover; margin-top: 0;" id="img_prev">
						<?php } else { ?>
							<img src="//sample.<?php echo $cdnurl?>/<?php echo ($module["slidex"] != "" ? $module["slidex"].'x'.$module["slidey"] : "1280x600"); ?>" style="width:100%; height:300px; object-fit:cover; cursor:pointer; margin-top: 0;" id="img_prev">
						<?php } ?>
					</div>

					<div class="col-xs-12 col-md-6 p-0" style="<?php if ($b6 != 2) echo 'display:none;'; ?>">
						<?php if ($rs["t3"] == "video") { ?>
							<video controls style="width:100%; height:300px; object-fit:cover;" id="video_prev2">
								<source src="../../../deppo/slide/<?php echo $rs['n2']; ?>" type="video/mp4">
								Tarayıcınız video etiketini desteklemiyor.
							</video>
						<?php } else { ?>
							<?php if ($rs["n2"] != "") { ?>
								<img src="../../../deppo/slide/<?php echo $rs['n2'];?>" style="width:100%; height:300px; object-fit:cover;" id="img_prev2">
							<?php } else { ?>
								<img src="//sample.<?php echo $cdnurl?>/<?php echo ($module["slidemx"] != "" ? $module["slidemx"].'x'.$module["slidemy"] : "400x600"); ?>" style="width:100%; height:300px; object-fit:cover; cursor:pointer;" id="img_prev2">
							<?php } ?>
						<?php } ?>
					</div>

				</div>
			</div>

			<div class="col-xs-12 col-md-4 bg-cloud-upload" style="min-height:100px;">
				<label style="margin-top:3px;"><?php echo ctranslate("Slide Resim / Video Resim")?> <span style="font-size:8px; color:#ccc;">(n1)</span></label>
				<input type="file" name="n1" id="imgupload" value="" class="form-control" style="width:89%;position: absolute; z-index: 99;" accept="*" onChange="document.frm.bresim.checked=true; readURL(this);">
				<input type="checkbox" name="bresim" value="ON" style="display:none;">
				<p style="margin-top: 50px;">* Eğer resim ise anaresim, video ise video görseli</p>
				<?php if($b6==2){?>
					<div class="clear"></div>
					<label style="margin-top:50px; display:block;"><?php echo ctranslate("Slide Mobil Resim / Video Dosyası")?><span style="font-size:8px; color:#ccc;">(n2)</span></label>
					<input type="file" name="n2" id="imgupload2" value="" class="form-control" style="width:89%;position: absolute; z-index: 99;" accept="*" onChange="document.frm.kresim.checked=true; readURL2(this);">
					<input type="checkbox" name="kresim" value="ON" style="display:none;">
				<p style="margin-top: 50px;">* Eğer resim ise mobil resim, video ise video dosyası</p>
				<?php }?>
			</div>

		</div>
		<div class="panel-heading"><?php echo $sdil[207] //Slide Detay?></div>
		<div class="panel-body">
			<div class="row">
				<div class="col-xs-12 col-md-12">
					<label style="margin-top:3px; display:block;"><?php echo $sdil[39] //Başlık?><span style="font-size:8px; color:#ccc;">(t6 , s1)</span></label>
				</div>
				<div class="col-xs-10 col-md-10">
					<input type="text" name="t6" value="<?php echo ($t6 != '' ? $t6 : 'Slide Başlık'); ?>" placeholder="<?php echo $sdil[39] //Başlık?>" class="form-control">
				</div>
				<div class="col-xs-2 col-md-2">
					<input type="text" name="s1" value="<?php echo $s1;?>" class="form-control">
				</div>
			</div>

			<div class="row">
				<div class="col-xs-10 col-md-10">
				<label style="margin-top:3px; display:block; clear:both;"><?php echo $sdil[207] //Detay?><span style="font-size:8px; color:#ccc;">(n3)</span></label>
					<textarea name="n3" rows="5" class="form-control"  placeholder="<?php echo $sdil[207] //Slide Detay?>"><?php echo ($n3 != '' ? $n3 : 'Slide Detay'); ?></textarea>
				</div>
				<div class="col-xs-2 col-md-2">
					<select size="1" name="t2" class="form-control" style="margin-top: 25px;">
						<option value="All" <?php if ($t2=="" or $t2=="All"){?>Selected<?php }?>>[0] <?php echo $sdil[132] //Tüm?></option>
						<?php $i = 0;$diller = explode(',',$m1); foreach($diller as $dil) {$i++; if($i > 99) {$i = 1;}?>
						<option value="lang<?php echo $i?>" <?php if ($t2=="lang".$i."" or ($t2=="" and $_SESSION["lang"]=="lang".$i."")){?>Selected<?php }?>>[<?php echo $i;?>] <?php echo $dil;?></option>
					<?php }?>
				</select>
			</div></div>
			
			<div class="row">
				<div class="col-xs-10 col-md-10">
					<label style="margin-top:3px; display:block; clear:both;"><?php echo $sdil[83] //Link?><span style="font-size:8px; color:#ccc;">(n4 / t4)</span></label>
					<input type="text" name="n4" value="<?php echo ($n4 != '' ? $n4 : '#'); ?>" class="form-control" placeholder="http://domainadi.com/urundetay">
				</div>
				<div class="col-xs-2 col-md-2">
					<select size="1" name="t4" class="form-control">
						<option value="_self" <?php if ($t4=="_self"){?>Selected<?php }?>>Self</option>
						<option value="_blank" <?php if ($t4=="_blank"){?>Selected<?php }?>>_Blank</option>
					</select>
				</div>
			</div></div>
			
			<div class="">
				<div class="col-xs-4 col-md-4">
					<label style="margin-top:3px; display:block; clear:both;"><?php echo ctranslate("Yayın Tarihi")?> <span style="font-size:8px; color:#ccc;">(t9)</span> </label>
					<input type="date" name="t9" id="simple-calendar" class="form-control datepicker" value="<?php if($rs["t9"]==""){ echo date('Y-m-d'); }else{ echo $rs["t9"]; }?>" placeholder="Yayın Tarihi">
				</div>
				<div class="col-xs-4 col-md-4">
					<label style="margin-top:3px; display:block; clear:both;"><?php echo ctranslate("Geçerlilik Tarihi")?> <span style="font-size:8px; color:#ccc;">(t10)</span></label>
					<input type="date" name="t10" id="simple-calendar" class="form-control datepicker" value="<?php echo $rs["t10"];?>" placeholder="Geçerlilik Tarihi">
				</div>
				<div class="col-xs-4 col-md-4">
					<label style="margin-top:3px; display:block; clear:both;"><?php echo ctranslate("İşlem Sorumlusu")?> <span style="font-size:8px; color:#ccc;">(t10)</span></label>
					<select size=1 name="s19" style="width: 300px; float: right; margin-top: -7px; background: whitesmoke;" class="form-control e">
						<option value="0">Admin</option>
						<?php $sqlm=mysqli_query($conn, "select id,t1 from t5 where s1=1"); while($rsm=mysqli_fetch_assoc($sqlm)){?>
						<option value="<?php echo $rsm["id"];?>" title="<?php echo $rsm["t1"];?>" alt="<?php echo $rsm["t1"];?>" style="color:#6699FF" <?php if (($rs["s19"]=="" and $rsm["id"]==$_SESSION["user_id"]) or ($rs["s19"]<>"" and $rsm["id"]==$rs["s19"])) {?>Selected<?php }?>><?php echo $rsm["t1"];?></option>
						<?php }?>
					</select>
				</div>
			</div>
		</div>
	</div>
<?php $i=0;$sqli=mysqli_query($conn, "select * from genel where ne='inputs' and (t2='All' or t2='".$_SESSION["lang"]."') and n3='slide' and n4<>'switch' and (n2='0' or n2='".$rs["id"]."' or n2='".$rs["s2"]."' or n2='".$rs["s3"]."') and s1=0 order by s10 asc");?>
<?php if(mysqli_num_rows($sqli)>0){?>
	<div class="panel-heading"><?php echo $sdil[126] //Slide Detay?></div>
	<div class="panel-body">
		<div class="col-md-12 row">
			<?php  while($rsi=mysqli_fetch_assoc($sqli)){$i++;?>
				<div class="col-xs-6 col-md-6" 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 if($rsi["n4"]=="input"){?>
						<input class="form-control <?php echo $rsi["n13"]?>" type="<?php echo $rsi["n5"]?>" name="i<?php echo $i;?>" value="<?php echo $jsi["i".$i.""];?>" placeholder="<?php echo $rsi["n11"]?>" style="" <?php echo $rsi["n8"]?> <?php echo $rsi["n6"]?>>
					<?php }elseif($rsi["n4"]=="textarea"){?>
						<textarea class="form-control <?php echo $rsi["n13"]?>" name="i<?php echo $i;?>" placeholder="<?php echo $rsi["n11"]?>" <?php echo $rsi["n8"]?> <?php echo $rsi["n6"]?>><?php echo $jsi["i".$i.""];?></textarea>
					<?php }elseif($rsi["n4"]=="select"){?>
						<select class="form-control <?php echo $rsi["n13"]?> chosen-select" name="i<?php echo $i;?>[]" style="" <?php echo $rsi["n8"]?> <?php echo $rsi["n6"]?>>
							<option value=""><?php echo $sdil[221]; //Lütfen Seçiniz?></option>
							<?php $o=0;$sqlo=mysqli_query($conn, "select * from genel where ne='inputs' and s1=".$rsi["id"]." order by s10 asc"); while($rso=mysqli_fetch_assoc($sqlo)){$o++;?>
								<option value="<?php echo $rso["n2"]?>" <?php $q="";foreach($jsi["i".$i.""] as $q){?><?php if($q==$rso["n2"] or $rso["n3"]==1){echo "selected";}}?>><?php echo $rso["n1"]?></option>
							<?php }?>
						</select>
					<?php }elseif($rsi["n4"]=="radio"){?>
						<?php $o=0;$sqlo=mysqli_query($conn, "select * from genel where ne='inputs' and t2='".$_SESSION["lang"]."' and s1=".$rsi["id"]." order by s10 asc"); while($rso=mysqli_fetch_assoc($sqlo)){$o++;?>
							<div style="float:left; margin-right: 5px;">
								<label style="float:left; margin-right:5px;"><?php echo $rso["n1"];?>
								<input type="radio" name="i<?php echo $i;?>" value="<?php echo $rso["n2"];?>" <?php echo $rsi["n8"]?> <?php if($jsi["i".$i.""]==$rso["n2"] or $rso["n3"]==1){echo "checked";}?>></label>
							</div>
						<?php }?>
					<?php }elseif($rsi["n4"]=="checkbox"){?>
						<?php $o=0;$sqlo=mysqli_query($conn, "select * from genel where ne='inputs' and t2='".$_SESSION["lang"]."' and s1=".$rsi["id"]." order by s10 asc"); while($rso=mysqli_fetch_assoc($sqlo)){$o++;?>
							<div style="float:left; margin-right: 5px;">
								<label style="float:left; margin-right:5px;"><?php echo $rso["n1"];?>
								<input type="checkbox" name="i<?php echo $i;?>[]" value="<?php echo $rso["n2"];?>" <?php echo $rsi["n8"]?> <?php foreach($jsi["i".$i.""] as $q){?><?php if($q==$rso["n2"] or $rso["n3"]==1){echo "checked";}?><?php }?>></label>
							</div>
						<?php }?>
					<?php }?>
					<p style="font-size:10px;"><?php echo $rsi["n9"];?></p>
				</div>
			<?php }?>
		</div>
		<div class="col-md-12 row">
			<?php $w=0;$sqlw=mysqli_query($conn, "select * from genel where ne='inputs' and t2='".$_SESSION["lang"]."' and n3='slide' and n4='switch' and (n2='0' or n2='".$rs["id"]."' or n2='".$rs["s2"]."' or n2='".$rs["s3"]."') and s1=0 order by s10 asc"); while($rsw=mysqli_fetch_assoc($sqlw)){$w++;?>
				<fieldset class="grey-bg col-xs-3" style="">
					<legend><?php echo $rsw["n1"];?><span style="font-size:8px; color:#ccc;">(m<?php echo $w;?>)</span></legend>
					<div class="switch switch-sm switch-success">
						<input type="checkbox" name="m<?php echo $w;?>" data-plugin-ios-switch="" value="1" class="switch" <?php if($jsm["m".$w.""]=="1"){?>Checked<?php }?>>
					</div>
				</fieldset>
			<?php }?>
		</div>
	</div>
<?php }?>
	<button type="submit" style="float:right; margin-right:5px; margin-top:15px; display:block;" id="btn" class="btn btn-primary loading demo"><?php echo $sdil[203]; //Kaydet?></button>
</div>
</form>
<?php
if($_GET["durum"]=="slide"){
    $kaydedilecekyer = "../../../deppo/slide";
    $ne_tipi = "slide";

    $n1 = ""; // resim dosyası
    $n2 = ""; // video dosyası

    $resim_uzantilari = ['jpg', 'jpeg', 'png', 'gif', 'webp'];
    $video_uzantilari = ['mp4', 'avi', 'mov', 'webm', 'mkv'];

    // Önce resim dosyasını işle (opsiyonel)
    if (!empty($_FILES['n1']['name'])) {
        $uzanti1 = strtolower(pathinfo($_FILES['n1']['name'], PATHINFO_EXTENSION));
        if (in_array($uzanti1, $resim_uzantilari)) {
            $rand1 = substr(md5(uniqid(rand())), 0, 10);
            $n1 = "slide-" . $rand1 . "." . $uzanti1;
            move_uploaded_file($_FILES['n1']['tmp_name'], $kaydedilecekyer . "/" . $n1);
            if ($module["webp"] == 1) {
                $n1 = towebp($n1, $kaydedilecekyer . "/");
            }
        } else {
            // Geçersiz resim uzantısı ise hiçbir işlem yapma
            $n1 = ""; 
        }
    }

    // n2 dosyasını işle (resim veya video olabilir, boşsa atla)
if (!empty($_FILES['n2']['name'])) {
    $uzanti2 = strtolower(pathinfo($_FILES['n2']['name'], PATHINFO_EXTENSION));
    $tum_uzantilar = array_merge($resim_uzantilari, $video_uzantilari); // örn: ['jpg','jpeg','png','mp4','mov']

    if (in_array($uzanti2, $tum_uzantilar)) {
        $rand2 = substr(md5(uniqid(rand())), 0, 10);
        $n2 = "dosya-" . $rand2 . "." . $uzanti2;
        move_uploaded_file($_FILES['n2']['tmp_name'], $kaydedilecekyer . "/" . $n2);

        // Türü belirle
        if (in_array($uzanti2, $video_uzantilari)) {
            $ne_tipi = "video";
        } else {
            $ne_tipi = "resim";
        }
    } else {
        die("n2 için geçersiz dosya uzantısı. Resim veya video yükleyin.");
    }
}
// eğer boşsa hiçbir işlem yapılmaz


    // SQL işlemine hazırlanacak değerler
    $update_fields = [
        "t2" => $_POST['t2'],
        "t4" => $_POST['t4'],
        "t6" => addslashes($_POST['t6']),
        "t9" => $_POST['t9'],
        "t10" => $_POST['t10'],
        "n3" => addslashes($_POST['n3']),
        "n4" => $_POST['n4'],
        "s1" => $_POST['s1'],
        "s19" => $_POST['s19'],
        "t3" => $ne_tipi
    ];

    // Sadece gönderilmiş dosyaları ekle
    if($n1 != "") $update_fields["n1"] = $n1;
    if($n2 != "") $update_fields["n2"] = $n2;

    if (!empty($_GET["id"])) {
        $updateSQL = "UPDATE medya SET ";
        foreach($update_fields as $key => $val){
            $updateSQL .= "$key='$val',";
        }
        $updateSQL = rtrim($updateSQL, ",") . " WHERE id='" . $_GET['id'] . "'";
        mysqli_query($conn, $updateSQL);
    } else {
        $columns = implode(",", array_keys($update_fields));
        $values = implode("','", array_values($update_fields));
        $conn->query("INSERT INTO medya (ne, t2, t3, t6, t9, t10, s1, s13, s19, n1, n2, n3, n4, s4) VALUES ('slide', '{$_SESSION['lang']}', '{$ne_tipi}', '" . addslashes($_POST['t6']) . "', '{$_POST['t9']}', '{$_POST['t10']}', '{$_POST['s1']}', '1', '{$_POST['s19']}', '{$n1}', '{$n2}', '" . addslashes($_POST['n3']) . "', '{$_POST['n4']}', '{$_POST['s4']}')");
    }

    include("../public/log.php");
    echo location("?ne=" . $_GET["ne"] . "&islem=" . $_GET["islem"] . "&id=" . $_GET["id"] . "&durum=ok", self, 1, 1);
}
?>

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js"></script>