<?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 = 'Tag' 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 Tag For <?php echo $college_name;?></h5>
                    </div>
                    <div class="card-body">
                        <form action="" method="POST">
                            <label for="q">Tag Title:</label>
                            <input type="text" class="form-control" name="q"
                                value="<?php echo $edit_row["data_short_des"];?>">
                            <label for="ans">Tag URL:</label>
                            <textarea class="form-control" name="ans"
                                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 = 'Tag' 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">Tag For <?php echo $college_name;?></h5>
                    </div>
                    <div class="card-body">
                        <p><strong>Tag Title: </strong><?php echo $view_row["data_short_des"];?></p>
                        <p class="m-0"><strong>Tag URL: </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">Tag For <?php echo $college_name;?></h5>
                    </div>
                    <div class="card-body">
                        <button id="toggleButton" class="btn btn-primary btn-sm" onclick="toggleDiv()">Add Tag
                        </button>
                        <div id="toggleDiv"
                            style="display: none; padding: 10px; border: 1px solid #ddd; margin-top: 10px;">
                            <form action="" method="POST">
                                <label for="q">Tag Title:</label>
                                <input type="text" class="form-control" name="q" placeholder="Enter Tag Title">
                                <label for="ans">Tag URL:</label>
                                <textarea class="form-control" name="ans" placeholder="Enter Tag URL.."
                                    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>
                        </div>
                    </div>
                    <div class="card-body mt-2">
                        <?php
                        $faq_sql = mysqli_query($con, "SELECT * FROM data WHERE data_catagory = 'Tag' AND data_up_for = '$college_id' ");
                        if(mysqli_num_rows($faq_sql)>0){
                            ?>
                        <div class="table-responsive">
                            <table class="table table-hover table-bordered table-sm" id="thisismytable">
                                <thead>
                                    <tr>
                                        <th>No.</th>
                                        <th>Tag Title</th>
                                        <th>Action</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php
                                        $c = 1;
                                        while($faq_row = mysqli_fetch_assoc($faq_sql)){
                                            ?>
                                    <tr id='<?php echo $faq_row["data_sl_no"]; ?>'>
                                        <td>
                                            <span class='text_dark'><?php echo $c; ?></span>
                                        </td>
                                        <td>
                                            <?php echo $faq_row['data_short_des']; ?>
                                        </td>
                                        <td class='text-center align-middle'>
                                            <a class="btn btn-primary btn-sm" style="margin-left:5px;"
                                                href="ctag.php?college=<?php echo $_GET['college']; ?>&view_id=<?php echo $faq_row['data_sl_no']; ?>">
                                                View
                                            </a>
                                            <a class="btn btn-primary btn-sm" style="margin-left:5px;"
                                                href="ctag.php?college=<?php echo $_GET['college']; ?>&edit_id=<?php echo $faq_row['data_sl_no']; ?>">
                                                Edit
                                            </a>
                                            <a href='ctag.php?college=<?php echo $_GET['college']; ?>&del_id=<?php echo $faq_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 Tag 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 TAG';
        }
    }
    </script>

</body>

</html>

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

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

    $up_sql = mysqli_query($con, "UPDATE data SET data_short_des = '$q', data_long_des = '$ans' WHERE data_catagory = 'Tag' AND data_up_for = '$college_id' AND data_sl_no = '$_GET[edit_id]' ");
    if($up_sql){
        echo "<script>window.location = 'ctag.php?college=" . $_GET["college"] . "';</script>";
    } else {
        echo "<script>alert('OOPS !!!!'); setTimeout(() => { window.location = 'ctag.php?college=" . $_GET["college"] . "&edit_id=". $_GET["edit_id"] ."'; }, 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 = 'ctag.php?college=" . $_GET["college"] . "';</script>";
    } else {
        echo "<script>alert('OOPS !!!!'); setTimeout(() => { window.location = 'ctag.php?college=" . $_GET["college"] . "'; }, 1200);</script>";
    }
}
?>