<?php
// doctor_cases.php - Doctor Case Management
session_start();
require_once 'config.php';

// Check if doctor is logged in
if (!isset($_SESSION['doctor_id'])) {
    header('Location: doctor_login.php');
    exit;
}

$doctor_id = (int)$_SESSION['doctor_id'];
$doctor = $conn->query("SELECT * FROM doctors WHERE id = $doctor_id")->fetch_assoc();

if (!$doctor) {
    session_destroy();
    header('Location: doctor_login.php');
    exit;
}

// ============================================
// HANDLE ACTIONS
// ============================================

// Create new case
if (isset($_POST['create_case'])) {
    $patient_id = (int)$_POST['patient_id'];
    $title = $conn->real_escape_string($_POST['title']);
    $chief_complaint = $conn->real_escape_string($_POST['chief_complaint']);
    $history_present = $conn->real_escape_string($_POST['history_present']);
    $past_history = $conn->real_escape_string($_POST['past_history']);
    $family_history = $conn->real_escape_string($_POST['family_history']);
    $mental_state = $conn->real_escape_string($_POST['mental_state']);
    $physical_generals = $conn->real_escape_string($_POST['physical_generals']);
    $particulars = $conn->real_escape_string($_POST['particulars']);
    $modalities = $conn->real_escape_string($_POST['modalities']);
    $concomitants = $conn->real_escape_string($_POST['concomitants']);
    $prescribed_medicine = $conn->real_escape_string($_POST['prescribed_medicine']);
    $potency = $conn->real_escape_string($_POST['potency']);
    $dosage = $conn->real_escape_string($_POST['dosage']);
    $is_visible = isset($_POST['is_visible_to_patient']) ? 1 : 0;
    
    $case_number = generateCaseNumber();
    
    $sql = "INSERT INTO hom_cases (case_number, patient_id, doctor_id, title, chief_complaint, 
            history_present, past_history, family_history, mental_state, physical_generals, 
            particulars, modalities, concomitants, prescribed_medicine, potency, dosage, 
            is_visible_to_patient, created_at) 
            VALUES ('$case_number', $patient_id, $doctor_id, '$title', '$chief_complaint', 
            '$history_present', '$past_history', '$family_history', '$mental_state', 
            '$physical_generals', '$particulars', '$modalities', '$concomitants', 
            '$prescribed_medicine', '$potency', '$dosage', $is_visible, NOW())";
    
    if ($conn->query($sql)) {
        $_SESSION['msg'] = "✅ Case created successfully! Case #: $case_number";
    } else {
        $_SESSION['msg'] = "❌ Error: " . $conn->error;
    }
    header('Location: doctor_cases.php');
    exit;
}

// Add follow-up
if (isset($_POST['add_followup'])) {
    $case_id = (int)$_POST['case_id'];
    $followup_date = $_POST['followup_date'];
    $complaint_status = $conn->real_escape_string($_POST['complaint_status']);
    $response_to_medicine = $_POST['response_to_medicine'];
    $new_prescription = $conn->real_escape_string($_POST['new_prescription']);
    $new_potency = $conn->real_escape_string($_POST['new_potency']);
    $new_dosage = $conn->real_escape_string($_POST['new_dosage']);
    $advice = $conn->real_escape_string($_POST['advice']);
    $next_followup = $_POST['next_followup_date'];
    $is_visible = isset($_POST['is_visible_to_patient']) ? 1 : 0;
    
    // Get current followup count
    $count_result = $conn->query("SELECT COUNT(*) as total FROM hom_followups WHERE case_id = $case_id");
    $followup_num = $count_result->fetch_assoc()['total'] + 1;
    
    $sql = "INSERT INTO hom_followups (case_id, followup_date, followup_number, complaint_status, 
            response_to_medicine, new_prescription, new_potency, new_dosage, advice, 
            next_followup_date, is_visible_to_patient, created_at) 
            VALUES ($case_id, '$followup_date', $followup_num, '$complaint_status', 
            '$response_to_medicine', '$new_prescription', '$new_potency', '$new_dosage', 
            '$advice', '$next_followup', $is_visible, NOW())";
    
    if ($conn->query($sql)) {
        // Update case status
        $conn->query("UPDATE hom_cases SET status = 'followup', updated_at = NOW() WHERE id = $case_id");
        $_SESSION['msg'] = "✅ Follow-up #$followup_num added!";
    } else {
        $_SESSION['msg'] = "❌ Error: " . $conn->error;
    }
    header('Location: doctor_cases.php?view=' . $case_id);
    exit;
}

// Update case status
if (isset($_POST['update_status'])) {
    $case_id = (int)$_POST['case_id'];
    $status = $_POST['status'];
    $conn->query("UPDATE hom_cases SET status = '$status' WHERE id = $case_id AND doctor_id = $doctor_id");
    $_SESSION['msg'] = "✅ Case status updated!";
    header('Location: doctor_cases.php?view=' . $case_id);
    exit;
}

// Toggle visibility
if (isset($_POST['toggle_visibility'])) {
    $case_id = (int)$_POST['case_id'];
    $is_visible = (int)$_POST['is_visible'];
    $conn->query("UPDATE hom_cases SET is_visible_to_patient = $is_visible WHERE id = $case_id AND doctor_id = $doctor_id");
    $_SESSION['msg'] = "✅ Visibility updated!";
    header('Location: doctor_cases.php?view=' . $case_id);
    exit;
}

// AI Analysis
if (isset($_GET['ai_analyze'])) {
    $case_id = (int)$_GET['ai_analyze'];
    $result = analyzeCaseWithAI($case_id);
    if (isset($result['success'])) {
        $_SESSION['msg'] = "🤖 AI Analysis complete!";
    } else {
        $_SESSION['msg'] = "⚠️ AI Analysis failed: " . ($result['error'] ?? 'Unknown error');
    }
    header('Location: doctor_cases.php?view=' . $case_id);
    exit;
}

// ============================================
// GET DATA
// ============================================

// Get all cases for this doctor
$status_filter = $_GET['status'] ?? 'all';
$cases = getDoctorCases($doctor_id, $status_filter);

// Get patients for dropdown
$patients = $conn->query("SELECT id, name, mobile FROM opd_patients ORDER BY name");

// View specific case
$case_view = isset($_GET['view']) ? (int)$_GET['view'] : 0;
$view_case = null;
$followups = null;
$ai_analysis = null;

if ($case_view > 0) {
    $view_case = $conn->query("SELECT c.*, p.name as patient_name, p.mobile, p.age, p.gender, p.address 
                               FROM hom_cases c 
                               LEFT JOIN opd_patients p ON c.patient_id = p.id 
                               WHERE c.id = $case_view AND c.doctor_id = $doctor_id")->fetch_assoc();
    if ($view_case) {
        $followups = getCaseFollowups($case_view, false);
        $ai_analysis = $conn->query("SELECT * FROM hom_ai_analysis WHERE case_id = $case_view ORDER BY id DESC LIMIT 1")->fetch_assoc();
    }
}

$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>Case Management | Dr. <?=htmlspecialchars($doctor['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}
        .btn-success{background:#2d6a4f;border:none}
        .btn-success:hover{background:#1b4332}
        .case-card{transition:all .3s;border-left:4px solid #2d6a4f;cursor:pointer;margin-bottom:5px}
        .case-card:hover{transform:translateX(5px);background:#f8f9fa}
        .case-card.active{background:#e8f5e9;border-left-color:#1b4332}
        .status-badge{padding:4px 12px;border-radius:20px;font-size:.7rem;font-weight:600}
        .followup-timeline{position:relative;padding-left:30px}
        .followup-item{position:relative;margin-bottom:20px;padding:15px;background:#f8f9fa;border-radius:10px}
        .followup-item::before{content:'';position:absolute;left:-20px;top:20px;width:12px;height:12px;border-radius:50%;background:#2d6a4f}
        .followup-item::after{content:'';position:absolute;left:-14px;top:32px;width:2px;height:calc(100% - 10px);background:#dee2e6}
        .followup-item:last-child::after{display:none}
        .ai-response{background:#e8f5e9;padding:20px;border-radius:10px;border-left:4px solid #2d6a4f;white-space:pre-wrap}
        @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> Homeopathic Case Management</h3>
        <div class="mt-2 mt-sm-0">
            <button class="btn btn-success" onclick="showCreateCase()"><i class="fas fa-plus"></i> New Case</button>
            <a href="doctor.php" class="btn btn-secondary"><i class="fas fa-arrow-left"></i> Back</a>
        </div>
    </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
                    <div class="float-end">
                        <select class="form-select form-select-sm" onchange="location.href='doctor_cases.php?status='+this.value" style="width:120px">
                            <option value="all" <?=$status_filter=='all'?'selected':''?>>All Cases</option>
                            <option value="active" <?=$status_filter=='active'?'selected':''?>>Active</option>
                            <option value="followup" <?=$status_filter=='followup'?'selected':''?>>Follow-up</option>
                            <option value="cured" <?=$status_filter=='cured'?'selected':''?>>Cured</option>
                            <option value="referred" <?=$status_filter=='referred'?'selected':''?>>Referred</option>
                            <option value="closed" <?=$status_filter=='closed'?'selected':''?>>Closed</option>
                        </select>
                    </div>
                </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="doctor_cases.php?view=<?=$case['id']?>&status=<?=$status_filter?>" 
                               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 fw-bold"><?=htmlspecialchars($case['patient_name'])?></div>
                                <div class="small text-muted"><?=htmlspecialchars($case['title'])?></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>Click "New Case" to start.</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">
                        <div class="d-flex justify-content-between align-items-center flex-wrap">
                            <span><i class="fas fa-file-medical"></i> Case #<?=htmlspecialchars($view_case['case_number'])?></span>
                            <div class="mt-2 mt-sm-0">
                                <button class="btn btn-sm btn-info" onclick="showAddFollowup(<?=$view_case['id']?>)"><i class="fas fa-plus"></i> Follow-up</button>
                                <button class="btn btn-sm btn-primary" onclick="location.href='doctor_cases.php?ai_analyze=<?=$view_case['id']?>&view=<?=$view_case['id']?>'"><i class="fas fa-brain"></i> AI Analysis</button>
                                <form method="POST" class="d-inline">
                                    <input type="hidden" name="case_id" value="<?=$view_case['id']?>">
                                    <input type="hidden" name="is_visible" value="<?=$view_case['is_visible_to_patient'] ? 0 : 1?>">
                                    <button type="submit" name="toggle_visibility" class="btn btn-sm btn-secondary">
                                        <i class="fas fa-<?=$view_case['is_visible_to_patient'] ? 'eye-slash' : 'eye'?>"></i>
                                        <?=$view_case['is_visible_to_patient'] ? 'Hide' : 'Show'?>
                                    </button>
                                </form>
                                <form method="POST" class="d-inline">
                                    <input type="hidden" name="case_id" value="<?=$view_case['id']?>">
                                    <select name="status" class="form-select form-select-sm d-inline-block w-auto" onchange="this.form.submit()">
                                        <option value="active" <?=$view_case['status']=='active'?'selected':''?>>Active</option>
                                        <option value="followup" <?=$view_case['status']=='followup'?'selected':''?>>Follow-up</option>
                                        <option value="cured" <?=$view_case['status']=='cured'?'selected':''?>>Cured</option>
                                        <option value="referred" <?=$view_case['status']=='referred'?'selected':''?>>Referred</option>
                                        <option value="closed" <?=$view_case['status']=='closed'?'selected':''?>>Closed</option>
                                    </select>
                                    <button type="submit" name="update_status" class="btn btn-sm btn-warning">Update</button>
                                </form>
                            </div>
                        </div>
                    </div>
                    <div class="card-body">
                        <!-- Patient Info -->
                        <div class="row mb-3">
                            <div class="col-md-6">
                                <div class="p-2 bg-light rounded">
                                    <strong><i class="fas fa-user"></i> Patient:</strong> <?=htmlspecialchars($view_case['patient_name'])?><br>
                                    <strong><i class="fas fa-phone"></i> Mobile:</strong> <?=$view_case['mobile']?><br>
                                    <strong><i class="fas fa-calendar"></i> Age/Gender:</strong> <?=$view_case['age']?> / <?=$view_case['gender']?>
                                </div>
                            </div>
                            <div class="col-md-6">
                                <div class="p-2 bg-light rounded">
                                    <strong><i class="fas fa-map-marker-alt"></i> Address:</strong> <?=htmlspecialchars($view_case['address'])?><br>
                                    <strong><i class="fas fa-eye"></i> Visibility:</strong> <?=$view_case['is_visible_to_patient'] ? 'Visible to patient' : 'Hidden from patient'?>
                                </div>
                            </div>
                        </div>
                        
                        <!-- Chief Complaint -->
                        <h6><i class="fas fa-stethoscope"></i> Chief Complaint</h6>
                        <p class="border-bottom pb-2"><?=nl2br(htmlspecialchars($view_case['chief_complaint']))?></p>
                        
                        <div class="row">
                            <div class="col-md-6">
                                <h6><i class="fas fa-history"></i> History</h6>
                                <p class="small"><?=nl2br(htmlspecialchars($view_case['history_present']))?></p>
                            </div>
                            <div class="col-md-6">
                                <h6><i class="fas fa-brain"></i> Mental State</h6>
                                <p class="small"><?=nl2br(htmlspecialchars($view_case['mental_state']))?></p>
                            </div>
                        </div>
                        
                        <div class="row">
                            <div class="col-md-6">
                                <h6><i class="fas fa-thermometer"></i> Physical Generals</h6>
                                <p class="small"><?=nl2br(htmlspecialchars($view_case['physical_generals']))?></p>
                            </div>
                            <div class="col-md-6">
                                <h6><i class="fas fa-sun"></i> Modalities</h6>
                                <p class="small"><?=nl2br(htmlspecialchars($view_case['modalities']))?></p>
                            </div>
                        </div>
                        
                        <!-- Prescription -->
                        <hr>
                        <h6><i class="fas fa-prescription"></i> Prescription</h6>
                        <div class="row">
                            <div class="col-md-4"><strong>Medicine:</strong> <?=htmlspecialchars($view_case['prescribed_medicine'])?></div>
                            <div class="col-md-4"><strong>Potency:</strong> <?=htmlspecialchars($view_case['potency'])?></div>
                            <div class="col-md-4"><strong>Dosage:</strong> <?=htmlspecialchars($view_case['dosage'])?></div>
                        </div>
                        
                        <!-- AI Analysis -->
                        <?php if($ai_analysis): ?>
                        <hr>
                        <div class="ai-response">
                            <h6><i class="fas fa-brain"></i> AI Analysis</h6>
                            <?=nl2br(htmlspecialchars($ai_analysis['ai_response']))?>
                        </div>
                        <?php endif; ?>
                        
                        <!-- Follow-ups -->
                        <?php if($followups && $followups->num_rows > 0): ?>
                        <hr>
                        <h6><i class="fas fa-history"></i> Follow-up History</h6>
                        <div class="followup-timeline">
                            <?php while($f = $followups->fetch_assoc()): ?>
                            <div class="followup-item">
                                <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="mb-1"><?=nl2br(htmlspecialchars($f['complaint_status']))?></p>
                                <div class="small">
                                    <strong>Response:</strong> <span class="badge bg-<?=$f['response_to_medicine']=='great_improvement'?'success':'warning'?>"><?=str_replace('_',' ',ucfirst($f['response_to_medicine']))?></span>
                                    <?php if($f['new_prescription']): ?>
                                        <br><strong>New Rx:</strong> <?=htmlspecialchars($f['new_prescription'])?> (<?=$f['new_potency']?>, <?=$f['new_dosage']?>)
                                    <?php endif; ?>
                                    <?php if($f['advice']): ?>
                                        <br><strong>Advice:</strong> <?=nl2br(htmlspecialchars($f['advice']))?>
                                    <?php endif; ?>
                                    <?php if($f['next_followup_date']): ?>
                                        <br><strong>Next:</strong> <?=date('d M Y', strtotime($f['next_followup_date']))?>
                                    <?php endif; ?>
                                </div>
                            </div>
                            <?php endwhile; ?>
                        </div>
                        <?php endif; ?>
                    </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 from the left</h5>
                        <p>Or click "New Case" to create your first homeopathic case</p>
                    </div>
                </div>
            <?php endif; ?>
        </div>
    </div>
</div>

<!-- Create Case Modal -->
<div class="modal fade" id="createCaseModal" tabindex="-1">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header bg-success text-white">
                <h5 class="modal-title"><i class="fas fa-plus-circle"></i> New Homeopathic Case</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <form method="POST">
                <div class="modal-body">
                    <div class="row">
                        <div class="col-md-6">
                            <div class="mb-2"><label>Patient *</label>
                                <select name="patient_id" class="form-select" required>
                                    <option value="">-- Select Patient --</option>
                                    <?php $patients->data_seek(0); while($p = $patients->fetch_assoc()): ?>
                                    <option value="<?=$p['id']?>"><?=htmlspecialchars($p['name'])?> (<?=$p['mobile']?>)</option>
                                    <?php endwhile; ?>
                                </select>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="mb-2"><label>Case Title *</label>
                                <input type="text" name="title" class="form-control" placeholder="e.g., Chronic Migraine" required>
                            </div>
                        </div>
                    </div>
                    
                    <div class="mb-2"><label>Chief Complaint *</label>
                        <textarea name="chief_complaint" class="form-control" rows="3" placeholder="Main symptoms..." required></textarea>
                    </div>
                    
                    <div class="mb-2"><label>History of Presenting Illness</label>
                        <textarea name="history_present" class="form-control" rows="2"></textarea>
                    </div>
                    
                    <div class="row">
                        <div class="col-md-6"><div class="mb-2"><label>Past History</label><textarea name="past_history" class="form-control" rows="2"></textarea></div></div>
                        <div class="col-md-6"><div class="mb-2"><label>Family History</label><textarea name="family_history" class="form-control" rows="2"></textarea></div></div>
                    </div>
                    
                    <div class="row">
                        <div class="col-md-6"><div class="mb-2"><label>Mental State</label><textarea name="mental_state" class="form-control" rows="2"></textarea></div></div>
                        <div class="col-md-6"><div class="mb-2"><label>Physical Generals</label><textarea name="physical_generals" class="form-control" rows="2"></textarea></div></div>
                    </div>
                    
                    <div class="row">
                        <div class="col-md-6"><div class="mb-2"><label>Particulars</label><textarea name="particulars" class="form-control" rows="2"></textarea></div></div>
                        <div class="col-md-6"><div class="mb-2"><label>Modalities (Better/Worse)</label><textarea name="modalities" class="form-control" rows="2" placeholder="Better: ... | Worse: ..."></textarea></div></div>
                    </div>
                    
                    <div class="mb-2"><label>Concomitants</label>
                        <textarea name="concomitants" class="form-control" rows="1"></textarea>
                    </div>
                    
                    <hr>
                    <h6>Initial Prescription</h6>
                    <div class="row">
                        <div class="col-md-6"><div class="mb-2"><label>Medicine</label><input type="text" name="prescribed_medicine" class="form-control" placeholder="Belladonna"></div></div>
                        <div class="col-md-3"><div class="mb-2"><label>Potency</label><input type="text" name="potency" class="form-control" placeholder="200C"></div></div>
                        <div class="col-md-3"><div class="mb-2"><label>Dosage</label><input type="text" name="dosage" class="form-control" placeholder="3 pellets"></div></div>
                    </div>
                    
                    <div class="form-check mb-2">
                        <input type="checkbox" name="is_visible_to_patient" class="form-check-input" value="1" checked>
                        <label class="form-check-label">Visible to patient</label>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
                    <button type="submit" name="create_case" class="btn btn-success">Create Case</button>
                </div>
            </form>
        </div>
    </div>
</div>

<!-- Add Follow-up Modal -->
<div class="modal fade" id="followupModal" tabindex="-1">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header bg-info text-white">
                <h5 class="modal-title"><i class="fas fa-stethoscope"></i> Add Follow-up</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <form method="POST">
                <input type="hidden" name="case_id" id="followup_case_id">
                <div class="modal-body">
                    <div class="row">
                        <div class="col-md-6"><div class="mb-2"><label>Follow-up Date</label><input type="date" name="followup_date" class="form-control" value="<?=date('Y-m-d')?>" required></div></div>
                        <div class="col-md-6"><div class="mb-2"><label>Response</label>
                            <select name="response_to_medicine" class="form-select">
                                <option value="worse">Worse</option>
                                <option value="no_change">No Change</option>
                                <option value="slight_improvement">Slight Improvement</option>
                                <option value="moderate_improvement">Moderate Improvement</option>
                                <option value="great_improvement">Great Improvement</option>
                                <option value="cured">Cured</option>
                            </select>
                        </div></div>
                    </div>
                    
                    <div class="mb-2"><label>Complaint Status / Current Symptoms *</label>
                        <textarea name="complaint_status" class="form-control" rows="3" required></textarea>
                    </div>
                    
                    <hr>
                    <h6>New Prescription (if changed)</h6>
                    <div class="row">
                        <div class="col-md-6"><div class="mb-2"><label>Medicine</label><input type="text" name="new_prescription" class="form-control" placeholder="New medicine"></div></div>
                        <div class="col-md-3"><div class="mb-2"><label>Potency</label><input type="text" name="new_potency" class="form-control" placeholder="Potency"></div></div>
                        <div class="col-md-3"><div class="mb-2"><label>Dosage</label><input type="text" name="new_dosage" class="form-control" placeholder="Dosage"></div></div>
                    </div>
                    
                    <div class="mb-2"><label>Advice / Instructions</label>
                        <textarea name="advice" class="form-control" rows="2"></textarea>
                    </div>
                    
                    <div class="row">
                        <div class="col-md-6"><div class="mb-2"><label>Next Follow-up Date</label><input type="date" name="next_followup_date" class="form-control"></div></div>
                        <div class="col-md-6"><div class="form-check mt-4"><input type="checkbox" name="is_visible_to_patient" class="form-check-input" value="1" checked><label>Visible to patient</label></div></div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
                    <button type="submit" name="add_followup" class="btn btn-primary">Add Follow-up</button>
                </div>
            </form>
        </div>
    </div>
</div>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
<script>
function showCreateCase() {
    new bootstrap.Modal(document.getElementById('createCaseModal')).show();
}

function showAddFollowup(caseId) {
    document.getElementById('followup_case_id').value = caseId;
    new bootstrap.Modal(document.getElementById('followupModal')).show();
}
</script>
</body>
</html>