<?php 

session_start();
include 'config.php';

$ip = $_SERVER["REMOTE_ADDR"];
$user_onesignal = $_COOKIE['user_onesignal'] ?? null;
$user_token = $_COOKIE['user_token'] ?? null;
$myUser = $db->query("SELECT * FROM kullanicilar WHERE user_token = '$user_token'")->fetch(PDO::FETCH_ASSOC);

if (!empty($user_onesignal) && !empty($user_token)) {
    if (!empty($myUser['user_yas']) && !empty($myUser['user_cinsiyet']) && !empty($myUser['user_sehir'])) {
        header("Location: index.php");
        exit();
    } else {
        header("Location: bilgi_doldur.php");
        exit();
    }
}

?>
<!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">
  <!-- Fonts & Icons -->
  <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&display=swap" rel="stylesheet">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
  <!-- SweetAlert2 CSS -->
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/limonte-sweetalert2/11.7.32/sweetalert2.min.css">
  <!-- Google Analytics -->
  <script async src="https://www.googletagmanager.com/gtag/js?id=UA-110488191-7"></script>
  <script>
    window.dataLayer = window.dataLayer || [];
    function gtag(){dataLayer.push(arguments);}
    gtag('js', new Date());
    gtag('config', 'UA-110488191-7');
  </script>
  <title>HeySen - Giriş Yap</title>
  <style>
/* ===== RESET & BASE ===== */
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

html, body {
  width: 100%;
  min-height: 100vh;
  margin: 0;
  padding: 0;
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', 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;
  min-height: 100vh;
  padding: 20px;
  overflow-x: hidden;
}

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

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

/* ===== LOGIN CONTAINER ===== */
.login-container {
  width: 100%;
  max-width: 500px;
  animation: fadeInUp 0.6s ease;
}

/* ===== LOGO & BRANDING ===== */
.brand-header {
  text-align: center;
  margin-bottom: 35px;
}

.brand-header img {
  height: 45px;
  margin-bottom: 20px;
  filter: drop-shadow(0 4px 20px rgba(255, 255, 255, 0.3));
}

.brand-header h1 {
  font-size: 32px;
  font-weight: 800;
  color: white;
  text-shadow: 0 4px 20px rgba(0, 0, 0, 0.2);
  margin: 0;
}

.brand-header p {
  font-size: 16px;
  color: rgba(255, 255, 255, 0.9);
  margin-top: 8px;
}

/* ===== LOGIN CARD ===== */
.login-card {
  background: white;
  border-radius: 28px;
  box-shadow: 0 25px 80px rgba(0, 0, 0, 0.2);
  overflow: hidden;
}

.login-card-header {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  padding: 35px 40px;
  color: white;
  text-align: center;
}

.login-card-header h2 {
  font-size: 24px;
  font-weight: 700;
  margin: 0 0 8px 0;
}

.login-card-header p {
  font-size: 15px;
  opacity: 0.95;
  margin: 0;
}

.login-card-body {
  padding: 20px 50px;
}

/* ===== FORM ELEMENTS ===== */
.form-group {
  margin: 10px 0;
}

input[type="email"],
input[type="text"],
input[type="password"] {
  width: 100%;
  padding: 16px 20px;
  border: 2px solid #e2e8f0;
  border-radius: 12px;
  font-size: 15px;
  font-weight: 500;
  color: #2d3748;
  background: #f7fafc;
  transition: all 0.3s ease;
  font-family: 'Inter', sans-serif;
}

input:hover {
  border-color: #cbd5e0;
  background: white;
}

input:focus {
  outline: none;
  border-color: #667eea;
  background: white;
  box-shadow: 0 0 0 4px rgba(102, 126, 234, 0.1);
}

input::placeholder {
  color: #a0aec0;
}

/* ===== BUTTONS ===== */
.button {
  width: 100%;
  padding: 16px 30px;
  border: none;
  border-radius: 12px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s ease;
  font-family: 'Inter', sans-serif;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  margin: 10px 0;
}

.btn-primary {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  box-shadow: 0 4px 15px rgba(102, 126, 234, 0.35);
}

.btn-primary:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 25px rgba(102, 126, 234, 0.45);
}

.btn-warning {
  background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
  color: white;
  box-shadow: 0 4px 15px rgba(240, 147, 251, 0.35);
  text-decoration: none;
  display: flex;
}

.btn-warning:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 25px rgba(240, 147, 251, 0.45);
}

/* ===== LINKS ===== */
.form-links {
  text-align: right;
  margin: 15px 0;
}

.form-links a {
  color: #667eea;
  text-decoration: none;
  font-size: 14px;
  font-weight: 500;
  transition: all 0.2s ease;
}

.form-links a:hover {
  color: #764ba2;
  text-decoration: underline;
}

/* ===== ALT ACTIONS ===== */
.alt-actions {
  text-align: center;
  margin-top: 15px;
  padding-top: 12px;
  border-top: 2px solid rgba(255, 255, 255, 0.2);
}

.alt-actions p {
  color: rgba(255, 255, 255, 0.9);
  font-size: 15px;
  margin: 10px 0;
}

.login-with-google-btn {
  width: 100%;
  max-width: 450px;
  padding: 18px 30px 18px 60px;
  background: white;
  color: #2d3748;
  border: none;
  border-radius: 14px;
  font-size: 16px;
  font-weight: 600;
  font-family: 'Inter', sans-serif;
  cursor: pointer;
  transition: all 0.3s ease;
  display: block;
  margin: 0 auto;
  background-image: url(data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMTgiIGhlaWdodD0iMTgiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyI+PGcgZmlsbD0ibm9uZSIgZmlsbC1ydWxlPSJldmVub2RkIj48cGF0aCBkPSJNMTcuNiA5LjJsLS4xLTEuOEg5djMuNGg0LjhDMTMuNiAxMiAxMyAxMyAxMiAxMy42djIuMmgzYTguOCA4LjggMCAwIDAgMi42LTYuNnoiIGZpbGw9IiM0Mjg1RjQiIGZpbGwtcnVsZT0ibm9uemVybyIvPjxwYXRoIGQ9Ik05IDE4YzIuNCAwIDQuNS0uOCA2LTIuMmwtMy0yLjJhNS40IDUuNCAwIDAgMS04LTIuOUgxVjEzYTkgOSAwIDAgMCA4IDV6IiBmaWxsPSIjMzRBODUzIiBmaWxsLXJ1bGU9Im5vbnplcm8iLz48cGF0aCBkPSJNNCAxMC43YTUuNCA1LjQgMCAwIDEgMC0zLjRWNUgxYTkgOSAwIDAgMCAwIDhsMy0yLjN6IiBmaWxsPSIjRkJCQzA1IiBmaWxsLXJ1bGU9Im5vbnplcm8iLz48cGF0aCBkPSJNOSAzLjZjMS4zIDAgMi41LjQgMy40IDEuM0wxNSAyLjNBOSA5IDAgMCAwIDEgNWwzIDIuNGE1LjQgNS40IDAgMCAxIDUtMy43eiIgZmlsbD0iI0VBNDMzNSIgZmlsbC1ydWxlPSJub256ZXJvIi8+PHBhdGggZD0iTTAgMGgxOHYxOEgweiIvPjwvZz48L3N2Zz4=);
  background-repeat: no-repeat;
  background-position: 24px center;
  background-size: 20px;
  box-shadow: 0 4px 15px rgba(0, 0, 0, 0.15);
}

.login-with-google-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.2);
}

/* ===== RESPONSIVE ===== */
@media (max-width: 768px) {
  body { padding: 25px 15px; }
  .brand-header h1 { font-size: 26px; }
  .login-card-header { padding: 28px 30px; }
  .login-card-header h2 { font-size: 20px; }
  .login-card-body { padding: 20px 30px; }
}
  </style>
</head>
<body>

<div class="login-container">
  <div class="brand-header">
    <img src="https://app.heysen.net/assets/img/logo.png" alt="HeySen Logo">
    <h1>Tekrar Hoş Geldin!</h1>
    <p>Hesabına giriş yap ve devam et</p>
  </div>

  <div class="login-card">
    <div class="login-card-header">
      <h2>Giriş Yap</h2>
      <p>E-posta ve şifrenle devam et</p>
    </div>

    <div class="login-card-body">
      <form>
        <input id="onesignal" type="hidden" value="<?=$user_onesignal?>">
        
        <div class="form-group">
          <input type="email" id="email1" placeholder="E-posta Adresiniz" required>
        </div>

        <div class="form-group">
          <input type="text" id="password1" placeholder="Şifreniz" required>
        </div>

        <div class="form-links">
          <a href="javascript:void(0)" onclick="alert('Yakında aktif olacak');">Şifremi Unuttum?</a>
        </div>

        <button type="button" id="gonder" onclick="girisYap();" class="button btn-primary">
          <i class="fas fa-sign-in-alt"></i> Giriş Yap
        </button>

        <a href="register.php" class="button btn-warning">
          <i class="fas fa-user-plus"></i> Üyeliğin Yoksa Kaydol
        </a>
      </form>
    </div>
  </div>

  <div class="alt-actions">
    <p>veya</p>
    <button onclick="goTetikle();" type="button" class="login-with-google-btn">
      Google Hesabı ile Hızlı Giriş
    </button>
  </div>
</div>

<!-- Scripts -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/js-cookie/3.0.5/js.cookie.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/limonte-sweetalert2/11.7.32/sweetalert2.all.min.js"></script>
<script>
function goTetikle() {
  try { Android.goLoginAc(); } catch(e) { console.log('Android bridge not available'); }
}

function girisYap() {
  document.getElementById("email1").disabled = true;
  document.getElementById("password1").disabled = true;
  $("#gonder").html('<i class="fa-solid fa-spinner fa-spin"></i> Lütfen Bekleyin...');

  const data = {
    type: 'login',
    email: $('#email1').val(),
    password: $('#password1').val(),
    onesignal: $('#onesignal').val()
  };

  $.post('assets/api.php', data, function(response) {
    document.getElementById("email1").disabled = false;
    document.getElementById("password1").disabled = false;
    $("#gonder").html('<i class="fas fa-sign-in-alt"></i> Giriş Yap');

    const json = JSON.parse(response);
    const messages = {
      "100": "Kullanıcı adı veya şifrenizi yazmadınız!",
      "101": "Sadece mobil uygulamamız üzerinden kayıt olabilirsiniz!",
      "0": "Kullanıcı adı veya şifre yanlış!"
    };

    if (json.result === "1") {
      Cookies.set('user_token', json.cookie, { expires: 365, domain: 'app.heysen.net' });
      Swal.fire({
        icon: 'success',
        title: 'Giriş Yapıldı!',
        html: 'Başarılı bir şekilde giriş yapıldı, yönlendiriliyorsunuz!',
        showConfirmButton: false,
        timer: 4000
      }).then(function() {
        window.location.href = "bilgi_doldur.php";
      });
    } else if (messages[json.result]) {
      Swal.fire({
        icon: 'error',
        title: 'Hata!',
        html: messages[json.result],
        confirmButtonColor: '#667eea',
        confirmButtonText: 'Tamam'
      });
    }
  });
}
</script>
</body>
</html>
