<?php
session_start();
require_once __DIR__ . '/../../includes/config.php';

if (isset($_SESSION['logged_in']) && $_SESSION['logged_in'] === true) {
    header('Location: /aractakip/index.php');
    exit;
}

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $username_or_email = trim($_POST['username_or_email'] ?? '');
    $password = $_POST['password'] ?? '';

    if (!$username_or_email || !$password) {
        $error = "Tüm alanları doldurun.";
    } else {
        $stmt = $pdo->prepare("SELECT id, username, password FROM users WHERE username = :username OR email = :email LIMIT 1");
            $stmt->execute([
            ':username' => $username_or_email,
            ':email' => $username_or_email
]);
        $user = $stmt->fetch();

        if ($user && password_verify($password, $user['password'])) {
            // Başarılı giriş
            session_regenerate_id(true);
            $_SESSION['user_id'] = $user['id'];
            $_SESSION['username'] = $user['username'];
            $_SESSION['logged_in'] = true;
            $status = "success";
        } else {
            $status = "error";
        }
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<!-- [Head] start -->

<head>
  <title>Giriş Yap - Araç Takip Otomasyonu</title>
  <!-- [Meta] -->
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=0, minimal-ui">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="description" content="Mantis is made using Bootstrap 5 design framework. Download the free admin template & use it for your project.">
  <meta name="keywords" content="Mantis, Dashboard UI Kit, Bootstrap 5, Admin Template, Admin Dashboard, CRM, CMS, Bootstrap Admin Template">
  <meta name="author" content="CodedThemes">

  <!-- [Favicon] icon -->
  <link rel="icon" href="../assets/images/favicon.svg" type="image/x-icon"> <!-- [Google Font] Family -->
<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Public+Sans:wght@300;400;500;600;700&display=swap" id="main-font-link">
<!-- [Tabler Icons] https://tablericons.com -->
<link rel="stylesheet" href="../assets/fonts/tabler-icons.min.css" >
<!-- [Feather Icons] https://feathericons.com -->
<link rel="stylesheet" href="../assets/fonts/feather.css" >
<!-- [Font Awesome Icons] https://fontawesome.com/icons -->
<link rel="stylesheet" href="../assets/fonts/fontawesome.css" >
<!-- [Material Icons] https://fonts.google.com/icons -->
<link rel="stylesheet" href="../assets/fonts/material.css" >
<!-- [Template CSS Files] -->
<link rel="stylesheet" href="../assets/css/style.css" id="main-style-link" >
<link rel="stylesheet" href="../assets/css/style-preset.css" >
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>

</head>
<!-- [Head] end -->
<!-- [Body] Start -->

<body>
  <!-- [ Pre-loader ] start -->
  <div class="loader-bg">
    <div class="loader-track">
      <div class="loader-fill"></div>
    </div>
  </div>
  <!-- [ Pre-loader ] End -->
<?php if (!empty($error)): ?><p style="color:red;"><?=htmlspecialchars($error)?></p><?php endif; ?>
  <form method="post" action="">
  <div class="auth-main">
    <div class="auth-wrapper v3">
      <div class="auth-form">
        <div class="auth-header">
          <a href="#"><img src="../assets/images/logo-dark.png" alt="img"></a>
        </div>
        <div class="card my-5">
          <div class="card-body">
            <div class="d-flex justify-content-between align-items-end mb-4">
              <h3 class="mb-0"><b>Giriş yap</b></h3>
            </div>
            <div class="form-group mb-3">
              <label class="form-label">Kullanıcı adı</label>
              <input type="username" name="username_or_email" required class="form-control" placeholder="Kullanıcı adı">
            </div>
            <div class="form-group mb-3">
              <label class="form-label">Şifreniz</label>
              <input type="password" name="password" required class="form-control" placeholder="Şifre">
            </div>
              </form>
            <div class="d-grid mt-4">
              <button type="submit" class="btn btn-primary">Giriş yap</button>
            </div>
            <?php if (isset($status) && $status == "success") : ?>
<script>
Swal.fire({
  icon: 'success',
  title: 'Giriş başarılı!',
  text: 'Yönlendiriliyorsunuz...',
  showConfirmButton: false,
  timer: 1500,
}).then(() => {
  window.location.href = '../../index.php'; // Admin paneline yönlendir
});
</script>
<?php elseif (isset($status) && $status == "error") : ?>
<script>
Swal.fire({
  icon: 'error',
  title: 'Hatalı giriş!',
  text: 'E-posta veya şifre yanlış.',
  confirmButtonText: 'Tekrar dene'
});
</script>
<?php endif; ?>
          </div>
        </div>
        <div class="auth-footer row">
          <!-- <div class=""> -->
            <div class="col my-1">
              <p class="m-0">Copyright © <a href="#">stama</a></p>
            </div>
          <!-- </div> -->
        </div>
      </div>
    </div>
  </div>
  <!-- [ Main Content ] end -->
  <!-- Required Js -->
  <script src="../assets/js/plugins/popper.min.js"></script>
  <script src="../assets/js/plugins/simplebar.min.js"></script>
  <script src="../assets/js/plugins/bootstrap.min.js"></script>
  <script src="../assets/js/fonts/custom-font.js"></script>
  <script src="../assets/js/pcoded.js"></script>
  <script src="../assets/js/plugins/feather.min.js"></script>

  
  
  
  
  <script>layout_change('light');</script>
  
  
  
  
  <script>change_box_container('false');</script>
  
  
  
  <script>layout_rtl_change('false');</script>
  
  
  <script>preset_change("preset-1");</script>
  
  
  <script>font_change("Public-Sans");</script>
  
    
 
</body>
<!-- [Body] end -->

</html>