<?php
    // session_start();
    include "../db.php";
    require '../auth.php';
    restrictAccess(['Associate']);
    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();
    }
?>

<!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["view_id"])){
                    $view_sql = "SELECT * FROM clg_course WHERE sl_no ='$_GET[view_id]'";
                    $view_data = mysqli_query($con,$view_sql);
                    if($view_rows = mysqli_fetch_array($view_data)){
                        $update_count = $view_rows["count"] + 1;
                        
                        $updare_sql = mysqli_query($con, "UPDATE clg_course SET count = '$update_count' WHERE sl_no='$_GET[view_id]'");
                    ?>
                <div class="card">
                    <div class="card-body">
                        <div class="row">
                            <div class="col-md-8 mb-2">
                                <div class="mb-2">
                                    <?php if (!empty($view_rows['course_image'])){ ?>
                                    <img src="<?php 
                                    if (!empty($view_rows["course_image"])) {
                                        $image_path = $view_rows["course_image"];
                                        $image_extension = strtolower(pathinfo($image_path, PATHINFO_EXTENSION));

                                        // List of valid image extensions
                                        $valid_image_extensions = ["jpg", "jpeg", "png", "gif"];

                                        // Check if the file extension is a valid image type
                                        if (in_array($image_extension, $valid_image_extensions)) {
                                            echo '../upload/course/' . $image_path;
                                        } else {
                                            echo "./assets/image/favicon.ico";
                                        }
                                    } else {
                                        echo "./assets/image/favicon.ico";
                                    }
                                    ?>" class="img-fluid" alt="<?php echo $view_rows["course_name"];?>">
                                    <?php } ?>
                                </div>
                                <hr>
                                <div class="mb-2">
                                    <h4><?php echo $view_rows['course_name']; ?></h4>
                                    <div id="ck-img-preview">
                                        <?php echo $view_rows['course_des']; ?>
                                    </div>
                                </div>
                            </div>
                            <div class="col-md-4">
                                <div class="mb-2" style="padding: 20px 10px;background: #ddd;border-radius: 5px;">
                                    <h5>Premium Courses</h5>
                                    <?php
                                         $blog_sql = "SELECT * FROM clg_course WHERE recycle = '1' ORDER BY course_name ASC";
                                         $blog_run = mysqli_query($con, $blog_sql);
     
                                         $c = 1;
                                         while ( $blog_rows = mysqli_fetch_assoc($blog_run) ) {
                                            if($blog_rows["sl_no"] != $_GET["view_id"]){
                                                ?>
                                    <div class="card mb-2 p-2">
                                        <a href="service.php?view_id=<?php echo $blog_rows["sl_no"];?>"
                                            style="text-decoration:none;color:#000;">
                                            <div class="row align-items-center">
                                                <div class="col-md-2 col-2 text-center">
                                                    <img src="<?php 
                                                    if (!empty($blog_rows["course_image"])) {
                                                        $image_path = $blog_rows["course_image"];
                                                        $image_extension = strtolower(pathinfo($image_path, PATHINFO_EXTENSION));

                                                        // List of valid image extensions
                                                        $valid_image_extensions = ["jpg", "jpeg", "png", "gif"];

                                                        // Check if the file extension is a valid image type
                                                        if (in_array($image_extension, $valid_image_extensions)) {
                                                            echo '../upload/course/' . $image_path;
                                                        } else {
                                                            echo "./assets/image/favicon.ico";
                                                        }
                                                    } else {
                                                         echo "./assets/image/favicon.ico";
                                                    }
                                                    ?>" class="img-fluid"
                                                        alt="<?php echo $blog_rows["course_name"];?>">
                                                </div>
                                                <div class="col-md-10 col-10">
                                                    <h6 class="m-0">
                                                        <?php echo $blog_rows['course_name']; ?>
                                                    </h6>
                                                </div>
                                            </div>
                                        </a>
                                    </div>
                                    <?php 
                                            }}
                                        ?>
                                </div>
                            </div>
                        </div>
                        <br>
                        <a href="service.php?show_list" class="btn btn-warning text-white btn-user m-2"> <i
                                class="fa fa-arrow-left"></i>
                            Back </a>
                    </div>
                </div>
                <?php } } ?>
                <?php if(isset($_GET["show_list"])){?>
                <div class="card">
                    <div class="card-header">
                        <h3 class="m-0 text-center">Course List</h3>
                    </div>
                    <div class="card-body">
                        <div class="row justify-content-center">
                            <?php 
                            $cat_sql = mysqli_query($con, "SELECT * FROM clg_course_cat WHERE recycle = '1' ORDER BY cat_name ASC");
                            while($cat_row = mysqli_fetch_assoc($cat_sql)){
                                ?>
                            <div class="col-md-4 mb-3">
                                <div class="card">
                                    <div class="card-header text-center"><?php echo $cat_row["cat_name"];?></div>
                                    <div class="card-body">
                                        <div class="row justify-content-center align-items-center mt-3">
                                            <?php
                                                $sql = "SELECT * FROM clg_course WHERE  course_cat = '{$cat_row["cat_id"]}' AND recycle = '1' ORDER BY course_name ASC";
                                                $run = mysqli_query($con, $sql);

                                                $c = 1;
                                                while ( $rows = mysqli_fetch_assoc($run) ) {
                                            ?>
                                            <div class="col-md-12 mb-2">
                                                <a href="service.php?view_id=<?php echo $rows["sl_no"];?>"
                                                    style="text-decoration:none;">
                                                    <div class="blog-preview">
                                                        <div class="row align-items-center">
                                                            <div class="col-lg-2 col-md-2 col-2">
                                                                <img src="<?php 
                                                                if (!empty($rows["course_image"])) {
                                                                    $image_path = $rows["course_image"];
                                                                    $image_extension = strtolower(pathinfo($image_path, PATHINFO_EXTENSION));

                                                                    // List of valid image extensions
                                                                    $valid_image_extensions = ["jpg", "jpeg", "png", "gif"];

                                                                    // Check if the file extension is a valid image type
                                                                    if (in_array($image_extension, $valid_image_extensions)) {
                                                                        echo "../upload/course/" . $image_path;
                                                                    } else {
                                                                        echo "./assets/image/favicon.ico";
                                                                    }
                                                                } else {
                                                                    echo "./assets/image/favicon.ico";
                                                                }
                                                                ?>" class="img-fluid"
                                                                    alt="<?php echo $rows['course_name']; ?>">
                                                            </div>
                                                            <div class="col-lg-10 col-md-10 col-10">
                                                                <h6 class="m-0"><?php echo $rows['course_name']; ?></h6>

                                                            </div>
                                                        </div>
                                                    </div>
                                                </a>
                                            </div>
                                            <?php $c++; }?>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <?php
                            }
                            ?>
                        </div>
                    </div>
                </div>
                <?php }?>
            </div>
        </div>

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

    <!-- JavaScript Libraries -->

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

</body>

</html>