<?php
require_once __DIR__ . '/inc/config.php';
$categories = getInsuranceCategories();
$products = getProductCards();
$page_title = 'Ucretsiz Teklif Al';
$page_desc = 'Sigortabaz ile kasko, trafik, saglik, DASK ve daha fazlasi icin ucretsiz teklif alin.';
$success = $error = '';
if($_SERVER['REQUEST_METHOD'] === 'POST'){
    $name = trim($_POST['name'] ?? '');
    $phone = trim($_POST['phone'] ?? '');
    $email = trim($_POST['email'] ?? '');
    $tc = trim($_POST['tc'] ?? '');
    $product = trim($_POST['product'] ?? '');
    $city = trim($_POST['city'] ?? '');
    $notes = trim($_POST['notes'] ?? '');
    if($name && $phone && $tc && $product){
        try{
            $stmt = $db->prepare("INSERT INTO quote_requests (name, phone, email, tc_kimlik, insurance_type, city, notes, status, created_at) VALUES (?, ?, ?, ?, ?, ?, ?, 'new', NOW())");
            $stmt->execute([$name, $phone, $email, $tc, $product, $city, $notes]);
            $success = 'Teklif talebiniz alindi!';
        }catch(PDOException $e){
            try{
                $stmt = $db->prepare("INSERT INTO customer_requests (full_name, phone, email, tc_no, request_type, city, notes, status, created_at) VALUES (?, ?, ?, ?, ?, ?, ?, 'pending', NOW())");
                $stmt->execute([$name, $phone, $email, $tc, $product, $city, $notes]);
                $success = 'Teklif talebiniz alindi!';
            }catch(PDOException $e2){ $error = 'Bir hata olustu.'; }
        }
    }else{ $error = 'Lutfen tum zorunlu alanlari doldurun.'; }
}
require_once __DIR__ . '/inc/header.php';
?>
<div class="breadcrumb"><div class="container"><a href="/">Ana Sayfa</a> / Teklif Al</div></div>
<section class="section section-alt">
  <div class="container" style="max-width:600px">
    <h1 class="section-title">Ucretsiz Teklif Al</h1>
    <p class="section-subtitle">Bilgilerinizi birakin, size en uygun teklifi hazirlayalim</p>
    <?php if($success): ?><div class="alert alert-success"><?=htmlspecialchars($success)?></div><?php endif; ?>
    <?php if($error): ?><div class="alert alert-error"><?=htmlspecialchars($error)?></div><?php endif; ?>
    <div class="card">
      <form method="POST">
        <div class="form-group"><label class="form-label">Ad Soyad *</label><input type="text" name="name" class="form-control" required></div>
        <div class="form-group"><label class="form-label">Telefon *</label><input type="tel" name="phone" class="form-control" required placeholder="05XX XXX XX XX"></div>
        <div class="form-group"><label class="form-label">E-posta</label><input type="email" name="email" class="form-control"></div>
        <div class="form-group"><label class="form-label">TC Kimlik No *</label><input type="text" name="tc" class="form-control" required maxlength="11"></div>
        <div class="form-group"><label class="form-label">Sigorta Turu *</label>
          <select name="product" class="form-select" required>
            <option value="">Seciniz</option>
            <?php foreach($products as $p): ?><option value="<?=htmlspecialchars($p['slug'])?>" <?=($_GET['urun']??'')==$p['slug']?'selected':''?>><?=htmlspecialchars($p['title'])?></option><?php endforeach; ?>
          </select>
        </div>
        <div class="form-group"><label class="form-label">Sehir</label><input type="text" name="city" class="form-control"></div>
        <div class="form-group"><label class="form-label">Notlar</label><textarea name="notes" class="form-control" rows="3"></textarea></div>
        <button type="submit" class="btn btn-primary" style="width:100%"><i class="bi bi-lightning"></i> Teklif Al</button>
      </form>
    </div>
  </div>
</section>
<?php require_once __DIR__ . '/inc/footer.php'; ?>