<?php
    session_start();
    include "../../../db.php";
    date_default_timezone_set('Asia/Kolkata');
    $currentTime = date('M j, Y g:i a');

    if ( !isset($_SESSION['uid']) && !isset($_SESSION['uref']) && !isset($_SESSION['utype']) )
        header("Location: logout.php");
    
    $page_type = $_GET['sec'];
    $page_url = 'videos.php?sec='.$page_type ;
?>
<!DOCTYPE html>
<html lang="en">

<head>
    <?php include "includes/meta.php"; ?>
    <?php include "includes/css.php"; ?>
    <link href="css/all.min.css" rel="stylesheet" type="text/css">
    <link href="css/alltable.css" rel="stylesheet">
    <link rel="stylesheet" href="vendor/croppie/croppie.css" />

    <style>
    #uploadimageModal .modal-dialog {
        max-width: 100% !important;
    }
    </style>
</head>

<body>

    <?php include "includes/header2.php"; ?>


    <div class="container">
        <div class="row mx-0 just ify-content-center">
            <div class="col-12 col-sm-12 col-md-12">
                <div class="card-body">

                    <h1 class="h3 mb-2 text-gray-800 text-center" style="margin-top: 80px">Video Linking</h1>
                    <hr>
                    <form method='post' action='' enctype='multipart/form-data'>
                        <input type="text" class="form-control mb-2" id="title" placeholder="Video Title" name="title">
                        <input type='text' name='video' class="form-control"
                            placeholder="Enter Youtube Video Id , which placed at the end of the video link">
                        <br>
                        <input type='submit' value='Submit Youtube Link' class="btn btn-primary" name='v_submit'>
                    </form>
                    <BR><BR>
                    <div class="row m-0">


                        <div class="col-md-12 p-0">
                            <div class="table-responsive">
                                <table class="table table-hover table-bordered table-sm" id="thisismytable">
                                    <thead>
                                        <tr>
                                            <th>No.</th>
                                            <th>Title</th>
                                            <th>Video</th>
                                            <th>Action</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <?php
                                            $sql = "SELECT * FROM data_table  WHERE page_type = '$page_type' AND up_by = '$_SESSION[uid]' && recycle = '1' ORDER BY id DESC";
                                            $run = mysqli_query($con, $sql);

                                            $c = 1;
                                            while ( $rows = mysqli_fetch_assoc($run) ) {
                                                 echo "
                                                <tr id='$rows[id]'>
                                                    <td>
                                                        <span class='badge badge-info badge-pill'>$c</span>
                                                    </td>
                                                    <td>
                                                            $rows[title]
                                                    </td>
                                                    <td>
                                                        <iframe width='120' height='85' src='https://www.youtube.com/embed/$rows[short_des]'></iframe>
                                                    </td>
                                                ";
                                        ?>
                                        <td class='text-center align-middle'>

                                            <a href='<?php echo $page_url;?>&del_id=<?php echo $rows["id"]; ?>'
                                                class='delete m-2 btn btn-danger btn-sm rounded-circle'
                                                title='Click to Delete'> <i class='fas fa-trash'></i> </a>
                                        </td>
                                        </tr>
                                        <?php
                                                        $c++;
                                                    }
                                                ?>
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

        </div>

        </BR></BR>
    </div>

    <?php include "includes/ft.php"; ?>

    <script src="js/jquery.min.js"></script>
    <script src="js/bootstrap.min.js"></script>
    <script src="js/jquery.easing.min.js"></script>
    <script src="js/sb-admin-2.min.js"></script>
    <script src="js/sweetalert2.min.js"></script>
    <script src="js/jquery.dataTables.min.js"></script>
    <script src="js/dataTables.bootstrap4.min.js"></script>
    <script src="js/datatables.mark.min.js"></script>
    <script src="vendor/croppie/croppie.js"></script>
    <script src="js/jquery-ui-custom.min.js"></script>
    <script src="js/custom.js"></script>

    <script>
    $(document).ready(function() {
        $('#thisismytable').DataTable();
    });
    </script>
</body>

</html>
<?php
    
   if(isset($_POST['v_submit'])){

     // User Input

     $title = $_POST["title"];
     $video = $_POST["video"];

     // Auto Input

     $up_date = date("Y-m-d");

     $up_ip = $_SERVER['REMOTE_ADDR'];

     $up_by = $_SESSION['uid'] ;

     $refid = $up_by. "digix" . mt_rand(0, 9999) . substr(time(), -5);

     $ins_sql = "INSERT INTO `data_table` (`refid`, `title`, `page_type`, `short_des`, `up_by`, `up_date`, `up_ip`) VALUES ('$refid', '$title', '$page_type', '$video', '$up_by', '$up_date', '$up_ip');";
     if( mysqli_query($con, $ins_sql) ){
     ?>
<script>
alert("Details Sent Successfully");
window.location = "<?php echo $page_url;?>";
</script>
<?php
                }
                else{
                    ?>
<script>
alert("Something's Wrong");
window.location = "<?php echo $page_url;?>";
</script>
<?php
                }     
   }
?>


<?php
  if ( isset($_GET["del_id"]) ) {

        $del_sql = "DELETE FROM data_table WHERE id = '$_GET[del_id]'";

        if ( mysqli_query($con, $del_sql) ) {
            ?>
<script>
window.location = "<?php echo $page_url;?>";
</script>
<?php
        } else {
            ?>
<script>
alert('OOPS !!!!');
setTimeout(() => {
    window.location = '<?php echo $page_url;?>';
}, 1200);
</script>
<?php
        }   
    }
?>