<?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">
                <div class="card mt-3">
                    <?php
                        if ( isset($_GET["category_tag"]) ) {
                        $run = mysqli_query($con, "SELECT * FROM clg_course_cat  WHERE cat_id = '$_GET[category_tag]'");
                        $row = mysqli_fetch_assoc($run);
                        $name = $row['cat_name'];
                        $get_id = $row['cat_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-header">
                        <h5 class="m-0 text-center">Tag for: <?php echo $name; ?></h5>
                    </div>
                    <div class="card-body">
                        <form class="user" method="post" enctype="multipart/form-data">
                            <h2 class="mb-3 text-center"> <?php echo $find_edit_row["all_tag_for"]; ?></h2>
                            <div class="col-md-12 mb-3">
                                <label for="exampleFormControlInput1" 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="customFile">Tag Url</label>
                                <textarea name="all_tag_url" class="form-control" id="" cols="30"
                                    rows="5"><?php echo $find_edit_row['all_tag_url'];?></textarea>
                            </div>
                            <div class="col-md-12">
                                <button type="submit" class="btn btn-success btn-user m-2" name="Editsubmit">
                                    <i class="fa fa-edit"></i>Edit Tag
                                </button>
                            </div>
                        </form>
                    </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-header">
                        <h5 class="m-0 text-center">View Tags for: <?php echo $name; ?></h5>
                    </div>
                    <div class="card-body">
                        <h2 class="mb-3 text-center"> <?php echo $find_edit_row["all_tag_for"]; ?></h2>
                        <div class="mb-5">
                            <h4>Tag Title:</h4><span><?php echo $find_edit_row["all_tag_title"]; ?></span>
                        </div>
                        <div>
                            <h4>Tag Url:</h4>
                            <p><?php echo $find_edit_row['all_tag_url'];?></p>
                        </div>
                    </div>
                    <?php
                    }
                    else {
                    ?>
                    <div class="card-header">
                        <h5 class="m-0 text-center">Tag for: <?php echo $name; ?></h5>
                    </div>
                    <div class="card-body">
                        <form class="user" method="post" enctype="multipart/form-data">
                            <div class="col-md-12 mb-2">
                                <label for="exampleFormControlInput1" 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-12 mb-2">
                                <label class="form-label" for="customFile">Tag Url</label>
                                <textarea name="all_tag_url" class="form-control" id="" cols="30" rows="3"></textarea>
                            </div>
                            <div class="col-md-12">
                                <button type="submit" class="btn btn-success btn-user" name="submit">
                                    <i class="fa fa-edit"></i>Add Tag
                                </button>
                            </div>
                        </form>
                    </div>

                    <div class="row m-0" style="padding:20px;">
                        <?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="tag.php?category_tag=<?php echo $_GET['category_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 = 'courseCategory' 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="tag.php?category_tag=<?php echo $_GET['category_tag']; ?>&view_id=<?php echo $table_rows['all_tag_id']; ?>">
                                                    View
                                                </a>
                                                <a class="btn btn-primary btn-sm" style="margin-left:5px;"
                                                    href="tag.php?category_tag=<?php echo $_GET['category_tag']; ?>&edit_id=<?php echo $table_rows['all_tag_id']; ?>">
                                                    Edit
                                                </a>
                                                <a href="tag.php?category_tag=<?php echo $_GET['category_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='tag.php?category_tag=<?php echo $_GET['category_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 mb-2">
                                <div>
                                    <h5>Active Tag List</h5>
                                </div>
                                <div>
                                    <a href="tag.php?category_tag=<?php echo $_GET['category_tag']; ?>&recycle_table"
                                        class="btn btn-info text-white btn-sm">Recycle 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
                                                $sql = "SELECT * FROM all_tag WHERE all_tag_recycle = '1' AND all_tag_catagory = 'courseCategory' 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="tag.php?category_tag=<?php echo $_GET['category_tag'];?>&view_id=<?php echo $rows['all_tag_id']; ?>">View</a>
                                            <a class="btn btn-primary btn-sm" style="margin-left:5px;"
                                                href="tag.php?category_tag=<?php echo $_GET['category_tag'];?>&edit_id=<?php echo $rows['all_tag_id']; ?>">
                                                Edit</a>

                                            <a href="tag.php?category_tag=<?php echo $_GET['category_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>
                    <?php }
                    ?>
                    <?php
								}
                            ?>
                </div>
            </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, 9999), 4, '0', STR_PAD_LEFT);
        $upload_date = date("j F, Y, g:i a");
        $recycle="1";
        $up_by= $_SESSION['utype'];
        $tag_catagory = "courseCategory";

        $find_run = mysqli_query($con, "SELECT * FROM clg_course_cat  WHERE cat_id = '$_GET[category_tag]'");
        $find_row = mysqli_fetch_assoc($find_run);
        $tag_for = $find_row['cat_name'];
        $tag_get_id = $find_row['cat_id'];

  		
        {
            $up_sql = "INSERT INTO all_tag ( all_tag_id, all_tag_catagory, all_tag_get_id, 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_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 = 'tag.php?category_tag=<?php echo $find_row['cat_id']; ?>';
</script>
<?php
            }
            else{
                
                ?>
<script>
alert('OOPS !!!! <?php echo $tag_for;?>');
setTimeout(() => {
    window.location = 'tag.php?category_tag=<?php echo $find_row['cat_id']; ?>';
}, 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_cat  WHERE cat_id = '$_GET[category_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 = 'tag.php?category_tag=<?php echo $_GET["category_tag"]; ?>';
</script>
<?php
            }
            else{
                
                ?>
<script>
alert('OOPS !!!! <?php echo $tag_for;?>');
setTimeout(() => {
    window.location = 'tag.php?category_tag=<?php echo $find_row['cat_id']; ?>';
}, 1200);
</script>
<?php
            }
            ?>
<?php
        }
    }
?>

<?php
  if ( isset($_GET["del_id"]) ) {
    $find_run = mysqli_query($con, "SELECT * FROM clg_course_cat  WHERE cat_id = '$_GET[category_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 = "tag.php?category_tag=<?php echo $find_row['cat_id']; ?>";
</script>
<?php
        } else {
            ?>
<script>
alert('OOPS !!!!');
setTimeout(() => {
    window.location = 'tag.php?category_tag=<?php echo $find_row['cat_id']; ?>';
}, 1200);
</script>
<?php
        }
        
    }
?>

<?php
  if ( isset($_GET["rec_id"]) ) {
    $find_run = mysqli_query($con, "SELECT * FROM clg_course_cat  WHERE cat_id = '$_GET[category_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 = "tag.php?category_tag=<?php echo $find_row['cat_id']; ?>";
</script>
<?php
        } else {
            ?>
<script>
alert('OOPS !!!!');
setTimeout(() => {
    window.location = 'tag.php?category_tag=<?php echo $find_row['cat_id']; ?>';
}, 1200);
</script>
<?php
        }   
    }
}
?>