<?php
include_once('include/functions.php');

$course_id = $_POST['course_id'] ?? '';
$limit = 4;

$latest_news = get_courses_display($course_id,'','',1,$limit);
$cart_ids = get_cart_details();

for($i=0;$i<count($latest_news);$i++)
{
    if(in_array($latest_news[$i]['id'],$cart_ids))
        $add=1;
    else
        $add=0;
?>

<div class="col-lg-4 col-md-4 col-sm-6">
    <div class="product-grid">
        <div class="product-image">

            <?php if(!empty($latest_news[$i]['course_img'])){ ?>
                <img src="../instructor/courses_upload/<?php echo $latest_news[$i]['course_img']; ?>">
            <?php } else { ?>
                <img src="assets/images/banner/course-bg-p2.png">
            <?php } ?>

         	 <ul class="social">
				<li> 
                   <a href="view_course.php?course_id=<?php echo $latest_news[$i]['id']; ?>" ><i class="ti-zoom-in"></i></a>
                </li>
				<li><a href="" data-tip="Add to Wishlist"><i class="ti-bag"></i></a></li>
										
                <li><a class="cart_main_add<?php echo $latest_news[$i]['id']; ?>" onclick="add_to_cart('<?php echo $latest_news[$i]['id']; ?>','<?php echo $_SESSION['login_id']; ?>','add')" data-tip="Add to Cart"><i class="ti-shopping-cart"></i></a></li>
                                        
                <li><a style="background-color:#2eca7f" class="cart_main_remove<?php echo $latest_news[$i]['id']; ?>" onclick="add_to_cart('<?php echo $latest_news[$i]['id']; ?>','<?php echo $_SESSION['login_id']; ?>','remove')" data-tip="Remove From Cart"><i class="ti-close text-light"></i></a></li>
                                        
            </ul>

        </div>

        <div class="product-content">
            <h3 class="title"><a href="view_course.php?course_id=<?php echo $latest_news[$i]['id']; ?>">
									<?php 
                                        $text = $latest_news[$i]['course_title'];
										$text = trim($text);
										
										if (mb_strlen($text) > 26) {
											echo mb_substr($text, 0, 26) . '...';
										} else {
											echo $text;
										}
                                    ?></a></h3>
			<div class="price">$<?php echo $latest_news[$i]['course_price']/100*$latest_news[$i]['course_discount']; ?>
				<span>$<?php echo $latest_news[$i]['course_price']; ?></span>
			</div>
			<a class="add-to-cart" href="">+ Add To Cart</a>

          
        </div>
    </div>
</div>

<script>
toggle_cart('<?php echo $latest_news[$i]['id']; ?>','<?php echo $add; ?>');
</script>

<?php } ?>