<?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">
                <div class="card">
                    <div class="card-header">
                        <h5 class="m-0">Admission For <?php echo $college_name;?></h5>
                    </div>
                    <div class="card-body">
                        <?php
                        $Admission_sql = mysqli_query($con, "SELECT * FROM data WHERE data_catagory = 'Admission' AND data_up_for = '$college_id' ");
                        if(mysqli_num_rows($Admission_sql)>0){
                            $Admission_row = mysqli_fetch_assoc($Admission_sql);
                            ?>
                        <form action="" method="POST">
                            <label for="Admission">Edit College Admission:</label>
                            <textarea class="form-control ckeditor" name="Admission" id="content"
                                rows="3"><?php echo $Admission_row["data_long_des"];?></textarea>
                            <button type="submit" class="btn btn-success btn-user m-2" name="edit"><i
                                    class="fa fa-edit"></i> UPDATE DETAILS
                            </button>
                        </form>
                        <?php
                        } else{
                            ?>
                        <form action="" method="POST">
                            <label for="Admission">Enter College Admission:</label>
                            <textarea class="form-control ckeditor" name="Admission" id="content"
                                placeholder="Enter Admission Details.." rows="3"></textarea>
                            <button type="submit" class="btn btn-success btn-user m-2" name="submit"><i
                                    class="fa fa-edit"></i> Submit DETAILS
                            </button>
                        </form>
                        <?php
                        }
                        ?>
                    </div>
                </div>
            </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"
    });
    </script>

</body>

</html>

<?php 
if(isset($_POST["submit"])){
    $Admission = $_POST["Admission"];

    $ins_sql = mysqli_query($con, "INSERT INTO data (data_catagory, data_long_des, data_up_for, data_up_date, data_recycle) VALUES ('Admission', '$Admission', '$college_id', '$currentTime', '1')");
    if($ins_sql){
        echo "<script>window.location = 'admission.php?college=" . $_GET["college"] . "';</script>";
    } else {
        echo "<script>alert('OOPS !!!!'); setTimeout(() => { window.location = 'admission.php?college=" . $_GET["college"] . "'; }, 1200);</script>";
    }
}
?>
<?php
if(isset($_POST["edit"])){
    $Admission = $_POST["Admission"];

    $up_sql = mysqli_query($con, "UPDATE data SET data_long_des = '$Admission' WHERE data_catagory = 'Admission' AND data_up_for = '$college_id' ");
    if($up_sql){
        echo "<script>window.location = 'admission.php?college=" . $_GET["college"] . "';</script>";
    } else {
        echo "<script>alert('OOPS !!!!'); setTimeout(() => { window.location = 'admission.php?college=" . $_GET["college"] . "'; }, 1200);</script>";
    }
}
?>