<?php
require("top.php"); // ✅ Database Connection

$msg = "";

// ✅ अगर डिलीट बटन दबाया गया है
if (isset($_POST['delete'])) {
    $delete_id = mysqli_real_escape_string($con, $_POST['delete_id']); // Get video_id to delete

    // 🎯 सबसे पहले चेक करें कि वीडियो मौजूद है या नहीं
    $check_res = mysqli_query($con, "SELECT * FROM video_day WHERE video_id = '$delete_id'");
    if (mysqli_num_rows($check_res) > 0) {

        // 🔴 FOREIGN KEY CONSTRAINT ISSUE FIX
        mysqli_query($con, "SET FOREIGN_KEY_CHECKS=0");

        // ✅ STEP 1: `video_day` टेबल से डेटा हटाएं
        $delete_video_day = mysqli_query($con, "DELETE FROM video_day WHERE video_id = '$delete_id'");
        if (!$delete_video_day) {
            echo "❌ वीडियो दिन (video_day) से डिलीट नहीं हो सका: " . mysqli_error($con);
            exit();
        }

        // ✅ STEP 2: `user_video_access` से डेटा हटाएं
        $delete_user_video_access = mysqli_query($con, "DELETE FROM user_video_access WHERE video_id = '$delete_id'");
        if (!$delete_user_video_access) {
            echo "❌ यूज़र वीडियो एक्सेस (user_video_access) से डिलीट नहीं हो सका: " . mysqli_error($con);
            exit();
        }

        // ✅ STEP 3: `course_video` से वीडियो डिलीट करें
        $delete_course_video = mysqli_query($con, "DELETE FROM course_video WHERE id = '$delete_id'");
        if (!$delete_course_video) {
            echo "❌ वीडियो (course_video) से डिलीट नहीं हो सका: " . mysqli_error($con);
            exit();
        }

        // 🔴 FOREIGN KEY CHECK को वापस ON करें
        mysqli_query($con, "SET FOREIGN_KEY_CHECKS=1");

        // ✅ सफल डिलीट होने पर रीडायरेक्ट करें
        $msg = "✅ वीडियो सफलतापूर्वक डिलीट कर दिया गया!";
        header("Location: video_day.php");
        exit();
    } else {
        $msg = "❌ वीडियो नहीं मिला!";
    }
}

// ✅ जब वीडियो आईडी डाली जाए, तब वीडियो नाम प्राप्त करें
$video_title = "";
if (isset($_POST['delete_id'])) {
    $video_id = mysqli_real_escape_string($con, $_POST['delete_id']);
    $result = mysqli_query($con, "SELECT video_title FROM course_video WHERE id = '$video_id'");
    if (mysqli_num_rows($result) > 0) {
        $row = mysqli_fetch_assoc($result);
        $video_title = $row['video_title'];
    }
}

// ✅ Pagination Setup
$limit = 10; // Number of videos per page
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
$offset = ($page - 1) * $limit;

// ✅ Fetch All Video Titles for Display with Pagination
$all_video_titles = mysqli_query($con, "SELECT id, video_title FROM course_video ORDER BY video_title ASC LIMIT $limit OFFSET $offset");

// ✅ Total number of videos
$total_res = mysqli_query($con, "SELECT COUNT(*) AS total FROM course_video");
$total_row = mysqli_fetch_assoc($total_res);
$total_videos = $total_row['total'];

// ✅ Calculate total pages
$total_pages = ceil($total_videos / $limit);
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Delete Video from Video Day</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css">
    <style>
        /* Custom CSS for Pagination */
        .pagination {
            display: flex;
            justify-content: center;
            list-style: none;
            padding: 0;
        }
        .pagination li {
            margin: 0 5px;
        }
        .pagination li a {
            text-decoration: none;
            padding: 8px 16px;
            background-color: #007bff;
            color: white;
            border-radius: 4px;
        }
        .pagination li a:hover {
            background-color: #0056b3;
        }
    </style>
</head>
<body>
    <div class="container mt-5">
        <h2 class="text-center">Delete Video from Video Day</h2>

        <!-- ✅ Delete Message -->
        <?php if (!empty($msg)): ?>
            <div class="alert alert-info text-center">
                <?php echo htmlspecialchars($msg); ?>
            </div>
        <?php endif; ?>

        <!-- ✅ Delete Form -->
        <form method="post">
            <div class="mb-3">
                <label for="video_id" class="form-label">Enter Video ID to Delete:</label>
                <input type="number" name="delete_id" class="form-control" id="video_id" required>
            </div>

            <!-- Show Video Title based on video_id -->
            <?php if (!empty($video_title)): ?>
                <div class="mb-3">
                    <label for="video_title" class="form-label">Video Title:</label>
                    <input type="text" class="form-control" id="video_title" value="<?php echo $video_title; ?>" disabled>
                </div>
            <?php endif; ?>

            <button type="submit" name="delete" class="btn btn-danger w-100">Delete Video</button>
        </form>

        <h4 class="mt-3"><a href="course_video.php" class="text-primary">🔙 Go Back to Course Video</a></h4>

        <!-- ✅ Display All Video Titles -->
        <h4 class="mt-5">All Video Titles:</h4>
        <ul class="list-group">
            <?php while ($row = mysqli_fetch_assoc($all_video_titles)): ?>
                <li class="list-group-item">
                    <strong>Video ID:</strong> <?php echo $row['id']; ?> <br>
                    <strong>Video Title:</strong> <?php echo $row['video_title']; ?>
                </li>
            <?php endwhile; ?>
        </ul>

        <!-- ✅ Pagination Links -->
        
        <div class="pagination mt-12">
            <ul>
                <?php if ($page > 1): ?>
                    <li><a href="?page=<?php echo $page - 1; ?>">Previous</a></li>
                <?php endif; ?>

                <?php for ($i = 1; $i <= $total_pages; $i++): ?>
                    <li><a href="?page=<?php echo $i; ?>"><?php echo $i; ?></a></li>
                <?php endfor; ?>

                <?php if ($page < $total_pages): ?>
                    <li><a href="?page=<?php echo $page + 1; ?>">Next</a></li>
                <?php endif; ?>
            </ul>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>
