<?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>
 <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>Write a Blog</title>

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


    <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="vendor/croppie/croppie.js"></script>
    <script src="js/custom.js"></script>

    <script src="vendor/ckeditor.js"></script>

    <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/topbar.php"; ?>
    <div class="">
        <h3 class="text-center mb-4" style="margin-top: 80px"><font color="black"> Sylabus</h3>
        <input type="hidden" name="bref" id="bref" value="<?php echo $bref; ?>">
        <div class="card">
            <div class="card-body">
               
                          
        <?php
        
if(isset($_GET['edit_id'])){
                 $edit_run = mysqli_query($con, "SELECT * FROM sylabus WHERE s_sl = '$_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['s_title']; ?>' required>
                                </div>
                            </div>
                                <font color="black">Description</font>
                            <textarea name="content" id="content" class="form-control ckeditor" required><?php echo $edit_row['s_content']; ?></textarea>
                  
                          
                            <button type="submit" class="btn btn-success my-4"  name="up_submit"> <i class="fas fa-cloud-upload-alt"></i> Update </button>
                             <?php //  echo "<a href='active_patient.php?see_id=$_GET[b_id]' class='btn btn-warning my-4'>Back </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" required>
                                </div>
                            </div>
                                <font color="black">Description</font>
                            <textarea name="content" id="content" class="form-control ckeditor" required></textarea>
                  
                          
                            <button type="submit" class="btn btn-success my-4"  name="s_submit"> <i class="fas fa-cloud-upload-alt"></i> Update </button>
                             <?php //  echo "<a href='active_patient.php?see_id=$_GET[b_id]' class='btn btn-warning my-4'>Back </a> ";?>                                                                              
                        </form>


    <?php  }

     ?> 
                       
                        <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>Action</th>
                                                </tr>
                                            </thead>
                                            <tbody>
                                                <?php
                                                    $sql = "SELECT * FROM sylabus WHERE s_by = '$_SESSION[uid]' ORDER BY s_sl DESC";
                                                    $run = mysqli_query($con, $sql);

                                                    $c = 1;
                                                    while ( $rows = mysqli_fetch_assoc($run) ) {
                                                                                                            
                                                       echo "
                                                        <tr id='$rows[s_sl]'>
                                                            <td>
                                                                <span class='badge badge-info badge-pill'>$c</span>
                                                            </td>
                                                            <td>
                                                            $rows[s_title] <br>
                                                            
                                                            </td>
                                                            
                                                        ";
                                                        ?>
                                                            <td class='text-center align-middle'>
                                                               
                                                                <a href='syllabus.php?del_id=<?php echo $rows["s_sl"]; ?>' class='delete m-2 btn btn-danger btn-sm rounded-circle' title='Click to Delete'> <i class='fas fa-trash'></i> </a>

                                                                <a href='syllabus.php?edit_id=<?php echo $rows["s_sl"]; ?>' class='delete m-2 btn btn-success btn-sm rounded-circle' title='Click to Edit'> <i class='fas fa-pencil-alt'></i> </a>
                                                            </td>
                                                        </tr>
                                                        <?php
                                                        $c++;
                                                    }
                                                ?>
                                            </tbody>
                                        </table>
                                    </div>
                                </div>
                            </div>
                      
                        
            </div>
        </div>
    </div>
</body>
</html>



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



   
</script>


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



   
</script>
      


<?php 

if(isset($_POST['s_submit'])){
       
       
        $sql = "INSERT INTO sylabus (s_title, s_content , s_by) VALUES ('$_POST[title]', '$_POST[content]', '$_SESSION[uid]')";


   

        if(mysqli_query($con, $sql)){
            ?>
            <script>
                alert("Record updated successfully");
            </script>
            <?php
        }
        else{
            ?>
            <script>
                alert('OOPS !!!!');
                setTimeout(() => {
                    window.location = 'syllabus.php';
                }, 500);
            </script>
            <?php
        }
    }


    if(isset($_POST['up_submit'])){
       
 
    $up_sql = "UPDATE sylabus SET s_title = '$_POST[title]', s_content = '$_POST[content]'   WHERE s_sl = '$_GET[edit_id]'";

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