<?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");
        exit();
    }
    if ( $_SESSION['utype'] != 'master_admin' && $_SESSION['uref'] != 'fssdf4543frts' ) {
        header("Location: logout.php");
        exit();
    }

    $nav_for = $_GET['nav_for'];
?>
<!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"> 
       
 
    
</head>

<body id="page-top" class="mt-55">

  <!--==========================
  Header
  ============================-->
  
  <?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 tex t-sm-left" style="margin-top: 70px">Nav Bar</h1>
                            <hr>

<form method='post' action='' enctype='multipart/form-data'>
        <div class="row justi fy-content-center ">
             <div class="col-md-5 ">
                  <div class="mb-1 mb-sm-0">
                    Nav Name      
                      <select name="name" id="name" class="form-control mb-3" >
                          <option>Select a Heading</option>
                          <option value="Wallpaper Slider">Wallpaper Slider</option>
                          <option value="About">About</option>
                          <option value="Mission-Vission">Mission-Vission</option>
                          <option value="Category">Category</option>
                          <option value="Blog/Activity">Blog/Activity</option>
                           <option value="Admission Process">Admission Process</option>
                          <option value="Gallery">Gallery</option>
                           <option value="Gallery Video">Gallery Video</option>
                          <option value="Careers">Careers</option>
                          <option value="Contact">Contact</option>
                          <option value="Profile">Profile</option>
                          <option value="Map Link">Map Link</option>
                          <option value="Message">Message</option>
                          <option value="Career">Career</option>
                          <option value="Admission">Admission</option>
                          <option value="Service">Service</option>
                          <option value="Facility">Facility</option>
                          <option value="Doctor">Doctor</option>
                          <option value="Doctor Category">Doctor Category</option>
                          <option value="Bill Category">Bill Category</option>
                          <option value="Notice">Notice</option>
                          <option value="Appointment">Appointment</option>
                      </select>
                  </div>
              
              <br>
                    Page Link      
                      <select name="link" id="link" class="form-control mb-3" >
                          <option>Select a Page/Link</option>
                          <option value="slider.php">slider.php</option>
                          <option value="slider2.php">slider2.php</option>
                          <option value="about.php">about.php</option>
                          <option value="vission.php">vission.php</option>
                          <option value="category.php">category.php</option>
                          <option value="review-blog.php">review-blog.php</option>
                         
                           <option value="admission_procedurers.php">admission_procedurers.php</option>
                          <option value="gallery.php">gallery.php</option>
                           <option value="videos.php">videos.php</option>
                          <option value="careers.php">careers.php</option>
                          <option value="contact.php">contact.php</option>
                          <option value="profile.php">profile.php</option>
                          <option value="contact_page.php">contact_page.php</option>
                          <option value="message.php">message.php</option>
                          <option value="careers.php">careers.php</option>
                          <option value="donations.php">admission.php</option>
                          <option value="service.php">service.php</option>
                          <option value="facility.php">facility.php</option>
                          <option value="doctor.php">doctor.php</option>
                          <option value="doctor_cat.php">doctor_cat.php</option>
                          <option value="bill_cat.php">bill_cat.php</option>
                          <option value="notice.php">notice.php</option>
                          <option value="appointment.php">appointment.php</option>
                      </select>
                  </div>
        </div>
        <br>
            <button type="submit" class="btn btn-success btn-user m-2" name="submit_nav">
                <i class="far fa-image"></i> Submit
            </button>
            <a href="admin_lvl2.php?see_id=<?php echo $nav_for; ?>" class="btn btn-warning btn-user m-2"> <i class="fas fa-ban"></i> Back </a>

</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>Index</th>
                                                    <th>Action</th>
                                                </tr>
                                            </thead>
                                            <tbody>
                                                <?php
                                                    $sql = "SELECT * FROM nav WHERE navfor = '$nav_for' AND nlevel= 'lv2' ORDER BY npositon ASC";
                      
                          
                                                    $run = mysqli_query($con, $sql);

                                                    $c = 1;
                                                    while ( $rows = mysqli_fetch_assoc($run) ) {
                                                        echo "
                                                        <tr id='$rows[nid]' >
                                                            <td>
                                                               <span class='badge badge-info badge-pill'>$c</span>
                                                               
                                                            </td>
                                                            <td>
                                                                <b>$rows[nname]</b> <br>
                                                                <b>Llink-></b> $rows[nlink]
                                                            </td>
                                                            <td class='text-center align-middle'>
                                                                
                                                            <a href='nav_lv2.php?nav_for=$nav_for&del_id=$rows[nid]' class='delete m-2 btn btn-danger btn-sm rounded-circle' title='Click to Delete'> <i class='fas fa-trash'></i> </a>
   </td>
                                                        </tr>
                                                        ";
                                                        $c++;
                                                    }
                                                ?>
                                            </tbody>
                                        </table>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>

                </div>

            </div>

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



    <div class="modal fade" id="show-img" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-body">
                    <img src="" alt="sliderImage" id="image_modal" class="w-100">
                    <p class="small mt-3 mb-2" id="dimension"> <span class="font-weight-bold"> Name </span> : </p>
                    <p class="small mb-2" id="dimension"> <span class="font-weight-bold"> Dimension </span> : </p>
                    <p class="small mb-2" id="dimension"> <span class="font-weight-bold"> Size </span> : </p>
                </div>
                <div class="modal-footer">
                    <button type="close" class="btn btn-secondary" data-dismiss="modal"> Close </button>
                </div>
            </div>
        </div>
    </div>




     <script>
        function checkFileDetails() {
            var fi = document.getElementById('file');
            if (fi.files.length > 0) {
                for (var i = 0; i <= fi.files.length - 1; i++) {
                    var fileName, fileExtension, fileSize, fileType, dateModified;
                    fileName = fi.files.item(i).name;
                    fileExtension = fileName.replace(/^.*\./, '');
                    if (fileExtension == 'png' || fileExtension == 'jpg' || fileExtension == 'jpeg') {
                        readImageFile(fi.files.item(i));
                    }
                }
                function readImageFile(file) {
                    var reader = new FileReader();
                    reader.onload = function (e) {
                        var img = new Image();
                        img.src = e.target.result;
                        img.onload = function () {
                            var imgWidth = this.width;
                            var imgHeight = this.height;

                            if ( imgWidth == 1200 && imgHeight == 512 || imgWidth == 1500 && imgHeight == 640 ) {
                                var imgCheck = "<i class='fas fa-check-circle text-success'></i>";
                            } else {
                                var imgCheck = "<i class='fas fa-times-circle text-danger'></i>";
                            }
                            document.getElementById('fileInfo').innerHTML =
                                document.getElementById('fileInfo').innerHTML + '<br> ' +
                                'Name: <b>' + file.name + '</b> <br>' +
                                'Size: <b>' + Math.round((file.size / 1024)) + '</b> KB <br>' +
                                'Dimension: <b>' + imgWidth + '</b> x <b>' + imgHeight + '</b> ' + imgCheck + ' <br>';
                        }
                    };
                    reader.readAsDataURL(file);
                }
            }
        }
    </script>




    <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="js/jquery-ui-custom.min.js"></script>
    <script src="js/custom.js"></script>

     <script src="js/mobile-nav/mobile-nav.js"></script>


 <script>
    $(document).ready(function(){
            $('#thisismytable').DataTable();
            $('#show-img').on('show.bs.modal', function (e) {
                var src = $(e.relatedTarget).attr('src');
                $(this).find('.modal-body > img').attr('src', src);
            });
    });

        var $sortable = $("#thisismytable > tbody");

        $sortable.sortable({
            stop: function ( event, ui ) {
                var parameters = $sortable.sortable( "toArray" );
                $.post("slider_position.php",{value:parameters},function(result){
                    $(document).ready(function() {
                        const Toast = Swal.mixin({
                            toast: true,
                            position: 'bottom',
                            showConfirmButton: false,
                            timer: 2000
                        });
                        Toast.fire({
                            type: 'success',
                            title: result
                        });
                    });
                });
            }
        });
  </script>
<script>
        var $sortable = $("#thisismytable > tbody");

        $sortable.sortable({
            stop: function ( event, ui ) {
                var parameters = $sortable.sortable( "toArray" );
                $.post("nav_lv2_position.php",{value:parameters},function(result){
                    console.log("Position Complete");
                });
            }
        });
  </script>
</body>
</html>



<?php
    
    if(isset($_POST['submit_nav']))
    {
              

              $name = mysqli_real_escape_string($con, strip_tags($_POST['name']));
              $link = mysqli_real_escape_string($con, strip_tags($_POST['link']));

              $level = "lv2";
              

        

         $sql = "INSERT INTO nav (nname, nlink, nlevel, navfor) VALUES ('$name', '$link', '$level', '$nav_for')";

                
                if(mysqli_query($con, $sql)){
                    ?>
                    <script>
                      alert("Record updated successfully");
                    window.location = 'nav_lv2.php?nav_for=<?php echo $nav_for; ?>';
                    </script>
                    <?php
                }
                else{
                    ?>
                    <script>
                        alert('OOPS !!!!');
                        setTimeout(() => {
                            window.location = 'nav_lv2.php?nav_for=<?php echo $nav_for; ?>';
                        }, 500);
                    </script>
                    <?php
                }

    }




    if ( isset($_GET["del_id"]) ) {

        $del_sql = "DELETE FROM nav WHERE nid = '$_GET[del_id]'";

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


?>