﻿﻿??????????????
﻿﻿??????????????
<?php
   include('session-st.php');  // Student session check
?>
<?php
include 'connection.php';  // Adjust path if needed

// Ensure course exists in session
if (!isset($_SESSION['course'])) {
    die("Course not found in session.");
}

$student_course = $_SESSION['course'];

// Fetch tasks
$sql = "SELECT DISTINCT t.id, t.task_no, t.task_name, t.full_marks, t.end_date 
        FROM tasks t 
        WHERE t.course = ? AND t.status IN ('active', 'published') 
        ORDER BY t.created_at DESC";

$stmt = $pdo->prepare($sql);
$stmt->execute([$student_course]);
$tasks = $stmt->fetchAll(PDO::FETCH_ASSOC);

// Preload all task files
$task_files = [];
$file_stmt = $pdo->prepare("SELECT file_name, file_path FROM task_files WHERE task_id = ?");

foreach ($tasks as $task) {
    $file_stmt->execute([$task['id']]);
    $task_files[$task['id']] = $file_stmt->fetchAll(PDO::FETCH_ASSOC);
}

$a = 0;
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Tasks | Student</title>
    <!-- Favicon icon -->
    <link rel="icon" type="image/png" href="../images/logo-wide.png">
    <!-- Base Styling  -->
    <link rel="stylesheet" href="assets/main/css/fonts.css">
    <link rel="stylesheet" href="assets/main/css/style.css">
    <script src="https://code.jquery.com/jquery-3.6.4.min.js" integrity="sha256-oP6HI9z1XaZNBrJURtCoUT5SUnxFr8s3BzRl+cbzUq8=" crossorigin="anonymous"></script>
    <style>
    .btn { background-color: #199319; border: none; color: white; padding: 5px 10px; text-decoration: none; border-radius: 5px; margin: 2px; display: inline-block; }
    .actions-container { display: flex; flex-wrap: wrap; }
    </style>
</head>

<body>
    <div id="main-wrapper" class="show">
        <?php include "sidebar.php"; ?>
        <?php include "header.php"; ?>

        <div class="content-body">
            <div class="warper container-fluid">
                <div class="new-patients main_container">
                    <div class="row">
                        <div class="col-md-12">
                            <div class="card shadow">
                                <div class="card-header">
                                    <h4 class="card-title">My Tasks</h4>
                                </div>
                                <div class="card-body">
                                    <div class="table-responsive">
                                                                                                                                      </tr>
                                         <table id="example1" class="display nowrap">
                                            <thead>
                                                <tr>
                                                    <th>SL No</th>
                                                    <th>Task No</th>
                                                    <th>Task Name</th>
                                                    <th>Full Marks</th>
                                                    <th>Submit Date</th>
                                                    <th>Actions</th>
                                                </tr>
                                            </thead>

                                            <tbody>
                                                <?php foreach ($tasks as $rows) { 
                                                    $task_id = $rows['id'];
                                                    $files = $task_files[$task_id];
                                                ?>
                                                <tr>
                                                    <td><?= ++$a ?></td>
                                                    <td><?= htmlspecialchars($rows['task_no']) ?></td>
                                                    <td><?= htmlspecialchars($rows['task_name']) ?></td>
                                                    <td><?= htmlspecialchars($rows['full_marks']) ?></td>
                                                    <td><?= htmlspecialchars($rows['end_date']) ?></td>

                                                    <td>
                                                        <button 
                                                            class="btn doc-btn" 
                                                            data-bs-toggle="modal" 
                                                            data-bs-target="#docModal"
                                                            data-task-id="<?= $task_id ?>"
                                                            data-task-name="<?= htmlspecialchars($rows['task_name']) ?>"
                                                            <?= empty($files) ? 'disabled' : '' ?>>
                                                            Doc
                                                        </button>
                                                    </td>
                                                </tr>
                                                <?php } ?>
                                            </tbody>
                                        </table>
                                    </div>
                                    <?php if (count($tasks) == 0) { ?>
                                        <p>No tasks available for your course.</p>
                                    <?php } ?>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>



       <!-- Modal for Documents -->
        <div class="modal fade" id="docModal" tabindex="-1">
            <div class="modal-dialog">
                <div class="modal-content">

                    <div class="modal-header">
                        <h5 class="modal-title">Documents</h5>
                        <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                    </div>

                    <div class="modal-body" id="modalBody"></div>

                    <div class="modal-footer">
                        <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
                    </div>

                </div>
            </div>
        </div>

        <?php include('footer.php'); ?>

    </div>

    <script>
    $(document).ready(function () {
        $('#docModal').on('show.bs.modal', function (event) {

            var button = $(event.relatedTarget);
            var taskId = button.data('task-id');
            var taskName = button.data('task-name');
            var modal = $(this);

            modal.find('.modal-title').text('Documents for ' + taskName);

            var filesHtml = '';

            <?php foreach ($task_files as $tid => $files) { ?>
                if (taskId == <?= $tid ?>) {

                    <?php if (!empty($files)) { ?>
                        <?php foreach ($files as $file) { ?>
                            filesHtml += `
                                <div class="modal-file">
                                    <strong><?= addslashes($file['file_name']) ?></strong><br>
                                    <a href="view.php?file=<?= urlencode($file['file_path']) ?>" class="btn" target="_blank">View</a>
                                    <a href="download.php?file=<?= urlencode($file['file_path']) ?>" class="btn">Download</a>
                                </div>`;
                        <?php } ?>
                    <?php } else { ?>
                        filesHtml = 'No documents available.';
                    <?php } ?>

                }
            <?php } ?>

            modal.find('#modalBody').html(filesHtml);
        });
    });
    </script>


    <!-- (Rest of JS from template remains the same) -->
    <script src="assets/plugins/popper/popper.min.js"></script>
    <script src="assets/plugins/bootstrap/js/bootstrap.js"></script>
    <script src="assets/plugins/moment/moment.min.js"></script>
    <script src="assets/plugins/daterangepicker/daterangepicker.min.js"></script>
    <script src="assets/plugins/datatables/jquery.dataTables.min.js"></script>
    <script src="assets/js/init-tdatatable.js"></script>
    <script src="assets/plugins/chart/chart/Chart.min.js"></script>
    <script src="assets/js/charts-custom.js"></script>
    <script src="assets/js/toggleFullScreen.js"></script>
    <script src="assets/js/main.js"></script>
</body>
</html>