<?php
function exwoofood_get_opcls_of_day($day){
	$opcl_time = exwoofood_get_option('exwfood_'.$day.'_opcl_time','exwoofood_advanced_options');
	// for each location
	$log_selected = exwf_get_loc_selected();
	if($log_selected!=''){
		$loc_opcls = exwoofood_get_option('exwoofood_open_close_loc','exwoofood_advanced_options');
		if($loc_opcls=='yes'){
			$opcl_time_log = get_term_meta( $log_selected, 'exwfood_'.$day.'_opcl_time', true );
			$opcl_time = is_array($opcl_time_log) && !empty($opcl_time_log) ? $opcl_time_log : $opcl_time;
		}
	}
	$html = '';
	if(is_array($opcl_time) && !empty($opcl_time)){
		foreach ($opcl_time as $it_time) {
			$fp = isset($it_time['open-time']) && $it_time['open-time']!='' ? date_i18n(get_option('time_format'),strtotime($it_time['open-time'])) : '';
			$to = isset($it_time['close-time']) && $it_time['close-time']!='' ? date_i18n(get_option('time_format'),strtotime($it_time['close-time'])) : '';
			
			$html .=  $fp .' - '.$to.'</br>';
		}
	}else{
		$html = esc_html__( 'Closed','woocommerce-food');
	}
	return $html;
}
function exwoofood_shortcode_opcls_table( $atts ) {
	if(phpversion()>=7){
		$atts = (array)$atts;
	}
	if(is_admin() || (defined('REST_REQUEST') && REST_REQUEST)){ return;}
	$enable_time = exwoofood_get_option('exwoofood_open_close','exwoofood_advanced_options');
	if($enable_time==''){ return;}
	$img_bg   = isset($atts['img_bg']) &&  $atts['img_bg'] !=''? $atts['img_bg'] : '';
	$location   = isset($atts['location']) &&  $atts['location'] !=''? $atts['location'] : '';
	$phone   = isset($atts['phone']) &&  $atts['phone'] !=''? $atts['phone'] : '';
	$hide_time   = isset($atts['hide_time']) &&  $atts['hide_time'] !=''? $atts['hide_time'] : '';
	$closed_dates = exwoofood_get_option('exwfood_opcl_datetodate','exwoofood_advanced_options');
	$loc_opcls = exwoofood_get_option('exwoofood_open_close_loc','exwoofood_advanced_options');
	ob_start();
	$bg_img = '';
	if(is_numeric($img_bg)){ 
		$image_src = wp_get_attachment_image_src( $img_bg,'full' );
		$bg_img = isset($image_src[0]) ? 'style="background-image: url('.esc_url($image_src[0]).');"' : '';
	}else if($img_bg!=''){
		$bg_img = 'style="background-image: url('.esc_url($img_bg).');"';
	}
	?>
	<div class="exwf-opcls-table" <?php echo $bg_img; ?>>
		<div class="exwf-opcls-table-inner">
		<h3 class="exwf-opcls-title"><?php esc_html_e('Opening hours','woocommerce-food');?></h3>
		<div class="exwf-opcls-table-content">
			<?php
			
			$log_selected = exwf_get_loc_selected();
			$closed_loc = $text ='';
			if($log_selected!=''){
				$loc_opcls = exwoofood_get_option('exwoofood_open_close_loc','exwoofood_advanced_options');
				if($loc_opcls=='yes'){
					$closed_loc = get_term_meta( $log_selected, 'exwfood_loc_closed', true );
				}
			}
			if($enable_time=='closed' || $closed_loc == 'yes'){
				$text = esc_html__( 'Ordering food is now closed','woocommerce-food');
			
			}else{

				if(is_array($closed_dates) && !empty($closed_dates)){
					$cure_time =  exwfd_get_current_time();
					foreach ($closed_dates as $key => $closed_date) {
						$cls_start = isset($closed_date['opcl_start']) ? $closed_date['opcl_start'] :'';
						$cls_end = isset($closed_date['opcl_end']) ? $closed_date['opcl_end'] :'';
						if($cls_start!='' && $cure_time <= $cls_end ){
							$fr_date = date_i18n(get_option('date_format'),$cls_start);
							$to_date = date_i18n(get_option('date_format'),$cls_end);
							if($hide_time!='yes'){
								$fr_date = $fr_date.' '.date_i18n(get_option('time_format'),$cls_start);
								$to_date = $to_date.' '.date_i18n(get_option('time_format'),$cls_end);	
							}
							echo '<div><span>'.esc_html__( 'From: ','woocommerce-food').$fr_date.'<br/>'.esc_html__( 'To: ','woocommerce-food').$to_date.'</span><span>'.esc_html__( 'Closed','woocommerce-food').'</span></div>';
						}
					}
				}


				$mon_html = exwoofood_get_opcls_of_day('Mon');
				echo '<div><span>'.esc_html__( 'Monday','woocommerce-food').'</span><span>'.$mon_html.'</span></div>';
				$tue_html = exwoofood_get_opcls_of_day('Tue');
				echo '<div><span>'.esc_html__( 'Tuesday','woocommerce-food').'</span><span>'.$tue_html.'</span></div>';
				$wed_html = exwoofood_get_opcls_of_day('Wed');
				echo '<div><span>'.esc_html__( 'Wednesday','woocommerce-food').'</span><span>'.$wed_html.'</span></div>';
				$thu_html = exwoofood_get_opcls_of_day('Thu');
				echo '<div><span>'.esc_html__( 'Thursday','woocommerce-food').'</span><span>'.$thu_html.'</span></div>';
				$fri_html = exwoofood_get_opcls_of_day('Fri');
				echo '<div><span>'.esc_html__( 'Friday','woocommerce-food').'</span><span>'.$fri_html.'</span></div>';
				$sat_html = exwoofood_get_opcls_of_day('Sat');
				echo '<div><span>'.esc_html__( 'Saturday','woocommerce-food').'</span><span>'.$sat_html.'</span></div>';
				$sun_html = exwoofood_get_opcls_of_day('Sun');
				echo '<div><span>'.esc_html__( 'Sunday','woocommerce-food').'</span><span>'.$sun_html.'</span></div>';
			}
			echo $text !='' ? '<p class="exfd-out-notice excls-notice">' .$text.'</p>' : '';
			?>
		</div>
		<h3 class="exwf-opcls-tel"><?php echo $phone!='' ? esc_html__('Tel: ','woocommerce-food').'<a href="tel:'.$phone.'">'.$phone.'</a>' : '';?></h3>

		</div>
	</div>
	<?php

	$output_string = ob_get_contents();
	ob_end_clean();
	return $output_string;
}
add_shortcode( 'ex_wf_opcls_table', 'exwoofood_shortcode_opcls_table' );
add_action( 'after_setup_theme', 'ex_reg_wf_opcls_table_vc' );
function ex_reg_wf_opcls_table_vc(){
    if(function_exists('vc_map')){
	vc_map( array(
	   "name" => esc_html__("Opening and Closing table", "woocommerce-food"),
	   "base" => "ex_wf_opcls_table",
	   "class" => "",
	   "icon" => "icon-grid",
	   "controls" => "full",
	   "category" => esc_html__('Woocommerce Food','woocommerce-food'),
	   "params" => array(
	   		array(
			  	"admin_label" => true,
				"type" => "textfield",
				"heading" => esc_html__("Location", "woocommerce-food"),
				"param_name" => "location",
				"value" => "",
				"description" => esc_html__("Show Opening and Closing of special location", 'woocommerce-food'),
			),
			array(
			  	"admin_label" => true,
				"type" => "attach_image",
				"heading" => esc_html__("Background Image", "woocommerce-food"),
				"param_name" => "img_bg",
				"value" => "",
				"description" => esc_html__("Select Background image", 'woocommerce-food'),
			),
			array(
			  	"admin_label" => true,
				"type" => "textfield",
				"heading" => esc_html__("Phone number", "woocommerce-food"),
				"param_name" => "phone",
				"value" => "",
				"description" => esc_html__("Enter the Phone number", 'woocommerce-food'),
			),
			array(
			  	"admin_label" => true,
				"type" => "dropdown",
				"class" => "",
				"heading" => esc_html__("Hide time", 'woocommerce-food'),
				"param_name" => "hide_time",
				"value" => array(
					esc_html__('No', 'woocommerce-food') => '',
				esc_html__('Yes', 'woocommerce-food') => 'yes',
				),
				"description" => esc_html__("Hide time of Closed from date to date", "woocommerce-food"),
			),
	   )
	));
	}
}
