<?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"; ?>
</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["course_tag"]) ) {
                        $run = mysqli_query($con, "SELECT * FROM clg_course  WHERE sl_no = '$_GET[course_tag]'");
                        $row = mysqli_fetch_assoc($run);
                        $name = $row['course_name'];
                        $get_id = $row['course_id'];
                    ?>
                <?php
                    if(isset($_GET['edit_id'])){
                        $id = $_GET['edit_id'];
                        $find_edit_run = mysqli_query($con, "SELECT * FROM all_tag WHERE all_tag_id = '$id'");
                        $find_edit_row = mysqli_fetch_assoc($find_edit_run);
                    ?>
                <div class="card">
                    <div class="card-header">
                        <h5 class="m-0 text-center"> <?php echo $name; ?></h5>
                    </div>
                    <div class="card-body">
                        <form class="user" method="post" enctype="multipart/form-data">
                            <div class="row">
                                <div class="col-md-12 mb-3">
                                    <label for="all_tag_title" class="form-label">Tag Title:</label>
                                    <input type="text" class="form-control form-control-sm mb-2"
                                        value="<?php echo $find_edit_row["all_tag_title"]; ?>" name="all_tag_title">
                                </div>
                                <div class="col-md-12 mb-3">
                                    <label class="form-label" for="all_tag_url">Tag Url:</label>
                                    <input type="text" class="form-control form-control-sm mb-2"
                                        value="<?php echo $find_edit_row["all_tag_url"]; ?>" name="all_tag_url">
                                </div>
                                <div class="col-md-12">
                                    <button type="submit" class="btn btn-success btn-user" name="Editsubmit">
                                        <i class="fa fa-edit"></i>Edit Tag
                                    </button>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
                <?php
                    }
                    elseif(isset($_GET['view_id'])){
                        $id = $_GET['view_id'];
                        $find_edit_run = mysqli_query($con, "SELECT * FROM all_tag WHERE all_tag_id = '$id'");
                        $find_edit_row = mysqli_fetch_assoc($find_edit_run);
                        ?>
                <div class="card">
                    <div class="card-header">
                        <h5 class="m-0 text-center"> <?php echo $name; ?></h5>
                    </div>
                    <div class="card-body">
                        <div class="mb-3">
                            <h6>Tag Title:</h6><span><?php echo $find_edit_row["all_tag_title"]; ?></span>
                        </div>
                        <div>
                            <h6>Tag Url:</h6>
                            <p><?php echo $find_edit_row['all_tag_url'];?></p>
                        </div>
                    </div>
                </div>
                <?php
                    }
                    else {
                    ?>
                <div class="card mt-3">
                    <div class="card-header">
                        <h5 class="m-0 text-center"> <?php echo $name; ?></h5>
                    </div>
                    <div class="card-body">
                        <form class="user" method="post" enctype="multipart/form-data">
                            <div class="row">
                                <div class="col-md-6 mb-3">
                                    <label for="all_tag_title" class="form-label">Tag Title:</label>
                                    <input type="text" class="form-control form-control-sm mb-2" value=""
                                        name="all_tag_title">
                                </div>
                                <div class="col-md-6 mb-3">
                                    <label class="form-label" for="all_tag_url">Tag Url:</label>
                                    <input type="text" class="form-control form-control-sm mb-2" value=""
                                        name="all_tag_url">
                                </div>
                                <div class="col-md-12">
                                    <button type="submit" class="btn btn-success btn-user m-2" name="submit">
                                        <i class="fa fa-edit"></i>Add Tag
                                    </button>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>

                <div class="card mt-2">
                    <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">
                                    <div>
                                        <h5>Recycle Tag List</h5>
                                    </div>
                                    <div>
                                        <a href="tagcourse.php?course_tag=<?php echo $_GET['course_tag']; ?>"
                                            class="btn btn-primary btn-sm">Active Catagory</a>
                                    </div>
                                </div>
                                <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
                                                $table_sql = "SELECT * FROM all_tag WHERE all_tag_recycle = '0' AND all_tag_catagory = 'course' and all_tag_get_id= '$get_id'  ORDER BY all_tag_sl_no DESC";

                                                $table_run = mysqli_query($con, $table_sql);

                                                $c = 1;
                                                while ($table_rows = mysqli_fetch_assoc($table_run)) {
                                            ?>
                                            <tr id='<?php echo $table_rows["all_tag_sl_no"]; ?>'>
                                                <td>
                                                    <span
                                                        class='badge badge-info badge-pill text-dark'><?php echo $c; ?></span>
                                                </td>
                                                <td>
                                                    <?php echo $table_rows['all_tag_title']; ?>
                                                </td>
                                                <td class='text-center align-middle'>
                                                    <a class="btn btn-primary btn-sm" style="margin-left:5px;"
                                                        href="tagcourse.php?course_tag=<?php echo $_GET['course_tag']; ?>&view_id=<?php echo $table_rows['all_tag_id']; ?>">
                                                        View
                                                    </a>
                                                    <a class="btn btn-primary btn-sm" style="margin-left:5px;"
                                                        href="tagcourse.php?course_tag=<?php echo $_GET['course_tag']; ?>&edit_id=<?php echo $table_rows['all_tag_id']; ?>">
                                                        Edit
                                                    </a>
                                                    <a href="tagcourse.php?course_tag=<?php echo $_GET['course_tag']; ?>&rec_id=<?php echo $table_rows['all_tag_sl_no']; ?>"
                                                        class='worning m-2 btn btn-danger btn-sm rounded-circle'
                                                        title='Click to Recycle'>
                                                        <i class="fa fa-recycle" aria-hidden="true"></i>
                                                    </a>
                                                    <a href='tagcourse.php?course_tag=<?php echo $_GET['course_tag']; ?>&del_id=<?php echo $table_rows["all_tag_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">
                                    <div>
                                        <h4>Active Tag List</h4>
                                    </div>
                                    <div>
                                        <a href="tagcourse.php?course_tag=<?php echo $_GET['course_tag']; ?>&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>Tag Title</th>
                                                <th>Action</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            <?php
                                                $sql = "SELECT * FROM all_tag WHERE all_tag_recycle = '1' AND all_tag_catagory = 'course' and all_tag_get_id = '$get_id'  ORDER BY all_tag_sl_no DESC";
                                                $run = mysqli_query($con, $sql);

                                                $c = 1;
                                                while ( $rows = mysqli_fetch_assoc($run) ) {                                                   
                                                echo "
                                                <tr id='$rows[all_tag_sl_no]'>
                                                <td>
                                                <span class='badge badge-info badge-pill text-dark'>$c</span>
                                                </td>
                                                <td>
                                                $rows[all_tag_title]
                                                </td>";
                                            ?>
                                            <td class='text-center align-middle'>
                                                <a class="btn btn-primary btn-sm" style="margin-left:5px;"
                                                    href="tagcourse.php?course_tag=<?php echo $_GET['course_tag'];?>&view_id=<?php echo $rows['all_tag_id']; ?>">View</a>
                                                <a class="btn btn-primary btn-sm" style="margin-left:5px;"
                                                    href="tagcourse.php?course_tag=<?php echo $_GET['course_tag'];?>&edit_id=<?php echo $rows['all_tag_id']; ?>">
                                                    Edit</a>

                                                <a href="tagcourse.php?course_tag=<?php echo $_GET['course_tag'];?>&rec_id=<?php echo $rows['all_tag_sl_no']; ?>"
                                                    class='worning m-2 btn btn-danger btn-sm rounded-circle'
                                                    title='Click to Recycle'> <i class="fa fa-recycle"
                                                        aria-hidden="true"></i>
                                                </a>
                                            </td>
                                            </tr>
                                            <?php $c++; } ?>
                                        </tbody>
                                    </table>
                                </div>
                            </div>
                            <?php  } ?>
                        </div>
                    </div>
                </div>
                <?php } } ?>
            </div>
        </div>

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

    <!-- JavaScript Libraries -->

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


</body>

</html>


<?php
    if(isset($_POST['submit'])){ 
	
        $tag_title = mysqli_real_escape_string($con, strip_tags($_POST['all_tag_title']));
        $tag_url = mysqli_real_escape_string($con, strip_tags($_POST['all_tag_url']));

        $tag_id = str_pad(rand(0, 9999999), 7, '0', STR_PAD_LEFT);
        $upload_date = date("j F, Y, g:i a");
        $recycle="1";
        $up_by= $_SESSION['utype'];
        $tag_catagory = "course";

        $find_run = mysqli_query($con, "SELECT * FROM clg_course  WHERE sl_no = '$_GET[course_tag]'");
        $find_row = mysqli_fetch_assoc($find_run);
        $tag_for = $find_row['course_name'];
        $tag_get_id = $find_row['course_id'];

  		
        {
            $up_sql = "INSERT INTO all_tag ( all_tag_id, all_tag_catagory, all_tag_get_id, all_tag_for, all_tag_title, all_tag_url, all_tag_upload_date, all_tag_up_by, all_tag_recycle) VALUES ( '$tag_id', '$tag_catagory', '$tag_get_id', '$tag_for', '$tag_title', '$tag_url', '$upload_date', '$up_by', $recycle)";
   
		 if(mysqli_query($con, $up_sql)){
            ?>
<script>
alert("Data Updated Successfully  <?php echo $tag_for;?>");
window.location = 'tagcourse.php?course_tag=<?php echo $find_row['sl_no']; ?>';
</script>
<?php
            }
            else{
                
                ?>
<script>
alert('OOPS !!!! <?php echo $tag_for;?>');
setTimeout(() => {
    window.location = 'tagcourse.php?course_tag=<?php echo $find_row['sl_no']; ?>';
}, 1200);
</script>
<?php
            }
            ?>
<?php
        }
    }
?>

<?php
    if(isset($_POST['Editsubmit'])){ 
	
        $tag_title = mysqli_real_escape_string($con, strip_tags($_POST['all_tag_title']));
        $tag_url = mysqli_real_escape_string($con, strip_tags($_POST['all_tag_url']));

        $edit_date = date("j F, Y, g:i a");

        $find_run = mysqli_query($con, "SELECT * FROM clg_course  WHERE sl_no = '$_GET[course_tag]'");
        $find_row = mysqli_fetch_assoc($find_run);

  		
        {
            $up_sql = "UPDATE all_tag SET all_tag_title='$tag_title', all_tag_url='$tag_url', all_tag_edit_date='$edit_date'";
   
            $up_sql .= " WHERE all_tag_id = '$_GET[edit_id]'";
		 if(mysqli_query($con, $up_sql)){
            ?>
<script>
alert("Data Updated Successfully  <?php echo $tag_for;?>");
window.location = 'tagcourse.php?course_tag=<?php echo $find_row['sl_no']; ?>';
</script>
<?php
            }
            else{
                
                ?>
<script>
alert('OOPS !!!! <?php echo $tag_for;?>');
setTimeout(() => {
    window.location = 'tagcourse.php?course_tag=<?php echo $find_row['sl_no']; ?>';
}, 1200);
</script>
<?php
            }
            ?>
<?php
        }
    }
?>

<?php
  if ( isset($_GET["del_id"]) ) {
    $find_run = mysqli_query($con, "SELECT * FROM clg_course  WHERE sl_no = '$_GET[course_tag]'");
    $find_row = mysqli_fetch_assoc($find_run);

    $find1_sql = "SELECT * FROM all_tag WHERE all_tag_sl_no = '$_GET[del_id]'";
    $find1_run = mysqli_query($con, $find1_sql);
    
        $del_sql = "DELETE FROM all_tag WHERE all_tag_sl_no = '$_GET[del_id]'";
        $del_run = mysqli_query($con, $del_sql);

        if ( $del_run ) {
            ?>
<script>
alert("Deleted Successfully");
window.location = "tagcourse.php?course_tag=<?php echo $find_row['sl_no']; ?>";
</script>
<?php
        } else {
            ?>
<script>
alert('OOPS !!!!');
setTimeout(() => {
    window.location = 'tagcourse.php?course_tag=<?php echo $find_row['sl_no']; ?>';
}, 1200);
</script>
<?php
        }
    }
?>

<?php
  if ( isset($_GET["rec_id"]) ) {
    $find_run = mysqli_query($con, "SELECT * FROM clg_course  WHERE sl_no = '$_GET[course_tag]'");
    $find_row = mysqli_fetch_assoc($find_run);

    $find1_sql = "SELECT * FROM all_tag WHERE all_tag_sl_no = '$_GET[rec_id]'";
    $find1_run = mysqli_query($con, $find1_sql);
    if ($f_rows = mysqli_fetch_assoc($find1_run)) {
        $old_rec = $f_rows["all_tag_recycle"];
    
        if ($old_rec == 1) {
            $new_rec = 0;
        } elseif ($old_rec == 0) {
            $new_rec = 1;
        }
    
        $update_sql = "UPDATE all_tag SET all_tag_recycle = '$new_rec' WHERE all_tag_sl_no = '$_GET[rec_id]'";
        $update_run = mysqli_query($con, $update_sql);

        if ( $update_run ) {
            ?>
<script>
alert("Sent to Recycle Successfully");
window.location = "tagcourse.php?course_tag=<?php echo $find_row['sl_no']; ?>";
</script>
<?php
        } else {
            ?>
<script>
alert('OOPS !!!!');
setTimeout(() => {
    window.location = 'tagcourse.php?course_tag=<?php echo $find_row['sl_no']; ?>';
}, 1200);
</script>
<?php
        }   
    }
}
?>