<?php
    session_start();
   include "../../db.php";
   date_default_timezone_set('Asia/Kolkata');
      $currentDate = date('jS  F Y');
      $currentDT = 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 id="page-top" class="mt-55">

  <!--==========================
  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: 70px">Category</h1>
                            <hr>
<form method='post' action='' enctype='multipart/form-data'>
 <b>Category Name :-</b>   <br> <br>
 <input type="text" class="form-control mb-2" id="cat" placeholder="enter the Category" name="cat"><br>
    <input type='submit' value='Submit' class="btn btn-primary" name='upload'>  
    <a href='form.php' class="btn btn-warning btn-user m-2"> <i class="fas fa-ban"></i> Back </a>  
</form>
 



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

    <hr>

    <div class="container m t-5">
            <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" >Your Category list</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>No.</th>
                                                    <th>Category</th>
                                                    <th>Action</th>
                                                </tr>
                                            </thead>
                                            <tbody>
                                                <?php
                                                    $sql = "SELECT * FROM cat ORDER BY sl DESC";
                                                    $run = mysqli_query($con, $sql);

                                                    $c = 1;
                                                    while ( $rows = mysqli_fetch_assoc($run) ) {
                                                                                                            
                                                       echo "
                                                        <tr>
                                                            <td>
                                                                <span class='badge badge-info badge-pill'>$c</span>
                                                            </td>
                                                            <td>
                                                            $rows[category]
                                                            </td>
                                                            
                                                        ";
                                                        ?>
                                                            <td class='text-center align-middle'>
                                                               
                                                                <a href='cat.php?del_id=<?php echo $rows["sl"]; ?>' 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>



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

 <!--==========================
    Footer
  ============================-->

  
  <!-- #footer -->

    
 
     <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 src="js/mobile-nav/mobile-nav.js"></script>

       <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>
        $(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>


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

 $ins_sql = "INSERT INTO cat (category, type) VALUES ('$_POST[cat]','bill_category')";
 if( mysqli_query($con, $ins_sql) ){
        ?>
        <script>
            alert("Details Sent Successfully");
            window.location = "cat.php";
        </script>
        <?php
    }
    else{
        ?>
        <script>
            alert("Something's Wrong");
            window.location = "index.php";
        </script>
        <?php
    }
  
  
   }
?>
<?php
  if ( isset($_GET["del_id"]) ) {

        $del_sql = "DELETE FROM cat WHERE sl = '$_GET[del_id]'";

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

   
?>
