<?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>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="description" content="">
    <meta name="author" content="">

    <title>Admin Panel</title>

    <link href="css/all.min.css" rel="stylesheet" type="text/css">
    <link href="css/sb-admin-2.min.css" rel="stylesheet">
    <link href="css/alltable.css" rel="stylesheet">
	<link rel="stylesheet" href="vendor/croppie/croppie.css" />
    <link href="css/style.css" rel="stylesheet">

    <style>
        #uploadimageModal .modal-dialog{
            max-width: 100% !important;
        }
    </style>
</head>
<body id="page-top" class="mt-55">

<div class="overlay"></div>

    <div id="wrapper">

        <div id="content-wrapper" class="d-flex flex-column">

            <div id="content">

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

                <div class="container-fluid px-0">

                    <div class="card shadow mb-4">
                        <div class="card-body">

                        
                            <h1 class="h3 mb-2 text-gray-800 text-center">Placement</h1>
                            <hr>
              <form method='post' action='' enctype='multipart/form-data'>
                  <b>Company Name : </b> <br>
                 <input type="text" class="form-control mb-2" placeholder="Enter Company Name" name="cname" required> <br> 
                 <b>Date</b> <br>
                 <input type="date" class="form-control mb-2" placeholder=" " name="dt" required> <br>
                 <b>PassOut Year : </b> <br>
                 <SELECT NAME="pass_yr">
                        <OPTION VALUE="2018">2018</OPTION>
                        <OPTION VALUE="2019">2019</OPTION>
                        <OPTION VALUE="2020">2020</OPTION>
                </SELECT> <br> <br>
                  <b>Company Logo : </b> <br>
                 <input type='file' name='image' class="form-control"><br>


                <input type='submit' value='Submit' class="btn btn-primary" name='upload'>    
             </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>Image</th>
                                                    <th>Action</th>
                                                </tr>
                                            </thead>
                                            <tbody>
                                                <?php
                                                    $sql = "SELECT * FROM placement WHERE p_by = '$_SESSION[uid]' ORDER BY p_date DESC";
                                                    $run = mysqli_query($con, $sql);

                                                    $c = 1;
                                                    while ( $rows = mysqli_fetch_assoc($run) ) {
                                                                                                            
													   echo "
                                                        <tr id='$rows[p_sl]'>
                                                            <td>
                                                                <span class='badge badge-info badge-pill'>$c</span>
                                                            </td>
                                                            <td>
                                                            <b>Company Name: </b>$rows[p_name] <br>
                                                            
                                                            <b>Date: </b>$rows[p_date] <br>
                                                            </td>
                                                            <td>
                                                                <img src='$rows[p_icon]' height='50' data-toggle='modal' data-target='#show-img' class='showModal'>
                                                            </td>
                                                        ";
                                                        ?>
                                                            <td class='text-center align-middle'>
                                                               
                                                                <a href='placement.php?del_id=<?php echo $rows["p_sl"]; ?>&imgpath=<?php echo $rows["p_icon"]; ?>' 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>

            </div>

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

        </div>

    </div>











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

  
</body>
</html>
<?php
    
   if(isset($_POST['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/training/'.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.";
        }
    
    

   $dt = mysqli_real_escape_string($con, strip_tags($_POST['dt']));  
   $pass_yr = mysqli_real_escape_string($con, strip_tags($_POST['pass_yr']));  
   $cname = mysqli_real_escape_string($con, strip_tags($_POST['cname']));

 $ins_sql = "INSERT INTO placement (p_name, p_icon, p_by, p_date, p_pass_yr) VALUES ('$cname','$new_profle_pic','$_SESSION[uid]','$dt','$pass_yr')";
 if( mysqli_query($con, $ins_sql) ){
        ?>
        <script>
            alert("Details Sent Successfully");
            window.location = "placement.php";
        </script>
        <?php
    }
    else{
        ?>
        <script>
            alert("Something's Wrong");
            window.location = "index.php";
        </script>
        <?php
    }
  
  
   }
?>
<?php
  if ( isset($_GET["del_id"]) ) {
           unlink($_GET["imgpath"]);
        $del_sql = "DELETE FROM placement WHERE p_sl = '$_GET[del_id]'" ;

        if ( mysqli_query($con, $del_sql) ) {
            ?>
            <script>window.location = "placement.php";</script>
            <?php
        } else {
            ?>
            <script>
                alert('OOPS !!!!');
                setTimeout(() => {
                    window.location = 'placement.php';
                }, 1200);
            </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);

    }

?>