<?php

use WpBookingHotel\Factory;
use WpBookingHotel\Language\Text;
use WpBookingHotelFrontend\app\helpers\CurrencyHelper;
use WpBookingHotelFrontend\app\helpers\FrontendHelper;
use WpBookingHotelFrontend\app\Html\HtmlFrontend;
use WpBookingHotel\Router\Route;
$hotel = $this->item;

$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);
}
$input = Factory::getInput();
$near_me=$input->getInt('near_me',0);
$appConfig = Factory::getAppConfig();
$unit_for_search = $appConfig->get('unit_for_search', '');

?>
<div class="box-hotel row-1 box-1">
    <a href="<?php echo esc_url(Route::_($this->page_hotel_detail.'?hotel_id='.$hotel->id,0)); ?>" style="display: block">
        <div class="img-block">
            <div>
                <img src="<?php echo(Factory::getRootUrlPlugin())?><?php echo (isset($hotel->image)?$hotel->image:'');?>"
                     alt="<?php echo (isset($hotel->name)?$hotel->name:'');?>">
            </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_html_e('Reviews','wpbookinghotel');?></span>
                </div>
            </div>
            <div class="name-hotel text-1">
                <h4 title="<?php echo (isset($hotel->name)?$hotel->name:'');?>"><?php echo (isset($hotel->name)?$hotel->name:'');?></h4>
            </div>
            <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>
    </a>
    <div class="price">
        <span><?php echo(CurrencyHelper::convertSymbols(CurrencyHelper::convertCurrencyFront((float)isset($hotel->price)?$hotel->price:0)))?></span>
        <span><?php esc_html_e('Night','wpbookinghotel');?></span>
    </div>
    <?php if($near_me == 1) {?>
        <div class="distance-between-your-location">
            <i class="fas fa-map-pin"></i>
            <span>
                        <?php echo (esc_html(isset($hotel->distance)?$hotel->distance:0))?>
                        <?php
                        if($unit_for_search == 'Km') {
                            echo (esc_html('Km'));
                        } elseif ($unit_for_search='Mi') {
                            echo (esc_html('Mile'));
                        }
                        ?>
                    </span>
        </div>
    <?php }?>
</div>
