<?php
session_start();
include('includes/dbconnection.php');

// Check if feedback_id is set in the URL
if (isset($_GET['feedback_id'])) {
    $feedback_id = $_GET['feedback_id'];

    // Fetch the feedback details using feedback_id
    $query = "SELECT * FROM feedback WHERE id = '$feedback_id'";
    $result = mysqli_query($con, $query);
    $feedback = mysqli_fetch_assoc($result);

    // If the feedback doesn't exist, redirect or show an error
    if (!$feedback) {
        echo "Feedback not found.";
        exit();
    }
}

// Handle form submission when the user replies
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $reply = mysqli_real_escape_string($con, $_POST['reply']);
        // Insert the admin's reply into the conversation table
        $query_conversation = "INSERT INTO conversation (message_id, sender, message_content) 
        VALUES ('$feedback_id', 'user', '$reply')";
    if (mysqli_query($con, $query_conversation)) {

    // Update the feedback with the user's reply
    $query_update = "UPDATE feedback SET user_reply='$reply', status='pending', submitted_at = CURRENT_TIMESTAMP WHERE id='$feedback_id'";
    
    if (mysqli_query($con, $query_update)) {
        echo "
        <script>
            alert('Your reply has been submitted successfully.');
            window.location.href = document.referrer;  // Redirect back to the email link the user clicked
        </script>
        ";
    } else {
        echo "
        <script>
            alert('Error submitting your reply. Please try again.');
        </script>
        ";
    }
} else {
    echo "
    <script>
        alert('Error inserting conversation');
    </script>
";
}
}
?>

<!DOCTYPE html>
<html>
<head>
    <title>Reply to Feedback</title>
    <style>
        /* Modal styles */
        .modal {
            display: none;
            position: fixed;
            z-index: 1;
            left: 0;
            top: 0;
            width: 100%;
            height: 100%;
            overflow: auto;
            background-color: rgba(0, 0, 0, 0.6);
        }

        .modal-content {
            background-color: #fff;
            margin: 10% auto;
            padding: 30px;
            border-radius: 10px;
            width: 50%;
            box-shadow: 0px 5px 15px rgba(0, 0, 0, 0.1);
        }

        h3 {
            color: #2c3e50;
            font-family: 'Arial', sans-serif;
            text-align: center;
        }

        .form-container {
            display: flex;
            flex-direction: column;
            gap: 20px;
        }

        label {
            font-size: 16px;
            font-weight: bold;
            color: #34495e;
        }

        textarea {
            width: 100%;
            height: 150px;
            padding: 10px;
            border: 1px solid #ccc;
            border-radius: 8px;
            font-size: 14px;
            font-family: 'Arial', sans-serif;
            resize: none;
            box-sizing: border-box;
            transition: border-color 0.3s ease;
        }

        textarea:focus {
            border-color: #2980b9;
            outline: none;
        }

        button {
            padding: 12px 20px;
            font-size: 16px;
            background-color: #2980b9;
            color: #fff;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            transition: background-color 0.3s ease;
        }

        button:hover {
            background-color: #3498db;
        }

        .close {
            color: #aaa;
            font-size: 30px;
            font-weight: bold;
            position: absolute;
            top: 10px;
            right: 20px;
            cursor: pointer;
        }

        .close:hover {
            color: #000;
        }

        .blockquote {
            font-size: 16px;
            font-style: italic;
            background-color: #f4f6f7;
            padding: 15px;
            border-left: 5px solid #2980b9;
            margin: 20px 0;
        }

        /* Responsive */
        @media (max-width: 768px) {
            .modal-content {
                width: 80%;
                padding: 20px;
            }

            textarea {
                height: 120px;
            }

            button {
                font-size: 14px;
                padding: 10px 18px;
            }
        }
    </style>
</head>
<body class="hold-transition sidebar-mini layout-fixed">

<!-- Modal -->
<div id="replyModal" class="modal">
    <div class="modal-content">
        <span class="close">&times;</span>
        <h3>Reply to Admin</h3>

        <form method="POST" action="" class="form-container">
            <div>
                <label>Admin's Reply:</label>
                <blockquote class="blockquote"><?php echo nl2br(htmlspecialchars($feedback['reply'])); ?></blockquote>
            </div>

            <div>
                <label>Your Reply:</label>
                <textarea name="reply" required placeholder="Type your reply here..."></textarea>
            </div>

            <div>
                <button type="submit">Submit Reply</button>
            </div>
        </form>
    </div>
</div>

<!-- Show modal when the page loads -->
<script>
    window.onload = function() {
        var modal = document.getElementById("replyModal");
        var span = document.getElementsByClassName("close")[0];
        modal.style.display = "block"; // Show the modal when the page loads

        span.onclick = function() {
            modal.style.display = "none"; // Close the modal when the user clicks on the close button
        }

        // Close the modal if the user clicks anywhere outside the modal
        window.onclick = function(event) {
            if (event.target == modal) {
                modal.style.display = "none";
            }
        }
    }
</script>

</body>
</html>
