<?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">
            <div class="container-fluid">
                <?php
                    if ( isset($_GET["edit_id"]) ) {
                    $edit_run = mysqli_query($con, "SELECT * FROM clg_course_cat  WHERE cat_id = '$_GET[edit_id]'");
                    $edit_row = mysqli_fetch_assoc($edit_run);
                    // $sm=$edit_row['course_cat'];
                ?>
                <div class="card radius-10" style="margin-top:20px;">
                    <div class="card-header">
                        <h4 cla ss="mb-3 badge badge-secondary"> <?php echo $edit_row["cat_name"]; ?></h4>
                    </div>
                    <div class="card-body mb-4">
                        <form class="user" method="post" enctype="multipart/form-data">
                            <div class="row">
                                <div class="col-md-7">
                                    <div class="mb-3">
                                        <label for="exampleFormControlInput1" class="form-label">Catagory Name</label>
                                        <input type="text" class="form-control form-control-sm mb-2" id="course_name"
                                            value="<?php echo $edit_row["cat_name"]; ?>" name="cat_name">
                                    </div>
                                    <div class=" mb-3">
                                        <label class="form-label" for="customFile">Course Short Description</label>
                                        <textarea name="cat_short_des" class="form-control" id="" cols="30"
                                            rows="5"><?php echo $edit_row['cat_short_des'];?></textarea>
                                    </div>
                                </div>
                                <div class="col-md-5 mb-3">
                                    <label class="form-label" for="customFile">Course Image</label>
                                    <div class="mb-4 d-flex justify-content-center">
                                        <img id="selectedImage" src="../upload/category/<?php echo $edit_row['cat_img'];?>"
                                            alt="example placeholder" style="width: 150px;" />
                                    </div>
                                    <div class="d-flex justify-content-center">
                                        <div class="btn btn-primary btn-rounded">
                                            <label class="form-label text-white m-1" for="customFile1">Edit Image</label>
                                            <input type="file" class="form-control d-none" id="customFile1" name="file"
                                                onchange="displaySelectedImage(event, 'selectedImage')" />
                                        </div>
                                    </div>
                                </div>
                                <div class="col-md-12 mt-5 mb-3">
                                    Catagory ABOUT
                                    <textarea name="content" id="content" class="form-control ckeditor"><?php echo $edit_row["cat_des"]; ?></textarea>
                                </div>
                                <div class="col-md-12 mb-3">
                                    Catagory SEO TITLE
                                    <input type="text" class="form-control form-control-sm mb-2" id="course_seotitle"
                                        value="<?php echo $edit_row["cat_seotitle"]; ?>" name="cat_seotitle">
                                </div>
                                <div class="col-md-12 mb-3">
                                    Catagory KEYWORD
                                    <input type="text" class="form-control form-control-sm mb-2" id="course_keyword"
                                        value="<?php echo $edit_row["cat_keyword"]; ?>" name="cat_keyword">
                                </div>
                                <div class="col-md-12 mb-3">
                                    Catagory Other Details
                                    <textarea name="content1" id="content1" class="form-control ckeditor"><?php echo $edit_row["cat_tag"]; ?></textarea>
                                </div>
    
    
                                <div class="col-md-12">
                                    <button type="submit" class="btn btn-success btn-user m-2" name="edit_users">
                                        <i class="fa fa-edit"></i> Update
                                    </button>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
                <?php
				}
                elseif ( isset($_GET["vm"]) ) 
                {
                $count = mysqli_query($con, "UPDATE clg_course_cat SET count = count + 1 WHERE cat_id = '$_GET[vm]'");
                if($count){
                $notice_sql = "SELECT * FROM clg_course_cat WHERE cat_id='$_GET[vm]'";
                $notice_data = mysqli_query($con,$notice_sql);
                while($notice_rows = mysqli_fetch_array($notice_data))
                {
                ?>
                <div class="card radius-10 mt-3">
                    <div class="card-header">
                        <h5 class="m-0">Course Department Name: <?php echo $notice_rows['cat_name']; ?></h5>
                    </div>
                    <div class="card-body mb-4">
                        <font color="black">
                            <?php
                            if (!empty($notice_rows['cat_img']))
                            {
                                echo "
                                <h5>Category Image</h5>";
                                echo " <a href='../upload/category/$notice_rows[cat_img]' title='Click to open' target='_blank'> (<b>Already Uploaded - <img src='../upload/category/$notice_rows[cat_img]' height='50' data-toggle='modal' data-target='#show-img' class='showModal'>
                                                            </b> Click To View</a> )";
                                }
                            ?>
                        </font>
                        <br>
                        <?php
                        if (!empty($edit_row['cat_img']))
                            {
                            echo " <a href='../upload/category/$edit_row[cat_img]' title='Click to open' target='_blank'> (<b>Already Uploaded - <img src='../upload/category/$edit_row[cat_img]' height='50' data-toggle='modal' data-target='#show-img' class='showModal'>
                                                         </b> Click To View</a> )";
                            }
                        ?>

                        <br> <br>
                        <div class="mb-2">
                            <h5>Course Department Short Description:</h5>
                            <?php echo $notice_rows['cat_short_des']; ?>
                        </div>
                        <br>
                        <div class="mb-2">
                            <h5>Course Department ABOUT:</h5>
                            <?php echo $notice_rows['cat_des']; ?>
                        </div>
                        <br>
                        <h5>Course Department SEO:</h5>
                        <?php echo $notice_rows['cat_seotitle']; ?>
                        <br>
                        <h5>Course Department KEYWORD:</h5>
                        <?php echo $notice_rows['cat_keyword']; ?>
                        <br>
                        <h5>Course Department Other Details:</h5>
                        <?php echo $notice_rows['cat_tag']; ?>
                        <br>
                        <h5>Course Department upload by:</h5>
                        <?php echo $notice_rows['up_by']; ?>
                        <br>
                        <?php if(!empty($notice_rows['edit_date'])){?>
                        <h5>Course Department edit date:</h5>
                        <?php echo $notice_rows['edit_date']; ?>
                        <?php }?>
                        <br>
                        <!-- Course Department Visitor Count <br>
                        <?php echo $notice_rows['count']; ?> <br> <br> -->

                        <h5>Course Department Tag List:</h5>
                        <?php
                        $c=1;
                        $tag_sql = "SELECT * FROM all_tag WHERE all_tag_catagory = 'courseCategory' AND all_tag_get_for='$notice_rows[cat_id]' AND all_tag_recycle = '1'";
                        $tag_data = mysqli_query($con,$tag_sql);
                        while($tag_rows = mysqli_fetch_array($tag_data))
                        {
                        ?>
                        <br>
                        <p><?php echo $c;?> > Tag Title:
                            <a href="<?php echo $tag_rows['all_tag_url']; ?>">
                                <?php echo $tag_rows['all_tag_title']; ?></a>
                        </p>
                        <?php $c++;}?>
                    </div>
                </div>
                <?php         
                    }  
                } }
                else{
                    ?>
                <div class="card radius-10 mt-3">
                    <div class="card-header">
                        <h5 class="card-title mb-0">Course Department</h5>
                    </div>
                    <div class="card-body">
                        <button id="toggleButton" class="btn btn-primary btn-sm" onclick="toggleDiv()">Add Course
                            Catagory</button>
                        <div id="toggleDiv"
                            style="display: none; padding: 10px; border: 1px solid #ddd; margin-top: 10px;">
                            <form action="" method="POST" enctype="multipart/form-data">
                                <div class="row">
                                    <div class="col-md-9 mb-3">
                                        <div class="mb-2">
                                            <label for="exampleFormControlInput1" class="form-label">Course Department
                                                Name</label>
                                            <input type="Text" class="form-control" name="cat_name"
                                                placeholder="Enter Catagory Name">
                                        </div>
                                        <div>
                                            <label for="exampleFormControlTextarea1" class="form-label">Course
                                                Department Short
                                                About</label>
                                            <textarea class="form-control" name="cat_short_des" rows="5"></textarea>
                                        </div>
                                    </div>
                                    <div class="col-md-3 mb-3 text-center">
                                        <label for="exampleFormControlInput1" class="form-label">Course Department
                                            Image</label>
                                        <div class="mb-2">
                                            <img id="selectedImage" src="../upload/category/img.png"
                                                alt="example placeholder" style="height:150px;" />
                                        </div>
                                        <div class="btn btn-primary btn-rounded btn-sm">
                                            <label class="form-label text-white m-1" for="customFile1">Add
                                                Image</label>
                                            <input type="file" class="form-control d-none" id="customFile1" name="file"
                                                accept="image/*"
                                                onchange="displaySelectedImage(event, 'selectedImage')" />
                                        </div>
                                    </div>

                                    <div class="col-md-12 mb-3">
                                        <label for="exampleFormControlTextarea1" class="form-label">Course Department
                                            description</label>
                                        <textarea class="form-control ckeditor" name="content" id="content"
                                            rows="3"></textarea>
                                    </div>
                                    <br><br>
                                    <div class="col-md-6 mb-3">
                                        <label for="exampleFormControlInput1" class="form-label">Course Department SEO
                                            Title</label>
                                        <input type="Text" class="form-control" name="cat_seotitle"
                                            placeholder="Enter Catagory SEO Title">
                                    </div>
                                    <div class="col-md-6 mb-3">
                                        <label for="exampleFormControlInput1" class="form-label">Course Department SEO
                                            Keyword
                                            With
                                            Coma</label>
                                        <input type="Text" class="form-control" name="cat_keyword"
                                            placeholder="SEO Keyword">
                                    </div>
                                    <div class="col-md-12 mb-3">
                                        <label for="exampleFormControlTextarea1" class="form-label">Course Department
                                            Other
                                            Details</label>
                                        <textarea class="form-control ckeditor" name="content1" id="content1"
                                            rows="3"></textarea>
                                    </div>
                                    <div class="mb-3">
                                        <button class="btn btn-success text-white me-md-2" name="addsubmit"
                                            type="submit">Submit</button>
                                    </div>
                                </div>
                            </form>
                        </div>

                        <div class="row mt-5">
                            <?php 
                            if(isset($_GET['recycle_table']))
                                {
                                ?>
                            <div class="col-md-12 p-0">
                                <div class="d-flex justify-content-between mb-1">
                                    <div>
                                        <h5>Recycle Course Department List</h5>
                                    </div>
                                    <div>
                                        <a href="courseCategory.php" class="btn btn-primary btn-sm">Active Course
                                            Department</a>
                                    </div>
                                </div>

                                <div class="table-responsive">
                                    <table class="table table-hover table-bordered table-sm" id="thisismytable">
                                        <thead>
                                            <tr>
                                                <th>No.</th>
                                                <th>Course Department</th>
                                                <th>Action</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            <?php
                                            $sql = "SELECT * FROM clg_course_cat WHERE recycle = '0' ORDER BY sl_no DESC";

                                            $run = mysqli_query($con, $sql);

                                            $c = 1;
                                            while ( $rows = mysqli_fetch_assoc($run) ) {
                                            echo "
                                            <tr id='$rows[sl_no]'>
                                                <td>
                                                    <span class='badge badge-info badge-pill text-dark'>$c</span>
                                                </td>
                                                <td>
                                                    $rows[cat_name]
                                                </td>";
                                                ?>
                                            <td class='text-center align-middle'>
                                                <a class="btn btn-primary btn-sm" style="margin-left:5px;"
                                                    href="courseCategory.php?vm=<?php echo $rows['cat_id']; ?>">View</a>
                                                <a class="btn btn-primary btn-sm" style="margin-left:5px;"
                                                    href="courseCategory.php?edit_id=<?php echo $rows['cat_id']; ?>">
                                                    Edit</a>
                                                <a class="btn btn-primary btn-sm" style="margin-left:5px;"
                                                    href="tag.php?category_tag=<?php echo $rows['cat_id']; ?>">Tag</a>

                                                <a href='courseCategory.php?rec_id=<?php echo $rows["sl_no"]; ?>'
                                                    class='delete m-2 btn btn-danger btn-sm rounded-circle'
                                                    title='Click to Delete'>
                                                    <i class="fa fa-recycle" aria-hidden="true"></i>
                                                </a>
                                                <a href='courseCategory.php?del_id=<?php echo $rows["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-1">
                                    <div>
                                        <h5>Active Course Department List</h5>
                                    </div>
                                    <div>
                                        <a href="courseCategory.php?recycle_table"
                                            class="btn btn-info btn-sm text-white">Recycle
                                            Course
                                            Department</a>
                                    </div>
                                </div>
                                <div class="table-responsive">
                                    <table class="table table-hover table-bordered table-sm" id="thisismytable">
                                        <thead>
                                            <tr>
                                                <th>No.</th>
                                                <th>Course Department</th>
                                                <th>Action</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            <?php
                                            $sql = "SELECT * FROM clg_course_cat WHERE recycle = '1' ORDER BY sl_no DESC";
                                            $run = mysqli_query($con, $sql);

                                            $c = 1;
                                            while ( $rows = mysqli_fetch_assoc($run) ) {
                                            echo "
                                            <tr id='$rows[sl_no]'>
                                                <td>
                                                    <span class='badge badge-pill text-dark'>$c</span>
                                                </td>
                                                <td>
                                                    $rows[cat_name]
                                                </td>";
                                                ?>
                                            <td class='text-center align-middle'>
                                                <a class="btn btn-primary btn-sm" style="margin-left:5px;"
                                                    href="courseCategory.php?vm=<?php echo $rows['cat_id']; ?>">View</a>
                                                <a class="btn btn-primary btn-sm" style="margin-left:5px;"
                                                    href="courseCategory.php?edit_id=<?php echo $rows['cat_id']; ?>">
                                                    Edit</a>
                                                <a class="btn btn-primary btn-sm" style="margin-left:5px;"
                                                    href="tag.php?category_tag=<?php echo $rows['cat_id']; ?>">Tag</a>

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

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

    <!-- JavaScript Libraries -->

    <?php include "include/js.php"; ?>
    <script>
    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 Course Catagory';
        }
    }

    function displaySelectedImage(event, elementId) {
        const selectedImage = document.getElementById(elementId);
        const fileInput = event.target;

        if (fileInput.files && fileInput.files[0]) {
            const reader = new FileReader();

            reader.onload = function(e) {
                selectedImage.src = e.target.result;
            };

            reader.readAsDataURL(fileInput.files[0]);
        }
    }
    </script>
    <script>
    CKEDITOR.replace('content', {
        height: 300,
        filebrowserUploadUrl: "upload.php"
    });
    CKEDITOR.replace('content1', {
        height: 300,
        filebrowserUploadUrl: "upload.php"
    });
    </script>

</body>

</html>


<!-- Add to database -->
<?php
  if (isset($_POST['addsubmit'])) {
    $cat_name = mysqli_real_escape_string($con, strip_tags($_POST['cat_name']));
    $cat_seotitle = mysqli_real_escape_string($con, strip_tags($_POST['cat_seotitle']));
    $cat_keyword = mysqli_real_escape_string($con, strip_tags($_POST['cat_keyword']));
    $cat_short_des = mysqli_real_escape_string($con, strip_tags($_POST['cat_short_des']));
    $cat_tag = $_POST['content1'];
    $cat_des = $_POST['content'];
    $cat_id = str_pad(rand(0, 99999999), 8, '0', STR_PAD_LEFT);
    
    $cat_date = date("j F, Y, g:i a");
    $recycle = "1";
    $up_by= $_SESSION['utype'];

    // new File Upload
    $file = $_FILES["file"]["tmp_name"];
    $f = $_FILES["file"]["name"];
    $ext = pathinfo($f,PATHINFO_EXTENSION);
    $fname = rand(10000, 999999).".".$ext;
  

    $courseCatagory_sql = "INSERT INTO clg_course_cat ( cat_id, cat_name, cat_short_des, cat_img, cat_des, cat_seotitle, cat_keyword, cat_tag, cat_date, up_by, recycle) VALUES ( '$cat_id', '$cat_name', '$cat_short_des',  '$fname', '$cat_des', '$cat_seotitle', '$cat_keyword', '$cat_tag', '$cat_date', '$up_by', $recycle)";
    
    if( mysqli_query($con, $courseCatagory_sql) ){
        // Move Files
      move_uploaded_file($file,'../upload/category/'.$fname);
        ?>
<script>
alert("Details Sent Successfully");
window.location = "courseCategory.php";
</script>
<?php
    }
    else{
        ?>
<script>
alert("Something's Wrong");
window.location = "index.php";
</script>
<?php
    }
  }
?>


<!-- Edit database -->
<?php
    if(isset($_POST['edit_users'])){ 
	
        $cat_name = mysqli_real_escape_string($con, strip_tags($_POST['cat_name']));
        $cat_seotitle = mysqli_real_escape_string($con, strip_tags($_POST['cat_seotitle']));
        $cat_keyword = mysqli_real_escape_string($con, strip_tags($_POST['cat_keyword']));
        $cat_short_des = mysqli_real_escape_string($con, strip_tags($_POST['cat_short_des']));
        $cat_tag = $_POST['content1'];
        $cat_des = $_POST['content'];
        
        $cat_edit_date = date("j F, Y, g:i a");
        $edit_by= $_SESSION['utype'];

        
        if ($_FILES["file"]["tmp_name"]) {
            // new File Upload
            $file = $_FILES["file"]["tmp_name"];
            $f = $_FILES["file"]["name"];
            $ext = pathinfo($f, PATHINFO_EXTENSION);
            $fname = rand(10000, 999999) . "." . $ext;
            $sql_select = "SELECT cat_img FROM clg_course_cat WHERE cat_id = '$_GET[edit_id]'";
            $result = $con->query($sql_select);
            if ($result && $row = $result->fetch_assoc()) {
                $previous_logo = $row['cat_img'];
                if (!empty($previous_logo)) {
                    $previous_image_path = '../upload/category/' . $previous_logo;
                    if (file_exists($previous_image_path)) {
                        unlink($previous_image_path);
                    }
                }
            }
        } else {
            // If no new file is uploaded, keep the previous image filename
            $fname = $edit_row['cat_img'];
        }
	
        {
            $up_sql = "UPDATE clg_course_cat SET cat_name='$cat_name', cat_seotitle='$cat_seotitle', cat_keyword='$cat_keyword', cat_short_des='$cat_short_des', cat_tag = '$cat_tag', cat_des='$cat_des', cat_img = '$fname', cat_edit_date = '$cat_edit_date'";
   
        $up_sql .= " WHERE cat_id = '$_GET[edit_id]'";
		 if(mysqli_query($con, $up_sql)){

         // Move Files
         move_uploaded_file($file,'../upload/category/'.$fname);
       
            ?>
<script>
alert("Data Updated Successfully");
window.location = 'courseCategory.php';
</script>
<?php
        }
     else{
?>
<script>
alert('OOPS !!!!');
setTimeout(() => {
    window.location = 'courseCategory.php';
}, 1200);
</script>
<?php
        }
        
?>
<?php
        }
    }

?>


<!-- Recycle database -->
<?php
  if ( isset($_GET["rec_id"]) ) {
    $find_sql = "SELECT * FROM clg_course_cat WHERE sl_no = '$_GET[rec_id]'";
    $find_run = mysqli_query($con, $find_sql);
    if ($f_rows = mysqli_fetch_assoc($find_run)) {
        $old_rec = $f_rows["recycle"];
    
        if ($old_rec == 1) {
            $new_rec = 0;
        } elseif ($old_rec == 0) {
            $new_rec = 1;
        }
    
        $update_sql = "UPDATE clg_course_cat SET recycle = '$new_rec' WHERE sl_no = '$_GET[rec_id]'";
        $update_run = mysqli_query($con, $update_sql);

        if ( $update_run ) {
            ?>
<script>
alert("Sent to Recycle Successfully");
window.location = "courseCategory.php";
</script>
<?php
        } else {
            ?>
<script>
alert('OOPS !!!!');
setTimeout(() => {
    window.location = 'courseCategory.php';
}, 1200);
</script>
<?php
        }
        
    }

}
?>


<!-- delete database -->
<?php
if (isset($_GET['del_id'])) {
    $delete_cat_id = mysqli_real_escape_string($con, $_GET['del_id']);
    
    $delete_sql = "DELETE FROM clg_course_cat WHERE sl_no = '$delete_cat_id'";
    
    if (mysqli_query($con, $delete_sql)) {
        ?>
<script>
alert("Category Deleted Successfully");
window.location = "courseCategory.php?recycle_table";
</script>
<?php
    } else {
        ?>
<script>
alert("Failed to delete category");
window.location = "courseCategory.php?recycle_table";
</script>
<?php
    }
}
?>