<?php
	header('Content-Type: text/html; charset=utf-8');
	error_reporting(0);
	$path="../../";
	include("../../map.php");
	include("../../scripts.php");
	include("../../secure.php");
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?php echo $sdil[8]; //iKod?></title>
	<style>
		.promo-box { display: flex; align-items: center; justify-content: space-between; padding: 20px; border-radius: 10px; border: 1px solid #ddd; background: #f8f9fa; margin-bottom: 15px; }
		.spromo-box { display: block; min-height: 120px; align-items: center; justify-content: space-between; padding: 20px; border-radius: 10px; border: 1px solid #ddd; background: #f8f9fa; margin-bottom: 15px; } 
		.promo-icon { font-size: 30px; width: 50px; height: 50px; display: flex; align-items: center; justify-content: center; border-radius: 50%; } 
		.text-content { flex-grow: 1; margin-left: 15px; } 
		.promo-title { font-weight: bold; } 
		.btn-green { background-color: #7ed321; color: white; border: none; padding: 10px 15px; border-radius: 5px; } 
		.btn-green:hover { background-color: #5aa300; } 
		.badge-aktif { background-color: #d4f8c4; color: #28a745; padding: 5px 10px; border-radius: 10px; font-weight: bold; } 
		.table th { background: #f8f9fa; }
		.input {width:60px;}
		.select { max-width: 100px !important;}
    </style>
</head>
<body>
    <div class="qmodal">
        <div class="qmodal-content">
            <!-- Header with Action Buttons -->
            <div class="qmodal-header">
				<button type="button" class="btn btn-info" onclick="location.href='?ne=<?php echo $_GET["ne"];?>&islem=<?php echo $_GET["islem"];?>'"><i class="fas fa-home"></i></button>
                <button type="button" class="btn btn-warning" onclick="location.href='?ne=<?php echo $_GET["ne"];?>&islem=<?php echo $_GET["islem"];?>&durum=new'"><i class="fas fa-plus"></i> Yeni Promosyon</button>
            </div>
            <!-- Left Tab Menu -->
            <div class="v8-columns">
                <!-- Content Area -->
                <div class="" 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");?>">
<?php
$categories="<select class=\"select categories\" name=\"categories\"></select>";
$products="<select class=\"select products\" name=\"products\"></select>";
$select1="<select class=\"select s1\" name=\"s1\" readonly> <option value=\"\">toplam ürün miktarı</option> <option value=\"\">toplam sepet tutarı</option> </select>";
$select2="<select class=\"select s2\" name=\"s2\" readonly> <option value=\"\">aynı üründen</option> <option value=\"\">seçtiğim</option> </select>";
$select3="<select class=\"select s3\" name=\"s3\" readonly> <option value=\"\">sabit fiyatlı</option> <option value=\"\">yüzde</option> <option value=\"\">fiyat indirimli</option> </select>";
$qtys="<input type=\"text\" class=\"input qtys\" onclick=\"gosterModal()\" name=\"qtys\">";
$input1="<input type=\"text\" class=\"input i1\" name=\"i1\">";
$input2="<input type=\"text\" class=\"input i2\" name=\"i2\">";
$input3="<input type=\"text\" class=\"input i3\" name=\"i3\">";
$input4="<input type=\"text\" class=\"input i4\" name=\"i4\">";
function ptext($text) {
    global $input1, $input2, $input3, $input4, $qtys;
	global $categories, $products, $select1, $select2, $select3;
    return str_replace( ['{categories}', '{products}', '{select1}', '{select2}', '{select3}', '{qtys}', '{input1}', '{input2}', '{input3}', '{input4}'], [$categories, $products, $select1, $select2, $select3, $qtys, $input1, $input2, $input3, $input4], $text );
}
?>
<!-- Modal -->
<div id="adetAraligiModal" style="display:none; position:fixed; top:0; left:0; width:100%; height:100%; background:rgba(0,0,0,0.5);">
    <div style="background:white; width:600px; margin:10% auto; padding:20px; border-radius:5px;">
        <h3>Adet Aralığı</h3>
        <button class="btn" onclick="ekleSatir()">+ Adet Aralığı Ekle</button>
        <table class="table" id="adetTablosu" border="1" style="width:100%; margin-top:10px;">
            <tr>
                <th>Başlangıç Adedi</th>
                <th>Bitiş Adedi</th>
                <th>İndirim Yüzdesi</th>
            </tr>
            <tr>
                <td><input type="text" class="form-control" value="0"></td>
                <td><input type="text" class="form-control" value="0"></td>
                <td><input type="text" class="form-control" value="0"></td>
            </tr>
        </table>
        <button class="btn" onclick="kapatModal()">Vazgeç</button>
        <button class="btn" onclick="onayla()">Onayla</button>
    </div>
</div>
<script>
    let maxSatir = 5;
    function ekleSatir() {
        let tablo = document.getElementById("adetTablosu");
        let satirSayisi = tablo.rows.length - 1;
        if (satirSayisi >= maxSatir) return;
        
        let yeniSatir = tablo.insertRow();
        yeniSatir.innerHTML = `
            <td><input type="text" class="form-control" value="0"></td>
            <td><input type="text" class="form-control" value="${satirSayisi === maxSatir - 1 ? 'MAX' : '0'}"></td>
            <td><input type="text" class="form-control" value="0"></td>
        `;
    }
    function kapatModal() {
        document.getElementById("adetAraligiModal").style.display = "none";
    }
    function gosterModal() {
        document.getElementById("adetAraligiModal").style.display = "block";
    }
    function onayla() {
        let tablo = document.getElementById("adetTablosu");
        let satirlar = Array.from(tablo.rows).slice(1);
        let veriler = satirlar.map(row => {
            let baslangic = row.cells[0].querySelector("input").value;
            let bitis = row.cells[1].querySelector("input").value;
            let yuzde = row.cells[2].querySelector("input").value;
            return `${baslangic}-${bitis}:${yuzde}`;
        }).join(",");
        document.querySelector(".qtys").value = veriler;
        kapatModal();
    }
</script>
<?php if($_GET["durum"]=="new"){?>
<div class="container mt-4" style="margin-top:15px;">
    <div class="promo-box">
        <div class="promo-icon" style="background: #e3e9ff;"><i class="fas fa-shopping-cart text-primary"></i></div>
        <div class="text-content">
            <div class="promo-title text-primary">SEPET BAZLI PROMOSYON</div>
            <div>Sepet toplamı belirli bir rakama ulaştığında, indirim yüzdesi veya tutarı belirleyebilirsiniz.</div>
        </div>
        <button class="btn-green" onclick="toggle('#promo1')">Ekle</button>
    </div>
	<div class="spromo-box" id="promo1" style="display:none;">
		<?php echo ptext("KDV Dahil {input1} TL ve Üzeri alışverişlerde sepet toplamına fiyat indirimli {input2} TL indirimle satmak istiyorum.");?>
		<button class="btn-green" style="display:block; float:right; margin-top:15px;">Ekle</button>
	</div>
    <div class="promo-box">
        <div class="promo-icon" style="background: #fde8f3;"><i class="fas fa-gift text-danger"></i></div>
        <div class="text-content">
            <div class="promo-title text-danger">ÜRÜN BAZLI PROMOSYON</div>
            <div>Ürün bazında promosyon belirleyebilmenizi sağlar.</div>
        </div>
        <button class="btn-green"  onclick="toggle('#promo2')">Ekle</button>
    </div>
	<div class="spromo-box" id="promo2" style="display:none;">
		<?php echo ptext("Seçtiğim {products} ürününden alana {select1} en az {input2} tane olması durumunda {select2} {input3} tanesini, {select3} {input4} TL fiyatla satmak istiyorum.");?>
		<button class="btn-green" style="display:block; float:right; margin-top:15px;">Ekle</button>
	</div>
    <div class="promo-box">
        <div class="promo-icon" style="background: #e3ffe6;"><i class="fas fa-tags text-success"></i></div>
        <div class="text-content">
            <div class="promo-title text-success">KATEGORİ/MARKA BAZLI PROMOSYON</div>
            <div>Kategori veya Marka bazında promosyon belirleyebilirsiniz.</div>
        </div>
        <button class="btn-green"  onclick="toggle('#promo3')">Ekle</button>
    </div>
	<div class="spromo-box" id="promo3" style="display:none;">
		<?php echo ptext("Seçtiğiniz {categories} kategorili ürünlerden alana, toplam ürün miktarı en az {input1} tane olması durumunda en ucuz olan {input2} tanesini sabit fiyatlı {input3} TL fiyatla satmak istiyorum.");?>
		<button class="btn-green" style="display:block; float:right; margin-top:15px;">Ekle</button>
	</div>
    <div class="promo-box">
        <div class="promo-icon" style="background: #fff5d7;"><i class="fas fa-percentage text-warning"></i></div>
        <div class="text-content">
            <div class="promo-title text-warning">ÇOK AL AZ ÖDE</div>
            <div>Ürün adedine bağlı olarak farklı indirim oranları uygulayabilirsiniz.</div>
        </div>
        <button class="btn-green"  onclick="toggle('#promo4')">Ekle</button>
    </div>
	<div class="spromo-box" id="promo4" style="display:none;">
		<form action="?durum=ekle" method="post">
			<input type="hidden" name="type" value="ÇOK AL AZ ÖDE">
			<?php echo ptext("Seçtiğiniz {products} ürünlerden alana, {qtys} Göre Seçenekleri ile çok al az öde indirimi uygulamak istiyorum.");?>
			<button type="submit" class="btn-green" style="display:block; float:right; margin-top:15px;">Ekle</button>
		</form>
	</div>
</div>
<script>
jQuery(document).ready(function(){
    jQuery('.products').load('../public/datalist.php?ne=products&val=id');
	jQuery('.categories').load('../ajax/categorie_ajax.php?ne=kayit');
});
</script>
<?php }elseif($_GET["durum"]==""){?>
<table class="table table-bordered">
        <thead>
            <tr>
                <th><input type="checkbox"></th>
                <th>ID</th>
                <th>ADI</th>
                <th>BAŞLANGIÇ TARİHİ</th>
                <th>BİTİŞ TARİHİ</th>
                <th>KULLANIM</th>
                <th>TİP</th>
                <th>DURUM</th>
            </tr>
        </thead>
        <tbody>
<?php $sql=mysqli_query($conn, "select * from genel where ne='promotions' order by id asc"); while($rs=mysqli_fetch_assoc($sql)){$jsi = json_decode($rs["n1"],true);?>
            <tr>
                <td><input type="checkbox"></td>
                <td><?php echo $rs["id"]?></td>
                <td>
				<?php
					if($jsi["type"]=="ÇOK AL AZ ÖDE"){
						echo $jsi["type"];
					}
				?>
			</td>
                <td>-</td>
                <td>-</td>
                <td>0 / 99999</td>
                <td>Kategori / Marka</td>
                <td><span class="badge-aktif">aktif</span></td>
            </tr>
<?php }?>
        </tbody>
    </table>
<?php }?>
* hazırlanmaya devam ediyor, arge aşamasında
                 </div>
            </div>
</body>
<?php echo $footer?>
</html>
<?php
if($_GET["durum"]=="ekle"){
	$jsonData = $conn->real_escape_string(json_encode($_POST, JSON_UNESCAPED_UNICODE));
	$sql = $conn->query("insert into genel(s1,ne,n1,ts1) VALUES ('".$_POST["products"]."', 'promotions', '".$jsonData."', now())");
	echo location("?",self,500,1);
	
}elseif($_GET["durum"]=="duzenle"){
	$jsonData = $conn->real_escape_string(json_encode($_POST, JSON_UNESCAPED_UNICODE));
	$Rs = mysqli_query($conn, "update genel set n1='".$jsonData."' Where id=".$_GET["id"]."");
	echo location("?",self,500,1);
	
}elseif($_GET["durum"]=="sil"){
	$Rs = mysqli_query($conn, "delete from genel where id=".$_GET["id"]);
	echo location("?",self,500,1);	
}
?>