<?php
require_once __DIR__ . '/../includes/auth.php';
require_once __DIR__ . '/../includes/db.php';

if (is_logged_in()) { header('Location: dashboard.php'); exit; }

$error = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['submit'])) {
    $username = trim($_POST['username'] ?? '');
    $password = trim($_POST['password'] ?? '');
    if ($username && $password) {
        $stmt = $pdo->prepare('SELECT * FROM users WHERE (email = ? OR name = ?) AND is_active = 1 LIMIT 1');
        $stmt->execute([$username, $username]);
        $user = $stmt->fetch();
        if ($user && password_verify($password, $user['password'])) {
            $_SESSION['admin_id']   = $user['id'];
            $_SESSION['admin_name'] = $user['name'];
            $_SESSION['admin_role'] = $user['role'];
            header('Location: dashboard.php'); exit;
        } else { $error = 'Invalid username or password.'; }
    } else { $error = 'Please fill in all fields.'; }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <title>Admin Sign In | Turi Express</title>
  <link rel="stylesheet" href="vendors/mdi/css/materialdesignicons.min.css">
  <link rel="stylesheet" href="vendors/base/vendor.bundle.base.css">
  <link rel="stylesheet" href="css/style.css">
  <link rel="shortcut icon" href="images/favicon.png">
</head>
<body>
  <div class="container-scroller">
    <div class="container-fluid page-body-wrapper full-page-wrapper">
      <div class="main-panel">
        <div class="content-wrapper d-flex align-items-center auth px-0">
          <div class="row w-100 mx-0">
            <div class="col-lg-4 mx-auto">
              <div class="auth-form-light text-left py-5 px-4 px-sm-5">
                <div style="text-align:center;margin-bottom:20px;">
                  <img src="../uploads/686313cb0cbcasocratesLogistics.png" style="height:60px;" alt="logo">
                </div>
                <h4>Admin Panel</h4>
                <h6 class="font-weight-light">Sign in to continue.</h6>
                <?php if ($error): ?>
                <div style="padding:10px;margin:10px 0;background:#fff3f3;border:1px solid #e74c3c;border-radius:4px;color:#c00;"><?= htmlspecialchars($error) ?></div>
                <?php endif ?>
                <form class="pt-3" action="signin.php" method="post">
                  <div class="form-group">
                    <input type="text" class="form-control form-control-lg" name="username" placeholder="Username or Email" value="<?= htmlspecialchars($_POST['username'] ?? '') ?>">
                  </div>
                  <div class="form-group">
                    <input type="password" class="form-control form-control-lg" name="password" placeholder="Password">
                  </div>
                  <div class="mt-3">
                    <button class="btn btn-block btn-primary btn-lg font-weight-medium auth-form-btn" name="submit">SIGN IN</button>
                  </div>
                  <div style="margin-top:15px;text-align:center;">
                    <a href="../track.php" style="color:#888;font-size:13px;">Back to website</a>
                  </div>
                </form>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
  <script src="vendors/base/vendor.bundle.base.js"></script>
  <script src="js/template.js"></script>
</body>
</html>
