<?php
/**
 * Print Case Report
 * Print-friendly version of case details
 */

require_once '../../config/config.php';
require_once '../../includes/functions.php';

requireLogin();

$pdo = getDB();
$caseId = (int)($_GET['id'] ?? 0);
$userId = $_SESSION['user_id'];

$stmt = $pdo->prepare("SELECT c.*, r.name as remedy_name 
                      FROM cases c
                      LEFT JOIN remedies r ON c.prescribed_remedy_id = r.id
                      WHERE c.id = ? AND c.user_id = ?");
$stmt->execute([$caseId, $userId]);
$case = $stmt->fetch();

if (!$case) { die("Case not found"); }

$selectedRubrics = json_decode($case['selected_rubrics'], true) ?: [];
$rubricNames = [];
if (!empty($selectedRubrics)) {
    $placeholders = implode(',', array_fill(0, count($selectedRubrics), '?'));
    $stmt = $pdo->prepare("SELECT full_path FROM rubrics WHERE id IN ($placeholders)");
    $stmt->execute($selectedRubrics);
    $rubricNames = $stmt->fetchAll();
}

$followUps = $pdo->prepare("SELECT * FROM follow_ups WHERE case_id = ? ORDER BY follow_up_date DESC");
$followUps->execute([$caseId]);
$followUps = $followUps->fetchAll();
?>
<!DOCTYPE html>
<html>
<head><title>Case Report - <?php echo escape($case['patient_name']); ?></title>
<style>
body { font-family: Arial, sans-serif; margin: 20px; }
.container { max-width: 1000px; margin: 0 auto; }
.header { text-align: center; margin-bottom: 30px; border-bottom: 2px solid #333; padding-bottom: 10px; }
.section { margin-bottom: 20px; }
.section-title { background: #f0f0f0; padding: 8px; font-weight: bold; border-left: 4px solid #2ecc71; margin-bottom: 10px; }
table { width: 100%; border-collapse: collapse; }
td, th { padding: 8px; border: 1px solid #ddd; vertical-align: top; }
th { background: #f5f5f5; width: 30%; }
.footer { margin-top: 30px; text-align: center; font-size: 12px; color: #666; border-top: 1px solid #ddd; padding-top: 10px; }
@media print { body { margin: 0; } .no-print { display: none; } }
</style>
</head>
<body>
<div class="container">
    <div class="header"><h2>Homoeopathy Case Record</h2><p>Kent's Repertory System</p></div>
    
    <div class="section"><div class="section-title">Patient Information</div>
        <table><tr><th>Patient Name:</th><td><?php echo escape($case['patient_name']); ?></td>
        <th>Date:</th><td><?php echo date('d-m-Y', strtotime($case['created_at'])); ?></td></tr>
        <tr><th>Mobile:</th><td><?php echo $case['patient_mobile']; ?></td>
        <th>Age/Gender:</th><td><?php echo $case['patient_age'] . ' / ' . $case['patient_gender']; ?></td></tr>
        <tr><th>Case Status:</th><td colspan="3"><?php echo ucfirst($case['status']); ?></td></tr>
    </table></div>
    
    <div class="section"><div class="section-title">Case History</div>
        <table><tr><th>Chief Complaint:</th><td><?php echo nl2br(escape($case['chief_complaint'])); ?></td></tr>
        <tr><th>Case History:</th><td><?php echo nl2br(escape($case['case_history'])); ?></td></tr>
        <tr><th>Physical Generals:</th><td><?php echo nl2br(escape($case['physical_generals'])); ?></td></tr>
        <tr><th>Mental/Emotional:</th><td><?php echo nl2br(escape($case['mental_emotional'])); ?></td></tr>
        <tr><th>Modalities:</th><td><?php echo nl2br(escape($case['modalities'])); ?></td></tr>
    </table></div>
    
    <?php if($case['prescribed_remedy_id']): ?>
    <div class="section"><div class="section-title">Prescription</div>
        <table><th>Remedy:</th><td><strong><?php echo $case['remedy_name']; ?></strong> (\(<?php echo $case['prescription_potency']; ?>\))</td>
        <th>Follow-up Date:</th><td><?php echo $case['follow_up_date'] ? date('d-m-Y', strtotime($case['follow_up_date'])) : 'Not scheduled'; ?></td></table>
    </div>
    <?php endif; ?>
    
    <div class="section"><div class="section-title">Selected Rubrics</div>
        <?php if(empty($rubricNames)): ?><p>No rubrics selected</p><?php else: ?><ul><?php foreach($rubricNames as $r): ?><li><?php echo escape($r['full_path']); ?></li><?php endforeach; ?></ul><?php endif; ?>
    </div>
    
    <?php if(!empty($followUps)): ?>
    <div class="section"><div class="section-title">Follow-up History</div>
        <?php foreach($followUps as $fu): ?>
        <div style="margin-bottom: 15px; border-left: 3px solid #2ecc71; padding-left: 10px;">
            <strong>Date: <?php echo date('d-m-Y', strtotime($fu['follow_up_date'])); ?></strong>
            <p><strong>Progress:</strong> <?php echo nl2br(escape($fu['progress'])); ?></p>
            <?php if($fu['new_symptoms']): ?><p><strong>New Symptoms:</strong> <?php echo escape($fu['new_symptoms']); ?></p><?php endif; ?>
            <?php if($fu['remedy_repeated']): ?><p><em>Remedy was repeated</em></p><?php endif; ?>
        </div>
        <?php endforeach; ?>
    </div>
    <?php endif; ?>
    
    <div class="footer">Generated on <?php echo date('d-m-Y H:i:s'); ?> | Dr. <?php echo escape($_SESSION['full_name']); ?></div>
</div>
<div class="no-print" style="text-align: center; margin-top: 20px;"><button onclick="window.print()" class="btn btn-primary">Print</button> <button onclick="window.close()" class="btn btn-secondary">Close</button></div>
</body>
</html>