<?php
    session_start();
    include "../../db.php";
    date_default_timezone_set('Asia/Kolkata');
      $currentTime = date('jS  F Y');

    if ( !isset($_SESSION['uid']) && !isset($_SESSION['uref']) && !isset($_SESSION['utype']) )
        header("Location: logout.php");
?>
<!DOCTYPE html>
<html>
<head>
<?php include "includes/meta.php"; ?>             
    <?php include "includes/css.php"; ?>    

  
     <link href="css/alltable.css" rel="stylesheet"> 
    <link href="css/all.min.css" rel="stylesheet" type="text/css">
	<link rel="stylesheet" href="vendor/croppie/croppie.css" />


    <style>
        .custom-file-label::after {
            height: 8.25rem;
            padding-top: 50px;
        }
        .custom-file-label-edit::after{
            height: 2.25rem;
            padding-top: 5px;
        }
        #uploadimageModal .modal-dialog{
            max-width: 100% !important;
        }
    </style>
</head>
<body>
    <?php include "includes/header2.php"; ?>
     <div class="">
        <h3 class="text-center mb-4" style="margin-top: 90px"><font color="black"> Career</font></h3>
        <hr>
        <input type="hidden" name="bref" id="bref" value="<?php echo $bref; ?>">
    </div>
    <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">

                        <?php

                            if ( isset($_GET["edit_id"]) ) {
                                    $edit_run = mysqli_query($con, "SELECT * FROM career WHERE car_id = '$_GET[edit_id]'");
                                    $edit_row = mysqli_fetch_assoc($edit_run); 

                            ?>

                            <form method="post" enctype="multipart/form-data">
                           
                           <div class="form-row">                             
                                <div class="col-md-12">
                                <font color="black">Title</font>
                                    <input type="text" name="title" class="form-control mb-3" value="<?php echo $edit_row['car_title'];?>">
                                </div>
                            </div>
                            <br>
                                <font color="black">Description</font>
                            <textarea name="content" id="content" class="form-control ckeditor" ><?php echo $edit_row["career"]; ?></textarea>
                          
                                                        
                            <button type="submit" class="btn btn-success my-4"  name="edit_cat"> <i class="fas fa-cloud-upload-alt"></i> Update </button>
                            <a href="careers.php" class="btn btn-warning my-4"> Cancel </a>                                                                                
                        </form>

                            <?php 
                            }
                            else{
                             ?>

                             <form method="post" enctype="multipart/form-data">
                           
                           <div class="form-row">                             
                                <div class="col-md-12">
                                <font color="black">Title</font>
                                    <input type="text" name="title" class="form-control mb-3">
                                </div>
                            </div>
                            <br>
                                <font color="black">Description</font>
                            <textarea name="content" id="content" class="form-control ckeditor" ></textarea>
                                                        
                            <button type="submit" class="btn btn-success my-4"  name="car_submit"> <i class="fas fa-cloud-upload-alt"></i> Update </button>
                            <a href="index.php" class="btn btn-warning my-4"> Back </a>                                                                                
                        </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">Careers</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> Index</th>
                                                   
                                                </tr>
                                            </thead>
                                            <tbody>
                                                <?php

                                                    $sql = "SELECT * FROM career WHERE car_by = '$_SESSION[uid]' ORDER BY car_date DESC";
                                                    $run = mysqli_query($con, $sql);

                                                    $c = 1;
                                                    while ( $rows = mysqli_fetch_assoc($run) ) 
                                                    {
                                                        ?>
                                                        <tr>
                                                            <td>
                                                                <span class='badge badge-info badge-pill'><?php echo $c; ?></span>
                                                            </td>
                                                            <td>
                                                              <p class='text-dark mb-0'> <b>Title: </b><?php echo "$rows[car_title]"; ?></p>
                                                              <p class='text-dark mb-0'> <b>Date: </b><?php echo "$rows[car_date]"; ?> </p>
                                                              
                                                                <?php echo "
                                                                
                                                                 <a href='careers.php?edit_id=$rows[car_id]' class='m-2 btn btn-warning btn-sm' title='Click to Edit'> <i class='fas fa-pencil-alt'></i> Edit</a>
                                                                 <a href='careers.php?del_id=$rows[car_id]' class='m-2 btn btn-danger btn-sm' title='Click to Delete'> Delete</a>
                                                                 "; 
                                                                
                                                               ?>
                                                              
                                                            </td>
                                                                                                                    
                                                        </tr>
                                                        <?php
                                                        $c++;
                                                    }
                                                ?> 
                                            </tbody>
                                        </table>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>

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

        <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="vendor/ckeditor.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>



<script>
     CKEDITOR.replace( 'content', {
        height: 300,
        filebrowserUploadUrl: "upload.php"
    });
   
</script>

      
<?php

if(isset($_POST['car_submit'])){

        $title = $_POST["title"];
        $career = $_POST["content"];
       
        
        $ins_sql = "INSERT INTO career (car_title, career, car_by, car_date) VALUES ('$title', '$career','$_SESSION[uid]', '$currentTime')";
   

        if(mysqli_query($con, $ins_sql)){
            ?>
            <script>window.location = 'careers.php';</script>
            <?php
        }
        else{
            ?>
            <script>
                alert('OOPS !!!!');
                setTimeout(() => {
                    window.location = 'careers.php';
                }, 500);
            </script>
            <?php
        }
    }

   if(isset($_POST['edit_cat'])){

         $title = $_POST["title"];
        $career = $_POST["content"];
		
       
        $up_sql = "UPDATE career SET car_title = '$title', career = '$career' WHERE car_id = '$_GET[edit_id]'";

   

        if(mysqli_query($con, $up_sql)){
            ?>
            <script>window.location = 'careers.php';</script>
            <?php
        }
        else{
            ?>
            <script>
                alert('OOPS !!!!');
                setTimeout(() => {
                    window.location = 'careers.php';
                }, 500);
            </script>
            <?php
        }
    }

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


        $del_sql = "DELETE FROM career WHERE car_id = '$_GET[del_id]'";

        if ( mysqli_query($con, $del_sql) ) {
            ?>
            <script>
                alert('Are You Want To Delete ?');
            window.location = "careers.php";
        </script>
            <?php
        } else {
            ?>
            <script>
                alert('OOPS !!!!');
                setTimeout(() => {
                    window.location = 'careers.php';
                }, 500);
            </script>
            <?php
        }
        
    }
?>