<?php

session_name('super_admin_session');

if (!is_dir(dirname(__DIR__) . '/session_data/super')) {
    mkdir(dirname(__DIR__) . '/session_data/super', 0700, true);
}
session_save_path(dirname(__DIR__) . '/session_data/super');

session_set_cookie_params(['path' => '/', 'httponly' => true, 'samesite' => 'Strict']);

session_start();

error_reporting(0);

include('includes/dbconnection.php');
require_once 'includes/session_term_helper.php';
$stResult = resolveCurrentSessionTerm($con);
$currentSessionName = $stResult['session_name'];
$currentTermName = $stResult['term_name'];



// Ensure session is valid

if (strlen($_SESSION['sid']) == 0) {

    header('location:logout.php');

    exit();

}



// Get all feedback messages

$query = "SELECT * FROM feedback  WHERE status = 'pending' ORDER BY submitted_at DESC";

$result = mysqli_query($con, $query);

?>



<!DOCTYPE html>

<html lang="en">



<?php include("includes/head.php"); ?>



<body class="hold-transition sidebar-mini layout-fixed">

    <div class="wrapper">

        <!-- Navbar -->

        <?php include("includes/header.php"); ?>

        <!-- /.navbar -->



        <!-- Main Sidebar Container -->

        <?php include("includes/sidebar.php"); ?>



        <!-- Content Wrapper. Contains page content -->

        <div class="content-wrapper">

            <!-- Content Header (Page header) -->

            <section class="content-header premium-header">

                <div class="container-fluid">

                    <div class="header-glass-card">

                        <div class="row align-items-center">

                            <div class="col-md-8">

                                <div class="title-section">

                                    <div class="icon-box bg-primary-gradient shadow-sm">

                                        <i class="fas fa-comments"></i>

                                    </div>

                                    <div>

                                        <h1 class="premium-title text-primary">

                                            Feedback Management

                                            <span class="premium-subtitle text-secondary">Manage Feedback</span>

                                        </h1>

                                        <div class="mt-1">
                                            <?php if ($currentSessionName): ?>
                                                <span class="badge badge-primary mr-1">
                                                    <i class="fas fa-calendar-alt"></i>
                                                    <?= htmlspecialchars($currentSessionName) ?>
                                                </span>
                                            <?php endif; ?>
                                            <?php if ($currentTermName): ?>
                                                <span class="badge badge-success">
                                                    <i class="fas fa-bookmark"></i>
                                                    <?= htmlspecialchars($currentTermName) ?>
                                                </span>
                                            <?php else: ?>
                                                <span class="badge badge-warning">
                                                    <i class="fas fa-exclamation-triangle"></i> No active term
                                                </span>
                                            <?php endif; ?>
                                        </div>

                                    </div>

                                </div>

                            </div>

                            <div class="col-md-4">

                                <nav aria-label="breadcrumb">

                                    <ol class="breadcrumb premium-breadcrumb">

                                        <li class="breadcrumb-item"><a href="dashboard.php"
                                                class="text-primary">Home</a></li>

                                        <li class="breadcrumb-item active" aria-current="page">Feedback History</li>

                                    </ol>

                                </nav>

                            </div>

                        </div>

                    </div>

                </div>

            </section>



            <!-- Main content -->

            <section class="content">

                <div class="container-fluid">

                    <div class="row">

                        <div class="col-12">

                            <div class="card card-primary">

                                <div class="card-header">

                                    <h5 class="card-title font-weight-bold"><i class="fas fa-comments"></i> Admin
                                        Feedback Response</h5>

                                    <a href="feedback_history.php" class="btn btn-primary btn-sm float-right">View
                                        Feedback History</a>

                                </div>

                                <!-- /.card-header -->



                                <div class="card-body mt-2">

                                    <!-- Show alert if there's a success or error message -->

                                    <?php if (isset($_SESSION['success'])): ?>

                                        <div class="alert alert-success">

                                            <?php echo $_SESSION['success']; ?>

                                        </div>

                                        <?php unset($_SESSION['success']); ?>

                                    <?php elseif (isset($_SESSION['error'])): ?>

                                        <div class="alert alert-danger">

                                            <?php echo $_SESSION['error']; ?>

                                        </div>

                                        <?php unset($_SESSION['error']); ?>

                                    <?php endif; ?>



                                    <table id="example1" class="table table-bordered table-hover">

                                        <thead>

                                            <tr>

                                                <th>Name</th>

                                                <th>Student Name</th>

                                                <th>Class.</th>

                                                <th>Email</th>

                                                <th>Message</th>

                                                <th>Conversation</th>

                                                <th>Action</th>

                                            </tr>

                                        </thead>

                                        <tbody>

                                            <?php while ($row = mysqli_fetch_assoc($result)): ?>

                                                <tr>

                                                    <td><?php echo htmlspecialchars($row['name']); ?></td>

                                                    <td><?php echo htmlspecialchars($row['stdname']); ?></td>

                                                    <td><?php echo htmlspecialchars($row['admission_no']); ?></td>

                                                    <td><?php echo htmlspecialchars($row['email']); ?></td>

                                                    <td><?php echo nl2br(htmlspecialchars($row['message'])); ?></td>

                                                    <td>

                                                        <?php

                                                        // Get the conversation thread for this feedback
                                                    
                                                        $message_id = mysqli_real_escape_string($con, $row['id']);

                                                        $conv_query = "SELECT * FROM conversation WHERE message_id = $message_id ORDER BY timestamp ASC";

                                                        $conv_result = mysqli_query($con, $conv_query);



                                                        if ($conv_result) {

                                                            while ($conv_row = mysqli_fetch_assoc($conv_result)) {

                                                                echo "<p><strong>" . ucfirst(htmlspecialchars($conv_row['sender'])) . ":</strong> " . nl2br(htmlspecialchars($conv_row['message_content'])) . "</p>";

                                                            }

                                                        } else {

                                                            echo "<p class='text-danger'>Error loading conversation.</p>";

                                                        }

                                                        ?>

                                                        <!-- Admin can reply to the thread -->

                                                        <form method="POST" action="reply_feedback.php">

                                                            <textarea name="reply" placeholder="Your reply..." required
                                                                class="form-control reply-textarea"></textarea><br>

                                                            <input type="hidden" name="feedback_id"
                                                                value="<?php echo htmlspecialchars($row['id']); ?>">

                                                            <button type="submit" class="btn btn-primary">Reply</button>

                                                            <?php if (!empty($row['file_name'])): ?>

                                                                <a href="downloadf.php?file=<?php echo urlencode($row['file_name']); ?>"
                                                                    class="btn btn-danger btn-xs">

                                                                    <i class="fa fa-download"></i> Attachment

                                                                </a>

                                                            <?php endif; ?>

                                                        </form>

                                                    </td>

                                                    <td>

                                                        <?php if (empty($row['reply'])): ?>

                                                            <button class="btn btn-warning btn-xs" disabled><i
                                                                    class="fas fa-clock"></i> No reply yet</button>

                                                        <?php else: ?>

                                                            <button class="btn btn-success btn-xs"><i class="fas fa-reply"></i>
                                                                Replied</button>

                                                        <?php endif; ?>



                                                        <!-- Add delete button -->

                                                        <form method="POST" action="delete_feedback.php"
                                                            style="display: inline;">

                                                            <input type="hidden" name="feedback_id"
                                                                value="<?php echo htmlspecialchars($row['id']); ?>">

                                                            <button type="submit" class="btn btn-danger btn-xs"
                                                                onclick="return confirm('Are you sure you want to delete this feedback?');">

                                                                <i class="fas fa-trash"></i> Delete

                                                            </button>

                                                        </form>

                                                    </td>

                                                </tr>

                                            <?php endwhile; ?>

                                        </tbody>

                                    </table>



                                </div>

                                <!-- /.card-body -->

                            </div>

                            <!-- /.card -->

                        </div>

                        <!-- /.col -->

                    </div>

                    <!-- /.row -->

                </div>

                <!-- /.container-fluid -->

            </section>

            <!-- /.content -->

        </div>

        <!-- /.content-wrapper -->



        <?php include("includes/footer.php"); ?>



        <!-- Control Sidebar -->

        <aside class="control-sidebar control-sidebar-dark">

            <!-- Control sidebar content goes here -->

        </aside>

        <!-- /.control-sidebar -->

    </div>



    <!-- ./wrapper -->

    <?php include("includes/foot.php"); ?>



    <style>
        .reply-textarea {

            width: 100%;

            height: 120px;

            padding: 10px;

            border-radius: 5px;

            border: 1px solid #ccc;

            font-size: 14px;

            margin-bottom: 15px;

            resize: vertical;

        }



        button {

            padding: 10px 15px;

            font-size: 14px;

            border-radius: 5px;

        }



        .btn-warning {

            background-color: #f39c12;

            color: white;

        }



        .btn-warning:disabled {

            background-color: #e6b000;

        }



        .btn-success {

            background-color: #2ecc71;

            color: white;

        }



        .btn-primary {

            background-color: #3498db;

            color: white;

        }



        .btn-primary:hover {

            background-color: #2980b9;

        }



        .card-body {

            padding: 20px;

        }



        .card-title {

            font-size: 1.5em;

            margin-bottom: 10px;

        }
    </style>



</body>



</html>