<?php
// =========================================================
// PATIENT LOGIN PAGE
// =========================================================
if (session_status() === PHP_SESSION_NONE) session_start();
require_once '../config.php';

// If already logged in, go to dashboard
if (isset($_SESSION['opd_patient'])) {
    header('Location: dashboard.php');
    exit;
}

// Fetch brand info for page title
$brand = getBrand();
$error = '';

// Handle login form submission
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $mobile = trim($_POST['mobile'] ?? '');
    $password = $_POST['password'] ?? '';

    if (empty($mobile) || empty($password)) {
        $error = 'মোবাইল নম্বর এবং পাসওয়ার্ড দিন';
    } else {
        $mobile_esc = $conn->real_escape_string($mobile);
        $patient = $conn->query("SELECT * FROM opd_patients WHERE mobile = '$mobile_esc'")->fetch_assoc();

        if ($patient && password_verify($password, $patient['password'])) {
            // Login successful – store patient ID and redirect
            $_SESSION['opd_patient'] = $patient['id'];
            header('Location: dashboard.php');
            exit;
        } else {
            $error = 'ভুল মোবাইল নম্বর বা পাসওয়ার্ড';
        }
    }
}
?>
<!DOCTYPE html>
<html lang="bn">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>রোগী লগইন | <?= htmlspecialchars($brand['name'] ?? 'ওয়ান হোমিওপ্যাথি') ?></title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
    <style>
        body {
            background: linear-gradient(135deg, #1b4332, #2d6a4f, #40916c);
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
            font-family: 'Segoe UI', 'Hind Siliguri', sans-serif;
            padding: 20px;
        }
        .login-box {
            background: #fff;
            border-radius: 24px;
            padding: 40px 35px;
            max-width: 420px;
            width: 100%;
            box-shadow: 0 20px 60px rgba(0,0,0,0.3);
            animation: fadeInUp 0.6s ease;
        }
        @keyframes fadeInUp {
            from { opacity: 0; transform: translateY(30px); }
            to { opacity: 1; transform: translateY(0); }
        }
        .brand-icon {
            font-size: 4rem;
            margin-bottom: 10px;
        }
        .brand-name {
            font-weight: 700;
            color: #2d6a4f;
        }
        .btn-login {
            background: #2d6a4f;
            color: #fff;
            font-weight: 700;
            padding: 12px;
            border-radius: 30px;
            transition: 0.3s;
            border: none;
        }
        .btn-login:hover {
            background: #1b4332;
            color: #fff;
            transform: translateY(-2px);
        }
        .forgot-link {
            font-size: 0.85rem;
            margin-top: 12px;
        }
        .forgot-link a {
            color: #2d6a4f;
            text-decoration: none;
        }
        .forgot-link a:hover {
            text-decoration: underline;
        }
        .register-link {
            font-size: 0.9rem;
        }
        .register-link a {
            color: #2d6a4f;
            font-weight: 600;
            text-decoration: none;
        }
        .register-link a:hover {
            text-decoration: underline;
        }
        .home-link {
            font-size: 0.8rem;
            color: #6c757d;
        }
        .home-link a {
            color: #6c757d;
        }
        .home-link a:hover {
            color: #2d6a4f;
        }
        .alert {
            border-radius: 12px;
        }
    </style>
</head>
<body>
<div class="login-box text-center">
    <div class="brand-icon">🏥</div>
    <h3 class="brand-name"><?= htmlspecialchars($brand['name'] ?? 'ওয়ান হোমিওপ্যাথি') ?></h3>
    <p class="text-muted">রোগী লগইন</p>

    <?php if (!empty($error)): ?>
        <div class="alert alert-danger text-start"><?= htmlspecialchars($error) ?></div>
    <?php endif; ?>

    <form method="POST" action="">
        <div class="mb-3">
            <div class="input-group">
                <span class="input-group-text"><i class="fas fa-phone"></i></span>
                <input type="text" name="mobile" class="form-control" placeholder="মোবাইল নম্বর" required>
            </div>
        </div>
        <div class="mb-3">
            <div class="input-group">
                <span class="input-group-text"><i class="fas fa-lock"></i></span>
                <input type="password" name="password" class="form-control" placeholder="পাসওয়ার্ড" required>
            </div>
        </div>
        <button type="submit" class="btn btn-login w-100 btn-lg">লগইন করুন</button>
    </form>

    <div class="forgot-link">
        <a href="forgot_password.php"><i class="fas fa-key"></i> পাসওয়ার্ড ভুলে গেছেন?</a>
    </div>

    <hr>

    <div class="register-link">
        <i class="fas fa-user-plus"></i> অ্যাকাউন্ট নেই? <a href="register.php">রেজিস্ট্রেশন করুন</a>
    </div>

    <div class="home-link mt-2">
        <a href="../index.php"><i class="fas fa-home"></i> হোমপেজে ফিরুন</a>
    </div>
</div>

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