<?php
require "config.php";
?>
<?php

$ip = $_SERVER['REMOTE_ADDR'];
$site_id = ($site['site_id'] ?? 0);
$now = date("Y-m-d H:i:s");

// Ziyaretçi daha önce kayıtlıysa güncelle, yoksa ekle
$stmt = $db->prepare("SELECT id FROM online_visitors WHERE ip_address=? AND site_id=?");
$stmt->execute([$ip, $site_id]);

if ($stmt->rowCount() > 0) {
    $update = $db->prepare("UPDATE online_visitors SET last_active=? WHERE ip_address=? AND site_id=?");
    $update->execute([$now, $ip, $site_id]);
} else {
    $insert = $db->prepare("INSERT INTO online_visitors (site_id, ip_address, last_active) VALUES (?, ?, ?)");
    $insert->execute([$site_id, $ip, $now]);
}

// Eski verileri (5 dakikadan eski) temizle
$delete = $db->prepare("DELETE FROM online_visitors WHERE last_active < DATE_SUB(?, INTERVAL 5 MINUTE)");
$delete->execute([$now]);
?>
<!DOCTYPE html>
<html lang="tr">

<head>
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
    <title><?= $urun["urun_baslik"]; ?> | Sipariş Formu</title>
    <meta name="description" content="">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
    <link rel="stylesheet" href="assets/css/bootstrap.css">
    <link rel="stylesheet" href="assets/css/font-awesome.min.css">
    <script src="https://code.jquery.com/jquery-3.7.1.min.js" integrity="sha256-/JqT3SQfawRcv/BIHPThkBvs0OEvtFFmqPF/lYI/Cxo=" crossorigin="anonymous"></script>
    <link rel="stylesheet" href="assets/css/animate.css">
    <link rel="stylesheet" href="assets/css/core.css">
    <link rel="stylesheet" href="assets/css/style.css">
    <link rel="stylesheet" href="assets/css/bpanel.css">
    <script>
        function SayiKontrol(e) {
            olay = document.all ? window.event : e;
            tus = document.all ? olay.keyCode : olay.which;
            if (tus < 48 || tus > 57) {
                if (document.all) {
                    olay.returnValue = false;
                } else {
                    olay.preventDefault();
                }
            }
        }

        function HarfKontrol(e) {
            olay = document.all ? window.event : e;
            tus = document.all ? olay.keyCode : olay.which;
            if (tus >= 48 && tus <= 57) {
                if (document.all) {
                    olay.returnValue = false;
                } else {
                    olay.preventDefault();
                }
            }
        }
    </script>
    
   
	
	<?php
	$fb_piksel = $site["site_fb_piksel"];
	if($fb_piksel):
	    $pikseller = preg_split('/\r\n|[\r\n]/', $fb_piksel);
	    for($f=0;$f<count($pikseller);$f++):
	?>
    <script>
            !function (f, b, e, v, n, t, s) {
                if (f.fbq) return;
                n = f.fbq = function () {
                    n.callMethod ?
                        n.callMethod.apply(n, arguments) : n.queue.push(arguments)
                };
                if (!f._fbq) f._fbq = n;
                n.push = n;
                n.loaded = !0;
                n.version = '2.0';
                n.queue = [];
                t = b.createElement(e);
                t.async = !0;
                t.src = v;
                s = b.getElementsByTagName(e)[0];
                s.parentNode.insertBefore(t, s)
            }(window, document, 'script',
                'https://connect.facebook.net/en_US/fbevents.js');
            fbq('init', '<?=$pikseller[$f];?>');
            fbq('track', 'PageView');
    </script>
    <noscript>
        <img height="1" width="1" style="display:none" src="https://www.facebook.com/tr?id=<?=$pikseller[$f];?>&ev=PageView&noscript=1"/>
    </noscript>
	<?php
	endfor;
	endif;
	?>


</head>

<body>

<style>
        .wp_btn {
            width: 250px;
            position:fixed;
            bottom:5px !important;
            z-index:1;
        }
        
        .wp_btn img{
            max-width:100%;
        }
        .paket {
            line-height: normal;
            font-family: "Lato", sans-serif !important;
            font-weight: 400;
            font-style: normal;
            margin-top: 10px !important;
            display: flex;
            gap: 10px;
            min-height: 55px;
            box-shadow: inset 0 0 0 1px var(--bar-selected-border-color, #212121);
            border-radius: .5em;
            padding: 15px;
            cursor: pointer;
        }
        .paket-input {
            text-align: center;
            align-items: center;
            justify-content: center;
            display: flex;
        }
        .paket-resim {
            align-content: center !important;
        }
        .paket-title {
            width: 100%;
            align-content: center;
        }
        .paket-title .row {
            box-shadow:none;
            background:none;
        }
        .paket-baslik {
            font-weight: bold;
        }
        .paket-price {
            align-content: center;
            text-align: right;
        }
        .guncel-fiyat {
            font-weight: bold;
            font-size: 23px;
        }
        .eski-fiyat {
            font-weight: bold;
            text-align: right;
            text-decoration: line-through;
            color: #9c9c9c;
        }
        .kargo-ucreti {
            color: #fff;
            font-size: 12px;
            padding: 2px 6px;
            border-radius: .5em;
            background: #212121;
            text-align: center;
            letter-spacing: 1px;
            text-transform: uppercase;
        }
        .ucretsiz-kargo {
            background: #ff6347;
        }
        @media screen and (max-width: 768px) {
            .paket-resim img {
                width: 100px;
            }
        }
    </style>

    <div class="container">
        <div class="row">
            <div class="col-xs-12">
                <a href="#siparis">
                <a href="#siparis"><img class="img-fluid" src="assets/images/ust.gif" title="" alt="Image"></a>
                <?php
                        for ($i = 1; $i < 21; $i++):
                        // Dosya uzantısını belirle
                        $jpgPath = "assets/images/$i.jpg";
                        $pngPath = "assets/images/$i.png";
                        $gifPath = "assets/images/$i.gif";
                    
                        // Hangi dosya varsa onu seç
                        if (file_exists($jpgPath)) {
                            $imagePath = $jpgPath;
                        } elseif (file_exists($pngPath)) {
                            $imagePath = $pngPath;
                        } elseif (file_exists($gifPath)) {
                            $imagePath = $gifPath;
                        } else {
                            $imagePath = ''; // Hiçbiri yoksa boş bırak
                        }// Eğer uygun dosya bulunduysa göster
                        if (!empty($imagePath)):
                    ?>
                    
                        <a href="#siparis"><img class="img-fluid" src="<?= $imagePath ?>" title="" alt="Image"></a>
                    <?php
                        endif;
                    endfor;
                    echo "<hr />";
                ?>
                <a href="#siparis"><img class="img-fluid" src="assets/images/hizli.jpg" title="" alt="Image"></a>
                </a>
            </div>
        </div>

        <div class="col-12" style="background: #fff !important">
            <form id="siparis"style="background-color: #fff;" action="gonder.php" class="thecheckout checkout-form" method="post" autocomplete="off" onsubmit="return formGonder();" data-locale="tr" data-autofocus="1" data-country-code="tr">
                <div class="row" style="background-color: #fff; padding: 10px;">
                    <?php 
                        $gorseller = json_decode($urun["urun_gorsel"], true);
                        foreach ($paketler as $key => $paket): ?>
                        <?php 
                            $radio_id = "paket_$key";
                            $checked = (!$key) ? 'checked' : '';
                        ?>
                        <label for="paket_<?= $key; ?>" class="paket">
                            <div class="paket-input">
                                <input 
                                class="paket-input"
                                type="radio" 
                                name="paket" 
                                id="<?= $radio_id; ?>" 
                                value="<?= $key; ?>" 
                                <?= $checked; ?> 
                                required 
                                style="width:30px; height:30px;">
                            </div>
                            <div class="paket-resim">
                                <?php 
                                    if($gorseller[$key][0]) {
                                ?>
                                    <img src="https://cdn.primemarketim.com/<?= $gorseller[$key][0]; ?>">
                                <?php
                                    } else {
                                ?>
                                    <img src="https://cdnprime.site/46_15_1_secin.png">
                                <?php
                                    }
                                ?>
                            </div>
                            <div class="paket-title">
                                <div class="row">
                                    <div class="col-12">
                                        <div class="paket-baslik"><?= $paket->baslik; ?></div>
                                        <?php 
                                            if($paket->opsiyonel_1) {
                                        ?>
                                            <div style="padding: 3px; color: red;" class="paket-baslik"><?= $paket->opsiyonel_1; ?></div>
                                        <?php
                                            }
                                        ?>
                                    </div>
                                </div>
                                <div class="row">
                                    <div class="col-12">
                                        <div class="paket-icerik">
                                            <?= $paket->icerik; ?></div>
                                    </div>
                                    <div class="col-12">
                                        <div class="paket-price">
                                            <div class="guncel-fiyat" style="display: flex; align-items: center; justify-content: end">
                                                    <span class="eski-fiyat" style="font-weight: normal; font-size:15px; margin-right: 5px;">
                                                        <?= number_format($paket->urun_eski_fiyat, 0, ',', '.'); ?> yerine
                                                    </span>
                                                <?= number_format($paket->urun_fiyat, 0, ',', '.'); ?>₺
                                            </div>
                                            <div class="eski-fiyat">
        
                                            </div>
                                        </div>
                                    </div>
                                    <div class="col-12">
                                        <div class="kargo-ucreti ucretsiz-kargo">
                                            Ücretsiz Kargo                                </div>
                                    </div>
                                </div>
                            </div>
                        </label>
                    <?php endforeach; ?>
                </div>
    
    
                <!-- varyasyonlar -->
                <div class="wrapper" style="background-color: #fff">
                    <div id="varyasyonlar" class="container" style="width: 96% !important;">
                        <?php
                        if ($varyasyonlar):
                            echo "<div id='varyasyonlar'>";
                            for ($i = 0; $i < count($varyasyonlar); $i++):
                                $degerler = explode(",", $varyasyonlar[$i]->degerler);
                                for ($k = 0; $k < $paketler[0]->urun_adet; $k++):
                        ?>
                                    <div class="form-group select-container">
                                        <select name="varyasyon[<?= strtolower(str_replace([" "], ["_"], $varyasyonlar[$i]->baslik)); ?>][]" class="form-control input-lg smart-payment-select" required>
                                            <option value="" disabled selected><?= $k + 1; ?>. <?= $varyasyonlar[$i]->baslik; ?> Seçin</option>
                                            <?php
                                            foreach ($degerler as $deger) {
                                                echo "<option value='{$deger}'>{$deger}</option>";
                                            }
                                            ?>
                                        </select>
                                    </div>
                        <?php
                                endfor;
                                echo "<hr />";
                            endfor;
                            echo "</div>";
                        endif;
                        ?>
                    </div>
                </div>
                
                <!-- varyasyonlar -->
    
    
                <div class="row">
                    <div class="col-xs-12">
                        <a href="#siparisformu"><img class="img-fluid" src="assets/images/header-alt.jpg?vvv3"></a>
                    </div>
                </div>
                <div id="bilgiler" class="wrapper" style="background-color: #fff"><br>
                    <div class="container" style="width: 96% !important;">
                        <div class="form-group">
                            <input type="text" name="sp_musteri" onkeypress="HarfKontrol(event)" class="form-control input-lg" placeholder="Ad Soyad">
                            <label class="validate_error">Ad soyad alanı minimum 5 karakter olmalıdır.</label>
                        </div>
                        <div class="form-group">
                            <input type="text" name="sp_telefon" class="form-control input-lg" placeholder="Telefon Numarası">
                            <label class="validate_error">Telefon Numarası alanı minimum 10 haneli olmalıdır.</label>
                        </div>
                        <div class="form-group select-container">
                            <select name="sp_sehir" class="form-control input-lg thecity">
                                <option value="">Şehir Seçin</option>
                                <?php
                                foreach ($sehirlerData as $sehir):
                                    echo '<option data-ilceler=\'' . $sehir['ilceler'] . '\' value="' . $sehir['sehir_id'] . '">' . $sehir['sehir_adi'] . '</option>';
                                endforeach;
                                ?>
                            </select>
                            <label class="validate_error">Şehir bilgisi seçilmelidir.</label>
                        </div>
                        <div class="form-group select-container">
                            <select name="sp_ilce" class="form-control input-lg smart-district-select" disabled>
                                <option value="">İlçe Seçin</option>
                            </select>
                            <label class="validate_error">İlçe bilgisi seçilmelidir.</label>
                        </div>
                        <div class="form-group">
                            <textarea name="sp_adres" placeholder="Adresiniz" class="form-control input-lg"></textarea>
                            <label class="validate_error">Adres bilgisi minimum 5 karakter olmalıdır.</label>
                        </div>
                        <div class="form-group select-container">
                            <select name="sp_odeme_yontemi" class="form-control input-lg smart-payment-select">
                                <option value="">Ödeme Yöntemi Seçin</option>
                                <?php
                                foreach ($odemeYontemleri as $oy) {
                                    echo '<option  value="' . $oy['od_id'] . '">' . $oy['od_baslik'] . '</option>';
                                }
                                ?>
                            </select>
                            <label class="validate_error">Ödeme yöntemi bilgisi seçilmelidir.</label>
                        </div>
                    </div>
                </div>
                <div class="wrapper" style="background-color: #fff;">
                    <div class="button-submit-container container text-center">
                        <input type="image" src="assets/images/send_button.jpg" value="gonder">
                    </div>
                </div>
            </form>
        </div>

        <div class="button-submit-container container text-center">
            <div class="row">
                <div class="col-xs-12">
                     <a href="#siparisformu"><img class="img-fluid" src="assets/images/alt.jpg"></a>
                </div>
            </div>

        </div>

        <script src="assets/js/jquery.min.js"></script>
        <script src="assets/js/jquery.countdown.min.js?xx"></script>
        <script src="assets/js/jquery.cookie.js"></script>
        <script src="assets/js/product-counter.js"></script>
        <script src="assets/js/bpanel.js?ver=2222"></script>
        <script src="assets/js/app.js"></script>
        <script src="assets/js/jquery.inputmask.js"></script>
        <!--script async="" src="../assets/library/js/app.js?v=58f5be642d312" id="smart-app-script"></script>
    <link rel="stylesheet" href="../assets/library/css/app.css?v=58f5be642d35a" id="smart-app-css"-->
        <script>
            $('input[name="paket"]').change(function() {
                var paket = $(this).val();
                var adet = $(this).data('adet');
                $.ajax({
                    url: 'get_varyasyonlar.php',
                    method: 'post',
                    data: {
                        paket,
                        adet
                    },
                    success: function(data) {
                        $('#varyasyonlar').html(data);
                    }
                })
            })
        </script>
    </div>
</body>

</html>