<?php include_once('header.php'); ?>
<?php
$action = '';
$video_id = '';
if (isset($_GET['action']) && isset($_GET['video_id'])) {
    $action = $_GET['action'];
    $video_id = $_GET['video_id'];
}
?>
<style type="text/css">
    .error {
        border-color: #ecbeb6;
        color: #b55351;
        text-transform: capitalize;
        text-align: left !important;
    }

    #error {
        width: 100%;
    }

    .alert {
        width: 100%;
        text-align: left;
    }
</style>
<div class="container mt-3">
    <div class="card-deck mb-3 text-center">
        <div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pb-2 mb-3 mt-3 border-bottom" style="width: 100%;">
            <h1 class="h2">Videos Management</h1>
            <div class="btn-toolbar mb-2 mb-md-0">
                <div class="btn-group mr-2">
                    <?php if($action == ''){ ?>
                    <button type="button" class="btn btn-sm btn-primary" onclick="scrollToForm()">Add Video</button>
                    <?php } else { ?>
                    <a class="btn btn-sm btn-outline-secondary" href="videos.php">Back to List</a>
                    <?php } ?>
                </div>
            </div>
        </div>

        <div id="error"></div>

        <div class="row text-left">
            <div id="live_data" class="col-md-8"></div>
            <div class="col-md-4">
                <?php if($action == ''){ ?>
                <h4 style="text-align: left;" id="formTitle">Add Video</h4><br>
                <div id="err"></div>
                <form class="needs-validation" style="text-align: left;" id="add-video-form" method="post" enctype="multipart/form-data" action="sql_statements.php">
                    <div class="row">
                        <div class="col-md-12 mb-3">
                            <label for="title" style="text-align:left; float:left">Video Title</label>
                            <input type="text" class="form-control" name="title" required>
                        </div>
                        <div class="col-md-12 mb-3">
                            <label for="category" style="text-align:left; float:left">Category</label>
                            <select class="form-control" name="category" required>
                                <option value="">Select Category</option>
                                <option value="Rooms">Rooms</option>
                                <option value="Restaurant">Restaurant</option>
                                <option value="Events">Events</option>
                                <option value="Other">Other</option>
                            </select>
                        </div>
                        <div class="col-md-12 mb-3">
                            <label for="video_file" style="text-align:left; float:left">Select Video (MP4, AVI, MOV, MKV, FLV, WMV)</label>
                            <input type="file" class="form-control" id="video_file" name="video_file" accept="video/*" required>
                            <small class="form-text text-muted">Max file size: 500MB</small>
                        </div>
                        <div class="col-md-12 mb-3">
                            <button class="btn btn-primary btn-md form-control" type="submit" name="add_video" id="add_video" style="float: left;">
                                Add Video
                            </button>
                        </div>
                    </div>
                </form>
                <?php } else if($action == 'update'){ ?>
                <h4 style="text-align: left;">Update Video <a href="videos.php" class="btn btn-primary btn-sm" style="float:right;"> Videos List</a></h4><br>
                <?php 
                    $video = $con->getRows('videos', array('where' => 'id="' . $video_id . '"', 'return_type' => 'single'));
                    if(!empty($video)){
                ?>
                <div id="err"></div>
                <form class="needs-validation" style="text-align: left;" id="update-video-form" method="post" enctype="multipart/form-data" action="sql_statements.php">
                    <div class="row">
                        <div class="col-md-12 mb-3">
                            <label for="title" style="text-align:left; float:left">Video Title</label>
                            <input type="text" class="form-control" name="title" value="<?=$video['title']?>" required>
                        </div>
                        <div class="col-md-12 mb-3">
                            <label for="description" style="text-align:left; float:left">Description</label>
                            <textarea class="form-control" name="description" rows="3" required><?=$video['description']?></textarea>
                        </div>
                        <div class="col-md-12 mb-3">
                            <label for="category" style="text-align:left; float:left">Category</label>
                            <select class="form-control" name="category" required>
                                <option value="Volver" <?=$video['category']=='Volver'?'selected':''?>>Volver</option>
                                <option value="Other" <?=$video['category']=='Other'?'selected':''?>>Other</option>
                            </select>
                        </div>
                        <div class="col-md-12 mb-3">
                            <label for="video_file" style="text-align:left; float:left">Current Video</label>
                            <video width="100%" height="180" controls style="border: 1px solid #ddd; margin-bottom: 10px; border-radius: 4px;">
                                <source src="../../../assets/uploads/videos/<?=$video['file_path']?>" type="video/mp4">
                                Your browser does not support the video tag.
                            </video>
                        </div>
                        <div class="col-md-12 mb-3">
                            <label for="video_file" style="text-align:left; float:left">Update Video (Leave empty to keep current)</label>
                            <input type="file" class="form-control" id="video_file" name="video_file" accept="video/*">
                            <input type="hidden" name="current_video" value="<?=$video['file_path']?>">
                        </div>
                        <div class="col-md-12 mb-3">
                            <input type="hidden" name="video_id" value="<?=$video['id']?>">
                            <button class="btn btn-primary btn-md form-control" type="submit" name="update_video" id="update_video" style="float: left;">
                                Update Video
                            </button>
                        </div>
                    </div>
                </form>
                <?php } } ?>
            </div>
        </div>
    </div>
</div>

<!-- Bootstrap core JavaScript -->
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"></script>
<script src="../../js/vendor/popper.min.js"></script>
<script src="../../js/bootstrap.min.js"></script>
<script src="../../js/vendor/holder.min.js"></script>
<script type="text/javascript" src="../../js/jquery-3.2.1.min.js"></script>
<script type="text/javascript" src="../../js/validation.min.js"></script>
<script src="../../js/tables/jquery.dataTables.js"></script>
<script src="../../js/tables/dataTables.bootstrap4.js"></script>

<script>
    function fetchData() {
        $.ajax({
            url: "select-videos.php",
            method: "GET",
            success: function(data) {
                $('#live_data').html(data);
            }
        });
    }

    function scrollToForm() {
        document.getElementById('formTitle').scrollIntoView({ behavior: 'smooth' });
    }

    $(document).ready(function() {
        fetchData();

        $("#add-video-form").validate({
            rules: {
                title: {
                    required: true
                },
                video_file: {
                    required: true
                },
                description: {
                    required: true
                },
                category: {
                    required: true
                }
            },
            messages: {
                title: "Please enter video title",
                video_file: "Please select a video",
                description: "Please enter description",
                category: "Please select a category"
            },
            submitHandler: function(form) {
                var formData = new FormData(form);
                formData.append('add_video', '1');
                $.ajax({
                    url: form.action || 'sql_statements.php',
                    type: form.method,
                    data: formData,
                    success: function(data) {
                        if(data == '1') {
                            alert('Video added successfully!');
                            form.reset();
                            fetchData();
                        } else {
                            alert('Error adding video: ' + data);
                        }
                    },
                    error: function(xhr) {
                        alert('Error: ' + xhr.status);
                    },
                    cache: false,
                    contentType: false,
                    processData: false
                });
                return false;
            }
        });

        $("#update-video-form").validate({
            rules: {
                title: {
                    required: true
                },
                description: {
                    required: true
                },
                category: {
                    required: true
                }
            },
            messages: {
                title: "Please enter video title",
                description: "Please enter description",
                category: "Please select a category"
            },
            submitHandler: function(form) {
                var formData = new FormData(form);
                formData.append('update_video', '1');
                $.ajax({
                    url: form.action || 'sql_statements.php',
                    type: form.method,
                    data: formData,
                    success: function(data) {
                        if(data == '1') {
                            alert('Video updated successfully!');
                            window.location.href = 'videos.php';
                        } else {
                            alert('Error updating video: ' + data);
                        }
                    },
                    error: function(xhr) {
                        alert('Error: ' + xhr.status);
                    },
                    cache: false,
                    contentType: false,
                    processData: false
                });
                return false;
            }
        });

        $(document).on('click', '.btn_delete', function(e) {
            if (confirm("Are you sure you want to delete this video?")) {
                var id = $(this).attr('data-id3');
                $.ajax({
                    url: "sql_statements.php",
                    type: "POST",
                    data: { action: 'delete_video', id: id },
                    success: function(data) {
                        if(data == '1') {
                            alert('Video deleted successfully!');
                            fetchData();
                        } else {
                            alert('Error deleting video');
                        }
                    },
                    error: function(xhr) {
                        alert('Error: ' + xhr.status);
                    }
                });
            }
        });

        $(document).on('click', '.btn_show', function(e) {
            var id = $(this).attr('data-id3');
            $.ajax({
                url: "sql_statements.php",
                type: "POST",
                data: { action: 'show_video', id: id },
                success: function(data) {
                    if(data == '1') {
                        fetchData();
                    }
                }
            });
        });

        $(document).on('click', '.btn_hide', function(e) {
            var id = $(this).attr('data-id3');
            $.ajax({
                url: "sql_statements.php",
                type: "POST",
                data: { action: 'hide_video', id: id },
                success: function(data) {
                    if(data == '1') {
                        fetchData();
                    }
                }
            });
        });
    });
</script>
