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

            <?php if(isset($_GET["college"])){

                $college_id = $_GET["college"];

                $college_sql = mysqli_query($con, "SELECT * FROM users where utype = 'College' AND uid = '$college_id'");

                if($college_row = mysqli_fetch_assoc($college_sql)){

                    $college_name = $college_row["uname"];

                }

                ?>

            <div class="container-fluid">

                <?php if(isset($_GET["college"]) && isset($_GET["edit_id"])){

                    $edit_id = $_GET["edit_id"];

                    $edit_sql = mysqli_query($con, "SELECT * FROM data WHERE data_catagory = 'Course' AND data_sl_no = '$edit_id'");

                    if($edit_row = mysqli_fetch_assoc($edit_sql)){

                        ?>

                <div class="card mt-3">

                    <div class="card-header">

                        <h5 class="m-0">Edit Course For <?php echo $college_name;?></h5>

                    </div>

                    <div class="card-body">

                        <form action="" method="POST">

                            <label for="q">Enter Question:</label>

                            <input type="text" class="form-control" name="q"

                                value="<?php echo $edit_row["data_short_des"];?>">

                            <label for="ans">Enter answer:</label>

                            <textarea class="form-control ckeditor" name="ans" id="content"

                                rows="3"><?php echo $edit_row["data_long_des"];?></textarea>

                            <button type="submit" class="btn btn-success btn-user m-2" name="edit"><i

                                    class="fa fa-edit"></i>

                                Edit DETAILS

                            </button>

                        </form>

                    </div>

                </div>

                <?php } ?>

                <?php } elseif(isset($_GET["view_id"])){

                    $view_id = $_GET["view_id"];

                    $view_sql = mysqli_query($con, "SELECT * FROM data WHERE data_catagory = 'Course' AND data_sl_no = '$view_id'");

                        if($view_row = mysqli_fetch_assoc($view_sql)){

                        ?>

                <div class="card mt-3">

                    <div class="card-header">

                        <h5 class="m-0">Course For <?php echo $college_name;?></h5>

                    </div>

                    <div class="card-body">

                        <p><strong>Q></strong><?php echo $view_row["data_short_des"];?></p>

                        <p class="m-0"><strong>Answer: </strong></p>

                        <div id="ck-img-preview">

                            <?php echo $view_row["data_long_des"];?>

                        </div>

                    </div>

                </div>

                <?php } }else{ ?>

                <div class="card">

                    <div class="card-header">

                        <h5 class="m-0">Course For <?php echo $college_name;?></h5>

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

                                <div class="row">

                                    <div class="col-md-4 mb-2">

                                        <label for="dept">Select Department:</label>

                                        <select name="dept" class="form-control" id="dept">

                                            <option value="">Select From List</option>

                                            <?php 

                                            $dept_sql = mysqli_query($con, "SELECT * FROM clg_course_cat WHERE recycle = '1' ORDER BY sl_no DESC");

                                            

                                            while ($dept_row = mysqli_fetch_assoc($dept_sql)) {

                                                $present_sql = mysqli_query($con, "SELECT * FROM data WHERE data_catagory = 'Department' AND data_up_for = '$college_id' AND data_short_des = '".$dept_row["sl_no"]."' ");



                                                if (mysqli_num_rows($present_sql) > 0) {

                                            ?>

                                            <option value="<?php echo $dept_row["sl_no"]; ?>"

                                                data-cat-id="<?php echo $dept_row["cat_id"]; ?>">

                                                <?php echo $dept_row["cat_name"]; ?>

                                            </option>

                                            <?php } } ?>

                                        </select>

                                    </div>



                                    <div class="col-md-4 mb-2">

                                        <label for="course">Select Course:</label>

                                        <select name="course" class="form-control" id="course">

                                            <option value="">Select From List</option>

                                        </select>

                                    </div>



                                    <div class="col-md-4 mb-2">

                                        <label for="course_avg_fees">Average Course Fees:</label>

                                        <input type="text" name="course_avg_fees" class="form-control"

                                            id="course_avg_fees">

                                    </div>

                                </div>



                                <button type="submit" class="btn btn-success btn-user m-2" name="submit">

                                    <i class="fa fa-edit"></i> Submit DETAILS

                                </button>

                            </form>



                        </div>

                    </div>

                    <div class="card-body mt-2">

                        <?php

                        $Course_sql = mysqli_query($con, "SELECT * FROM data WHERE data_catagory = 'Course' AND data_up_for = '$college_id' ORDER BY data_sl_no DESC ");

                        if(mysqli_num_rows($Course_sql)>0){

                            ?>

                        <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

                                        $c = 1;

                                        while($Course_row = mysqli_fetch_assoc($Course_sql)){

                                            ?>

                                    <tr id='<?php echo $Course_row["data_sl_no"]; ?>'>

                                        <td>

                                            <span class='text_dark'><?php echo $c; ?></span>

                                        </td>

                                        <td>

                                            <?php 

                                            $getCourse_sql = mysqli_query($con, "SELECT * FROM clg_course WHERE recycle = '1' AND sl_no = '$Course_row[data_title]' ");

                                            if($getCourse_row = mysqli_fetch_assoc($getCourse_sql)){

                                                echo $getCourse_row["course_name"];

                                            }

                                            $getDept_sql = mysqli_query($con, "SELECT * FROM clg_course_cat WHERE recycle = '1' AND sl_no = '$Course_row[data_subCategory]' ");

                                            if($getDept_row = mysqli_fetch_assoc($getDept_sql)){

                                                echo ' (' . $getDept_row["cat_name"] . ')';

                                            }

                                            if(!empty($Course_row["data_short_des"])){ echo ' [Course Fees: ' . $Course_row["data_short_des"] . ']'; }

                                            ?>

                                        </td>

                                        <td class='text-center align-middle'>

                                            <a href='Course.php?college=<?php echo $_GET['college']; ?>&del_id=<?php echo $Course_row["data_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>

                        <?php 

                        } else{echo "No Course yet.";}

                        ?>

                    </div>

                </div>

                <?php

                }?>

            </div>

            <?php } else {echo "Enter a valid college.";}?>

        </div>



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

    </section>



    <!-- JavaScript Libraries -->



    <?php include "include/js.php"; ?>

    <script>

    CKEDITOR.replace('content', {

        height: 300,

        filebrowserUploadUrl: "upload.php"

    });



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

        }

    }

    </script>



    <script>

    $(document).ready(function() {

        $("#dept").change(function() {

            var sl_no = $(this).val();

            var cat_id = $("#dept option:selected").data("cat-id");

            var college_id = <?php echo $college_id; ?>;



            if (sl_no !== "") {

                $.ajax({

                    url: "get_courses.php",

                    type: "POST",

                    data: {

                        college_id: college_id,

                        sl_no: sl_no,

                        cat_id: cat_id

                    },

                    success: function(response) {

                        $("#course").html(response);

                        $("#course_avg_fees").val(""); // Reset fees input

                    }

                });

            } else {

                $("#course").html('<option value="">Select From List</option>');

                $("#course_avg_fees").val(""); // Reset fees input

            }

        });



        // Fetch course fees when course is selected

        $("#course").change(function() {

            var course_id = $(this).val();



            if (course_id !== "") {

                $.ajax({

                    url: "get_course_fees.php",

                    type: "POST",

                    data: {

                        course_id: course_id

                    },

                    success: function(response) {

                        $("#course_avg_fees").val(response);

                    }

                });

            } else {

                $("#course_avg_fees").val(""); // Reset if no selection

            }

        });

    });

    </script>





</body>



</html>



<?php 

if(isset($_POST["submit"])){

    $dept = $_POST["dept"];

    $course = $_POST["course"];

    $course_avg_fees = $_POST["course_avg_fees"];



    $ins_sql = mysqli_query($con, "INSERT INTO data (data_catagory, data_title, data_short_des, data_subCategory, data_up_for, data_up_date, data_recycle) VALUES ('Course', '$course', '$course_avg_fees', '$dept', '$college_id', '$currentTime', '1')");

    if($ins_sql){

        echo "<script>window.location = 'course.php?college=" . $_GET["college"] . "';</script>";

    } else {

        echo "<script>alert('OOPS !!!!'); setTimeout(() => { window.location = 'course.php?college=" . $_GET["college"] . "'; }, 1200);</script>";

    }

}

?>



<?php

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

    $del_dql = mysqli_query($con, "DELETE FROM data WHERE data_sl_no = '$_GET[del_id]' AND data_up_for = '$_GET[college]' ");

    if($del_sql){

        echo "<script>window.location = 'course.php?college=" . $_GET["college"] . "';</script>";

    } else {

        echo "<script>alert('OOPS !!!!'); setTimeout(() => { window.location = 'course.php?college=" . $_GET["college"] . "'; }, 1200);</script>";

    }

}

?>