<?php
require_once('../source/session.php');
require_once('../source/error.php');
//require_once('../source/dbconfig3.php');
require_once('../source/function.php');
require_once('../source/restrict_admin.php');

// Check if an ID is provided for deletion
if (isset($_GET['delete_id'])) {
    $deleteId = $_GET['delete_id'];
    
    // First get the file path to delete the actual file
    $selectStmt = $db->prepare("SELECT file_path FROM tbl_lessonplan WHERE id = ?");
    $selectStmt->bind_param("i", $deleteId);
    $selectStmt->execute();
    $result = $selectStmt->get_result();
    $lessonPlan = $result->fetch_assoc();
    
    if ($lessonPlan) {
        // Delete the actual file from server
        $fileToDelete = '../teacher/lesson_plan/' . $lessonPlan['file_path'];
        if (file_exists($fileToDelete)) {
            unlink($fileToDelete);
        }
        
        // Perform deletion query
        $deleteStmt = $db->prepare("DELETE FROM tbl_lessonplan WHERE id = ?");
        $deleteStmt->bind_param("i", $deleteId);
        
        if ($deleteStmt->execute()) {
            $_SESSION['success_message'] = "Lesson Plan deleted successfully.";
        } else {
            $_SESSION['error_message'] = "Error deleting lesson plan from database.";
        }
    } else {
        $_SESSION['error_message'] = "Lesson plan not found.";
    }
    
    // Redirect to avoid form resubmission
    header("Location: lesson_plan_list.php");
    exit();
}

// Fetch lesson plans from the database
function getLessonPlans()
{
    global $db;
    $stmt = $db->prepare("SELECT * FROM tbl_lessonplan ORDER BY created_date DESC");

    if (!$stmt) {
        die('Error in SQL query: ' . $db->error);
    }

    $stmt->execute();
    $result = $stmt->get_result();
    $lessonPlans = array();
    
    while ($row = $result->fetch_assoc()) {
        $lessonPlans[] = $row;
    }
    
    return $lessonPlans;
}

// Fetch lesson plans
$lessonPlans = getLessonPlans();
?>

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Lesson Plan List - School Management System</title>
    <link rel="stylesheet" href="../css/styles.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            padding: 20px;
        }

        .container {
            max-width: 1400px;
            margin: 0 auto;
            background-color: rgba(255, 255, 255, 0.95);
            border-radius: 20px;
            padding: 30px;
            box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
            backdrop-filter: blur(10px);
        }

        h2 {
            color: #333;
            font-size: 2.2em;
            margin-bottom: 25px;
            text-align: center;
            font-weight: 600;
            border-bottom: 3px solid #667eea;
            padding-bottom: 15px;
        }

        .links-container {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            padding: 20px;
            border-radius: 15px;
            margin-bottom: 25px;
            display: flex;
            justify-content: center;
            gap: 20px;
            flex-wrap: wrap;
        }

        .links-container a {
            color: white;
            text-decoration: none;
            padding: 12px 25px;
            background: rgba(255, 255, 255, 0.2);
            border-radius: 10px;
            transition: all 0.3s ease;
            display: inline-flex;
            align-items: center;
            gap: 10px;
            font-weight: 500;
            backdrop-filter: blur(5px);
            border: 1px solid rgba(255, 255, 255, 0.3);
        }

        .links-container a:hover {
            background: rgba(255, 255, 255, 0.3);
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.2);
        }

        .links-container i {
            font-size: 1.1em;
        }

        .success-message {
            background: linear-gradient(135deg, #84fab0 0%, #8fd3f4 100%);
            color: #155724;
            padding: 15px 25px;
            border-radius: 10px;
            margin-bottom: 25px;
            display: flex;
            align-items: center;
            gap: 10px;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
            border-left: 4px solid #28a745;
        }

        .success-message i {
            font-size: 1.2em;
            color: #28a745;
        }

        .error-message {
            background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
            color: #721c24;
            padding: 15px 25px;
            border-radius: 10px;
            margin-bottom: 25px;
            display: flex;
            align-items: center;
            gap: 10px;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
            border-left: 4px solid #dc3545;
        }

        .error-message i {
            font-size: 1.2em;
            color: #dc3545;
        }

        .table-responsive {
            overflow-x: auto;
        }

        table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 20px;
            border-radius: 15px;
            overflow: hidden;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
        }

        th {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            font-weight: 600;
            text-transform: uppercase;
            font-size: 0.85em;
            letter-spacing: 0.5px;
            padding: 15px 10px;
        }

        td {
            padding: 15px 10px;
            border-bottom: 1px solid #e0e0e0;
            color: #555;
        }

        tr {
            background: white;
            transition: all 0.3s ease;
        }

        tr:hover {
            background: #f8f9fa;
            transform: scale(1.01);
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
        }

        .action-links {
            display: flex;
            gap: 10px;
            justify-content: center;
            flex-wrap: wrap;
        }

        .action-links a {
            padding: 8px 15px;
            border-radius: 8px;
            text-decoration: none;
            font-size: 0.9em;
            font-weight: 500;
            transition: all 0.3s ease;
            display: inline-flex;
            align-items: center;
            gap: 5px;
        }

        .view-link {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
        }

        .view-link:hover {
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(102, 126, 234, 0.4);
        }

        .delete-link {
            background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
            color: white;
        }

        .delete-link:hover {
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(245, 87, 108, 0.4);
        }

        .no-data {
            text-align: center;
            padding: 40px;
            color: #666;
            font-size: 1.1em;
        }

        .no-data i {
            font-size: 3em;
            color: #667eea;
            margin-bottom: 15px;
            display: block;
        }

        .badge {
            background: linear-gradient(135deg, #84fab0 0%, #8fd3f4 100%);
            color: #155724;
            padding: 5px 10px;
            border-radius: 20px;
            font-size: 0.85em;
            font-weight: 500;
        }

        @media (max-width: 768px) {
            .container {
                padding: 15px;
            }
            
            h2 {
                font-size: 1.8em;
            }
            
            .links-container a {
                padding: 10px 15px;
                font-size: 0.9em;
            }
            
            th, td {
                padding: 10px 5px;
                font-size: 0.9em;
            }
            
            .action-links {
                flex-direction: column;
                gap: 5px;
            }
        }
    </style>

    <script>
        function confirmDelete() {
            return confirm("Are you sure you want to delete this lesson plan? This action cannot be undone.");
        }
    </script>
</head>

<body>
    <div class="container">
        <h2><i class="fas fa-book-open" style="margin-right: 10px;"></i>Lesson Plan Management</h2>

        <!-- Navigation Links -->
        <div class="links-container">
            <a href="upload_lesson_plan.php"><i class="fas fa-upload"></i> Upload Lesson Plan</a>
            <a href="edit_lesson_plan.php"><i class="fas fa-edit"></i> Edit Descriptions</a>
            <a href="../admin_dashboard.php"><i class="fas fa-arrow-left"></i> Back to Dashboard</a>
            <a href="https://smt.lahs.com.ng" target="_blank"><i class="fas fa-school"></i> School Management</a>
        </div>

        <!-- Display Messages -->
        <?php if (isset($_SESSION['success_message'])): ?>
            <div class="success-message">
                <i class="fas fa-check-circle"></i>
                <?php 
                echo $_SESSION['success_message'];
                unset($_SESSION['success_message']);
                ?>
            </div>
        <?php endif; ?>

        <?php if (isset($_SESSION['error_message'])): ?>
            <div class="error-message">
                <i class="fas fa-exclamation-circle"></i>
                <?php 
                echo $_SESSION['error_message'];
                unset($_SESSION['error_message']);
                ?>
            </div>
        <?php endif; ?>

        <!-- Lesson Plans Table -->
        <div class="table-responsive">
            <table>
                <thead>
                    <tr>
                        <th><i class="fas fa-heading"></i> Title</th>
                        <th><i class="fas fa-align-left"></i> Description</th>
                        <th><i class="fas fa-user"></i> Creator</th>
                        <th><i class="fas fa-users"></i> Class</th>
                        <th><i class="fas fa-book"></i> Subject</th>
                        <th><i class="fas fa-calendar"></i> Lesson Date</th>
                        <th><i class="fas fa-cogs"></i> Actions</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (empty($lessonPlans)): ?>
                        <tr>
                            <td colspan="7" class="no-data">
                                <i class="fas fa-folder-open"></i>
                                No lesson plans found. Click "Upload Lesson Plan" to add one.
                            </td>
                        </tr>
                    <?php else: ?>
                        <?php foreach ($lessonPlans as $lessonPlan): ?>
                            <tr>
                                <td><strong><?php echo htmlspecialchars($lessonPlan['title']); ?></strong></td>
                                <td><?php echo htmlspecialchars(substr($lessonPlan['description'], 0, 50)) . (strlen($lessonPlan['description']) > 50 ? '...' : ''); ?></td>
                                <td><span class="badge"><?php echo htmlspecialchars($lessonPlan['created_by']); ?></span></td>
                                <td><?php echo htmlspecialchars($lessonPlan['class']); ?></td>
                                <td><?php echo htmlspecialchars($lessonPlan['subject']); ?></td>
                                <td><?php echo date('d M Y', strtotime($lessonPlan['lesson_date'])); ?></td>
                                <td>
                                    <div class="action-links">
                                        <?php
                                        $relativeFilePath = $lessonPlan['file_path'];
                                        $fullFilePath = '../teacher/lesson_plan/' . $relativeFilePath;
                                        ?>
                                        <a href="<?php echo $fullFilePath; ?>" target="_blank" class="view-link">
                                            <i class="fas fa-download"></i> View
                                        </a>
                                        <a href="?delete_id=<?php echo $lessonPlan['id']; ?>" 
                                           onclick="return confirmDelete()" 
                                           class="delete-link">
                                            <i class="fas fa-trash-alt"></i> Delete
                                        </a>
                                    </div>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>

        <!-- Summary Section -->
        <?php if (!empty($lessonPlans)): ?>
            <div style="margin-top: 20px; text-align: right; color: #666;">
                <i class="fas fa-chart-bar"></i> 
                Total Lesson Plans: <strong><?php echo count($lessonPlans); ?></strong>
            </div>
        <?php endif; ?>
    </div>
</body>

</html>