<?php require_once 'config.php'; ?>
<?php include 'header.php'; ?>

<div class="card">
    <?php if (isAdmin()): ?>
        <h2>Submit Feedback</h2>
        <form method="POST" action="">
            <div class="form-group">
                <label>Patient</label>
                <select name="patient_id" required>
                    <option value="">Select Patient</option>
                    <?php
                    $patients = $pdo->query("SELECT patient_id, name FROM patient ORDER BY name");
                    while ($p = $patients->fetch()): ?>
                        <option value="<?= $p['patient_id'] ?>"><?= htmlspecialchars($p['name']) ?></option>
                    <?php endwhile; ?>
                </select>
            </div>
            <div class="form-group">
                <label>Appointment (completed only)</label>
                <select name="appointment_id" required>
                    <option value="">Select Appointment</option>
                    <?php
                    $appts = $pdo->query("
                        SELECT a.appointment_id, p.name AS patient, d.name AS doctor, a.appointment_date
                        FROM appointment a
                        JOIN patient p ON a.patient_id = p.patient_id
                        JOIN doctor d ON a.doctor_id = d.doctor_id
                        WHERE a.status = 'Completed'
                        ORDER BY a.appointment_date DESC
                    ");
                    while ($a = $appts->fetch()): ?>
                        <option value="<?= $a['appointment_id'] ?>">
                            <?= htmlspecialchars($a['patient']) ?> with <?= htmlspecialchars($a['doctor']) ?> on <?= $a['appointment_date'] ?>
                        </option>
                    <?php endwhile; ?>
                </select>
            </div>
            <div class="form-group">
                <label>Rating (1-5)</label>
                <input type="number" name="rating" min="1" max="5" required>
            </div>
            <div class="form-group">
                <label>Comments</label>
                <textarea name="comments" rows="3"></textarea>
            </div>
            <button type="submit" class="btn">Submit Feedback</button>
        </form>

        <?php
        if ($_SERVER['REQUEST_METHOD'] === 'POST') {
            $sql = "INSERT INTO feedback (patient_id, appointment_id, rating, comments) VALUES (?, ?, ?, ?)";
            $stmt = $pdo->prepare($sql);
            try {
                $stmt->execute([$_POST['patient_id'], $_POST['appointment_id'], $_POST['rating'], $_POST['comments']]);
                echo "<p style='color:green;'>Feedback submitted.</p>";
            } catch (PDOException $e) {
                echo "<p style='color:red;'>Error: " . $e->getMessage() . "</p>";
            }
        }
        ?>
    <?php else: ?>
        <h2>Feedback Restricted</h2>
        <p style="color:#e74c3c;">Only administrators can submit feedback on behalf of patients.</p>
        <p><a href="login.php" class="btn">Go to Login</a></p>
    <?php endif; ?>
</div>

<?php include 'footer.php'; ?>