<?php 
session_start();

// ===== DEBUG MODE ===== 
// Tasarımı görmek için true yap, bitince false yap
$DEBUG_MODE = false;
// ===== DEBUG MODE =====

include 'config.php';

// Kullanıcı token kontrolü
$user_token = $_COOKIE['user_token'] ?? null;

if (!$DEBUG_MODE) {
    if (empty($user_token)) {
        header("Location: login.php");
        exit();
    }
    
    // Kullanıcı bilgilerini çek
    $stmt = $db->prepare("SELECT * FROM kullanicilar WHERE user_token = ?");
    $stmt->execute([$user_token]);
    $myUser = $stmt->fetch(PDO::FETCH_ASSOC);
    
    // Bilgileri doldurmuş mu kontrol et
    if(!empty($myUser['user_cinsiyet']) && !empty($myUser['user_sehir']) && !empty($myUser['user_yas'])){
        header("Location: index.php");
        exit();
    }
}

// Form gönderildiğinde
if($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['type']) && $_POST['type'] === 'bilgi_doldur') {
    if ($DEBUG_MODE) {
        // Debug modunda başarılı yanıt döndür
        sleep(1); // Loading efektini görmek için
        echo json_encode(['result' => '1', 'debug' => true]);
        exit();
    }
    
    $yas = $_POST['yas'] ?? 0;
    $cinsiyet = $_POST['cinsiyet'] ?? '';
    $sehir = $_POST['sehir'] ?? '';
    
    if($yas != 0 && !empty($cinsiyet) && $sehir != '0') {
        // Cinsiyet ve diğer bilgileri güncelle
        $update = $db->prepare("UPDATE kullanicilar SET user_yas = ?, user_cinsiyet = ?, user_sehir = ? WHERE user_token = ?");
        $update->execute([$yas, $cinsiyet, $sehir, $user_token]);
        
        // Cinsiyete göre jeton ataması yap (kadınlar için 5 -> 5000)
        if($cinsiyet == 'kadin') {
            $jetonQuery = $db->prepare("SELECT user_jeton FROM kullanicilar WHERE user_token = ?");
            $jetonQuery->execute([$user_token]);
            $currentJeton = $jetonQuery->fetchColumn();
            
            // Eğer jeton hala 5 ise (default, satın alma yapmamış), 5000'e çıkar
            if($currentJeton == 5) {
                $jetonUpdate = $db->prepare("UPDATE kullanicilar SET user_jeton = 5000 WHERE user_token = ?");
                $jetonUpdate->execute([$user_token]);
            }
        }
        
        echo json_encode(['result' => '1']);
    } else {
        echo json_encode(['result' => '2']);
    }
    exit();
}

// Şehir listesi
$iller = array('Adana', 'Adıyaman', 'Afyon', 'Ağrı', 'Amasya', 'Ankara', 'Antalya', 'Artvin',
'Aydın', 'Balıkesir', 'Bilecik', 'Bingöl', 'Bitlis', 'Bolu', 'Burdur', 'Bursa', 'Çanakkale',
'Çankırı', 'Çorum', 'Denizli', 'Diyarbakır', 'Edirne', 'Elazığ', 'Erzincan', 'Erzurum', 'Eskişehir',
'Gaziantep', 'Giresun', 'Gümüşhane', 'Hakkari', 'Hatay', 'Isparta', 'Mersin', 'İstanbul', 'İzmir', 
'Kars', 'Kastamonu', 'Kayseri', 'Kırklareli', 'Kırşehir', 'Kocaeli', 'Konya', 'Kütahya', 'Malatya', 
'Manisa', 'Kahramanmaraş', 'Mardin', 'Muğla', 'Muş', 'Nevşehir', 'Niğde', 'Ordu', 'Rize', 'Sakarya',
'Samsun', 'Siirt', 'Sinop', 'Sivas', 'Tekirdağ', 'Tokat', 'Trabzon', 'Tunceli', 'Şanlıurfa', 'Uşak',
'Van', 'Yozgat', 'Zonguldak', 'Aksaray', 'Bayburt', 'Karaman', 'Kırıkkale', 'Batman', 'Şırnak',
'Bartın', 'Ardahan', 'Iğdır', 'Yalova', 'Karabük', 'Kilis', 'Osmaniye', 'Düzce');
?>
<!DOCTYPE html>
<html lang="tr">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=0, minimal-ui">
  <link rel="preconnect" href="https://fonts.googleapis.com">
  <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
  <link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700;800;900&display=swap" rel="stylesheet">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/sweetalert2/11.7.32/sweetalert2.min.css">
  <title>HeySen - Bilgilerini Tamamla</title>
  <style>
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

html, body {
  width: 100%;
  min-height: 100vh;
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
}

body {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 25%, #f093fb 50%, #4facfe 75%, #00f2fe 100%);
  background-size: 400% 400%;
  animation: gradientShift 15s ease infinite;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  min-height: 100vh;
}

@keyframes gradientShift {
  0% { background-position: 0% 50%; }
  50% { background-position: 100% 50%; }
  100% { background-position: 0% 50%; }
}

/* ===== CONTAINER ===== */
.info-container {
  width: 100%;
  max-width: 420px;
  animation: fadeIn 0.5s ease;
}

@keyframes fadeIn {
  from { opacity: 0; transform: translateY(20px); }
  to { opacity: 1; transform: translateY(0); }
}

/* ===== HEADER ===== */
.brand-header {
  text-align: center;
  margin-bottom: 30px;
  animation: fadeInDown 0.6s ease;
}

@keyframes fadeInDown {
  from { opacity: 0; transform: translateY(-20px); }
  to { opacity: 1; transform: translateY(0); }
}

.brand-header img {
  height: 35px;
  filter: drop-shadow(0 4px 20px rgba(102, 126, 234, 0.4));
}

/* ===== CARD ===== */
.info-card {
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
  border-radius: 24px;
  padding: 35px 30px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.15);
  border: 1px solid rgba(255, 255, 255, 0.3);
  position: relative;
  overflow: hidden;
  animation: slideUp 0.6s ease;
}

@keyframes slideUp {
  from { opacity: 0; transform: translateY(30px); }
  to { opacity: 1; transform: translateY(0); }
}

.info-card::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 4px;
  background: linear-gradient(90deg, #667eea, #764ba2, #f093fb);
}

.info-card-header {
  text-align: center;
  margin-bottom: 30px;
}

.info-card-header h1 {
  font-size: 24px;
  font-weight: 800;
  background: linear-gradient(135deg, #667eea, #764ba2);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  margin-bottom: 8px;
  letter-spacing: -0.5px;
}

.info-card-header p {
  font-size: 14px;
  color: #64748b;
  font-weight: 500;
  line-height: 1.5;
}

/* ===== FORM ===== */
.form-group {
  margin-bottom: 20px;
  animation: fadeInUp 0.6s ease backwards;
}

.form-group:nth-child(1) { animation-delay: 0.1s; }
.form-group:nth-child(2) { animation-delay: 0.2s; }
.form-group:nth-child(3) { animation-delay: 0.3s; }

@keyframes fadeInUp {
  from { opacity: 0; transform: translateY(20px); }
  to { opacity: 1; transform: translateY(0); }
}

.form-group label {
  display: block;
  font-size: 13px;
  font-weight: 600;
  color: #334155;
  margin-bottom: 8px;
  letter-spacing: 0.2px;
}

.form-group select {
  width: 100%;
  padding: 14px 16px;
  border: 2px solid #e2e8f0;
  border-radius: 12px;
  font-size: 15px;
  font-weight: 500;
  color: #1e293b;
  background: white;
  transition: all 0.3s ease;
  font-family: 'Inter', sans-serif;
  cursor: pointer;
  appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 12 12'%3E%3Cpath fill='%23334155' d='M6 9L1 4h10z'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 16px center;
  padding-right: 40px;
}

.form-group select:focus {
  outline: none;
  border-color: #667eea;
  box-shadow: 0 0 0 4px rgba(102, 126, 234, 0.1);
  transform: translateY(-2px);
}

.form-group select option {
  padding: 10px;
  font-weight: 500;
}

/* Grid layout for age and gender */
.form-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 15px;
  margin-bottom: 20px;
}

/* ===== BUTTON ===== */
.btn-submit {
  width: 100%;
  padding: 16px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  border: none;
  border-radius: 14px;
  font-size: 16px;
  font-weight: 700;
  cursor: pointer;
  transition: all 0.3s ease;
  font-family: 'Inter', sans-serif;
  box-shadow: 0 8px 25px rgba(102, 126, 234, 0.35);
  margin-top: 10px;
  position: relative;
  overflow: hidden;
  animation: fadeInUp 0.6s ease backwards;
  animation-delay: 0.4s;
}

.btn-submit::before {
  content: '';
  position: absolute;
  top: 0;
  left: -100%;
  width: 100%;
  height: 100%;
  background: linear-gradient(90deg, transparent, rgba(255,255,255,0.2), transparent);
  transition: left 0.5s ease;
}

.btn-submit:hover {
  transform: translateY(-2px);
  box-shadow: 0 12px 35px rgba(102, 126, 234, 0.45);
}

.btn-submit:hover::before {
  left: 100%;
}

.btn-submit:active {
  transform: translateY(0);
}

.btn-submit:disabled {
  opacity: 0.6;
  cursor: not-allowed;
  transform: none;
}

.btn-submit .spinner {
  display: inline-block;
  width: 14px;
  height: 14px;
  border: 2px solid rgba(255,255,255,0.3);
  border-top-color: white;
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
  margin-right: 8px;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

/* ===== RESPONSIVE ===== */
@media (max-width: 480px) {
  body {
    padding: 15px;
  }
  
  .info-card {
    padding: 25px 20px;
  }
  
  .info-card-header h1 {
    font-size: 22px;
  }
  
  .info-card-header p {
    font-size: 13px;
  }
  
  .form-row {
    gap: 12px;
  }
}

@media (min-width: 768px) {
  .info-card {
    box-shadow: 0 30px 90px rgba(0, 0, 0, 0.25);
  }
}
  </style>
</head>
<body>

<div class="info-container">
  <!-- Brand Header -->
  <div class="brand-header">
    <img src="assets/img/logo.png" alt="HeySen">
  </div>

  <!-- Info Card -->
  <div class="info-card">
    <div class="info-card-header">
      <h1>Bilgilerini Tamamla</h1>
      <p>Son bir adım kaldı! Bilgilerini doldur ve<br>yeni insanlarla tanışmaya başla 💕</p>
    </div>

    <form id="infoForm">
      <!-- Şehir -->
      <div class="form-group">
        <label for="sehir">
          <i class="fas fa-map-marker-alt" style="margin-right: 5px; color: #667eea;"></i>
          Yaşadığın Şehir
        </label>
        <select id="sehir" name="sehir" required>
          <option value="0">Şehir Seçin</option>
          <?php foreach($iller as $il): ?>
          <option value="<?=$il?>"><?=$il?></option>
          <?php endforeach; ?>
        </select>
      </div>

      <!-- Yaş ve Cinsiyet -->
      <div class="form-row">
        <div class="form-group">
          <label for="yas">
            <i class="fas fa-birthday-cake" style="margin-right: 5px; color: #667eea;"></i>
            Yaşın
          </label>
          <select id="yas" name="yas" required>
            <option value="0">Yaş Seçin</option>
            <?php for($i=18; $i<=90; $i++): ?>
            <option value="<?=$i?>"><?=$i?></option>
            <?php endfor; ?>
          </select>
        </div>

        <div class="form-group">
          <label for="cinsiyet">
            <i class="fas fa-user" style="margin-right: 5px; color: #667eea;"></i>
            Cinsiyetin
          </label>
          <select id="cinsiyet" name="cinsiyet" required>
            <option value="erkek">Erkek</option>
            <option value="kadin">Kadın</option>
          </select>
        </div>
      </div>

      <!-- Submit Button -->
      <button type="submit" class="btn-submit" id="submitBtn">
        Arkadaş Aramaya Başla!
      </button>
    </form>
  </div>
</div>

<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/sweetalert2/11.7.32/sweetalert2.all.min.js"></script>
<script>
$(document).ready(function() {
  $('#infoForm').on('submit', function(e) {
    e.preventDefault();
    
    const yas = $('#yas').val();
    const cinsiyet = $('#cinsiyet').val();
    const sehir = $('#sehir').val();
    
    // Validation
    if(sehir === '0' || yas === '0') {
      Swal.fire({
        icon: 'error',
        title: 'Eksik Bilgi',
        text: 'Lütfen tüm alanları doldurun!',
        confirmButtonColor: '#667eea',
        confirmButtonText: 'Tamam'
      });
      return;
    }
    
    // Disable button and show loading
    const btn = $('#submitBtn');
    btn.prop('disabled', true);
    btn.html('<span class="spinner"></span>Kaydediliyor...');
    
    // Send data
    $.post('bilgi_doldur.php', {
      type: 'bilgi_doldur',
      yas: yas,
      cinsiyet: cinsiyet,
      sehir: sehir
    }, function(response) {
      const json = JSON.parse(response);
      
      if(json.result === '1') {
        // Debug modunda yönlendirme yapma
        if(json.debug === true) {
          Swal.fire({
            icon: 'success',
            title: 'Başarılı! (Debug Mode)',
            text: 'Bilgiler kaydedildi (Test modu - yönlendirme yapılmadı)',
            confirmButtonColor: '#667eea',
            confirmButtonText: 'Tamam'
          });
          btn.prop('disabled', false);
          btn.html('Arkadaş Aramaya Başla!');
        } else {
          Swal.fire({
            icon: 'success',
            title: 'Başarılı!',
            text: 'Bilgilerin kaydedildi. Yönlendiriliyorsun...',
            showConfirmButton: false,
            timer: 1500,
            timerProgressBar: true
          }).then(() => {
            window.location.href = 'index.php';
          });
        }
      } else {
        Swal.fire({
          icon: 'error',
          title: 'Hata!',
          text: 'Lütfen tüm alanları doldurun!',
          confirmButtonColor: '#667eea',
          confirmButtonText: 'Tamam'
        });
        btn.prop('disabled', false);
        btn.html('Arkadaş Aramaya Başla!');
      }
    }).fail(function(xhr, status, error) {
      console.log('AJAX Hatası:', {
        status: status,
        error: error,
        response: xhr.responseText,
        statusCode: xhr.status
      });
      
      Swal.fire({
        icon: 'error',
        title: 'Bağlantı Hatası',
        html: '<strong>Hata:</strong> ' + (xhr.responseText || error) + '<br><small>Status: ' + xhr.status + '</small>',
        confirmButtonColor: '#667eea',
        confirmButtonText: 'Tamam'
      });
      btn.prop('disabled', false);
      btn.html('Arkadaş Aramaya Başla!');
    });
  });
});
</script>

</body>
</html>
