<?php
// ============================================
// PATIENT LOGIN PAGE - COMPLETE FIX
// Path: /public_html/Appointment/opd/login.php
// ============================================

// --- Start Session ---
session_start();

// --- Include Config ---
require_once '../config.php';

// --- Define $brand FIRST ---
$brand = array(
    'name' => 'ওয়ান হোমিওপ্যাথি',
    'slogan' => 'প্রাকৃতিক চিকিৎসায় সুস্থ জীবন',
    'phone' => '017XXXXXXXX',
    'whatsapp' => '017XXXXXXXX',
    'email' => 'info@example.com',
    'address' => 'ঢাকা, বাংলাদেশ',
    'logo' => '',
    'facebook' => '',
    'youtube' => '',
    'hero_image' => 'assets/images/hero-bg.jpg'
);

// Try to fetch from database
try {
    $brand_result = mysqli_query($conn, "SELECT * FROM brand LIMIT 1");
    if ($brand_result && mysqli_num_rows($brand_result) > 0) {
        $brand_data = mysqli_fetch_assoc($brand_result);
        foreach ($brand_data as $key => $value) {
            if (!empty($value)) {
                $brand[$key] = $value;
            }
        }
    }
} catch (Exception $e) {
    // Table might not exist, use defaults
}

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

$error = '';

// --- Handle Login ---
if (isset($_POST['login'])) {
    $mobile = mysqli_real_escape_string($conn, $_POST['mobile']);
    $password = $_POST['password'];

    // Check if table exists first
    $table_check = mysqli_query($conn, "SHOW TABLES LIKE 'opd_patients'");
    if (mysqli_num_rows($table_check) > 0) {
        $result = mysqli_query($conn, "SELECT * FROM opd_patients WHERE mobile='$mobile'");
        if ($result && mysqli_num_rows($result) > 0) {
            $patient = mysqli_fetch_assoc($result);
            if (password_verify($password, $patient['password'])) {
                $_SESSION['opd_patient'] = $patient['id'];
                $_SESSION['opd_patient_name'] = $patient['name'];
                $_SESSION['opd_patient_mobile'] = $patient['mobile'];

                // Redirect to the original page if one was stored
                if (isset($_SESSION['redirect_after_login'])) {
                    $redirect = $_SESSION['redirect_after_login'];
                    unset($_SESSION['redirect_after_login']);
                    header('Location: ../' . $redirect);
                    exit;
                }

                header('Location: dashboard.php');
                exit;
            }
        }
        $error = "ভুল মোবাইল নম্বর বা পাসওয়ার্ড!";
    } else {
        $error = "সিস্টেমে কোনো রোগী নিবন্ধিত নেই। দয়া করে রেজিস্ট্রেশন করুন।";
    }
}
?>
<!DOCTYPE html>
<html lang="bn">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>রোগী লগইন | <?php echo 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>
        * { margin: 0; padding: 0; box-sizing: border-box; }
        body {
            background: linear-gradient(135deg, #f0f7f4, #e8f5e9);
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
            font-family: 'Hind Siliguri', 'SolaimanLipi', Arial, sans-serif;
            padding: 20px;
        }
        .login-card {
            background: #fff;
            border-radius: 20px;
            padding: 40px 35px;
            box-shadow: 0 15px 50px rgba(0,0,0,0.12);
            max-width: 440px;
            width: 100%;
            position: relative;
            overflow: hidden;
        }
        .login-card::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            height: 5px;
            background: linear-gradient(90deg, #2d6a4f, #52b788);
        }
        .login-logo {
            font-size: 3.5rem;
            margin-bottom: 5px;
        }
        .login-title {
            font-size: 1.5rem;
            font-weight: 700;
            color: #2d6a4f;
            margin-bottom: 5px;
        }
        .login-subtitle {
            color: #6c757d;
            font-size: 0.95rem;
            margin-bottom: 25px;
        }
        .form-control {
            border-radius: 12px;
            padding: 12px 18px;
            border: 2px solid #e9ecef;
            font-size: 1rem;
            transition: all 0.3s ease;
        }
        .form-control:focus {
            border-color: #2d6a4f;
            box-shadow: 0 0 0 4px rgba(45, 106, 79, 0.1);
        }
        .form-control::placeholder {
            color: #adb5bd;
        }
        .btn-login {
            background: #2d6a4f;
            color: #fff;
            border: none;
            padding: 13px;
            border-radius: 12px;
            font-weight: 600;
            font-size: 1.05rem;
            width: 100%;
            transition: all 0.3s ease;
            cursor: pointer;
        }
        .btn-login:hover {
            background: #1b4332;
            transform: translateY(-2px);
            box-shadow: 0 8px 25px rgba(45, 106, 79, 0.3);
        }
        .btn-login:active {
            transform: translateY(0);
        }
        .btn-login i {
            margin-right: 8px;
        }
        .alert {
            border-radius: 12px;
            font-size: 0.95rem;
            padding: 12px 18px;
        }
        .alert-danger {
            background: #fef0f0;
            border-color: #fdd;
            color: #c0392b;
        }
        .login-footer {
            margin-top: 20px;
            padding-top: 20px;
            border-top: 1px solid #e9ecef;
            display: flex;
            justify-content: space-between;
            align-items: center;
            flex-wrap: wrap;
            gap: 10px;
        }
        .login-footer a {
            color: #2d6a4f;
            text-decoration: none;
            font-weight: 500;
            transition: all 0.3s ease;
            font-size: 0.95rem;
        }
        .login-footer a:hover {
            color: #1b4332;
            text-decoration: underline;
        }
        .login-footer .home-link {
            color: #6c757d;
            font-weight: 400;
        }
        .login-footer .home-link:hover {
            color: #2d6a4f;
        }
        .input-group-text {
            background: transparent;
            border: 2px solid #e9ecef;
            border-right: none;
            border-radius: 12px 0 0 12px;
            color: #6c757d;
        }
        .input-group .form-control {
            border-radius: 0 12px 12px 0;
        }
        @media (max-width: 480px) {
            .login-card {
                padding: 30px 20px;
            }
            .login-title {
                font-size: 1.3rem;
            }
            .login-logo {
                font-size: 2.8rem;
            }
            .login-footer {
                flex-direction: column;
                text-align: center;
            }
        }
    </style>
</head>
<body>
    <div class="login-card">
        <div class="text-center">
            <div class="login-logo">🏥</div>
            <h4 class="login-title"><?php echo htmlspecialchars($brand['name']); ?></h4>
            <p class="login-subtitle">রোগী লগইন</p>
        </div>

        <?php if ($error): ?>
            <div class="alert alert-danger">
                <i class="fas fa-exclamation-circle"></i> <?php echo htmlspecialchars($error); ?>
            </div>
        <?php endif; ?>

        <form method="POST" action="">
            <div class="mb-3">
                <label class="form-label fw-semibold">মোবাইল নম্বর</label>
                <div class="input-group">
                    <span class="input-group-text"><i class="fas fa-phone"></i></span>
                    <input type="tel" name="mobile" class="form-control" placeholder="01XXXXXXXXX" required>
                </div>
            </div>
            <div class="mb-3">
                <label class="form-label fw-semibold">পাসওয়ার্ড</label>
                <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" name="login" class="btn-login">
                <i class="fas fa-sign-in-alt"></i> লগইন করুন
            </button>
        </form>

        <div class="login-footer">
            <a href="register.php">
                <i class="fas fa-user-plus"></i> রেজিস্ট্রেশন করুন
            </a>
            <a href="../index.php" class="home-link">
                <i class="fas fa-arrow-left"></i> হোম পেজ
            </a>
        </div>
    </div>

    <!-- Bootstrap JS for any toggles -->
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>