<?php

global $listingpro_options;

$listing_mobile_view    =   $listingpro_options['single_listing_mobile_view'];

$format = $listingpro_options['timing_option'];

$buisness_hours = listing_get_metabox('business_hours');

if(!empty($buisness_hours) && is_array($buisness_hours)){

    $lat = listing_get_metabox('latitude');
    $long = listing_get_metabox('longitude');
    //$timezone = getClosestTimezone($lat, $long);

    $timezone  = get_option('gmt_offset');
    $time = gmdate("H:i", time() + 3600*($timezone+date("I")));
    $day =  gmdate("l");
    $lang = get_locale();
    setlocale(LC_ALL, $lang.'.utf-8');
    $day = strftime("%A");
    $day = ucfirst($day);
    $time = strtotime($time);
    $dayName = esc_html__('Today','listingpro');

    foreach($buisness_hours as $key=>$value){
        $keyArray[] = $key;
        if($day == $key){
            $opencheck = $value['open'];
            $open = $value['open'];
            $open = str_replace(' ', '', $open);
            $close = $value['close'];
            $closecheck = $value['close'];
            $close = str_replace(' ', '', $close);
            $open = @strtotime($open);
            $close = @strtotime($close);

            if(!empty($format) && $format == '24'){
                $newTimeOpen = date("H:i", $open);
                $newTimeClose = date("H:i", $close);
            }else{
                $newTimeOpen = date('h:i A', $open);
                $newTimeClose = date('h:i A', $close);
            }

            echo '<div class="lp-today-timing"><strong>'.listingpro_icons('todayTime').' '.$dayName.'</strong>';
            if( empty($opencheck) && empty($closecheck) ){
                echo '<span class="lp-timing-status pull-right status-open">'.esc_html__('24 hours open','listingpro').'</span>';
            }
            elseif($time > $open && $time < $close){
                echo '<span class="lp-timing-status status-open">'.esc_html__('Open Now~','listingpro').'</span>';
            }else{
                echo '<span class="lp-timing-status status-close">'.esc_html__('Closed Now!','listingpro').'</span>';
            }

            if( $listing_mobile_view == 'responsive_view' || !wp_is_mobile() ){
                if( !empty($opencheck) && !empty($closecheck) ){
                    echo '<span class="lp-timings">'.$newTimeOpen.' - '.$newTimeClose.'</span>';
                }
            }
            echo '</div>';
        }
    }

    if(is_array($keyArray) && !in_array($day, $keyArray)){
        echo '<div class="lp-today-timing"><strong>'.listingpro_icons('todayTime').' '.$dayName.'</strong>';
        echo '<span class="lp-timing-status pull-right status-close">'.esc_html__('Day Off!','listingpro').'</span></div>';
    }
    echo '<ul class="lp-today-timing all-days-timings">';
    foreach($buisness_hours as $key=>$value){
        $dayName = $key;
        $opencheck = $value['open'];
        $open = $value['open'];
        $open = str_replace(' ', '', $open);
        $close = $value['close'];
        $closecheck = $value['close'];
        $close = str_replace(' ', '', $close);
        $open = @strtotime($open);
        $close = @strtotime($close);
        if(!empty($format) && $format == '24'){
            $newTimeOpen = date("H:i", $open);
            $newTimeClose = date("H:i", $close);
        }else{
            $newTimeOpen = date('h:i A', $open);
            $newTimeClose = date('h:i A', $close);
        }
        echo '<li><strong>'.$dayName.'</strong>';
        if(!empty($opencheck)&& !empty($closecheck)){
            echo '<span class="lp-timings">'.$newTimeOpen.' - '.$newTimeClose.'</span></li>';
        }
        else{
            echo '<span class="lp-timing-status pull-right status-open">'.esc_html__('24 hours open', 'listingpro').'</span></li>';

        }

    }

    echo '</ul><a data-contract="'. esc_html__( 'Contract', 'listingpro' ) .'" data-expand="'. esc_html__( 'Expand', 'listingpro' ) .'" href="#" class="toggle-all-days"><i class="fa fa-plus" aria-hidden="true"></i> '. esc_html__( 'Expand', 'listingpro' ) .'</a>';



}

?>

