<?php
session_start();
error_reporting(E_ALL);
ini_set('display_errors', 1);

if (!isset($_SESSION['doctor_id'])) {
    header("Location: login.php");
    exit();
}

include '../include/config.php';

$doctor_id = $_SESSION['doctor_id'];
$doctor_name = $_SESSION['doctor_name'];
$consult_id = isset($_GET['id']) ? intval($_GET['id']) : 0;

if ($consult_id == 0) {
    header("Location: consultations.php?error=Invalid consultation ID");
    exit();
}

// Get consultation details with patient information
$consult = $conn->query("
    SELECT c.*, 
           p.first_name, 
           p.last_name, 
           p.patient_id as patient_uid,
           p.email, 
           p.phone,
           p.date_of_birth,
           p.address,
           p.medical_history,
           p.blood_group,
           p.allergies,
           p.gender
    FROM consultations c
    JOIN patients p ON c.patient_id = p.patient_id
    WHERE c.id = '$consult_id' AND c.doctor_id = '$doctor_id'
");

if (!$consult || $consult->num_rows == 0) {
    header("Location: consultations.php?error=Consultation not found");
    exit();
}

$c = $consult->fetch_assoc();
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Consultation Details | UHD</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.6.0/css/all.min.css">
    <style>
        :root {
            --uhd-green: #0a3d2f;
            --uhd-gold: #d4a017;
            --uhd-dark: #064e3b;
        }
        body { background: #f0f2f5; font-family: 'Segoe UI', sans-serif; }
        
        /* Sidebar */
        .sidebar {
            position: fixed;
            left: 0;
            top: 0;
            width: 280px;
            height: 100%;
            background: linear-gradient(135deg, #0a3d2f, #064e3b);
            color: white;
            z-index: 1000;
            overflow-y: auto;
            transition: 0.3s;
        }
        .sidebar-header {
            padding: 25px;
            text-align: center;
            border-bottom: 1px solid rgba(255,255,255,0.2);
        }
        .sidebar-header h4 { font-size: 1.2rem; margin: 10px 0 5px; }
        .sidebar-header p { font-size: 0.75rem; opacity: 0.8; }
        .sidebar-menu a {
            display: block;
            padding: 12px 20px;
            color: white;
            text-decoration: none;
            transition: 0.2s;
        }
        .sidebar-menu a:hover, .sidebar-menu a.active {
            background: rgba(255,255,255,0.15);
            border-left: 4px solid var(--uhd-gold);
        }
        .sidebar-menu i { width: 28px; margin-right: 10px; }
        
        .main-content {
            margin-left: 280px;
            padding: 25px;
            transition: 0.3s;
        }
        
        .detail-card {
            background: white;
            border-radius: 24px;
            overflow: hidden;
            box-shadow: 0 5px 20px rgba(0,0,0,0.08);
            margin-bottom: 25px;
        }
        .detail-header {
            background: linear-gradient(135deg, var(--uhd-green), var(--uhd-dark));
            padding: 18px 25px;
            color: white;
        }
        .info-section {
            padding: 20px;
            border-bottom: 1px solid #e0e0e0;
        }
        .info-label {
            font-weight: 600;
            color: var(--uhd-green);
            width: 140px;
            display: inline-block;
        }
        .status-badge {
            padding: 6px 15px;
            border-radius: 30px;
            font-size: 0.8rem;
            font-weight: 600;
        }
        .btn-gold {
            background: var(--uhd-gold);
            color: white;
            border-radius: 30px;
            padding: 10px 25px;
            font-weight: 500;
            border: none;
        }
        .btn-gold:hover {
            background: #b8860b;
            color: white;
        }
        .menu-toggle {
            display: none;
            position: fixed;
            top: 15px;
            left: 15px;
            z-index: 1100;
            background: var(--uhd-green);
            color: white;
            border: none;
            padding: 8px 14px;
            border-radius: 8px;
        }
        
        @media (max-width: 768px) {
            .sidebar { transform: translateX(-100%); width: 260px; }
            .sidebar.active { transform: translateX(0); }
            .main-content { margin-left: 0; }
            .menu-toggle { display: block; }
            .info-label { width: 100%; margin-bottom: 5px; }
        }
    </style>
</head>
<body>

<button class="menu-toggle" onclick="document.getElementById('sidebar').classList.toggle('active')">
    <i class="fas fa-bars"></i> Menu
</button>

<div class="sidebar" id="sidebar">
    <div class="sidebar-header">
        <i class="fas fa-stethoscope fa-3x mb-2" style="color: var(--uhd-gold);"></i>
        <h4>Dr. <?= htmlspecialchars($doctor_name) ?></h4>
        <p>UHD Doctor Portal</p>
    </div>
    <div class="sidebar-menu">
        <a href="dashboard.php"><i class="fas fa-tachometer-alt"></i> Dashboard</a>
        <a href="my_patients.php"><i class="fas fa-users"></i> My Patients</a>
        <a href="consultations.php" class="active"><i class="fas fa-video"></i> Video Consult</a>
        <a href="appointments.php"><i class="fas fa-calendar-alt"></i> Appointments</a>
        <a href="prescriptions.php"><i class="fas fa-prescription-bottle"></i> Prescriptions</a>
        <a href="doctor_engagement.php"><i class="fas fa-handshake"></i> Engagement Hub</a>
        <a href="cme_sessions.php"><i class="fas fa-graduation-cap"></i> CME Sessions</a>
        <a href="ai_assistant.php"><i class="fas fa-robot"></i> AI Assistant</a>
        <a href="doctor_payments.php"><i class="fas fa-money-bill-wave"></i> Payments</a>
        <a href="profile.php"><i class="fas fa-user-edit"></i> Profile</a>
        <a href="logout.php"><i class="fas fa-sign-out-alt"></i> Logout</a>
    </div>
</div>

<div class="main-content">
    <div class="container-fluid">
        <!-- Header -->
        <div class="d-flex justify-content-between align-items-center mb-4">
            <div>
                <h2 class="text-success"><i class="fas fa-file-alt me-2" style="color: var(--uhd-gold);"></i> Consultation Details</h2>
                <p class="text-muted">View complete consultation information</p>
            </div>
            <a href="consultations.php" class="btn btn-outline-secondary">
                <i class="fas fa-arrow-left me-2"></i> Back to Consultations
            </a>
        </div>

        <div class="row">
            <!-- Patient Information Column -->
            <div class="col-lg-6">
                <div class="detail-card">
                    <div class="detail-header">
                        <h5 class="mb-0"><i class="fas fa-user-circle me-2"></i> Patient Information</h5>
                    </div>
                    <div class="info-section">
                        <p><span class="info-label"><i class="fas fa-user me-2"></i>Full Name:</span> Dr. <?= htmlspecialchars($c['first_name'] . ' ' . $c['last_name']) ?></p>
                        <p><span class="info-label"><i class="fas fa-id-card me-2"></i>Patient ID:</span> <?= htmlspecialchars($c['patient_uid']) ?></p>
                        <p><span class="info-label"><i class="fas fa-venus-mars me-2"></i>Gender:</span> <?= htmlspecialchars($c['gender'] ?? 'Not specified') ?></p>
                        <p><span class="info-label"><i class="fas fa-calendar-alt me-2"></i>Date of Birth:</span> <?= $c['date_of_birth'] ? date('M d, Y', strtotime($c['date_of_birth'])) : 'N/A' ?></p>
                        <p><span class="info-label"><i class="fas fa-envelope me-2"></i>Email:</span> <?= htmlspecialchars($c['email'] ?? 'N/A') ?></p>
                        <p><span class="info-label"><i class="fas fa-phone me-2"></i>Phone:</span> <?= htmlspecialchars($c['phone'] ?? 'N/A') ?></p>
                        <p><span class="info-label"><i class="fas fa-map-marker-alt me-2"></i>Address:</span> <?= nl2br(htmlspecialchars($c['address'] ?? 'N/A')) ?></p>
                    </div>
                </div>

                <!-- Medical Information -->
                <div class="detail-card">
                    <div class="detail-header">
                        <h5 class="mb-0"><i class="fas fa-notes-medical me-2"></i> Medical Information</h5>
                    </div>
                    <div class="info-section">
                        <p><span class="info-label"><i class="fas fa-tint me-2"></i>Blood Group:</span> <?= htmlspecialchars($c['blood_group'] ?? 'Not recorded') ?></p>
                        <p><span class="info-label"><i class="fas fa-allergies me-2"></i>Allergies:</span> <?= nl2br(htmlspecialchars($c['allergies'] ?? 'None reported')) ?></p>
                        <p><span class="info-label"><i class="fas fa-history me-2"></i>Medical History:</span> <?= nl2br(htmlspecialchars($c['medical_history'] ?? 'No significant history')) ?></p>
                    </div>
                </div>
            </div>

            <!-- Consultation Information Column -->
            <div class="col-lg-6">
                <div class="detail-card">
                    <div class="detail-header">
                        <h5 class="mb-0"><i class="fas fa-stethoscope me-2"></i> Consultation Details</h5>
                    </div>
                    <div class="info-section">
                        <p><span class="info-label"><i class="fas fa-hashtag me-2"></i>Consultation ID:</span> #<?= $c['id'] ?></p>
                        <p><span class="info-label"><i class="fas fa-calendar-check me-2"></i>Scheduled:</span> <?= date('l, F j, Y \a\t h:i A', strtotime($c['scheduled_time'])) ?></p>
                        <p>
                            <span class="info-label"><i class="fas fa-tag me-2"></i>Status:</span>
                            <span class="status-badge bg-<?= $c['status'] == 'completed' ? 'success' : ($c['status'] == 'ongoing' ? 'danger' : ($c['status'] == 'scheduled' ? 'warning' : 'secondary')) ?>">
                                <i class="fas fa-<?= $c['status'] == 'completed' ? 'check-circle' : ($c['status'] == 'ongoing' ? 'video' : ($c['status'] == 'scheduled' ? 'clock' : 'times-circle')) ?> me-1"></i>
                                <?= ucfirst($c['status']) ?>
                            </span>
                        </p>
                        <?php if ($c['started_at']): ?>
                            <p><span class="info-label"><i class="fas fa-play me-2"></i>Started:</span> <?= date('h:i A', strtotime($c['started_at'])) ?></p>
                        <?php endif; ?>
                        <?php if ($c['completed_at']): ?>
                            <p><span class="info-label"><i class="fas fa-stop me-2"></i>Completed:</span> <?= date('h:i A', strtotime($c['completed_at'])) ?></p>
                        <?php endif; ?>
                        <p><span class="info-label"><i class="fas fa-comment-medical me-2"></i>Reason:</span> <?= nl2br(htmlspecialchars($c['reason'] ?? 'General consultation')) ?></p>
                    </div>
                </div>

                <!-- Clinical Notes -->
                <?php if ($c['notes'] || $c['prescription']): ?>
                <div class="detail-card">
                    <div class="detail-header">
                        <h5 class="mb-0"><i class="fas fa-clinic-medical me-2"></i> Clinical Notes</h5>
                    </div>
                    <div class="info-section">
                        <?php if ($c['diagnosis']): ?>
                            <p><strong><i class="fas fa-stethoscope me-2" style="color: var(--uhd-gold);"></i>Diagnosis:</strong></p>
                            <p><?= nl2br(htmlspecialchars($c['diagnosis'])) ?></p>
                        <?php endif; ?>
                        
                        <?php if ($c['prescription']): ?>
                            <p><strong><i class="fas fa-prescription-bottle me-2" style="color: var(--uhd-gold);"></i>Prescription:</strong></p>
                            <div class="p-2 bg-light rounded">
                                <?= nl2br(htmlspecialchars($c['prescription'])) ?>
                            </div>
                        <?php endif; ?>
                        
                        <?php if ($c['notes']): ?>
                            <p class="mt-3"><strong><i class="fas fa-notes-medical me-2" style="color: var(--uhd-gold);"></i>Additional Notes:</strong></p>
                            <p><?= nl2br(htmlspecialchars($c['notes'])) ?></p>
                        <?php endif; ?>
                        
                        <?php if ($c['recommendations']): ?>
                            <p><strong><i class="fas fa-chalkboard-user me-2" style="color: var(--uhd-gold);"></i>Recommendations:</strong></p>
                            <p><?= nl2br(htmlspecialchars($c['recommendations'])) ?></p>
                        <?php endif; ?>
                    </div>
                </div>
                <?php endif; ?>

                <!-- Meeting Link -->
                <?php if ($c['status'] == 'ongoing' && $c['meeting_link']): ?>
                    <div class="detail-card">
                        <div class="detail-header">
                            <h5 class="mb-0"><i class="fas fa-video me-2"></i> Active Session</h5>
                        </div>
                        <div class="info-section text-center">
                            <a href="<?= $c['meeting_link'] ?>" target="_blank" class="btn btn-danger btn-lg">
                                <i class="fas fa-video me-2"></i> Join Video Call
                            </a>
                            <button class="btn btn-outline-secondary ms-2" onclick="copyLink('<?= htmlspecialchars($c['meeting_link']) ?>')">
                                <i class="fas fa-copy"></i> Copy Link
                            </button>
                        </div>
                    </div>
                <?php endif; ?>
                
                <!-- Action Buttons -->
                <?php if ($c['status'] == 'scheduled'): ?>
                    <div class="detail-card">
                        <div class="detail-header">
                            <h5 class="mb-0"><i class="fas fa-cog me-2"></i> Actions</h5>
                        </div>
                        <div class="info-section text-center">
                            <form method="POST" action="consultations.php" style="display: inline-block;">
                                <input type="hidden" name="consult_id" value="<?= $c['id'] ?>">
                                <button type="submit" name="start_consultation" class="btn btn-danger">
                                    <i class="fas fa-video me-2"></i> Start Consultation
                                </button>
                            </form>
                            <button class="btn btn-success" onclick="openCompleteModal(<?= $c['id'] ?>)">
                                <i class="fas fa-check me-2"></i> Complete
                            </button>
                        </div>
                    </div>
                <?php endif; ?>
            </div>
        </div>
    </div>
</div>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
<script>
    function copyLink(link) {
        navigator.clipboard.writeText(link);
        alert('Meeting link copied to clipboard!');
    }
    
    function openCompleteModal(consultId) {
        window.location.href = 'consultations.php?complete_id=' + consultId;
    }
</script>
</body>
</html>