<?php
session_start();
require 'db.php';

// 如果已經登入，直接跳轉到行程列表
if (isset($_SESSION['user_id'])) {
    header("Location: add_trip.php");
    exit;
}

$error_msg = "";

// 處理 POST 請求：只保留登入邏輯
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if (isset($_POST['action']) && $_POST['action'] === 'login') {
        $email = trim($_POST['email']);
        $password = $_POST['password'];

        $stmt = $pdo->prepare("SELECT * FROM users WHERE email = ?");
        $stmt->execute([$email]);
        $user = $stmt->fetch(PDO::FETCH_ASSOC);

        // 驗證密碼
        if ($user && password_verify($password, $user['password'])) {
            $_SESSION['user_id'] = $user['id'];
            $_SESSION['username'] = $user['username'];
            
            header("Location: add_trip.php");
            exit;
        } else {
            $error_msg = "❌ 電子郵件或密碼錯誤！";
        }
    }
}
?>

<!DOCTYPE html>
<html lang="zh-TW">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>登入 - TravelPlanner</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="icon" type="image/png" href="assets/fav.png">
    <style>
        body {
            background: linear-gradient(135deg, #e0eafc, #cfdef3);
            min-height: 100vh;
            display: flex;
            align-items: center;
        }
        .auth-card {
            border-radius: 15px;
            box-shadow: 0 10px 30px rgba(0,0,0,0.1);
            background: white;
        }
    </style>
</head>
<body>

<div class="container">
    <div class="row justify-content-center">
        <div class="col-md-4"> <?php if(!empty($error_msg)): ?>
                <div class="alert alert-danger text-center shadow-sm"><?php echo $error_msg; ?></div>
            <?php endif; ?>

            <div class="card auth-card p-4">
                <div class="text-center mb-4">
                    <h2 class="fw-bold text-primary">🧳 TravelPlanner</h2>
                    <p class="text-muted">管理您的專屬旅遊行程</p>
                    <hr>
                </div>

                <form method="POST">
                    <input type="hidden" name="action" value="login">
                    
                    <div class="mb-3">
                        <label class="form-label fw-bold">電子郵件 (Email)</label>
                        <input type="email" class="form-control py-2" name="email" required placeholder="請輸入您的 Email">
                    </div>
                    
                    <div class="mb-4">
                        <label class="form-label fw-bold">密碼</label>
                        <input type="password" class="form-control py-2" name="password" required placeholder="請輸入密碼">
                    </div>
                    
                    <button type="submit" class="btn btn-primary w-100 py-2 fw-bold shadow-sm">登入系統</button>
                </form>

            </div>
        </div>
    </div>
</div>

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