<?php
require("top.php");

// ✅ Handle Status Update (Active/Deactive)
if (isset($_GET['type']) && $_GET['type'] != '') {
    $type = mysqli_real_escape_string($con, $_GET['type']);

    if ($type == 'status') {
        $operation = mysqli_real_escape_string($con, $_GET['operation']);
        $id = mysqli_real_escape_string($con, $_GET['id']);
        $status = ($operation == 'active') ? 1 : 0;

        // Prepared statement for status update
        $update_status_sql = "UPDATE course_video SET status = ? WHERE id = ?";
        if ($stmt = mysqli_prepare($con, $update_status_sql)) {
            mysqli_stmt_bind_param($stmt, "ii", $status, $id);
            mysqli_stmt_execute($stmt);
            mysqli_stmt_close($stmt);
        }
        header("Location: course-video.php");
        exit();
    }

    // ✅ Handle Video Deletion
    if ($type == 'delete') {
        $id = mysqli_real_escape_string($con, $_GET['id']);
        $delete_sql = "DELETE FROM course_video WHERE id = ?";
        if ($stmt = mysqli_prepare($con, $delete_sql)) {
            mysqli_stmt_bind_param($stmt, "i", $id);
            mysqli_stmt_execute($stmt);
            mysqli_stmt_close($stmt);
        }
        header("Location: course-video.php");
        exit();
    }
}

// ✅ Search Functionality
$search = isset($_GET['search']) ? mysqli_real_escape_string($con, $_GET['search']) : "";

// ✅ Pagination Setup
$limit = 10;
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
$offset = ($page - 1) * $limit;

// ✅ Get total records for pagination
$search_sql = $search ? "WHERE course_video.video_title LIKE '%$search%'" : "";
$total_result = mysqli_query($con, "SELECT COUNT(*) AS total FROM course_video $search_sql");
$total_row = mysqli_fetch_assoc($total_result);
$total_records = $total_row['total'];
$total_pages = ceil($total_records / $limit);

// ✅ Fetch Video List with Pagination
$res = mysqli_query($con, "SELECT course_video.*, course_title.title FROM course_video INNER JOIN course_title ON course_video.course_title_id = course_title.id $search_sql ORDER BY course_video.video_day ASC, course_video.id DESC LIMIT $limit OFFSET $offset");
?>

<div class="container my-4">
    <div class="card">
        <div class="card-body">
            <h4 class="text-center">📽 Course Video List</h4>
            <a href="course_video_manage.php" class="btn btn-primary mb-3">➕ Add Video</a>
            
            <!-- ✅ Search Form -->
            <form method="GET" class="form-inline mb-3">
                <input type="text" name="search" class="form-control" placeholder="🔎 Search by video title" value="<?php echo htmlspecialchars($search); ?>">
                <button type="submit" class="btn btn-success ml-2">Search</button>
            </form>
        </div>

        <!-- ✅ Video List Table -->
        <div class="table-responsive">
            <table class="table table-bordered text-center">
                <thead class="thead-dark">
                    <tr>
                        <th>#</th>
                        <th>Course Name</th>
                        <th>Video Title</th>
                        <th>Video</th>
                        <th>Status</th>
                        <th>Date</th>
                        <th>Day</th>
                        <th>Action</th>
                    </tr>
                </thead>
                <tbody>
                    <?php 
                    $i = $offset + 1;
                    while ($row = mysqli_fetch_assoc($res)) { ?>
                        <tr>
                            <td><?php echo $i++; ?></td>
                            <td><?php echo $row['title']; ?></td>
                            <td><?php echo $row['video_title']; ?></td>
                            <td>
                                <iframe width="200" height="150" src="<?php echo $row['video_link']; ?>" allowfullscreen></iframe>
                            </td>
                            <td>
                                <?php if ($row['status'] == 1) { ?>
                                    <a href="?type=status&operation=deactive&id=<?php echo $row['id']; ?>" class="btn btn-success btn-sm">Active</a>
                                <?php } else { ?>
                                    <a href="?type=status&operation=active&id=<?php echo $row['id']; ?>" class="btn btn-warning btn-sm">Inactive</a>
                                <?php } ?>
                            </td>
                            <td><?php echo date("d M Y", strtotime($row['created_at'])); ?></td>
                            <td><?php echo "Day " . (int)$row['video_day']; ?></td>
                            <td>
                                <a class="btn btn-info btn-sm" href="course_video_edit.php?id=<?php echo $row['id']; ?>">✏ Edit</a>
                                <a class="btn btn-danger btn-sm" href="?type=delete&id=<?php echo $row['id']; ?>" onclick="return confirm('Are you sure?');">🗑 Delete</a>
                            </td>
                        </tr>
                    <?php } ?>
                </tbody>
            </table>
        </div>

        <!-- ✅ Pagination -->
        <nav>
            <ul class="pagination justify-content-center mt-3">
                <?php if ($page > 1): ?>
                    <li class="page-item">
                        <a class="page-link" href="?page=<?php echo $page - 1; ?>&search=<?php echo htmlspecialchars($search); ?>">⬅ Previous</a>
                    </li>
                <?php endif; ?>

                <?php for ($i = 1; $i <= $total_pages; $i++): ?>
                    <li class="page-item <?= ($i == $page) ? 'active' : '' ?>">
                        <a class="page-link" href="?page=<?php echo $i; ?>&search=<?php echo htmlspecialchars($search); ?>"><?php echo $i; ?></a>
                    </li>
                <?php endfor; ?>

                <?php if ($page < $total_pages): ?>
                    <li class="page-item">
                        <a class="page-link" href="?page=<?php echo $page + 1; ?>&search=<?php echo htmlspecialchars($search); ?>">Next ➡</a>
                    </li>
                <?php endif; ?>
            </ul>
        </nav>
    </div>
</div>

<?php require("footer.php"); ?>
