<?php
session_start();
if (!isset($_SESSION['admin_id'])) { header("Location: login.php"); exit(); }
include '../include/config.php';

$msg = '';
$error = '';

// Get users for dropdown
$patients = $conn->query("SELECT patient_id, first_name, last_name FROM patients ORDER BY first_name");
$doctors = $conn->query("SELECT doctor_id, name FROM doctors ORDER BY name");

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $recipient_type = $_POST['recipient_type'];
    $recipient_id = $_POST['recipient_id'] ?? null;
    $title = $conn->real_escape_string($_POST['title']);
    $message = $conn->real_escape_string($_POST['message']);
    $notification_type = $_POST['notification_type'];
    
    if ($recipient_type == 'all_patients') {
        // Send to all patients
        $all_patients = $conn->query("SELECT patient_id FROM patients");
        while($p = $all_patients->fetch_assoc()) {
            $conn->query("INSERT INTO patient_notifications (patient_id, title, message, type) VALUES ('{$p['patient_id']}', '$title', '$message', '$notification_type')");
        }
        $msg = "Notification sent to all patients!";
    } 
    elseif ($recipient_type == 'all_doctors') {
        // Send to all doctors (you'd need a doctor_notifications table)
        $msg = "Doctor notifications feature coming soon.";
    }
    elseif ($recipient_type == 'single_patient' && $recipient_id) {
        $conn->query("INSERT INTO patient_notifications (patient_id, title, message, type) VALUES ('$recipient_id', '$title', '$message', '$notification_type')");
        $msg = "Notification sent to patient!";
    }
    elseif ($recipient_type == 'single_doctor' && $recipient_id) {
        // For doctor notifications (create doctor_notifications table)
        $conn->query("CREATE TABLE IF NOT EXISTS doctor_notifications (
            id INT AUTO_INCREMENT PRIMARY KEY,
            doctor_id INT NOT NULL,
            title VARCHAR(255),
            message TEXT,
            type VARCHAR(50) DEFAULT 'info',
            is_read TINYINT DEFAULT 0,
            created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
        )");
        $conn->query("INSERT INTO doctor_notifications (doctor_id, title, message, type) VALUES ('$recipient_id', '$title', '$message', '$notification_type')");
        $msg = "Notification sent to doctor!";
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Send Notification - Admin</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.6.0/css/all.min.css">
    <style>body{background:#0a3d2f;}.navbar{background:#064e3b;}.card{border-radius:20px;background:rgba(255,255,255,0.95);}</style>
</head>
<body>
<nav class="navbar navbar-dark"><div class="container"><span class="navbar-brand"><i class="fas fa-bell"></i> Send Notification</span><a href="dashboard.php" class="btn btn-light btn-sm">Back</a></div></nav>
<div class="container mt-4">
    <div class="card">
        <div class="card-header bg-success text-white"><h5><i class="fas fa-paper-plane"></i> Push Notification</h5></div>
        <div class="card-body">
            <?php if($msg): ?><div class="alert alert-success"><?= $msg ?></div><?php endif; ?>
            <?php if($error): ?><div class="alert alert-danger"><?= $error ?></div><?php endif; ?>
            
            <form method="POST" id="notificationForm">
                <div class="mb-3">
                    <label class="form-label">Recipient</label>
                    <select name="recipient_type" class="form-control" id="recipientType" required>
                        <option value="">-- Select --</option>
                        <option value="all_patients">All Patients</option>
                        <option value="single_patient">Single Patient</option>
                        <option value="all_doctors">All Doctors</option>
                        <option value="single_doctor">Single Doctor</option>
                    </select>
                </div>
                
                <div class="mb-3" id="patientSelect" style="display:none;">
                    <label class="form-label">Select Patient</label>
                    <select name="recipient_id" class="form-control">
                        <option value="">-- Select Patient --</option>
                        <?php while($p = $patients->fetch_assoc()): ?>
                            <option value="<?= $p['patient_id'] ?>"><?= $p['first_name'] . ' ' . $p['last_name'] ?> (<?= $p['patient_id'] ?>)</option>
                        <?php endwhile; ?>
                    </select>
                </div>
                
                <div class="mb-3" id="doctorSelect" style="display:none;">
                    <label class="form-label">Select Doctor</label>
                    <select name="recipient_id" class="form-control">
                        <option value="">-- Select Doctor --</option>
                        <?php while($d = $doctors->fetch_assoc()): ?>
                            <option value="<?= $d['doctor_id'] ?>">Dr. <?= $d['name'] ?></option>
                        <?php endwhile; ?>
                    </select>
                </div>
                
                <div class="mb-3">
                    <label class="form-label">Notification Type</label>
                    <select name="notification_type" class="form-control" required>
                        <option value="info">Information</option>
                        <option value="success">Success</option>
                        <option value="warning">Warning</option>
                        <option value="danger">Urgent</option>
                    </select>
                </div>
                
                <div class="mb-3">
                    <label class="form-label">Title</label>
                    <input type="text" name="title" class="form-control" placeholder="e.g., Appointment Reminder" required>
                </div>
                
                <div class="mb-3">
                    <label class="form-label">Message</label>
                    <textarea name="message" class="form-control" rows="4" placeholder="Enter your message here..." required></textarea>
                </div>
                
                <button type="submit" class="btn btn-primary w-100">Send Notification</button>
            </form>
        </div>
    </div>
</div>

<script>
document.getElementById('recipientType').addEventListener('change', function() {
    var patientSelect = document.getElementById('patientSelect');
    var doctorSelect = document.getElementById('doctorSelect');
    
    patientSelect.style.display = 'none';
    doctorSelect.style.display = 'none';
    
    if (this.value === 'single_patient') {
        patientSelect.style.display = 'block';
    } else if (this.value === 'single_doctor') {
        doctorSelect.style.display = 'block';
    }
});
</script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>