<?php
    // session_start();
    include "../db.php";
    require '../auth.php';
    restrictAccess(['master_admin']);
    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");
        exit();
    }

    if ( $_SESSION['utype'] != 'master_admin' && $_SESSION['uref'] != 'fssdf4543frts' ) {
        header("Location: logout.php");
        exit();
    }
?>

<!DOCTYPE html>
<html lang="en">

<head>
    <?php include "include/meta.php"; ?>
    <?php include "include/css.php"; ?>
    <?php include "include/php-functions.php"; ?>
    <script src="https://cdn.ckeditor.com/4.6.2/standard-all/ckeditor.js"></script>
</head>

<body>
    <!--  Header -->
    <?php 
    include "include/header.php"; 
    ?>
    <!-- #header -->

    <section id="inner-body">
        <div class="body-content">
            <div class="container-fluid">
                <?php
                    if ( isset($_GET["edit_id"]) ) {
                    $edit_run = mysqli_query($con, "SELECT * FROM clg_course  WHERE course_id = '$_GET[edit_id]'");
                    $edit_row = mysqli_fetch_assoc($edit_run);
                    $sm=$edit_row['course_cat'];
                ?>
                <div class="card radius-10 mt-3">
                    <div class="card-header">
                        <h5>Edit Course Details: <?php echo $edit_row["course_name"]; ?></h5>
                    </div>
                    <div class="card-body mb-4">
                        <form class="user" method="post" enctype="multipart/form-data">
                            <div class="row">
                                <div class="col-md-6 mb-2">
                                    <label for="course_cat">Course Category:</label>
                                    <select name="course_cat" id="course_cat" class="form-control mb-3">
                                        <option value="<?php
                                            $sql = "SELECT cat_id FROM clg_course_cat WHERE cat_id=$sm";
                                            $run = mysqli_query($con, $sql);
                                            while ( $cat_rows2 = mysqli_fetch_assoc($run) ) {
                                                echo $cat_rows2["cat_id"];
                                            }
                                        ?>">
                                            Previous type :<?php  
                                            $sql = "SELECT cat_name FROM clg_course_cat WHERE cat_id=$sm";
                                            $run = mysqli_query($con, $sql);

                                            while ( $cat_rows1 = mysqli_fetch_assoc($run) ) {
                                                echo ''.$cat_rows1["cat_name"].'';
                                            }
                                        ?> </option>
                                        <?php   
                                        $sql = "SELECT cat_name, cat_id FROM clg_course_cat   ORDER BY cat_id ASC";
                                        $run = mysqli_query($con, $sql);
                                        while ( $cat_rows = mysqli_fetch_assoc($run) ) {
                                            echo '
                                                <option value="'.$cat_rows["cat_id"].'"> '.$cat_rows["cat_name"].' </option>
                                                ';
                                            }
                                        ?>
                                    </select>
                                </div>
                                <div class="col-md-6 mb-2">
                                    <label for="course_name">Course Name:</label>
                                    <input type="text" class="form-control mb-2" id="course_name"
                                        value="<?php echo $edit_row["course_name"]; ?>" name="course_name">
                                </div>
                                <div class="col-md-6 mb-2">
                                    <label for="course_duration">Course Duration:</label>
                                    <input type="Text" class="form-control" name="course_duration"
                                        value="<?php echo $edit_row['course_duration'];?>" required>
                                </div>
                                <div class="col-md-6 mb-2">
                                    <label for="course_eligibility" class="form-label">Course Eligibility:</label>
                                    <input type="Text" class="form-control" name="course_eligibility"
                                        value="<?php echo $edit_row['course_eligibility'];?>" required>
                                </div>
                                <div class="col-md-6 mb-3">
                                    <div class="mb-3">
                                        <label for="course_avg_fee">Course Average Fees:</label>
                                        <input type="Text" class="form-control" name="course_avg_fee"
                                            value="<?php echo $edit_row['course_avg_fee'];?>" required>
                                    </div>
                                    <div class=" mb-3">
                                        <label for="course_short_des">Course Short Description:</label>
                                        <textarea name="corse_short_des" class="form-control" id="" cols="30"
                                            rows="5"><?php echo $edit_row['corse_short_des'];?></textarea>
                                    </div>
                                </div>
                                <div class="col-md-6 mb-3">
                                    <label for="file">Course Image:</label>
                                    <div class="mb-2 d-flex justify-content-center">
                                        <img id="selectedImage"
                                            src="../upload/course/<?php echo $edit_row['course_image'];?>"
                                            alt="<?php echo $edit_row["course_name"]; ?>" style="width: 300px;" />
                                    </div>
                                    <div class="d-flex justify-content-center">
                                        <div class="btn btn-primary btn-rounded btn-sm">
                                            <label class="form-label text-white m-1" for="customFile1">Edit
                                                Image</label>
                                            <input type="file" class="form-control d-none" id="customFile1" name="file"
                                                accept="image/*"
                                                onchange="displaySelectedImage(event, 'selectedImage')" />
                                        </div>
                                    </div>
                                </div>
                                <div class="col-md-12 mt-2 mb-3">
                                    <label for="content">Course About:</label>
                                    <textarea name="content" id="content"
                                        class="form-control ckeditor"><?php echo $edit_row["course_des"]; ?></textarea>
                                </div>
                                <div class="col-md-12 mb-3">
                                    <label for="course_seotitle">Course SEO Title:</label>
                                    <input type="text" class="form-control form-control-sm mb-2" id="course_seotitle"
                                        value="<?php echo $edit_row["course_seotitle"]; ?>" name="course_seotitle">
                                </div>
                                <div class="col-md-12 mb-3">
                                    <label for="course_keyword">Course Keyword:</label>
                                    <input type="text" class="form-control form-control-sm mb-2" id="course_keyword"
                                        value="<?php echo $edit_row["course_keyword"]; ?>" name="course_keyword">
                                </div>
                                <div class="col-md-12 mb-3">
                                    <label for="content1">Course Other Details:</label>
                                    <textarea name="content1" id="content1"
                                        class="form-control ckeditor"><?php echo $edit_row["course_tag"]; ?></textarea>
                                </div>
                            </div>
                            <br>
                            <button type="submit" class="btn btn-success btn-user" name="edit_users">
                                <i class="fa fa-edit"></i> Update
                            </button>
                        </form>
                    </div>
                </div>
                <?php } elseif ( isset($_GET["vm"]) ) {
                    $count = mysqli_query($con, "UPDATE clg_course SET count = count + 1 WHERE course_id = '$_GET[vm]'");
                    if($count){
                    $notice_sql = "SELECT * FROM clg_course WHERE course_id='$_GET[vm]'";
                    $notice_data = mysqli_query($con,$notice_sql);
                    while($notice_rows = mysqli_fetch_array($notice_data))
                    {
                    ?>
                <div class="card radius-10 mt-3">
                    <div class="card-header">
                        <h5 class="m-0">Course Name:
                            <?php echo $notice_rows['course_name']; ?></h5>
                    </div>
                    <div class="card-body mb-4">
                        <div class="mb-3">
                            <h6>Course Catagory:</h6>
                            <?php 
                            $cat_sql = mysqli_query($con, "SELECT * FROM clg_course_cat WHERE cat_id='$notice_rows[course_cat]'");
                            if($cat_row = mysqli_fetch_assoc($cat_sql)){
                                $cat_name = $cat_row["cat_name"];
                            }
                            ?>
                            <?php echo $cat_name; ?>
                        </div>
                        <font color="black">
                            <?php
                                if (!empty($notice_rows['course_image']))
                                {
                                    echo "
                                        <h6>Course Image:</h6>";
                                    echo " <a href='../upload/course/$notice_rows[course_image]' title='Click to open' target='_blank'> (<b>Already Uploaded - <img src='$notice_rows[course_img]' height='50' data-toggle='modal' data-target='#show-img' class='showModal'>
                                                            </b> Click To View</a> )";
                                }
                            ?>
                        </font>
                        <br>
                        <?php if (!empty($edit_row['course_image']))
                            {
                                echo " <a href='../upload/course/$edit_row[course_image]' title='Click to open' target='_blank'> (<b>Already Uploaded - <img src='$edit_row[course_img]' height='50' data-toggle='modal' data-target='#show-img' class='showModal'>
                                                         </b> Click To View</a> )";
                            }?>
                        <br>
                        <div class="mb-2">
                            <h5>Course Duration:</h5>
                            <?php echo $notice_rows['course_duration']; ?>
                        </div>
                        <div class="mb-2">
                            <h5>Course Eligibility:</h5>
                            <?php echo $notice_rows['course_eligibility']; ?>
                        </div>
                        <div class="mb-2">
                            <h5>Course Average Fees:</h5>
                            <?php echo $notice_rows['course_avg_fee']; ?>
                        </div>
                        <div class="mb-2">
                            <h5>Course Short Description:</h5>
                            <?php echo $notice_rows['corse_short_des']; ?>
                        </div>
                        <div class="mb-2">
                            <h5>Course ABOUT:</h5>
                            <?php echo $notice_rows['course_des']; ?>
                        </div>
                        <div class="mb-2">
                            <h5>Course SEO:</h5>
                            <?php echo $notice_rows['course_seotitle']; ?>
                        </div>
                        <div class="mb-2">
                            <h5>Course KEYWORD:</h5>
                            <?php echo $notice_rows['course_keyword']; ?>
                        </div>
                        <div class="mb-2">
                            <h5>Course Other Details:</h5>
                            <?php echo $notice_rows['course_tag']; ?>
                        </div>
                        <div class="mb-2">
                            <h5>Course Uploadet By:</h5>
                            <?php echo $notice_rows['up_by']; ?>
                        </div>
                        <div class="mb-2">
                            <h5>Course Edit Date:</h5>
                            <?php echo $notice_rows['edit_date']; ?>
                        </div>
                        <div class="mb-2">
                            <h5>Course Visitor Count:</h5>
                            <?php echo $notice_rows['count']; ?>
                        </div>
                    </div>
                </div>
                <?php } } } else{?>

                <div class="card radius-10 mt-3">
                    <div class="card-header">
                        <div>
                            <h4 class="card-title mb-0">Add Course</h4>
                        </div>
                    </div>
                    <div class="card-body">
                        <button id="toggleButton" class="btn btn-primary btn-sm" onclick="toggleDiv()">Add Course
                        </button>
                        <div id="toggleDiv"
                            style="display: none; padding: 10px; border: 1px solid #ddd; margin-top: 10px;">
                            <form action="" method="POST" enctype="multipart/form-data">
                                <div class="row">
                                    <div class="col-md-6 mb-2">
                                        <label for="course_cat" class="form-label">College Course
                                            Catagory</label>
                                        <select class="form-control" name="course_cat"
                                            aria-label="Default select example" required>
                                            <option selected>Select from menu</option>
                                            <?php
                                            $sql = "SELECT * FROM clg_course_cat WHERE recycle = '1' ";
                                            $run = mysqli_query($con, $sql);
                                            $c = 1;
                                            // Check if there are any rows in the result set
                                            if (mysqli_num_rows($run) > 0) {
                                                while ($row = mysqli_fetch_assoc($run)) {
                                                    ?>
                                            <option value="<?php echo $row['cat_id']; ?>">
                                                <?php echo $row['cat_name']; ?>
                                            </option>
                                            <?php $c++;  } }?>
                                        </select>
                                    </div>
                                    <div class="col-md-6 mb-2">
                                        <label for="course_name" class="form-label">Course Name</label>
                                        <input type="Text" class="form-control" name="course_name"
                                            placeholder="Course Name" required>
                                    </div>
                                    <div class="col-md-6 mb-2">
                                        <label for="course_duration" class="form-label">Course Duration</label>
                                        <input type="Text" class="form-control" name="course_duration"
                                            placeholder="Course Duration" required>
                                    </div>
                                    <div class="col-md-6 mb-2">
                                        <label for="course_eligibility" class="form-label">Course
                                            Eligibility</label>
                                        <input type="Text" class="form-control" name="course_eligibility"
                                            placeholder="Course Eligibility" required>
                                    </div>
                                    <div class="col-md-6 mb-2">
                                        <label for="course_avg_fee" class="form-label">Course Average
                                            Fees</label>
                                        <input type="Text" class="form-control" name="course_avg_fee"
                                            placeholder="Course Average Fees" required>
                                    </div>
                                    <div class="col-md-6 mb-2">
                                        <label class="form-label" for="customFile">Course Image</label>
                                        <input type="file" accept="image/*" name="file" class="form-control"
                                            id="customFile" />
                                    </div>
                                    <div class="col-md-12 mb-2">
                                        <label class="form-label" for="course_short_des">Course Short
                                            Description</label>
                                        <textarea name="corse_short_des" class="form-control" id="" cols="30"
                                            rows="5"></textarea>
                                    </div>
                                    <div class="col-md-12 mb-2">
                                        <label for="content" class="form-label">Course About</label>
                                        <textarea name="content" id="content" class="form-control ckeditor"></textarea>
                                    </div>
                                    <div class="col-md-6 mb-2">
                                        <label for="course_seotitle" class="form-label">Course SEO
                                            Title</label>
                                        <input type="Text" class="form-control" name="course_seotitle"
                                            placeholder="Course SEO Title">
                                    </div>
                                    <div class="col-md-6 mb-2">
                                        <label for="course_keyword" class="form-label">Course SEO Keyword With
                                            coma</label>
                                        <input type="Text" class="form-control" name="course_keyword"
                                            placeholder="SEO  Keyword ">
                                    </div>
                                    <div class="col-md-12 mb-2">
                                        <label for="content1" class="form-label">Course Other
                                            Details</label>
                                        <textarea name="content1" id="content1"
                                            class="form-control ckeditor"></textarea>
                                    </div>
                                    <div class="mb-3 d-grid gap-2 d-md-flex justify-content-md-end">
                                        <button class="btn btn-success text-white me-md-2" name="addsubmit"
                                            type="submit">Submit</button>
                                    </div>
                                </div>
                            </form>
                        </div>
                    </div>
                    <!--Start Table-->
                    <div class="card-body">
                        <div class="row m-0">
                            <?php if(isset($_GET['recycle_table'])){?>
                            <div class="col-md-12 p-0">
                                <div class="d-flex justify-content-between mb-2">
                                    <div>
                                        <h5>Recycle Course List</h5>
                                    </div>
                                    <div>
                                        <a href="addcourse.php" class="btn btn-primary btn-sm">Active Course</a>
                                    </div>
                                </div>
                                <div class="table-responsive">
                                    <table class="table table-hover table-bordered table-sm" id="thisismytable">
                                        <thead>
                                            <tr>
                                                <th>No.</th>
                                                <th>Course</th>
                                                <th>Action</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            <?php
                                            $sql = "SELECT * FROM clg_course WHERE recycle = '0' ORDER BY sl_no DESC";
                                            $run = mysqli_query($con, $sql);
                                            $c = 1;
                                            while ( $rows = mysqli_fetch_assoc($run) ) {                                                                          
                                            echo "
                                            <tr id='$rows[sl_no]'>
                                                <td>
                                                    <span class='badge badge-info badge-pill text-dark'>$c</span>
                                                </td>
                                                <td>
                                                    $rows[course_name]
                                                </td>";
                                                ?>
                                            <td class='text-center align-middle'>
                                                <a class="btn btn-primary btn-sm" style="margin-left:5px;"
                                                    href="addcourse.php?vm=<?php echo $rows['course_id']; ?>">View</a>
                                                <a class="btn btn-primary btn-sm" style="margin-left:5px;"
                                                    href="addcourse.php?edit_id=<?php echo $rows['course_id']; ?>">
                                                    Edit</a>
                                                <a class="btn btn-primary btn-sm" style="margin-left:5px;"
                                                    href="tagcourse.php?course_tag=<?php echo $rows['sl_no']; ?>">
                                                    Tag</a>
                                                <a class="btn btn-primary btn-sm" style="margin-left:5px;"
                                                    href="faqcourse.php?faq=<?php echo $rows['sl_no']; ?>"> FAQ</a>

                                                <a href='addcourse.php?rec_id=<?php echo $rows["sl_no"]; ?>'
                                                    class='delete m-2 btn btn-danger btn-sm rounded-circle'
                                                    title='Click to Delete'>
                                                    <i class="fa fa-recycle" aria-hidden="true"></i>
                                                </a>
                                                <a href='addcourse.php?del_id=<?php echo $rows["sl_no"]; ?>'
                                                    class='delete m-2 btn btn-danger btn-sm rounded-circle'
                                                    title='Click to Delete'>
                                                    <i class="fa fa-trash" aria-hidden="true"></i>
                                                </a>
                                            </td>
                                            </tr>
                                            <?php $c++;}?>
                                        </tbody>
                                    </table>
                                </div>
                            </div>
                            <?php } else{ ?>
                            <div class="col-md-12 p-0">
                                <div class="d-flex justify-content-between mb-2">
                                    <div>
                                        <h5>Active Course List</h5>
                                    </div>
                                    <div>
                                        <a href="addcourse.php?recycle_table"
                                            class="btn btn-info btn-sm text-white">Recycle
                                            Course</a>
                                    </div>
                                </div>
                                <div class="table-responsive">
                                    <table class="table table-hover table-bordered table-sm" id="thisismytable">
                                        <thead>
                                            <tr>
                                                <th>No.</th>
                                                <th>Course</th>
                                                <th>Action</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            <?php
                                            $sql = "SELECT * FROM clg_course WHERE recycle = '1' ORDER BY sl_no DESC";
                                            $run = mysqli_query($con, $sql);
                                            $c = 1;
                                            while ( $rows = mysqli_fetch_assoc($run) ) {                                                                          
                                            echo "
                                            <tr id='$rows[sl_no]'>
                                                <td>
                                                    <span class='badge badge-info badge-pill text-dark'>$c</span>
                                                </td>
                                                <td>
                                                    $rows[course_name]
                                                </td>";
                                                ?>
                                            <td class='text-center align-middle'>
                                                <a class="btn btn-primary btn-sm" style="margin-left:5px;"
                                                    href="addcourse.php?vm=<?php echo $rows['course_id']; ?>">View</a>
                                                <a class="btn btn-primary btn-sm" style="margin-left:5px;"
                                                    href="addcourse.php?edit_id=<?php echo $rows['course_id']; ?>">
                                                    Edit</a>
                                                <a class="btn btn-primary btn-sm" style="margin-left:5px;"
                                                    href="tagcourse.php?course_tag=<?php echo $rows['sl_no']; ?>">
                                                    Tag</a>
                                                <a class="btn btn-primary btn-sm" style="margin-left:5px;"
                                                    href="faqcourse.php?faq=<?php echo $rows['sl_no']; ?>"> FAQ</a>
                                                <a href='addcourse.php?rec_id=<?php echo $rows["sl_no"]; ?>'
                                                    class='delete m-2 btn btn-danger btn-sm rounded-circle'
                                                    title='Click to Delete'>
                                                    <i class="fa fa-recycle" aria-hidden="true"></i>
                                                </a>
                                            </td>
                                            </tr>
                                            <?php $c++; } ?>
                                        </tbody>
                                    </table>
                                </div>
                            </div>
                            <?php } ?>
                        </div>
                    </div>
                    <!--end table-->

                </div>
                <?php }?>
            </div>
        </div>

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

    <!-- JavaScript Libraries -->

    <?php include "include/js.php"; ?>
    <script>
    function toggleDiv() {
        const div = document.getElementById('toggleDiv');
        const button = document.getElementById('toggleButton');

        if (div.style.display === 'none' || div.style.display === '') {
            div.style.display = 'block';
            button.textContent = 'Hide';
        } else {
            div.style.display = 'none';
            button.textContent = 'Add Course Catagory';
        }
    }

    function displaySelectedImage(event, elementId) {
        const selectedImage = document.getElementById(elementId);
        const fileInput = event.target;

        if (fileInput.files && fileInput.files[0]) {
            const reader = new FileReader();

            reader.onload = function(e) {
                selectedImage.src = e.target.result;
            };

            reader.readAsDataURL(fileInput.files[0]);
        }
    }
    CKEDITOR.replace('content', {
        height: 300,
        filebrowserUploadUrl: "upload.php"
    });

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

</body>

</html>


<!-- Add to database -->
<?php
  if (isset($_POST['addsubmit'])) {
    $course_cat = mysqli_real_escape_string($con, strip_tags($_POST['course_cat']));
    $course_name = mysqli_real_escape_string($con, strip_tags($_POST['course_name']));
    $course_duration = mysqli_real_escape_string($con, strip_tags($_POST['course_duration']));
    $course_eligibility = mysqli_real_escape_string($con, strip_tags($_POST['course_eligibility']));
    $course_avg_fee = mysqli_real_escape_string($con, strip_tags($_POST['course_avg_fee']));
    $corse_short_des = mysqli_real_escape_string($con, strip_tags($_POST['corse_short_des']));
  
    $course_seotitle = $_POST['course_seotitle'];
    $course_keyword = $_POST['course_keyword'];
  
    $course_tag = $_POST['content1'];
    $course_des = $_POST['content'];
    $course_id = str_pad(rand(0, 9999), 4, '0', STR_PAD_LEFT);

    $course_date = date("j F, Y, g:i a");
    $recycle = "1";
    $up_by = $_SESSION['utype'];

    // new File Upload
    $file = $_FILES["file"]["tmp_name"];
    $f = $_FILES["file"]["name"];
    $ext = pathinfo($f,PATHINFO_EXTENSION);
    $fname = rand(10000, 999999).".".$ext;

    $course_sql = "INSERT INTO clg_course (course_id, course_cat, course_name,course_des,course_seotitle,course_keyword, course_tag, course_duration, course_eligibility, course_avg_fee, course_image, corse_short_des, course_date, up_by, recycle) VALUES ( '$course_id', '$course_cat', '$course_name',  '$course_des','$course_seotitle', '$course_keyword','$course_tag', '$course_duration', '$course_eligibility', '$course_avg_fee', '$fname', '$corse_short_des', '$course_date', '$up_by', '$recycle')";
    
    if( mysqli_query($con, $course_sql) ){
        // Move Files
      move_uploaded_file($file,'../upload/course/'.$fname);
        ?>
<script>
alert("Details Sent Successfully");
window.location = "addcourse.php";
</script>
<?php
    }
    else{
        ?>
<script>
alert("Something's Wrong");
window.location = "addcourse.php";
</script>
<?php
    }
  }
?>

<!-- Edit to database -->
<?php
 if(isset($_POST['edit_users'])){ 
 
     $course_cat = $_POST["course_cat"];
     $course_name = $_POST["course_name"];
     $course_duration = $_POST["course_duration"];
     $course_eligibility = $_POST["course_eligibility"];
     $course_avg_fee = $_POST["course_avg_fee"];
     $corse_short_des = $_POST["corse_short_des"];
     $course_des = $_POST["content"];
     $course_seotitle = $_POST["course_seotitle"];
     $course_keyword = $_POST["course_keyword"];
 
     $course_tag = $_POST["content1"];
     $edit_date = date("j F, Y, g:i a");

     
     if ($_FILES["file"]["tmp_name"]) {
         // new File Upload
         $file = $_FILES["file"]["tmp_name"];
         $f = $_FILES["file"]["name"];
         $ext = pathinfo($f, PATHINFO_EXTENSION);
         $fname = rand(10000, 999999) . "." . $ext;
         $sql_select = "SELECT course_image FROM clg_course WHERE course_id = '$_GET[edit_id]'";
         $result = $con->query($sql_select);
         if ($result && $row = $result->fetch_assoc()) {
             $previous_logo = $row['course_image'];
             if (!empty($previous_logo)) {
                 $previous_image_path = '../upload/course/' . $previous_logo;
                 if (file_exists($previous_image_path)) {
                     unlink($previous_image_path);
                 }
             }
         }
     } else {
         // If no new file is uploaded, keep the previous image filename
         $fname = $edit_row['course_image'];
     }
 
     {
         $up_sql = "UPDATE clg_course SET course_des = '$course_des',course_cat = '$course_cat',course_name = '$course_name',  course_seotitle = '$course_seotitle',course_keyword = '$course_keyword',course_tag = '$course_tag', course_duration='$course_duration', course_eligibility = '$course_eligibility', course_avg_fee = '$course_avg_fee', course_image='$fname', corse_short_des='$corse_short_des', edit_date='$edit_date' ";
 
     $up_sql .= " WHERE course_id = '$_GET[edit_id]'";
      if(mysqli_query($con, $up_sql)){
      // Move Files
      move_uploaded_file($file,'../upload/course/'.$fname);
         ?>
<script>
alert("Data Updated Successfully");
window.location = 'addcourse.php';
</script>
<?php
         }
 else{
?>
<script>
alert('OOPS !!!!');
setTimeout(() => {
    window.location = 'addcourse.php';
}, 1200);
</script>
<?php
         }
         ?>
<?php
     }
 }
?>

<!-- Recycle to database -->
<?php
  if ( isset($_GET["rec_id"]) ) {
    $find_sql = "SELECT * FROM clg_course WHERE sl_no = '$_GET[rec_id]'";
    $find_run = mysqli_query($con, $find_sql);
    if ($f_rows = mysqli_fetch_assoc($find_run)) {
        $old_rec = $f_rows["recycle"];
    
        if ($old_rec == 1) {
            $new_rec = 0;
        } elseif ($old_rec == 0) {
            $new_rec = 1;
        }
    
        $update_sql = "UPDATE clg_course SET recycle = '$new_rec' WHERE sl_no = '$_GET[rec_id]'";
        $update_run = mysqli_query($con, $update_sql);

        if ( $update_run ) {
            ?>
<script>
alert("Sent to Recycle Successfully");
window.location = "addcourse.php";
</script>
<?php
        } else {
            ?>
<script>
alert('OOPS !!!!');
setTimeout(() => {
    window.location = 'addcourse.php';
}, 1200);
</script>
<?php
        }   
    }
}
?>

<!-- Delete to database -->
<?php
  if ( isset($_GET["del_id"]) ) {
    $del_sql = "DELETE FROM clg_course  WHERE sl_no = '$_GET[del_id]'";

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