<?php
session_start();
if(isset($_SESSION['username'])){

    if(!isset($_GET['id'])) {
        header("Location: manage-templates");
        exit();
    }

    $template_id = $_GET['id'];
    ?>
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <?php include 'includes/styles.php'; ?>
        <title>View Template | <?php echo $school_name; ?></title>

        <?php
        require_once 'Classes/Timetable.php';
        $timetable = new Timetable();

        $module_name = 'Timetable';
        include "check-admin-privilege.php";

        // Get template details
        $sql = "SELECT t.*, c.name as class_name, s.name as section_name
            FROM timetable_templates t
            LEFT JOIN classes c ON t.class_id = c.id
            LEFT JOIN sections s ON t.section_id = s.id
            WHERE t.id = :template_id";
        $conn->query($sql);
        $conn->bind(':template_id', $template_id);
        $template = $conn->fetchSingle();

        if(!$template) {
            header("Location: manage-templates.php");
            exit();
        }

        // Get template entries
        $sql = "SELECT td.*, 
            s.subject_name,
            t.name as teacher_name,
            d.day_name,
            p.period_name, p.start_time, p.end_time, p.period_number
            FROM timetable_template_details td
            LEFT JOIN subjects s ON td.subject_id = s.id
            LEFT JOIN teachers t ON td.teacher_id = t.id
            LEFT JOIN timetable_days d ON td.day_id = d.id
            LEFT JOIN timetable_periods p ON td.period_id = p.id
            WHERE td.template_id = :template_id
            ORDER BY d.day_number, p.period_number";
        $conn->query($sql);
        $conn->bind(':template_id', $template_id);
        $entries = $conn->fetchMultiple();

        ?>
        <style>
            .template-info-card {
                background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
                color: white;
                padding: 25px;
                border-radius: 8px;
                margin-bottom: 20px;
            }
            .timetable-table {
                width: 100%;
                border-collapse: separate;
                border-spacing: 0;
            }
            .timetable-table th {
                background: linear-gradient(135deg, #667eea, #764ba2);
                color: white;
                padding: 12px;
                text-align: center;
                font-weight: 600;
                border: 1px solid #764ba2;
            }
            .timetable-table td {
                border: 1px solid #dee2e6;
                padding: 10px;
                vertical-align: top;
            }
            .period-cell {
                background: #f8f9fa;
                font-weight: 600;
                text-align: center;
                width: 120px;
            }
            .class-entry {
                background: #e7f3ff;
                border-left: 4px solid #007bff;
                padding: 12px;
                margin: 5px 0;
                border-radius: 4px;
            }
            .subject-name {
                font-weight: 600;
                color: #0056b3;
            }
            .teacher-name {
                font-size: 0.85rem;
                color: #666;
            }
        </style>
    </head>
    <body class="hold-transition sidebar-mini layout-fixed">
    <div class="wrapper">
        <?php include 'includes/notifications.php'; ?>
        <?php include "includes/sidebar.php"; ?>

        <div class="content-wrapper">
            <div class="content-header">
                <div class="container-fluid">
                    <div class="row mb-2">
                        <div class="col-sm-6">
                            <h1 class="m-0">View Template</h1>
                        </div>
                        <div class="col-sm-6">
                            <ol class="breadcrumb float-sm-right">
                                <li class="breadcrumb-item"><a href="<?php echo $conn->base_url();?>">Home</a></li>
                                <li class="breadcrumb-item"><a href="<?php echo $conn->base_url();?>manage-templates">Manage Templates</a></li>
                                <li class="breadcrumb-item active">View Template </li>
                            </ol>
                        </div>
                    </div>
                </div>
            </div>

            <section class="content">
                <div class="container-fluid">

                    <!-- Template Info -->
                    <div class="template-info-card">
                        <div class="row align-items-center">
                            <div class="col-md-8">
                                <h2 style="margin: 0 0 10px 0;"><?php echo htmlspecialchars($template->template_name); ?></h2>
                                <p style="margin: 0; font-size: 1.1rem;"><?php echo htmlspecialchars($template->description); ?></p>
                                <hr style="border-color: rgba(255,255,255,0.3);">
                                <div class="row">
                                    <div class="col-md-4">
                                        <strong>Class:</strong> <?php echo htmlspecialchars($template->class_name); ?>
                                    </div>
                                    <div class="col-md-4">
                                        <strong>Section:</strong> <?php echo htmlspecialchars($template->section_name); ?>
                                    </div>
                                    <div class="col-md-4">
                                        <strong>Entries:</strong> <?php echo count($entries); ?>
                                    </div>
                                </div>
                            </div>
                            <div class="col-md-4 text-right">
                                <button class="btn btn-light" onclick="window.print()">
                                    <i class="fas fa-print"></i> Print
                                </button>
                                <a href="manage-templates.php" class="btn btn-light">
                                    <i class="fas fa-arrow-left"></i> Back
                                </a>
                            </div>
                        </div>
                    </div>

                    <!-- Timetable View -->
                    <div class="card">
                        <div class="card-body">
                            <h4>Template Timetable</h4>
                            <div class="table-responsive">
                                <table class="timetable-table">
                                    <thead>
                                    <tr>
                                        <th>Period / Day</th>
                                        <?php
                                        $days = $timetable->getActiveDays();
                                        foreach($days as $day) {
                                            echo "<th>{$day->day_name}</th>";
                                        }
                                        ?>
                                    </tr>
                                    </thead>
                                    <tbody>
                                    <?php
                                    $periods = $timetable->getAllPeriods();
                                    foreach($periods as $period) {
                                        if($period->is_break == 1) {
                                            echo "<tr>";
                                            echo "<td colspan='" . (count($days) + 1) . "' style='background: #fff3cd; text-align: center; font-style: italic;'>";
                                            echo "{$period->period_name} ({$period->start_time} - {$period->end_time})";
                                            echo "</td>";
                                            echo "</tr>";
                                        } else {
                                            echo "<tr>";
                                            echo "<td class='period-cell'>";
                                            echo "<strong>{$period->period_name}</strong><br>";
                                            echo "<small>" . date('h:i A', strtotime($period->start_time)) . " - " . date('h:i A', strtotime($period->end_time)) . "</small>";
                                            echo "</td>";

                                            foreach($days as $day) {
                                                $entry = null;
                                                foreach($entries as $e) {
                                                    if($e->day_id == $day->id && $e->period_id == $period->id) {
                                                        $entry = $e;
                                                        break;
                                                    }
                                                }

                                                echo "<td>";
                                                if($entry) {
                                                    echo "<div class='class-entry'>";
                                                    echo "<div class='subject-name'>{$entry->subject_name}</div>";
                                                    echo "<div class='teacher-name'><i class='fas fa-chalkboard-teacher'></i> {$entry->teacher_name}</div>";
                                                    if($entry->room_number) {
                                                        echo "<div class='teacher-name'><i class='fas fa-door-open'></i> {$entry->room_number}</div>";
                                                    }
                                                    echo "</div>";
                                                } else {
                                                    echo "<div style='text-align: center; color: #999;'>-</div>";
                                                }
                                                echo "</td>";
                                            }

                                            echo "</tr>";
                                        }
                                    }
                                    ?>
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </div>

                </div>
            </section>

        </div>

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

    </body>
    </html>
    <?php
} else {
    ?>
    <!DOCTYPEhtml>
    <html lang="en">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>Login to continue | <?php echo $school_name; ?></title>
        <?php include 'includes/styles.php'; ?>
    </head>
    <body>
    <div class="container" style="margin-top: 20%;">
        <div class="row">
            <div class="col-lg-12 col-md-12">
                <div class="section-title text-center">
                    <h6>You need to login to continue</h6>
                    <div class="spinner-border text-info" role="status">
                        <span class="sr-only">Loading...</span>
                        <meta http-equiv='refresh' content='3; <?php echo $link; ?>'>
                    </div>
                    <p>Redirecting to the login page...</p>
                </div>
            </div>
        </div>
    </div>
    </body>
    </html>
    <?php
}
?>