<?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">SEO For <?php echo $college_name;?></h5>
                    </div>
                    <div class="card-body">
                        <?php
                        $SEO_sql = mysqli_query($con, "SELECT * FROM data WHERE data_catagory = 'cSEO' AND data_up_for = '$college_id' ");
                        if(mysqli_num_rows($SEO_sql)>0){
                            $SEO_row = mysqli_fetch_assoc($SEO_sql);
                            ?>
                        <form action="" method="POST">
                            <label for="seo_title">Edit SEO Title:</label>
                            <input type="text" class="form-control mb-2" name="seo_title"
                                value="<?php echo $SEO_row["data_title"];?>">
                            <label for="seo_key">Edit SEO Keywords:</label>
                            <input type="text" class="form-control mb-2" name="seo_key"
                                value="<?php echo $SEO_row["data_short_des"];?>">

                            <label for="seo">Edit SEO Description:</label>
                            <textarea class="form-control" name="seo"
                                rows="3"><?php echo $SEO_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="seo_title">Enter SEO Title:</label>
                            <input type="text" class="form-control mb-2" name="seo_title" placeholder="Enter SEO Title">
                            <label for="seo_key">Enter SEO Keywords:</label>
                            <input type="text" class="form-control mb-2" name="seo_key"
                                placeholder="Enter SEO Keywords with Coma">

                            <label for="seo">Enter SEO Description:</label>
                            <textarea class="form-control" name="seo" placeholder="Enter SEO Description.."
                                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"])){
    $seo_title = $_POST["seo_title"];
    $seo_key = $_POST["seo_key"];
    $seo = $_POST["seo"];

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

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