<?php
// patient_cases.php - Patient View Cases
session_start();
require_once 'config.php';

// Check if patient is logged in
if (!isset($_SESSION['opd_patient'])) {
    header('Location: opd/index.php');
    exit;
}

$patient_id = (int)$_SESSION['opd_patient'];
$patient = $conn->query("SELECT * FROM opd_patients WHERE id = $patient_id")->fetch_assoc();

if (!$patient) {
    session_destroy();
    header('Location: opd/index.php');
    exit;
}

// Get visible cases only
$cases = getPatientCases($patient_id, true);

$case_view = isset($_GET['view']) ? (int)$_GET['view'] : 0;
$view_case = null;
$followups = null;

if ($case_view > 0) {
    $view_case = $conn->query("SELECT c.*, d.name as doctor_name 
                               FROM hom_cases c 
                               LEFT JOIN doctors d ON c.doctor_id = d.id 
                               WHERE c.id = $case_view AND c.patient_id = $patient_id AND c.is_visible_to_patient = 1")->fetch_assoc();
    if ($view_case) {
        $followups = getCaseFollowups($case_view, true);
    }
}

$msg = $_SESSION['msg'] ?? '';
unset($_SESSION['msg']);
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>My Cases | <?=$brand['name']?></title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" rel="stylesheet">
    <style>
        body{background:#f0f2f5;padding:20px;font-family:'Segoe UI',sans-serif}
        .card{border-radius:15px;margin-bottom:20px;border:none}
        .card-header{background:#2d6a4f;color:#fff;border-radius:15px 15px 0 0;padding:15px 20px}
        .case-card{transition:all .3s;cursor:pointer;margin-bottom:5px}
        .case-card:hover{transform:translateX(5px);background:#f8f9fa}
        .case-card.active{background:#e8f5e9;border-left:3px solid #2d6a4f}
        .status-badge{padding:3px 10px;border-radius:15px;font-size:.7rem}
        @media(max-width:768px){body{padding:10px}}
    </style>
</head>
<body>
<div class="container-fluid">
    <div class="d-flex justify-content-between align-items-center mb-4 flex-wrap">
        <h3><i class="fas fa-folder-medical"></i> My Medical Cases</h3>
        <a href="opd/dashboard.php" class="btn btn-secondary mt-2 mt-sm-0"><i class="fas fa-arrow-left"></i> Back to Dashboard</a>
    </div>
    
    <?php if($msg): ?>
        <div class="alert alert-success alert-dismissible fade show"><?=htmlspecialchars($msg)?><button class="btn-close" data-bs-dismiss="alert"></button></div>
    <?php endif; ?>
    
    <div class="row">
        <!-- Cases List -->
        <div class="col-md-4">
            <div class="card">
                <div class="card-header">
                    <i class="fas fa-list"></i> My Cases (<?=$cases->num_rows?>)
                </div>
                <div class="card-body p-0">
                    <div class="list-group list-group-flush">
                        <?php if($cases && $cases->num_rows > 0): ?>
                            <?php while($case = $cases->fetch_assoc()): ?>
                            <a href="patient_cases.php?view=<?=$case['id']?>" 
                               class="list-group-item list-group-item-action case-card <?=($case_view == $case['id']) ? 'active' : ''?>">
                                <div class="d-flex justify-content-between align-items-center">
                                    <strong><?=htmlspecialchars($case['case_number'])?></strong>
                                    <span class="status-badge bg-<?=$case['status']=='active'?'warning':($case['status']=='followup'?'info':'secondary')?>">
                                        <?=ucfirst($case['status'])?>
                                    </span>
                                </div>
                                <div class="small">Dr. <?=htmlspecialchars($case['doctor_name'])?></div>
                                <div class="small text-muted"><i class="far fa-calendar"></i> <?=date('d M Y', strtotime($case['created_at']))?></div>
                            </a>
                            <?php endwhile; ?>
                        <?php else: ?>
                            <div class="text-center py-4 text-muted">No cases yet.<br>Your doctor will create cases for you.</div>
                        <?php endif; ?>
                    </div>
                </div>
            </div>
        </div>
        
        <!-- Case Details -->
        <div class="col-md-8">
            <?php if($view_case): ?>
                <div class="card">
                    <div class="card-header">
                        <i class="fas fa-file-medical"></i> Case Details
                    </div>
                    <div class="card-body">
                        <h5><?=htmlspecialchars($view_case['title'])?></h5>
                        <p><strong>Doctor:</strong> Dr. <?=htmlspecialchars($view_case['doctor_name'])?></p>
                        <p><strong>Status:</strong> <span class="badge bg-<?=$view_case['status']=='active'?'warning':'info'?>"><?=ucfirst($view_case['status'])?></span></p>
                        <p><strong>Case Number:</strong> <?=htmlspecialchars($view_case['case_number'])?></p>
                        <p><strong>Created:</strong> <?=date('d M Y, h:i A', strtotime($view_case['created_at']))?></p>
                        
                        <hr>
                        <h6><i class="fas fa-stethoscope"></i> Chief Complaint</h6>
                        <p><?=nl2br(htmlspecialchars($view_case['chief_complaint']))?></p>
                        
                        <h6><i class="fas fa-prescription"></i> Prescribed Medicine</h6>
                        <p><strong>Medicine:</strong> <?=htmlspecialchars($view_case['prescribed_medicine'])?><br>
                        <strong>Potency:</strong> <?=htmlspecialchars($view_case['potency'])?><br>
                        <strong>Dosage:</strong> <?=htmlspecialchars($view_case['dosage'])?></p>
                        
                        <?php if($view_case['history_present']): ?>
                            <h6><i class="fas fa-history"></i> History</h6>
                            <p><?=nl2br(htmlspecialchars($view_case['history_present']))?></p>
                        <?php endif; ?>
                        
                        <?php if($view_case['modalities']): ?>
                            <h6><i class="fas fa-sun"></i> Modalities</h6>
                            <p><?=nl2br(htmlspecialchars($view_case['modalities']))?></p>
                        <?php endif; ?>
                        
                        <?php if($followups && $followups->num_rows > 0): ?>
                        <hr>
                        <h6><i class="fas fa-history"></i> Follow-up History</h6>
                        <?php while($f = $followups->fetch_assoc()): ?>
                        <div class="border p-3 mb-2 rounded bg-light">
                            <div class="d-flex justify-content-between">
                                <strong>Follow-up #<?=$f['followup_number']?></strong>
                                <small><?=date('d M Y', strtotime($f['followup_date']))?></small>
                            </div>
                            <p class="mt-2 mb-1"><?=nl2br(htmlspecialchars($f['complaint_status']))?></p>
                            <?php if($f['new_prescription']): ?>
                                <small><strong>New Prescription:</strong> <?=htmlspecialchars($f['new_prescription'])?> (<?=$f['new_potency']?>, <?=$f['new_dosage']?>)</small><br>
                            <?php endif; ?>
                            <?php if($f['advice']): ?>
                                <small><strong>Advice:</strong> <?=nl2br(htmlspecialchars($f['advice']))?></small>
                            <?php endif; ?>
                            <?php if($f['next_followup_date']): ?>
                                <small><strong>Next Follow-up:</strong> <?=date('d M Y', strtotime($f['next_followup_date']))?></small>
                            <?php endif; ?>
                        </div>
                        <?php endwhile; ?>
                        <?php endif; ?>
                        
                        <hr>
                        <div class="text-muted small">
                            <i class="fas fa-lock"></i> This information is shared by your doctor for treatment purposes.
                        </div>
                    </div>
                </div>
            <?php else: ?>
                <div class="card">
                    <div class="card-body text-center py-5">
                        <i class="fas fa-folder-open fa-4x text-muted mb-3"></i>
                        <h5>Select a case to view details</h5>
                        <p>Your medical cases will appear here</p>
                    </div>
                </div>
            <?php endif; ?>
        </div>
    </div>
</div>

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