<?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");
?>
<!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>

  <!--==========================
  Header
  ============================-->
   <?php include "includes/header2.php"; ?>
  
  <!-- #header -->

  <!--==========================
    Intro Section
  ============================-->
<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: 90px">Category</h1>
              <br>
            <p class="h4 mb-2 text-gray-800 text-center"> As a  website Must Create 2 Category <br>
              Blog <br>
              Activity</p>
                            <hr>

                            <?php
                                if ( isset($_GET["edit_id"]) ) {
                                    $edit_run = mysqli_query($con, "SELECT cname FROM category WHERE cid = '$_GET[edit_id]'");
                                    $edit_row = mysqli_fetch_assoc($edit_run);
                                    ?>
                                    <form class="user" method="post" enctype="multipart/form-data">
                                        <h4 class="mb-3 badge badge-secondary">Edit Category</h4>
                                        <div class="form-group row">
                                            <div class="col-sm-12 mb-1 mb-sm-0">
                                                <input type="text" class="form-control form-control-sm mb-2" id="cname" placeholder="Name of Category *" name="cname" value="<?php echo $edit_row["cname"]; ?>" required>
                                            </div>
                                        </div>
                                        <button type="submit" class="btn btn-success btn-user m-2" name="edit_cat">
                                            <i class="far fa-image"></i> Update Category
                                        </button>
                                        <a href="category.php" class="btn btn-warning btn-user m-2"> <i class="fas fa-ban"></i> Cancel </a>
                                    </form>
                                    <?php
                                } else {
                                    ?>
                                    <form class="user" method="post" enctype="multipart/form-data">
                                        <h4 class="mb-2 badge badge-secondary" data-toggle="collapse" data-target="#target" style="cursor:pointer;">Add New Category</h4>
                                        <p class="small"> <i class="fas fa-hand-point-up"></i> Tap Here to add New Category </p>
                                        <div class="collapse" id="target">
                                            <div class="form-group row">
                                                <div class="col-sm-12 mb-1 mb-sm-0">
                                                    <input type="text" class="form-control form-control-sm mb-2" id="cname" placeholder="Enter Category Name *" name="cname" required>
                                                </div>
                                            </div>
                                            <button type="submit" class="btn btn-primary btn-user m-2" name="new_category">
                                                <i class="fas fa-user-plus"></i> Add New Category
                                            </button>
                                            <a href="#target" data-toggle="collapse" class="btn btn-warning btn-user m-2"> <i class="fas fa-ban"></i> Cancel </a>
                                        </div>
                                    </form>
                                    <?php
                                }
                            ?>
                        </div>
                    </div>
                </div>
            </div>


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

                            <div class="row m-0">
                                <h4 class="mb-3 badge badge-secondary">Categories</h4>

                                <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>Category Index</th>
                                                    <th>Action</th>
                                                </tr>
                                            </thead>
                                            <tbody>
                                                <?php
                                                    $sql = "SELECT * FROM category WHERE catby = '$_SESSION[uid]' ORDER BY cpositon ASC";
                      
                          
                                                    $run = mysqli_query($con, $sql);

                                                    $c = 1;
                                                    while ( $rows = mysqli_fetch_assoc($run) ) {
                                                        echo "
                                                        <tr id='$rows[cid]' >
                                                            <td>
                                                               <span class='badge badge-info badge-pill'>$c</span>
                                                                $rows[cname]
                                                            </td>
                                                            <td class='text-center align-middle'>
                                                                <a href='category.php?edit_id=$rows[cid]' class='m-2 btn btn-success btn-sm rounded-circle' title='Click to Edit'> <i class='fas fa-pencil-alt'></i> </a>
                                                            <a href='category.php?del_id=$rows[cid]' 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><br><br><br><br>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
            </div>

 <!--==========================
    Footer
  ============================-->
    <?php include "includes/ft.php"; ?>
  
  <!-- #footer -->
   
<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("category_position.php",{value:parameters},function(result){
                    console.log("Position Complete");
                });
            }
        });
  </script>
   

</body>
</html>



 
<?php
    if(isset($_POST['new_category'])){
        $cname = mysqli_real_escape_string($con, strip_tags($_POST['cname']));

        $ins_sql = "INSERT INTO category (cname, catby) VALUES ('$cname', '$_SESSION[uid]')";

        if(mysqli_query($con, $ins_sql)){
            ?>
            <script>window.location = 'category.php';</script>
            <?php
        }
        else{
            ?>
            <script>
                alert('OOPS !!!!');
                setTimeout(() => {
                    window.location = 'category.php';
                }, 1200);
            </script>
            <?php
        }
    }
    if ( isset($_GET["del_id"]) ) {

        $del_sql = "DELETE FROM category WHERE cid = '$_GET[del_id]'";

        if ( mysqli_query($con, $del_sql) ) {
            ?>
            <script>window.location = "category.php";</script>
            <?php
        } else {
            ?>
            <script>
                alert('OOPS !!!!');
                setTimeout(() => {
                    window.location = 'category.php';
                }, 500);
            </script>
            <?php
        }
        
    }
    if(isset($_POST['edit_cat'])){
        $cname = mysqli_real_escape_string($con, strip_tags($_POST['cname']));
        $up_sql = "UPDATE category SET cname = '$cname' WHERE cid = '$_GET[edit_id]'";

        if(mysqli_query($con, $up_sql)){
            ?>
            <script>window.location = 'category.php';</script>
            <?php
        }
        else{
            ?>
            <script>
                alert('OOPS !!!!');
                setTimeout(() => {
                    window.location = 'category.php?edit_id=<?php echo $_GET["edit_id"]; ?>';
                }, 500);
            </script>
            <?php
        }
    }
?>