<?php
session_start();
if(isset($_SESSION['username'])){
    ?>
    <!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'; ?>

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

        // Get parameters
        $classId = isset($_GET['class_id']) ? (int)$_GET['class_id'] : 0;
        $sectionId = isset($_GET['section_id']) ? (int)$_GET['section_id'] : 0;
        $academicYear = isset($_GET['academic_year']) ? $_GET['academic_year'] : '';
        $term = isset($_GET['term']) ? $_GET['term'] : '';

        if(!$classId || !$sectionId) {
            die('Invalid parameters');
        }

        // Get class and section names
        $sql = "SELECT c.name as class_name, s.name as section_name
        FROM classes c, sections s
        WHERE c.id = :class_id AND s.id = :section_id";
        $timetable->query($sql);
        $timetable->bind(':class_id', $classId);
        $timetable->bind(':section_id', $sectionId);
        $classInfo = $timetable->fetchSingle();

        // Get timetable entries
        $entries = $timetable->getClassTimetable($classId, $sectionId, $academicYear, $term);

        // Get active days
        $days = $timetable->getActiveDays();

        // Get all periods
        $periods = $timetable->getAllPeriods();

        // Get school info
        $sql = "SELECT school_name, main_url FROM general_settings LIMIT 1";
        $timetable->query($sql);
        $schoolInfo = $timetable->fetchSingle();
        $schoolName = $schoolInfo->school_name ?? 'School';
        ?>
        <style>
            * {
                margin: 0;
                padding: 0;
                box-sizing: border-box;
            }
            body {
                font-family: 'Arial', sans-serif;
                padding: 20px;
                background: white;
            }
            .header {
                text-align: center;
                margin-bottom: 30px;
                padding-bottom: 20px;
                border-bottom: 3px solid #333;
            }
            .header h1 {
                font-size: 28px;
                color: #333;
                margin-bottom: 5px;
            }
            .header h2 {
                font-size: 20px;
                color: #666;
                font-weight: normal;
            }
            .info-section {
                margin-bottom: 20px;
                display: flex;
                justify-content: space-between;
                align-items: center;
            }
            .info-left, .info-right {
                font-size: 14px;
                color: #555;
            }
            .info-left p, .info-right p {
                margin: 5px 0;
            }
            .timetable-container {
                width: 100%;
                overflow-x: auto;
            }
            .timetable-table {
                width: 100%;
                border-collapse: collapse;
                margin-top: 20px;
            }
            .timetable-table th {
                background: #4a5568;
                color: white;
                padding: 12px 8px;
                text-align: center;
                font-weight: 600;
                border: 1px solid #2d3748;
                font-size: 13px;
            }
            .timetable-table td {
                border: 1px solid #cbd5e0;
                padding: 10px 8px;
                text-align: center;
                min-height: 80px;
                vertical-align: top;
            }
            .period-cell {
                background: #edf2f7;
                font-weight: 600;
                font-size: 12px;
                width: 100px;
            }
            .period-time {
                font-size: 10px;
                color: #666;
                display: block;
                margin-top: 3px;
            }
            .break-cell {
                background: #fef5e7;
                font-weight: 600;
                color: #d68910;
                font-style: italic;
            }
            .class-entry {
                background: #ebf8ff;
                border-left: 3px solid #3182ce;
                padding: 8px;
                border-radius: 4px;
                text-align: left;
            }
            .subject-name {
                font-weight: 600;
                color: #2c5282;
                font-size: 13px;
                margin-bottom: 4px;
            }
            .teacher-name {
                font-size: 11px;
                color: #4a5568;
                margin-bottom: 2px;
            }
            .room-number {
                font-size: 10px;
                color: #718096;
            }
            .empty-slot {
                color: #a0aec0;
                font-style: italic;
                font-size: 12px;
                padding: 20px;
            }
            .footer {
                margin-top: 40px;
                padding-top: 20px;
                border-top: 2px solid #e2e8f0;
                font-size: 11px;
                color: #718096;
                display: flex;
                justify-content: space-between;
            }
            .signature-section {
                margin-top: 60px;
                display: flex;
                justify-content: space-between;
            }
            .signature-box {
                text-align: center;
                width: 30%;
            }
            .signature-line {
                border-top: 1px solid #333;
                margin-top: 50px;
                padding-top: 5px;
                font-size: 12px;
            }
            @media print {
                body {
                    padding: 10px;
                }
                .no-print {
                    display: none !important;
                }
                @page {
                    margin: 15mm;
                }
            }
            .print-button {
                position: static;
                float: right;
                padding: 12px 24px;
                background: #3182ce;
                color: white;
                border: none;
                border-radius: 6px;
                cursor: pointer;
                font-size: 14px;
                font-weight: 600;
                box-shadow: 0 4px 6px rgba(0,0,0,0.1);
                z-index: 1000;
            }
            .print-button:hover {
                background: #2c5282;
            }
        </style>
        <title>Print Timetable - <?php echo $classInfo->class_name; ?> <?php echo $classInfo->section_name; ?></title>
    </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">Print Timetable</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 active">Print Timetable</li>
                            </ol>
                        </div>
                    </div>
                </div>
            </div>
            <section class="content">
                <div class="container-fluid">
                    <div class="card">
                        <div class="card-header">
                            <h3 class="card-title"> Print Timetable </h3>
                        </div>

                 <div class="card-body">
                    <button onclick="window.print()" class="print-button no-print">
                        🖨️ Print Timetable
                    </button>

                    <div class="header" style="clear: both;">
                        <h1><?php echo htmlspecialchars($schoolName); ?></h1>
                        <h2>Class Timetable</h2>
                    </div>

                    <div class="info-section">
                        <div class="info-left">
                            <p><strong>Class:</strong> <?php echo htmlspecialchars($classInfo->class_name); ?></p>
                            <p><strong>Section:</strong> <?php echo htmlspecialchars($classInfo->section_name); ?></p>
                        </div>
                        <div class="info-right">
                            <?php if($academicYear): ?>
                                <p><strong>Academic Year:</strong> <?php echo htmlspecialchars($academicYear); ?></p>
                            <?php endif; ?>
                            <?php if($term): ?>
                                <p><strong>Term:</strong> <?php echo htmlspecialchars($term); ?></p>
                            <?php endif; ?>
                            <p><strong>Printed On:</strong> <?php echo date('M d, Y h:i A'); ?></p>
                        </div>
                    </div>

                    <div class="timetable-container">
                        <table class="timetable-table">
                            <thead>
                            <tr>
                                <th>Period / Day</th>
                                <?php foreach($days as $day): ?>
                                    <th><?php echo htmlspecialchars($day->day_name); ?></th>
                                <?php endforeach; ?>
                            </tr>
                            </thead>
                            <tbody>
                            <?php foreach($periods as $period): ?>
                                <?php if($period->is_break == 1): ?>
                                    <tr>
                                        <td class="period-cell break-cell" colspan="<?php echo count($days) + 1; ?>">
                                            <?php echo htmlspecialchars($period->period_name); ?>
                                            <span class="period-time">
                                    (<?php echo date('h:i A', strtotime($period->start_time)); ?> -
                                    <?php echo date('h:i A', strtotime($period->end_time)); ?>)
                                </span>
                                        </td>
                                    </tr>
                                <?php else: ?>
                                    <tr>
                                        <td class="period-cell">
                                            <strong><?php echo htmlspecialchars($period->period_name); ?></strong>
                                            <span class="period-time">
                                    <?php echo date('h:i A', strtotime($period->start_time)); ?> -
                                    <?php echo date('h:i A', strtotime($period->end_time)); ?>
                                </span>
                                        </td>
                                        <?php foreach($days as $day): ?>
                                            <td>
                                                <?php
                                                $entry = null;
                                                if($entries) {
                                                    foreach($entries as $e) {
                                                        if($e->day_id == $day->id && $e->period_id == $period->id) {
                                                            $entry = $e;
                                                            break;
                                                        }
                                                    }
                                                }
                                                if($entry):
                                                    ?>
                                                    <div class="class-entry">
                                                        <div class="subject-name"><?php echo htmlspecialchars($entry->subject_name); ?></div>
                                                        <div class="teacher-name">👤 <?php echo htmlspecialchars($entry->teacher_name); ?></div>
                                                        <?php if($entry->room_number): ?>
                                                            <div class="room-number">🚪 <?php echo htmlspecialchars($entry->room_number); ?></div>
                                                        <?php endif; ?>
                                                    </div>
                                                <?php else: ?>
                                                    <div class="empty-slot">-</div>
                                                <?php endif; ?>
                                            </td>
                                        <?php endforeach; ?>
                                    </tr>
                                <?php endif; ?>
                            <?php endforeach; ?>
                            </tbody>
                        </table>
                    </div>

                    <div class="signature-section no-print">
                        <div class="signature-box">
                            <div class="signature-line">
                                Class Teacher
                            </div>
                        </div>
                        <div class="signature-box">
                            <div class="signature-line">
                                Head Teacher
                            </div>
                        </div>
                        <div class="signature-box">
                            <div class="signature-line">
                                Principal
                            </div>
                        </div>
                    </div>

                    <div class="footer">
                        <div>Generated by <?php echo htmlspecialchars($schoolName); ?> Timetable System</div>
                        <div>Page 1 of 1</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
}
?>