<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
require_once '../veritabani.php';
require_once '../netgsm.php';

$token = $_GET['token'] ?? '';
$mesaj = "";

$sorgu = $pdo->prepare("SELECT * FROM teklifler WHERE token = ? LIMIT 1");
$sorgu->execute([$token]);
$kayit = $sorgu->fetch();

if (!$kayit) {
    die("<div style='color:red;font-weight:bold'>Geçersiz veya süresi dolmuş bağlantı.</div>");
}

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $adsoyad = $_POST['adsoyad'];
    $firma = $_POST['firma'];
    $email = $_POST['email'];
    $telefon = $_POST['telefon'];
    $adres = $_POST['adres'];
    $vergi_no = $_POST['vergi_no'];
    $vergi_dairesi = $_POST['vergi_dairesi'];
    $fatura_istek = $_POST['fatura_istek'] ?? 'hayir';
    $not = $_POST['not'];
    $urun_id = $_POST['urun_id'];
    $ebat = $_POST['ebat'];
    $kabin = $_POST['kabin'];
    $kafa = $_POST['kafa'];
    $knife = $_POST['knife'] ?? '';
    $toz = $_POST['toz'] ?? '';
    $freze = $_POST['freze'] ?? '';
    $petek = $_POST['petek'] ?? '';
    $airasist = $_POST['airasist'] ?? '';

    if (!$urun_id) {
        $mesaj = "Lütfen bir ürün seçiniz.";
    } else {
        $guncelle = $pdo->prepare("UPDATE teklifler SET 
    adsoyad=?, firma_adi=?, email=?, telefon=?, adres=?, vergi_no=?, vergi_dairesi=?, 
    fatura_istek=?, notlar=?, urun_id=?, ebat=?, kabin=?, kafa=?, knife=?, toz=?, freze=?, petek=?, airsist=? 
    WHERE token=?");
       $guncelle->execute([
    $adsoyad, $firma, $email, $telefon, $adres, $vergi_no, $vergi_dairesi, 
    $fatura_istek, $not, $urun_id, $ebat, $kabin, $kafa, $knife, $toz, $freze, $petek, $airasist, $token
]);

        $mesaj = "success";

        $yoneticiTel = '5331614753';
        $link = "https://teklif.wanyweb.com/admin/teklif-detay.php?token=$token";
        $mesajYonetici = "Yeni teklif talebi alındı: $adsoyad\nDetay: $link";
        smsGonder($yoneticiTel, $mesajYonetici);
    }
}
?>
<!DOCTYPE html>
<html lang="tr">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Teklif Talep Formu</title>
  <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;600;800&display=swap" rel="stylesheet">
  <style>
    * { box-sizing: border-box; }
    body {
      margin: 0;
      padding: 0;
      font-family: 'Inter', sans-serif;
      background: linear-gradient(135deg, #0f0c29, #302b63, #24243e);
      color: #fff;
    }
    .wrapper {
      display: flex;
      flex-wrap: wrap;
      gap: 40px;
      max-width: 1400px;
      margin: auto;
      padding: 40px 20px;
    }
    .card {
      background: rgba(255,255,255,0.05);
      border-radius: 20px;
      padding: 40px;
      backdrop-filter: blur(10px);
      box-shadow: 0 12px 40px rgba(0,0,0,0.4);
      flex: 1;
      min-width: 320px;
      border: 1px solid rgba(255,255,255,0.1);
    }
    .section-title {
      font-size: 26px;
      font-weight: 800;
      color: #ffcc80;
      text-transform: uppercase;
      margin-bottom: 25px;
      text-align: center;
    }
    .info p {
      font-size: 16px;
      margin: 10px 0;
      color: #ddd;
    }
    .success-box {
      background: #4caf50;
      color: #fff;
      padding: 15px;
      border-radius: 10px;
      font-weight: bold;
      margin-bottom: 20px;
      text-align: center;
    }
    .error-box {
      background: #e53935;
      color: #fff;
      padding: 15px;
      border-radius: 10px;
      font-weight: bold;
      margin-bottom: 20px;
      text-align: center;
    }
    form {
      display: flex;
      flex-direction: column;
      gap: 16px;
    }
    input, select, textarea {
      padding: 14px;
      border: none;
      border-radius: 12px;
      font-size: 16px;
      background: rgba(255,255,255,0.15);
      color: #fff;
      width: 100%;
    }
    input::placeholder, textarea::placeholder { color: #ccc; }
    textarea { resize: vertical; min-height: 100px; }
    input:focus, select:focus, textarea:focus {
      outline: none;
      box-shadow: 0 0 0 3px rgba(255,128,10,0.3);
    }
    .btn {
      background: linear-gradient(to right, #ff800a, #ffaa00);
      color: white;
      padding: 16px;
      border: none;
      font-size: 17px;
      font-weight: 700;
      border-radius: 12px;
      cursor: pointer;
      transition: 0.3s;
    }
    .btn:hover { background: #e86f00; }
    .urun-karti {
      display: flex;
      flex-direction: column;
      gap: 10px;
      border: 2px solid #ffffff22;
      border-radius: 12px;
      padding: 15px;
      background-color: rgba(255,255,255,0.05);
      transition: all 0.2s ease;
      cursor: pointer;
    }
    .urun-karti.secili {
      border: 2px solid #ff800a;
      background-color: rgba(255,128,10,0.1);
    }
    .urun-karti .img {
      width: 100%;
      max-height: 200px;
      object-fit: contain;
      border-radius: 8px;
    }
    .urun-karti .detay {
      display: flex;
      justify-content: space-between;
      align-items: center;
      flex-direction: column;
    }
    .urun-karti .detay button {
      margin-top: 10px;
      background: linear-gradient(to right, #ff800a, #ffaa00);
      border: none;
      padding: 10px 20px;
      border-radius: 20px;
      color: white;
      font-weight: 600;
      cursor: pointer;
    }
    #ebat { display: none; }
    @media(min-width: 768px) {
      .urun-karti { flex-direction: row; align-items: center; }
      .urun-karti .detay { flex: 1; align-items: flex-start; }
      .urun-karti .img { width: 120px; height: 120px; }
    }
    .iframe-render {
  width: 100%;
  height: 300px;
  border-radius: 12px;
  background: #000;
}
@media(min-width: 768px) {
  .urun-karti {
    flex-direction: row;
    align-items: flex-start;
  }
  .iframe-render {
    width: 300px;
    height: 220px;
  }
}
  </style>
</head>
<body>
  <div class="wrapper">
    <div class="card info">
      <div class="section-title">Teklif Bilgileri</div>
      <p><strong>Firma:</strong> IHTIYAROĞLU CNC</p>
      <p><strong>Referans No:</strong> <?= strtoupper($token) ?></p>
      <p><strong>Formu doldurarak CNC hizmetleri için teklif talebinde bulunabilirsiniz. En kısa sürede geri dönüş yapılacaktır.</strong></p>
    </div>

    <div class="card">
      <div class="section-title">Teklif Talep Formu</div>

      <?php if ($mesaj === "success"): ?>
        <div class="success-box">Talebiniz başarıyla alındı. En kısa sürede sizinle iletişime geçeceğiz.</div>
      <?php elseif ($mesaj): ?>
        <div class="error-box"><?= htmlspecialchars($mesaj) ?></div>
      <?php endif; ?>

      <form method="POST">
        <input name="adsoyad" placeholder="Ad Soyad" required>
        <input name="telefon" placeholder="Telefon Numarası" required>
        <input name="firma" placeholder="Firma Adı">
        <input name="email" type="email" placeholder="E-posta" required>
        <input name="adres" placeholder="Açık Adres">
        <input name="vergi_no" placeholder="Vergi Numarası">
        <input name="vergi_dairesi" placeholder="Vergi Dairesi">
        <select name="fatura_istek">
          <option value="evet">Evet, fatura istiyorum</option>
          <option value="hayir">Hayır, istemiyorum</option>
        </select>

        <select name="kategori" id="kategori" onchange="kategoriSecildi(this.value)">
          <option value="">Kategori Seçin...</option>
          <option value="router">CNC ROUTER</option>
          <option value="lazer">CNC LAZER</option>
        </select>

        <input type="hidden" name="urun_id" id="urun_id" required>
        <div id="urunler" class="flex flex-col gap-4 mt-4"></div>

        <select name="ebat" id="ebat" required>
          <option value="">-- Ebat Seçin --</option>
          <option value="75x75">75x75 [+ 8.000 TL]</option>
          <option value="100x100">100x100 [+ 10.800 TL]</option>
          <option value="100x150">100x150 [+ 18.000 TL]</option>
          <option value="150x200">150x200 [+ 34.000 TL]</option>
          <option value="istemiyorum">İstemiyorum</option>
        </select>
<!-- KABİN SEÇİMİ -->
<div id="kabinSecimi" style="display:none;">
  <label for="kabin"><strong>* Masaüstü CNC Router ve Lazer İçin Full Demonte Kabin</strong></label>
  <select name="kabin" id="kabin">
    <option value="">-- SEÇİM YAPIN --</option>
    <option value="1 m³ [+ 75.000 TL]">1 m³ [+ 75.000 TL]</option>
    <option value="1.5 m³ [+ 100.000 TL]">1.5 m³ [+ 100.000 TL]</option>
    <option value="2 m³ [+ 125.000 TL]">2 m³ [+ 125.000 TL]</option>
    <option value="2.5 m³ [+ 150.000 TL]">2.5 m³ [+ 150.000 TL]</option>
    <option value="3 m³ [+ 175.000 TL]">3 m³ [+ 175.000 TL]</option>
    <option value="İstemiyorum">İstemiyorum</option>
  </select>
</div>

<!-- LAZER KESİCİ KAFA ENTEGRASYONU -->
<div id="kafaSecimi" style="display:none;">
  <label for="kafa"><strong>* Lazer Kesici Kafa Entegrasyonu</strong></label>
  <select name="kafa" id="kafa">
    <option value="">-- SEÇİM YAPIN --</option>
    <option value="10W [+ 10.000 TL]">10W [+ 10.000 TL]</option>
    <option value="20W [+ 20.000 TL]">20W [+ 20.000 TL]</option>
    <option value="30W [+ 30.000 TL]">30W [+ 30.000 TL]</option>
    <option value="40W [+ 40.000 TL]">40W [+ 40.000 TL]</option>
    <option value="60W [+ 60.000 TL]">60W [+ 60.000 TL]</option>
    <option value="İstemiyorum">İstemiyorum</option>
  </select>
</div>

<!-- DRAG KNIFE ENTEGRASYONU -->
<div id="knifeSecimi" style="display:none;">
  <label for="knife"><strong>* Drag Knife Entegrasyonu</strong></label>
  <select name="knife" id="knife">
    <option value="">-- SEÇİM YAPIN --</option>
    <option value="Drag Knife Entegrasyonu [+ 75.000 TL]">Drag Knife Entegrasyonu [+ 75.000 TL]</option>
    <option value="İstemiyorum">İstemiyorum</option>
  </select>
</div>

<!-- TOZ TOPLAMA APARATI -->
<div id="tozSecimi" style="display:none;">
  <label for="toz"><strong>* Toz Toplama Aparatı</strong></label>
  <select name="toz" id="toz">
    <option value="">-- SEÇİM YAPIN --</option>
    <option value="Toz Toplama Aparatı [+ 15.000 TL]">Toz Toplama Aparatı [+ 15.000 TL]</option>
    <option value="İstemiyorum">İstemiyorum</option>
  </select>
</div>

<!-- KESİCİ FREZE MOTORU -->
<div id="frezeSecimi" style="display:none;">
  <label for="freze"><strong>* Kesici Freze Motoru</strong></label>
  <select name="freze" id="freze">
    <option value="">-- SEÇİM YAPIN --</option>
    <option value="Arel 0.75 kW Spindle Full Otomatik [+ 75.000 TL]">Arel 0.75 kW Spindle Full Otomatik [+ 75.000 TL]</option>
    <option value="Makita Rt0700C Yarı Otomatik [+ 70.000 TL]">Makita Rt0700C Yarı Otomatik [+ 70.000 TL]</option>
    <option value="Dewalt Dw6600 Yarı Otomatik [+ 66.000 TL]">Dewalt Dw6600 Yarı Otomatik [+ 66.000 TL]</option>
    <option value="İstemiyorum">İstemiyorum</option>
  </select>
</div>
<!-- LAZER KESİM PETEK TABLA -->
<div id="petekSecimi" style="display:none;">
  <label for="petek"><strong>* Lazer Kesim Petek Tabla</strong></label>
  <select name="petek" id="petek">
    <option value="">-- SEÇİM YAPIN --</option>
    <option value="Lazer Kesim Petek Tabla [+ 25.000 TL]">Lazer Kesim Petek Tabla [+ 25.000 TL]</option>
    <option value="İstemiyorum">İstemiyorum</option>
  </select>
</div>
<!-- AIRASIST HAVA DESTEĞİ MODÜLÜ -->
<div id="airasistSecimi" style="display:none;">
  <label for="airasist"><strong>* AirAsist Hava Desteği Modülü</strong></label>
  <select name="airasist" id="airasist">
    <option value="">-- SEÇİM YAPIN --</option>
    <option value="AirAsist Hava Desteği Modülü [+ 90.000 TL]">AirAsist Hava Desteği Modülü [+ 90.000 TL]</option>
    <option value="İstemiyorum">İstemiyorum</option>
  </select>
</div>
        <textarea name="not" placeholder="Not / Açıklama"></textarea>
        <button type="submit" class="btn">Teklif Talebi Gönder</button>
      </form>
    </div>
  </div>

  <script>
function kategoriSecildi(kategori) {
  const urunler = document.getElementById("urunler");
  const urun_id_input = document.getElementById("urun_id");
  urun_id_input.value = "";
  urunler.innerHTML = "";
  document.getElementById("ebat").style.display = "none";
  document.getElementById("kafaSecimi").style.display = "none";
  document.getElementById("kabinSecimi").style.display = "none";

  const tumUrunler = {
    router: [
      {
        id: 101,
        ad: "E-Sat M5 Masaüstü CNC Router 50x50",
        render: "https://zetcarve.com/e-sat-m5.html",
        link: "https://zetcarve.com/e-sat-m5-masaustu-cnc-router-50x50-P586"
      },
      {
        id: 102,
        ad: "E-Sat M7 Masaüstü CNC Router 50x50",
        render: "https://zetcarve.com/e-sat-M7.html",
        link: "https://zetcarve.com/e-sat-m7-masaustu-cnc-router-P587"
      },
      {
        id: 103,
        ad: "ZTC Mill Model 50X50 Masaüstü CNC Router",
        render: "https://zetcarve.com/ztc-mill.html",
        link: "https://zetcarve.com/ztc-mill-model-50x50-masaustu-cnc-router-P590"
      }
    ],
    lazer: [
      {
        id: 201,
        ad: "E-Sat M5 Masaüstü CNC Lazer 50x50",
        render: "https://zetcarve.com/e-sat-m5.html",
        link: "https://zetcarve.com/lazerm5-P691"
      },
      {
        id: 202,
        ad: "E-Sat M8 Model 50X50 Masaüstü CNC Lazer",
        render: "https://zetcarve.com/e-sat-M8.html",
        link: "https://zetcarve.com/e-sat-m8-model-50x50-masaustu-cnc-lazer-P588"
      },
      {
        id: 203,
        ad: "ZTC M9 Model 50X50 Masaüstü CNC Lazer",
        render: "https://zetcarve.com/images/product/product_385992986311853.pngl",
        link: "https://zetcarve.com/ztc-m9-model-50x50-masaustu-cnc-lazer-P591"
      }
    ]
  };

  const secilenUrunler = tumUrunler[kategori] || [];

  if (secilenUrunler.length === 0) {
    urunler.innerHTML = `<p style="color:#ccc;">Bu kategoriye ait ürünler yakında yüklenecek.</p>`;
    return;
  }

  secilenUrunler.forEach(urun => {
    const div = document.createElement("div");
    div.className = "urun-karti";
    div.innerHTML = `
      <iframe src="${urun.render}" class="iframe-render" frameborder="0"></iframe>
      <div class="detay">
        <div><strong>${urun.ad}</strong></div>
        <a href="${urun.link}" target="_blank"><button type="button">Detaylı İncelemek İçin Tıklayınız</button></a>
      </div>
    `;
    div.onclick = function () {
      document.getElementById("urun_id").value = urun.id;
      document.querySelectorAll(".urun-karti").forEach(el => el.classList.remove("secili"));
      div.classList.add("secili");
      document.getElementById("ebat").style.display = "block";
      document.getElementById("kabinSecimi").style.display = "block";
      document.getElementById("kafaSecimi").style.display = "block";
      document.getElementById("knifeSecimi").style.display = "block";
      document.getElementById("tozSecimi").style.display = "block";
      document.getElementById("frezeSecimi").style.display = "block";
      document.getElementById("petekSecimi").style.display = "block";
      document.getElementById("airasistSecimi").style.display = "block";
    };
    urunler.appendChild(div);
  });
}
</script>
</body>
</html>