<?php
/**
 * Upcoming Follow-ups
 * Display and manage patient follow-up reminders
 */

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

requireLogin();

$pdo = getDB();
$userId = $_SESSION['user_id'];
$days = $_GET['days'] ?? 30;
$status = $_GET['status'] ?? 'upcoming';

// Build query for follow-ups
$where = "c.user_id = ? AND c.follow_up_date IS NOT NULL";
$params = [$userId];

if ($status == 'upcoming') {
    $where .= " AND c.follow_up_date >= CURDATE() AND c.follow_up_date <= DATE_ADD(CURDATE(), INTERVAL ? DAY)";
    $params[] = $days;
} elseif ($status == 'today') {
    $where .= " AND c.follow_up_date = CURDATE()";
} elseif ($status == 'overdue') {
    $where .= " AND c.follow_up_date < CURDATE()";
} elseif ($status == 'all') {
    // No additional filter
}

$sql = "SELECT c.id, c.patient_name, c.patient_mobile, c.follow_up_date, 
        c.prescribed_remedy_id, c.prescription_potency, c.status as case_status,
        r.name as remedy_name,
        DATEDIFF(c.follow_up_date, CURDATE()) as days_remaining
        FROM cases c
        LEFT JOIN remedies r ON c.prescribed_remedy_id = r.id
        WHERE $where
        ORDER BY c.follow_up_date ASC";

$stmt = $pdo->prepare($sql);
$stmt->execute($params);
$reminders = $stmt->fetchAll();

// Get statistics
$stmt = $pdo->prepare("SELECT 
                      COUNT(*) as total,
                      SUM(CASE WHEN follow_up_date = CURDATE() THEN 1 ELSE 0 END) as today,
                      SUM(CASE WHEN follow_up_date BETWEEN CURDATE() AND DATE_ADD(CURDATE(), INTERVAL 7 DAY) THEN 1 ELSE 0 END) as week,
                      SUM(CASE WHEN follow_up_date < CURDATE() THEN 1 ELSE 0 END) as overdue
                      FROM cases 
                      WHERE user_id = ? AND follow_up_date IS NOT NULL");
$stmt->execute([$userId]);
$stats = $stmt->fetch();
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Follow-up Reminders - <?php echo APP_NAME; ?></title>
    <link href="../../assets/vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet">
    <link href="../../assets/vendor/fontawesome/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.datatables.net/1.11.5/css/dataTables.bootstrap5.min.css" rel="stylesheet">
    <style>
        body { background: #f0f2f5; }
        .sidebar {
            min-height: 100vh;
            background: linear-gradient(135deg, #1a472a 0%, #0d2818 100%);
            position: fixed;
            left: 0;
            top: 0;
            width: 260px;
        }
        .sidebar .nav-link {
            color: rgba(255,255,255,0.8);
            padding: 12px 20px;
            margin: 5px 10px;
            border-radius: 8px;
            transition: all 0.3s;
        }
        .sidebar .nav-link:hover { background: rgba(255,255,255,0.1); transform: translateX(5px); }
        .sidebar .nav-link.active { background: #2ecc71; color: white; }
        .sidebar .nav-link i { margin-right: 10px; width: 20px; }
        .main-content { margin-left: 260px; padding: 20px; }
        .top-navbar {
            background: white;
            border-radius: 12px;
            padding: 15px 25px;
            margin-bottom: 25px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.05);
        }
        .stats-card {
            background: white;
            border-radius: 12px;
            padding: 20px;
            margin-bottom: 20px;
            text-align: center;
            box-shadow: 0 2px 10px rgba(0,0,0,0.05);
        }
        .stats-number { font-size: 32px; font-weight: bold; }
        .reminder-card {
            background: white;
            border-radius: 12px;
            padding: 15px;
            margin-bottom: 15px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.05);
            border-left: 4px solid #2ecc71;
            transition: all 0.2s;
        }
        .reminder-card:hover { transform: translateX(5px); }
        .reminder-overdue { border-left-color: #dc3545; background: #fff5f5; }
        .reminder-today { border-left-color: #ffc107; background: #fffbf0; }
        .reminder-week { border-left-color: #17a2b8; }
        @media (max-width: 768px) {
            .sidebar { width: 70px; }
            .sidebar .nav-link span { display: none; }
            .main-content { margin-left: 70px; }
        }
    </style>
</head>
<body>
    <div class="sidebar">
        <div class="text-center py-4"><h4 class="text-white mb-0">🏥</h4></div>
        <nav class="nav flex-column">
            <a class="nav-link" href="../dashboard.php"><i class="fas fa-tachometer-alt"></i> <span>Dashboard</span></a>
            <a class="nav-link" href="../cases/index.php"><i class="fas fa-folder-open"></i> <span>Cases</span></a>
            <a class="nav-link" href="../repertory/index.php"><i class="fas fa-book"></i> <span>Repertory</span></a>
            <a class="nav-link" href="../materia-medica/index.php"><i class="fas fa-capsules"></i> <span>Materia Medica</span></a>
            <a class="nav-link active" href="index.php"><i class="fas fa-bell"></i> <span>Reminders</span></a>
            <a class="nav-link" href="../profile.php"><i class="fas fa-user"></i> <span>Profile</span></a>
            <hr><a class="nav-link" href="../../logout.php"><i class="fas fa-sign-out-alt"></i> <span>Logout</span></a>
        </nav>
    </div>
    
    <div class="main-content">
        <div class="top-navbar">
            <div class="d-flex justify-content-between">
                <h4 class="mb-0"><i class="fas fa-bell"></i> Follow-up Reminders</h4>
                <div>
                    <a href="add.php" class="btn btn-primary btn-sm">
                        <i class="fas fa-plus"></i> Add Reminder
                    </a>
                </div>
            </div>
        </div>
        
        <!-- Statistics Cards -->
        <div class="row">
            <div class="col-md-3">
                <div class="stats-card">
                    <div class="stats-number text-danger"><?php echo $stats['overdue']; ?></div>
                    <div class="text-muted">Overdue</div>
                    <small>Past follow-up date</small>
                </div>
            </div>
            <div class="col-md-3">
                <div class="stats-card">
                    <div class="stats-number text-warning"><?php echo $stats['today']; ?></div>
                    <div class="text-muted">Today</div>
                    <small>Due today</small>
                </div>
            </div>
            <div class="col-md-3">
                <div class="stats-card">
                    <div class="stats-number text-info"><?php echo $stats['week']; ?></div>
                    <div class="text-muted">This Week</div>
                    <small>Next 7 days</small>
                </div>
            </div>
            <div class="col-md-3">
                <div class="stats-card">
                    <div class="stats-number"><?php echo $stats['total']; ?></div>
                    <div class="text-muted">Total Reminders</div>
                    <small>Active follow-ups</small>
                </div>
            </div>
        </div>
        
        <!-- Filter Tabs -->
        <ul class="nav nav-tabs mb-3">
            <li class="nav-item">
                <a class="nav-link <?php echo $status == 'upcoming' ? 'active' : ''; ?>" href="?status=upcoming&days=30">
                    Upcoming (30 days)
                </a>
            </li>
            <li class="nav-item">
                <a class="nav-link <?php echo $status == 'today' ? 'active' : ''; ?>" href="?status=today">
                    Today
                </a>
            </li>
            <li class="nav-item">
                <a class="nav-link <?php echo $status == 'overdue' ? 'active' : ''; ?>" href="?status=overdue">
                    Overdue
                </a>
            </li>
            <li class="nav-item">
                <a class="nav-link <?php echo $status == 'all' ? 'active' : ''; ?>" href="?status=all">
                    All Reminders
                </a>
            </li>
        </ul>
        
        <!-- Days Filter for Upcoming -->
        <?php if($status == 'upcoming'): ?>
        <div class="mb-3">
            <div class="btn-group">
                <a href="?status=upcoming&days=7" class="btn btn-outline-primary btn-sm <?php echo $days == 7 ? 'active' : ''; ?>">7 Days</a>
                <a href="?status=upcoming&days=14" class="btn btn-outline-primary btn-sm <?php echo $days == 14 ? 'active' : ''; ?>">14 Days</a>
                <a href="?status=upcoming&days=30" class="btn btn-outline-primary btn-sm <?php echo $days == 30 ? 'active' : ''; ?>">30 Days</a>
                <a href="?status=upcoming&days=60" class="btn btn-outline-primary btn-sm <?php echo $days == 60 ? 'active' : ''; ?>">60 Days</a>
                <a href="?status=upcoming&days=90" class="btn btn-outline-primary btn-sm <?php echo $days == 90 ? 'active' : ''; ?>">90 Days</a>
            </div>
        </div>
        <?php endif; ?>
        
        <!-- Reminders List -->
        <div class="row">
            <div class="col-md-12">
                <?php if(empty($reminders)): ?>
                    <div class="stats-card text-center text-muted py-5">
                        <i class="fas fa-bell-slash fa-3x mb-3"></i>
                        <p>No follow-up reminders found.</p>
                        <a href="add.php" class="btn btn-primary">Add a Reminder</a>
                    </div>
                <?php else: ?>
                    <?php foreach($reminders as $reminder): 
                        $reminderClass = '';
                        if($reminder['days_remaining'] < 0) $reminderClass = 'reminder-overdue';
                        elseif($reminder['days_remaining'] == 0) $reminderClass = 'reminder-today';
                        elseif($reminder['days_remaining'] <= 7) $reminderClass = 'reminder-week';
                    ?>
                    <div class="reminder-card <?php echo $reminderClass; ?>">
                        <div class="row align-items-center">
                            <div class="col-md-5">
                                <h5 class="mb-1"><?php echo escape($reminder['patient_name']); ?></h5>
                                <div class="text-muted small">
                                    <i class="fas fa-phone"></i> <?php echo $reminder['patient_mobile']; ?>
                                </div>
                            </div>
                            <div class="col-md-3">
                                <div class="mb-1">
                                    <i class="fas fa-calendar-alt"></i> 
                                    <strong>Follow-up:</strong> <?php echo date('d-m-Y', strtotime($reminder['follow_up_date'])); ?>
                                </div>
                                <div>
                                    <?php if($reminder['days_remaining'] < 0): ?>
                                        <span class="badge bg-danger">Overdue by <?php echo abs($reminder['days_remaining']); ?> days</span>
                                    <?php elseif($reminder['days_remaining'] == 0): ?>
                                        <span class="badge bg-warning">Due Today</span>
                                    <?php else: ?>
                                        <span class="badge bg-info"><?php echo $reminder['days_remaining']; ?> days left</span>
                                    <?php endif; ?>
                                </div>
                            </div>
                            <div class="col-md-2">
                                <?php if($reminder['remedy_name']): ?>
                                    <small class="text-muted">Prescribed:</small>
                                    <div><strong><?php echo escape($reminder['remedy_name']); ?></strong> (<?php echo $reminder['prescription_potency']; ?>)</div>
                                <?php endif; ?>
                            </div>
                            <div class="col-md-2 text-end">
                                <div class="btn-group btn-group-sm">
                                    <a href="../cases/view.php?id=<?php echo $reminder['id']; ?>" class="btn btn-info" title="View Case">
                                        <i class="fas fa-eye"></i>
                                    </a>
                                    <a href="send.php?id=<?php echo $reminder['id']; ?>&type=sms" class="btn btn-primary" title="Send SMS">
                                        <i class="fas fa-sms"></i>
                                    </a>
                                    <a href="send.php?id=<?php echo $reminder['id']; ?>&type=email" class="btn btn-success" title="Send Email">
                                        <i class="fas fa-envelope"></i>
                                    </a>
                                    <a href="edit.php?id=<?php echo $reminder['id']; ?>" class="btn btn-warning" title="Edit">
                                        <i class="fas fa-edit"></i>
                                    </a>
                                </div>
                            </div>
                        </div>
                    </div>
                    <?php endforeach; ?>
                <?php endif; ?>
            </div>
        </div>
    </div>
    
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script src="../../assets/vendor/bootstrap/js/bootstrap.bundle.min.js"></script>
</body>
</html>