<?php
require_once '../config/config.php';
require_once '../includes/auth.php';

// Eğer zaten giriş yapılmışsa dashboard'a yönlendir
if (isCompanyLoggedIn()) {
    redirect(COMPANY_URL . '/dashboard.php');
}

$error = '';

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

    if (empty($email) || empty($password)) {
        $error = 'E-posta ve şifre gereklidir.';
    } else {
        $stmt = $pdo->prepare("SELECT id, email, password, company_name, status FROM companies WHERE email = ?");
        $stmt->execute([$email]);
        $company = $stmt->fetch();

        if ($company && verifyPassword($password, $company['password'])) {
            if ($company['status'] === 'inactive') {
                $error = 'Hesabınız pasif durumda. Lütfen admin ile iletişime geçin.';
            } else {
                companyLogin($company['id'], $company['email']);
                redirect(COMPANY_URL . '/dashboard.php');
            }
        } else {
            $error = 'E-posta veya şifre hatalı.';
        }
    }
}

$flash = getFlashMessage();
?>
<!DOCTYPE html>
<html lang="tr">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Firma Girişi - <?= SITE_NAME ?></title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        body {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
        }

        .login-card {
            background: white;
            border-radius: 15px;
            box-shadow: 0 10px 40px rgba(0, 0, 0, 0.2);
            padding: 40px;
            max-width: 400px;
            width: 100%;
        }
    </style>
</head>

<body>
    <div class="login-card">
        <div class="text-center mb-4">
            <i class="fas fa-building fa-3x text-primary mb-3"></i>
            <h2>Firma Girişi</h2>
            <p class="text-muted">Kayak Firması Yönetim Paneli</p>
        </div>

        <?php if ($flash): ?>
            <div
                class="alert alert-<?= $flash['type'] === 'error' ? 'danger' : $flash['type'] ?> alert-dismissible fade show">
                <?= e($flash['message']) ?>
                <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
            </div>
        <?php endif; ?>

        <?php if ($error): ?>
            <div class="alert alert-danger alert-dismissible fade show">
                <?= e($error) ?>
                <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
            </div>
        <?php endif; ?>

        <form method="POST">
            <div class="mb-3">
                <label for="email" class="form-label">E-posta</label>
                <div class="input-group">
                    <span class="input-group-text"><i class="fas fa-envelope"></i></span>
                    <input type="email" class="form-control" id="email" name="email" required autofocus>
                </div>
            </div>

            <div class="mb-3">
                <label for="password" class="form-label">Şifre</label>
                <div class="input-group">
                    <span class="input-group-text"><i class="fas fa-lock"></i></span>
                    <input type="password" class="form-control" id="password" name="password" required>
                </div>
            </div>

            <button type="submit" class="btn btn-primary w-100 mb-3">
                <i class="fas fa-sign-in-alt me-2"></i>Giriş Yap
            </button>

            <div class="text-center">
                <p class="mb-0">Hesabınız yok mu? <a href="company-register.php" class="text-decoration-none">Hemen Üye
                        Olun</a></p>
                <p class="mt-2"><a href="../index.php" class="text-decoration-none"><i
                            class="fas fa-arrow-left me-1"></i>Ana Sayfaya Dön</a></p>
            </div>
        </form>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
</body>

</html>