<?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>

  <!--==========================
  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="padding-top: 60px;">Doctor Category</h1>

                            <hr>

<?php if (isset($_GET['img_id'])) {
    ?>
        <form method='post' action='' enctype='multipart/form-data'>
         <b>Upload Image For <u><?php echo $_GET['cat']; ?></u> Category :-</b>   <br> <br>
            <div class="form-group row">
            <div class="col-sm-12 mb-1 mb-sm-0">
                <div class="custom-file mb-2">
                    <input type='file' name='image' class="form-control mb-2" required>
                    <br>
                    <input type='submit' value='Submit' class="btn btn-primary" name='img_upload'>  
                      <a href='doctor_cat.php' class="btn btn-warning btn-user m-2"> <i class="fas fa-ban"></i> Back </a> 
                </div>
            </div>
        </div>

        </form>
<?php
    } 
    else{ 
    ?>
        <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='doctor_cat.php' class="btn btn-warning btn-user m-2"> <i class="fas fa-ban"></i> Back </a>  
        </form>
    <?php 
    }
    ?>

 



                 
                                    
        </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" >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>Image</th>
                                                    <th>Action</th>
                                                </tr>
                                            </thead>
                                            <tbody>
                                                <?php
                                                    $sql = "SELECT * FROM dropdown_category WHERE type ='doctor_category' AND uploadBy = '$_SESSION[uid]' ORDER BY pos ASC";
                                                    $run = mysqli_query($con, $sql);

                                                    $c = 1;
                                                    while ( $rows = mysqli_fetch_assoc($run) ) {
                                                                                                            
                                                       echo "
                                                        <tr id='$rows[sl]'>
                                                            <td>
                                                                <span class='badge badge-info badge-pill'>$c</span>
                                                            </td>
                                                            <td>
                                                            $rows[category]
                                                            </td>
                                                            
                                                        ";
                                                        ?>
                                                        <td>
                                                            
                                                            <img src="<?php echo $rows['cat_img']; ?>" height='50' >
                                                        </td>
                                                            <td class='text-center align-middle'>
                                                               <a href='doctor_cat.php?img_id=<?php echo $rows["sl"];?>&cat=<?php echo $rows["category"];?>' class='success m-2 btn btn-success btn-sm rounded-circle' title='Click to Upload Img'> Img </a>
                                                                <a href='doctor_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
  ============================-->

    
 
     <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>
    $(document).ready(function(){
            $('#thisismytable').DataTable();
            
    });


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

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



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

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

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

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

   
?>

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

       // Getting file name
       $filename = $_FILES['image']['name'];
         
       // Valid extension
       $valid_ext = array('png','jpeg','jpg');

      
     $photoExt1 = @end(explode('.', $filename)); // explode the image name to get the extension
     $phototest1 = strtolower($photoExt1);
      
     $new_profle_pic = '../upload/category/'.time().'.'.$phototest1;
      
       // Location
       $location = "".$new_profle_pic;

       // file extension
       $file_extension = pathinfo($location, PATHINFO_EXTENSION);
       $file_extension = strtolower($file_extension);

       // Check extension
       if(in_array($file_extension,$valid_ext)){  

            // Compress Image
            compressedImage($_FILES['image']['tmp_name'],$location,20);
        
        //Here i am enter the insert code in the step ........

        }
      else
        {
                echo "File format is not correct.";
        }

 
$up_sql = "UPDATE dropdown_category SET cat_img = '$new_profle_pic' WHERE sl = '$_GET[img_id]' ";

 if( mysqli_query($con, $up_sql) ){
        ?>
        <script>
            alert("Details Sent Successfully");
            window.location = "doctor_cat.php";
        </script>
        <?php
    }
    else{
        ?>
        <script>
            alert("Something's Wrong");
            window.location = "doctor_cat.php";
        </script>
        <?php
    }
  
  
   }
?>
<?php

// Compress image
    function compressedImage($source, $path, $quality) {

            $info = getimagesize($source);

            if ($info['mime'] == 'image/jpeg') 
                $image = imagecreatefromjpeg($source);

            elseif ($info['mime'] == 'image/gif') 
                $image = imagecreatefromgif($source);

            elseif ($info['mime'] == 'image/png') 
                $image = imagecreatefrompng($source);

            imagejpeg($image, $path, $quality);

    }

?>