<?php
// 1. Debugging ON
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

ob_start();
session_start();

// --- SMART CONFIG PATH FIX ---
// Yeh 3 alag-alag jagah check karega config file ke liye
$possible_paths = [
    __DIR__ . '/includes/config.php',       // 1. Check: Kya includes folder same jagah hai?
    __DIR__ . '/../includes/config.php',    // 2. Check: Kya includes folder ek step peeche hai?
    $_SERVER['DOCUMENT_ROOT'] . '/includes/config.php' // 3. Check: Kya main domain root par hai?
];

$config_loaded = false;
foreach ($possible_paths as $path) {
    if (file_exists($path)) {
        require_once $path;
        $config_loaded = true;
        // Debug msg (baad mein hata dena):
        // echo "Config loaded from: " . $path; 
        break;
    }
}

if (!$config_loaded) {
    echo "<div style='color:red; background:white; padding:20px; border:2px solid red;'>";
    echo "<h3>❌ CRITICAL ERROR: Config file nahi mili!</h3>";
    echo "<p>Maine in paths par check kiya:</p><ul>";
    foreach ($possible_paths as $p) {
        echo "<li>" . $p . "</li>";
    }
    echo "</ul><p>Please check karo ki <b>includes</b> folder aur <b>config.php</b> kahan upload kiye hain.</p>";
    die("</div>");
}

// Connection Check
if (!isset($conn) || $conn->connect_error) {
    die("<div style='color:red; background:white; padding:10px;'>❌ Database Connection Failed: " . ($conn->connect_error ?? '$conn variable missing') . "</div>");
}

if (isset($_SESSION['admin_id'])) { 
    header('Location: dashboard.php'); 
    exit; 
}

$error = '';

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

    $stmt = $conn->prepare("SELECT id, password_hash FROM admins WHERE username=? LIMIT 1");
    
    if (!$stmt) {
        $error = "❌ SQL Prepare Error: " . $conn->error;
    } else {
        $stmt->bind_param('s', $u); 
        $stmt->execute();
        $stmt->bind_result($aid, $hash);
        
        if ($stmt->fetch()) {
            if (password_verify($p, $hash)) {
                $_SESSION['admin_id'] = $aid; 
                $_SESSION['admin_username'] = $u;
                header('Location: dashboard.php'); 
                exit;
            } else { 
                $error = '❌ Password Galat hai'; 
            }
        } else { 
            $error = '❌ Username nahi mila'; 
        }
        $stmt->close();
    }
}
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>Admin Login - COSCO</title>
<style>
:root {
  --blue:#0047ab;
  --light:#e6f3ff;
  --danger:#e60012;
  --glass: rgba(255,255,255,0.08);
}
body {
  margin:0;
  font-family: 'Segoe UI', Arial, sans-serif;
  height:100vh;
  display:flex;justify-content:center;align-items:center;
  background:linear-gradient(135deg,#0b1220,#001d40);
  overflow:hidden;
}
.login-card {
  width:100%;
  max-width:350px;
  background:var(--glass);
  padding:40px 30px;
  border-radius:20px;
  box-shadow:0 8px 32px rgba(0,0,0,0.5);
  backdrop-filter:blur(10px);
  animation:fadeIn .7s ease;
}
@keyframes fadeIn {
  from{transform:translateY(30px);opacity:0}
  to{transform:translateY(0);opacity:1}
}
.logo {
  display:block;
  margin:0 auto 18px;
  width:80px;height:80px;
  border-radius:50%;
  background:#fff url('../images/loginlogo.png') no-repeat center/contain;
  box-shadow:0 0 25px rgba(0,180,255,0.6);
  animation:glow 2s infinite alternate;
}
@keyframes glow {
  from {box-shadow:0 0 15px rgba(0,180,255,0.4);}
  to {box-shadow:0 0 30px rgba(0,180,255,0.9);}
}
h2 {
  text-align:center;
  margin-bottom:20px;
  font-size:22px;
  color:var(--light);
  letter-spacing:1px;
}
.inp {
  width:100%;
  padding:12px 14px;
  margin:10px 0;
  border-radius:10px;
  border:1px solid rgba(255,255,255,0.2);
  background:rgba(255,255,255,0.08);
  color:#fff;
  font-size:14px;
  box-sizing: border-box;
}
.inp::placeholder { color:#ccc; }
.inp:focus {
  outline:none;
  border-color:var(--blue);
  box-shadow:0 0 10px rgba(0,71,171,0.8);
}
.btn {
  width:100%;
  padding:12px;
  margin-top:12px;
  border:none;
  border-radius:10px;
  color:#fff;
  font-weight:bold;
  font-size:16px;
  cursor:pointer;
  background:linear-gradient(90deg,#0047ab,#00b4ff,#0047ab);
  background-size:200% auto;
  animation:shine 3s linear infinite;
  transition:0.3s;
}
.btn:hover {transform:scale(1.03);}
@keyframes shine {
  0%{background-position:0% center;}
  100%{background-position:200% center;}
}
.alert {
  background:rgba(255,0,0,0.2);
  border:1px solid #ff6a6a;
  border-radius:8px;
  padding:10px;
  margin-bottom:12px;
  font-size:14px;
  text-align:center;
  color: #ffcccc;
}
</style>
</head>
<body>
  <div class="login-card">
    <div class="logo"></div>
    <h2>Admin Login</h2>
    <?php if($error): ?><div class="alert"><?=$error?></div><?php endif;?>
    <form method="post">
      <input class="inp" name="username" placeholder="👤 Username" required value="<?= htmlspecialchars($_POST['username'] ?? '') ?>">
      <input class="inp" type="password" name="password" placeholder="🔒 Password" required>
      <button class="btn">Login</button>
    </form>
  </div>
</body>
</html>