<?php

add_action(
	'woocommerce_blocks_validate_location_address_fields',
	function ( \WP_Error $errors, $fields, $group ) {
		$spc = $bpc ='';
		if($group=='shipping'){
			$spc = $fields['postcode'];
		} else{
			$bpc = $fields['postcode'];;
		}
		$method = WC()->session->get( '_user_order_method' );
		$user_date = WC()->session->get( '_user_deli_date' );
		$user_time = WC()->session->get( '_user_deli_time' );
		$user_log = WC()->session->get( '_user_deli_log' );
		// verify Minimum amont and opcls
		$minimum_mes = exwf_minimum_order_amount(true);
		if ( $minimum_mes!='' ) {
			$errors->add( 'exwf_vld_info', strip_tags($minimum_mes) );
		}
		// verify delivery adddress
		$ship_mode = exwoofood_get_option('exwoofood_ship_mode','exwoofood_shpping_options');
		if($method=='delivery'){
			if($ship_mode=='postcode'){
				$user_address = WC()->session->get( '_user_postcode' );
			}else{
				$vrf_addr = '';
				$user_address = WC()->session->get( '_user_deli_adress' );
				//$sta = isset($fields['state']) && $fields['state']!='' ? ' '.$fields['state']:'';
		        //$user_address = $fields['address_1'].' '.$fields['city'].$sta.' '.exwf_get_country_code($fields['country'],true);
		        $vrf_addr = exwf_verify_address_deli_field(true,$user_address,$user_log);
		        ($vrf_addr!='' ? $errors->add( 'exwf_vld_info', $vrf_addr ) : '');
		    }
		}
		// verify date time and location
		$vrf_dtl = exwf_verify_date_deli_field(true,$user_date,$user_time,$user_log);
		($vrf_dtl!='' ? $errors->add( 'exwf_vld_info', $vrf_dtl ) : '');
		// verify location by order method
	    $loc_bymt = exwf_verify_location_by_order_mt(true);
	    ($loc_bymt!='' ? $errors->add( 'exwf_vld_info', $loc_bymt ) : '');
	    // verify product by order method
		$fby_odmt = function_exists('exwf_if_product_is_not_inmethod') ? exwf_if_product_is_not_inmethod(true) : '';
		($fby_odmt!='' ? $errors->add( 'exwf_vld_info', $fby_odmt ) : '');
		//verify product in not in loc or postcode
		$user_postc = $spc!='' ? $spc : $bpc;
		$vrf_floc_pc =  exwf_if_product_is_not_inlocation_orpostcodes(true,$user_postc,$user_log);
		($vrf_floc_pc!='' ? $errors->add( 'exwf_vld_info', $vrf_floc_pc ) : '');
		//verify food by date
		$vrf_fbydate = function_exists('exwf_verify_food_by_date_') ? exwf_verify_food_by_date_(true) : '';
		($vrf_fbydate!='' ? $errors->add( 'exwf_vld_info', $vrf_fbydate ) : '');
	    //$errors->add( 'exwf_vld_info', strip_tags($user_time) );
	},
	10,
	3
);
add_action( 'woocommerce_new_order', 'exwf_save_data_from_session_for_block' );
function exwf_save_data_from_session_for_block($order_id){
	if(WC()->session->get( '_exwf_ckblock' ) !='1'){
		return;
	}
	//Save data for checkout block from session here
	$data = array();
	$data['date'] = WC()->session->get( '_user_deli_date' );
	$data['time'] = WC()->session->get( '_user_deli_time' );
	$data['loc'] = WC()->session->get( '_user_deli_log' );
	$data['person'] = WC()->session->get( '_user_di_person' );
	$data['address'] = WC()->session->get( '_user_deli_adress' );
	exwf_save_date_deli_field_hpos( $order_id,$data );
	WC()->session->set( '_user_deli_date', '');
	WC()->session->set( '_user_deli_time', '');
	WC()->session->set( '_user_deli_log', '');
	WC()->session->set( '_exwf_ckblock', '');
}
add_shortcode( 'exwf_orderinfo', 'exwf_orderinfo__sc' );
function exwf_orderinfo__sc( $atts ) {
	if(phpversion()>=7){
		$atts = (array)$atts;
	}
	$enable_mtod =  isset($atts['enable_mtod']) ? $atts['enable_mtod'] :'';
	if((is_admin()&& !defined( 'DOING_AJAX' )) || (defined('REST_REQUEST') && REST_REQUEST)){ return;}
	ob_start();
	WC()->session->set( '_exwf_ckblock', 1);
	echo '<div class="exwf-ckblock-sc">';
		wp_enqueue_script( 'exwf-ckblock');
		exwf_shipping_method_selectbox();
		echo '<div class="exwf-ckblock-fields" >';
		exwf_date_deli_field();
		echo '</div>';
		exwf_add_user_info_sidecart();
		echo '<div class="ex-fdlist" >';
		global $wp;
		$cr_url =  home_url( $wp->request );
		$cr_url = apply_filters('exwf_current_link',$cr_url);
		echo '<input type="hidden"  name="exwf_current_url" value="'.esc_url($cr_url).'">';
		echo '<input type="hidden"  name="ajax_url" value="'.esc_url(admin_url( 'admin-ajax.php' )).'">';
		if($enable_mtod!='no'){
			exwf_poup_delivery_type_html(array());
		}
		echo '</div>';
	echo '</div>';
	$html = ob_get_contents();
	ob_end_clean();
    return $html;
}
add_action( 'wp_ajax_exwf_ckblock_data', 'ajax_exwf_ckblock_data' );
add_action( 'wp_ajax_nopriv_exwf_ckblock_data', 'ajax_exwf_ckblock_data' );
function ajax_exwf_ckblock_data(){
	if(isset($_POST['time'])){
		WC()->session->set( '_user_deli_time', sanitize_text_field($_POST['time']));
	}
	if(isset($_POST['date'])){
		WC()->session->set( '_user_deli_date', $_POST['date']);
		(function_exists('exwf_query_by_menu_date') ? WC()->session->set( '_menudate', sanitize_text_field($_POST['date'])) :'');
	}
	if(isset($_POST['person'])){
		WC()->session->set( '_user_di_person', sanitize_text_field($_POST['person']));
	}
};
add_action( 'woocommerce_order_details_after_order_table_items', 'exwf_display_deli_address', 12, 1 );
function exwf_display_deli_address($order){
	$deli_to = $order->get_shipping_address_1();
    if($order->get_meta( 'exwfood_deli_address', true )!='' && $deli_to!=''){
	    echo '
	    <tr>
	    	<th>'.esc_html__('Delivery to','woocommerce-food').'</th>
	    	<td> ' . $deli_to . '</td>
	    </tr>';
	}
}