<?php
session_start();
ob_start();
error_reporting(E_ALL);
ini_set('display_errors', 1);
include "../include/config.php";

if (!isset($_SESSION['email'])) {
    header("location:../index.php");
    exit;
}

$email = $_SESSION['email'];

// Handle video upload
$message = "";
if (isset($_POST['submit'])) {
    $title = $_POST['title'];
    $video_link = $_POST['video_link'];
    $program = $_POST['program'];
    $intake = $_POST['intake'];
    $year = $_POST['year'];

    // Upload thumbnail if provided
    $thumbnail = "";
    if (!empty($_FILES['thumbnail']['name'])) {
        $thumbnail = time() . "_" . basename($_FILES['thumbnail']['name']);
        move_uploaded_file($_FILES['thumbnail']['tmp_name'], "../../students/video_image/" . $thumbnail);
    }

    $sql = "INSERT INTO videos (title, video_link, program, intake, year, thumbnail) 
            VALUES ('$title', '$video_link', '$program', '$intake', '$year', '$thumbnail')";

    if ($conn->query($sql)) {
        $message = "<p style='color:green;'>Video posted successfully!</p>";
    } else {
        $message = "<p style='color:red;'>Error: " . $conn->error . "</p>";
    }
}

// Fetch posted videos
$videos = $conn->query("SELECT id, thumbnail, title, video_link, program, intake, year, date FROM videos ORDER BY date DESC");

?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Post Videos</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css">
<style>
body{
    width:100%;
    padding:0px;
    margin:0px;
}
.content { margin-left:210px; padding:20px !important}
.video-thumb { width:120px; height:80px; object-fit:cover; border-radius:6px; }
</style>
</head>
<body>
<?php include "../include/nav.php"; ?>
<br><br><br>
<div class="content" style="padding: 20px; background: #f8f9fa; min-height: 100vh;">
    
    <h3 style="color: var(--primary); font-weight: bold; margin-bottom: 25px; padding-bottom: 10px; border-bottom: 2px solid #e9ecef;">Post New Video</h3>
    
    <?php 
    if (isset($message) && !empty($message)) {
        echo '<div class="alert alert-info" style="background: #d1ecf1; color: #0c5460; padding: 12px; border-radius: 5px; margin-bottom: 20px; border-left: 4px solid #0c5460;">' . $message . '</div>';
    }
    ?>
    
    
    <form method="POST" enctype="multipart/form-data" class="mb-4" style="background: white; padding: 25px; border-radius: 8px; box-shadow: 0 2px 10px rgba(0,0,0,0.05);">
        <div class="row g-3">
            <div class="col-md-6">
                <label class="form-label" style="font-weight: 600; color: #495057; margin-bottom: 8px;">Title</label>
                <input type="text" name="title" class="form-control" required style="padding: 12px; border-radius: 6px; border: 1px solid #ced4da;">
            </div>
            <div class="col-md-6">
                <label class="form-label" style="font-weight: 600; color: #495057; margin-bottom: 8px;">Video Link</label>
                <input type="text" name="video_link" class="form-control" placeholder="YouTube/Vimeo URL" required style="padding: 12px; border-radius: 6px; border: 1px solid #ced4da;">
            </div>
            <div class="col-md-4">
                <label class="form-label" style="font-weight: 600; color: #495057; margin-bottom: 8px;">Year</label>
                <select name="year" class="form-control" required style="padding: 12px; border-radius: 6px; border: 1px solid #ced4da; height: 46px;">
                    <option value="">Select Year</option>
                    <?php
                    $query = mysqli_query($conn, "SELECT * FROM year ORDER BY id DESC");
                    while($row = mysqli_fetch_array($query)) { ?>
                        <option value="<?php echo $row['year']; ?>"><?php echo $row['year']; ?></option>
                    <?php } ?>
                </select>
            </div>
            <div class="col-md-4">
                <label class="form-label" style="font-weight: 600; color: #495057; margin-bottom: 8px;">Intake</label>
                <select name="intake" class="form-control" required style="padding: 12px; border-radius: 6px; border: 1px solid #ced4da; height: 46px;">
                    <option value="">Select Intake</option>
                    <?php
                    $query = mysqli_query($conn, "SELECT * FROM intake ORDER BY id DESC");
                    while($row = mysqli_fetch_array($query)) { ?>
                        <option value="<?php echo $row['intake']; ?>"><?php echo $row['intake']; ?></option>
                    <?php } ?>
                </select>
            </div>
            <div class="col-md-4">
                <label class="form-label" style="font-weight: 600; color: #495057; margin-bottom: 8px;">Program</label>
                <select name="program" class="form-control" style="padding: 12px; border-radius: 6px; border: 1px solid #ced4da; height: 46px;">
                    <option value="">Select Program</option>
                    <?php
                    $query = mysqli_query($conn, "SELECT * FROM programs ORDER BY id DESC");
                    while($row = mysqli_fetch_array($query)) { ?>
                        <option value="<?php echo $row['program']; ?>"><?php echo $row['program']; ?></option>
                    <?php } ?>
                </select>
            </div>
            <div class="col-md-6">
                <label class="form-label" style="font-weight: 600; color: #495057; margin-bottom: 8px;">Thumbnail (optional)</label>
                <input type="file" name="thumbnail" class="form-control" style="padding: 10px; border-radius: 6px; border: 1px solid #ced4da;">
            </div>
        </div>
        <button type="submit" name="submit" class="btn mt-3" style="background: var(--primary); color: white; padding: 12px 25px; border-radius: 6px; border: none; font-weight: 600; margin-top: 20px !important;">
            Post Video
        </button>
    </form>

    <h3 style="color: var(--primary); font-weight: bold; margin: 30px 0 20px; padding-bottom: 10px; border-bottom: 2px solid #e9ecef;">Posted Videos</h3>
    
    <div style="background: white; padding: 20px; border-radius: 8px; box-shadow: 0 2px 10px rgba(0,0,0,0.05); overflow-x: auto;">
        <table style="width: 100%; border-collapse: collapse;">
            <thead>
                <tr style="background: #f8f9fa;">
                    <th style="padding: 15px; text-align: left; font-weight: 600; color: #495057; border-bottom: 2px solid #e9ecef;">Thumbnail</th>
                    <th style="padding: 15px; text-align: left; font-weight: 600; color: #495057; border-bottom: 2px solid #e9ecef;">Title</th>
                    <th style="padding: 15px; text-align: left; font-weight: 600; color: #495057; border-bottom: 2px solid #e9ecef;">Link</th>
                    <th style="padding: 15px; text-align: left; font-weight: 600; color: #495057; border-bottom: 2px solid #e9ecef;">Program</th>
                    <th style="padding: 15px; text-align: left; font-weight: 600; color: #495057; border-bottom: 2px solid #e9ecef;">Intake</th>
                    <th style="padding: 15px; text-align: left; font-weight: 600; color: #495057; border-bottom: 2px solid #e9ecef;">Year</th>
                    <th style="padding: 15px; text-align: left; font-weight: 600; color: #495057; border-bottom: 2px solid #e9ecef;">Date Posted</th>
                </tr>
            </thead>
            <tbody>
                <?php 
                if ($videos->num_rows > 0) {
                    while($row = $videos->fetch_assoc()) { 
                ?>
                <tr style="border-bottom: 1px solid #e9ecef;">
                    <td style="padding: 15px;">
                        <?php if (!empty($row['thumbnail'])) { ?>
                            <img src="../../students/video_image/<?php echo $row['thumbnail']; ?>" style="width: 80px; height: 60px; object-fit: cover; border-radius: 4px;">
                        <?php } else { ?>
                            <img src="https://via.placeholder.com/80x60?text=No+Image" style="width: 80px; height: 60px; object-fit: cover; border-radius: 4px;">
                        <?php } ?>
                    </td>
                    <td style="padding: 15px; color: #495057;"><?php echo htmlspecialchars($row['title']); ?></td>
                    <td style="padding: 15px;">
                        <a href="<?php echo $row['video_link']; ?>" target="_blank" style="color: var(--primary); text-decoration: none; font-weight: 500;">
                            Watch <i class="fa fa-external-link" style="font-size: 12px;"></i>
                        </a>
                    </td>
                    <td style="padding: 15px; color: #495057;"><?php echo htmlspecialchars($row['program']); ?></td>
                    <td style="padding: 15px; color: #495057;"><?php echo htmlspecialchars($row['intake']); ?></td>
                    <td style="padding: 15px; color: #495057;"><?php echo htmlspecialchars($row['year']); ?></td>
                    <td style="padding: 15px; color: #6c757d;"><?php echo date("M d, Y", strtotime($row['date'])); ?></td>
                </tr>
                <?php 
                    } 
                } else { 
                ?>
                <tr>
                    <td colspan="7" style="padding: 30px; text-align: center; color: #6c757d; font-style: italic;">
                        No videos posted yet
                    </td>
                </tr>
                <?php } ?>
            </tbody>
        </table>
    </div>
</div>
</body>
</html>
