<?php
/**
 * Add Follow-up
 * Add follow-up progress to case
 */

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 * FROM cases WHERE id = ? AND user_id = ?");
$stmt->execute([$caseId, $userId]);
$case = $stmt->fetch();

if (!$case) { header('Location: list.php?error=Case not found'); exit; }

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $stmt = $pdo->prepare("INSERT INTO follow_ups (case_id, follow_up_date, progress, new_symptoms, remedy_repeated) VALUES (?, ?, ?, ?, ?)");
    $stmt->execute([$caseId, $_POST['follow_up_date'], $_POST['progress'], $_POST['new_symptoms'], isset($_POST['remedy_repeated']) ? 1 : 0]);
    
    $newStatus = $_POST['case_status'];
    $stmt = $pdo->prepare("UPDATE cases SET status = ? WHERE id = ?");
    $stmt->execute([$newStatus, $caseId]);
    
    header("Location: view.php?id=$caseId&msg=Follow-up added successfully");
    exit;
}
?>
<!DOCTYPE html>
<html>
<head><title>Add Follow-up</title><link href="../../assets/vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet"></head>
<body>
<div class="container mt-5"><div class="row"><div class="col-md-8 mx-auto"><div class="card"><div class="card-header bg-warning"><h4>Follow-up for <?php echo escape($case['patient_name']); ?></h4></div>
<div class="card-body">
<form method="POST"><div class="mb-3"><label>Follow-up Date</label><input type="date" name="follow_up_date" class="form-control" value="<?php echo date('Y-m-d'); ?>" required></div>
<div class="mb-3"><label>Progress / Improvement</label><textarea name="progress" class="form-control" rows="4" required placeholder="Describe changes, improvement percentage..."></textarea></div>
<div class="mb-3"><label>New Symptoms (if any)</label><textarea name="new_symptoms" class="form-control" rows="3" placeholder="Any new symptoms..."></textarea></div>
<div class="mb-3"><div class="form-check"><input type="checkbox" name="remedy_repeated" class="form-check-input" value="1"><label class="form-check-label">Remedy Repeated</label></div></div>
<div class="mb-3"><label>Case Status</label><select name="case_status" class="form-control" required><option value="active">Active - Continue treatment</option><option value="follow-up">Needs Follow-up</option><option value="closed">Closed - Resolved</option></select></div>
<button type="submit" class="btn btn-primary">Save Follow-up</button><a href="view.php?id=<?php echo $caseId; ?>" class="btn btn-secondary">Cancel</a></form></div></div></div></div></div>
</body>
</html>