<?php

use WpBookingHotel\Factory;
use WpBookingHotel\Html\WpBookingHotelHtmlFrontend;
use WpBookingHotel\Language\Text;
use WpBookingHotelFrontend\app\helpers\ConfigHelper;
use WpBookingHotelFrontend\app\helpers\FrontendHelper;
$doc= Factory::getDocument();
WpBookingHotelHtmlFrontend::load_jquery_library('fontawesome');
WpBookingHotelHtmlFrontend::load_jquery_library('slick');
$doc->addLessStyleSheet('frontend/blocks/block_popular_hotel/assets/less/block_popular_hotel.less');
$doc->addStyleSheet('frontend/app/assets/nb-font-icon/nb-font-icon.css');

$curent_user_id=Factory::getUser()->id;


$page_hotel_detail= ConfigHelper::get_page_hotel_detail();
if($page_hotel_detail) {
    $this->page_hotel_detail = get_page_link($page_hotel_detail);
}else{
    $this->page_hotel_detail="";
}
$params=$this->item->params;
$title_block=$params->get('title_block','');
$limit=$params->get('limit','');
?>

<div class="block-popular-hotel">
    <div class="title">
        <h3 title="<?php echo (esc_attr($title_block))?>"><?php echo (esc_html($title_block)) ?></h3>
        <div class="block-arrow">
            <a href="javascript:void()" class="prev-arrow">
                <i class="fas fa-long-arrow-alt-left"></i>
            </a>
            <a href="javascript:void()" class="next-arrow">
                <i class="fas fa-long-arrow-alt-right"></i>
            </a>
        </div>
    </div>
    <div class="slide">
        <?php foreach ($list_hotel as $hotel){?>
            <?php
            $list_review = isset($hotel->list_review)?$hotel->list_review:array();
            if(!empty($list_review)) {
                $rating_sum = 0;
                foreach ($list_review as $review) {
                    if(isset($review->rating)) {
                        $rating_sum += $review->rating;
                    }
                }
                $max_star = 5;
                $hotel_rating=$rating_sum / count($list_review);
                while ($hotel_rating > $max_star) {
                    $hotel_rating = $hotel_rating - $max_star;
                }

                $star = $hotel_rating % 10;
                $non_star = ($max_star - $hotel_rating) % 10;
                $half_star = ceil($max_star-$star-$non_star);
            }
            ?>
            <div class="box-hotel row-1 box-1 wishlist-item" data-hotel_id= "<?php echo $hotel->id ?>" data-user_id= "<?php echo $curent_user_id ?>" >
                    <div class="img-block">
                        <a href="<?php echo($this->page_hotel_detail.'?hotel_id='.$hotel->id);?>" style="display: block">
                            <div class="wishlist-hover">
                                <img class="lazy" data-src="<?php echo(Factory::getRootUrlPlugin())?><?php echo(isset($hotel->image)?$hotel->image:'');?>"
                                    alt="<?php echo (isset($hotel->name)?$hotel->name:'');?>">
                            </div>
                        </a>
                        <div class="category-name-heart">
                            <span class="heart <?php echo in_array($curent_user_id,$hotel->user_wishlist) ? 'active':'' ?> ">
                                <i class="fas fa-heart"></i>
                            </span>
                        </div>
                    </div>
                    <div class="block-content">
                        <div class="block-rating">
                            <ul class="list-rating-star">
                                <?php if(!empty($list_review)) {?>
                                    <?php for ($i=0; $i <$star; $i++) {?>
                                        <li class="item-star"><i class="fas fa-star"></i></li>
                                    <?php }?>
                                    <?php for ($i=0; $i <$half_star; $i++) {?>
                                        <li class="item-star"><i class="fas fa-star-half-alt"></i></li>
                                    <?php }?>
                                    <?php for ($i=0; $i <$non_star; $i++) {?>
                                        <li class="item-star"><i class="far fa-star"></i></li>
                                    <?php }?>
                                <?php } else {?>
                                    <?php for($i = 1; $i <= 5; $i++) {?>
                                        <li class="item-star"><i class="far fa-star"></i></li>
                                    <?php }?>
                                <?php }?>
                            </ul>
                            <div class="rating-review">
                                <span><?php echo (esc_html(count($list_review)));?></span>
                                <span><?php esc_attr_e('Reviews','wpbookinghotel');?></span>
                            </div>
                        </div>
                        <a href="<?php echo($this->page_hotel_detail.'?hotel_id='.$hotel->id);?>" style="display: block">
                            <div class="name-hotel text-1">
                                <h4 title="<?php echo (isset($hotel->name)?$hotel->name:'');?>"><?php echo (isset($hotel->name)?$hotel->name:'');?></h4>
                            </div>
                        </a>
                        <div class="address-hotel text-1">
                            <i class="fas fa-map-marker-alt"></i>
                            <span title="<?php echo (isset($hotel->destination)?$hotel->destination->address:'');?>">
                            <?php echo (isset($hotel->destination)?$hotel->destination->address:'');?></span>
                        </div>
                    </div>
                <div class="price">
                    <span><?php echo(FrontendHelper::format_currency(isset($hotel->price)?$hotel->price:0))?></span>
                    <span><?php esc_html_e('Night','wpbookinghotel');?></span>
                </div>
            </div>
        <?php }?>
    </div>

</div>
<?php
$doc->addScriptApplySelector('.block-popular-hotel','block_popular_hotel',array(

),'frontend/blocks/block_popular_hotel/assets/js/block_popular_hotel.js');
?>
