<?php
add_action('wpc_multi_receiver_popup_after_header_title', 'wpc_multi_receiver_popup_button_callback', 10, 1);
add_action('wpcargo_before_shipment_meta_section', 'wpc_multi_receiver_popup_section_callback', 10, 1);
add_action('wpc_cf_after_display_edit_section', 'wpc_multi_receiver_popup_edit_settings_callback', 10, 1);
add_action('wpc_cf_after_form_field_add', 'wpc_multi_receiver_popup_add_settings_callback', 10, 1);
add_action('wpcargo_track_shipment_details', 'wpc_multi_receiver_popup_track_details_callback', 5, 1);
// Frontend Dashboard Hook
function wpc_multi_receiver_plugins_loaded_callback(){
	add_action('before_wpcfe_wpc_multiple_reciever_form_fields', 'wpc_multi_receiver_dashboard_callback', 1, 1);
	add_action('wpcsr_after_receiver_info', 'wpcmr_srate_dashboard_callback', 1, 1);
	add_action('wpc_pq_before_shipment_info', 'wpcmr_pq_dashboard_callback', 1, 1);
	add_action('after_wpcshcon_receiver_info_form_fields', 'wpcmr_srate_dashboard_callback', 1, 1);
	add_action('wpcshco_before_submit_order_fields', 'wpcshco_pq_after_submit_order_callback', 1, 1);
	add_action('wpcshcon_after_consolidate_details', 'wpcshcon_pq_after_consolidate_details_callback', 1, 1);
	
	add_filter( 'wpcfe_shipment_sections', 'wpc_multi_receiver_dashboard_section_callback' );
	add_filter( 'wpcfe_registered_scripts', 'wpcmr_registered_scripts_callback' );
	// Saving data
	add_action( 'after_wpcfe_save_shipment', 'wpcmr_after_wpcfe_save_shipment',10, 2 );
	add_action( 'wpcsr_after_save_shipment', 'wpcmr_after_wpcfe_save_shipment',10, 2 );
	add_action( 'wpcpq_after_save_shipment_details', 'wpcmr_after_wpcfe_save_shipment',10, 2 );
	add_action( 'wpcshco_after_submit_save_order_action', 'wpcmr_after_wpcfe_save_shipment',10, 2 );
	
	add_action( 'wp_head', 'wpcmr_modal_form_pop_callback' );
	// Print 
	add_action( 'wpcfe_end_invoice_section', 'wpcmr_frontend_print_template_hook' );
	add_action( 'wpcfe_middle_bol_section', 'wpcmr_frontend_print_template_hook' );
	add_action( 'wpcfe_end_label_section', 'wpcmr_frontend_print_template_hook' );
}
add_action( 'plugins_loaded', 'wpc_multi_receiver_plugins_loaded_callback' );

function wpc_multi_receiver_dashboard_section_callback( $sections ){
	$new_sections = array();
	foreach ($sections as $key => $value ) {
		$new_sections[ $key ] = $value;
		if( 'receiver_info' == $key ){
			$new_sections[ 'wpc_multiple_reciever' ] = 'Multiple Receiver';
		}
	}
	return $new_sections;
}
function wpcmr_after_wpcfe_save_shipment(  $post_id, $data ){
	if( isset( $data['multi-receiver'] ) && !empty( $data['multi-receiver']  ) ){
		update_post_meta( $post_id, 'multi-receiver', $data['multi-receiver'] );
	}else{
		update_post_meta( $post_id, 'multi-receiver', '' );
	}
}
	
function wpcmr_modal_form_pop_callback(){
	global $post, $WPCCF_Fields;
	if( empty( $post ) ){
		return false;
	}
	$template = get_page_template_slug( $post->ID );
	if( 'dashboard.php' === $template || ( function_exists('wpcfe_admin_page') && $post->ID == wpcfe_admin_page() ) ){
		include_once( WPCARGO_MULTI_RECEIVER_POPUP_PATH.'templates/modal.tpl.php');
	}	
}
function wpcmr_registered_scripts_callback( $scripts ){
	$scripts[] = 'wpcmr-scripts';
	return $scripts;
}
//** Hook search result
add_action('wpcargo_track_shipper_details', 'wpc_multi_receiver_popup_search_result_callback', 10, 1 );
add_action('admin_footer', 'wpc_multi_receiver_popup_modal_callback' );
function wpc_multi_receiver_popup_modal_callback(){
	?>
	<div id="multi-receiver-wrapper" title="<?php esc_html_e('Add Additional Receiver', 'wpcargo-multi-receiver-popup'); ?>" style="display:none;">    
	    <div id="additional-receiver">
			<?php
			if( function_exists( 'wpc_address_book_get_list' ) ):
				$address_list   = wpc_address_book_get_list('receiver');
				$search_option 	= get_option('wpc_address_receiver_search');
				// Dont display option if the address is empty
				if( !empty( $address_list ) ):
					uasort($address_list, function( $receiver_a, $receiver_b ){
						return strcmp($receiver_a["wpcargo_receiver_name"], $receiver_b["wpcargo_receiver_name"]);
					});
					?>
					<table class="form-table">
						<tr>
							<th><?php esc_html_e('Search Receiver', 'wpcargo-multi-receiver-popup'); ?></th>
							<td>
							<select class="form-control browser-default custom-select wpcmr_autofill" id="wpcmr-address-list">
								<option value=""><?php esc_html_e('-- Select One --', 'wpcargo-multi-receiver-popup'); ?></option>
								<?php foreach ( $address_list as $key => $address ): ?>
									<?php
										if( $search_option ){
											$label = $address[$search_option];
										}else{
											$address = array_values( $address );
											$label = $address[0];
										}
									?>
									<option value="<?php echo $key; ?>"><?php echo $label; ?></option>
								<?php endforeach; ?>
							</select>
							</td>
						</tr>
					</table>

					<?php 
				endif; 
			endif; // End- Checking if Address Book is installed and activated 
			?>
	        <?php wpc_multi_receiver_popup_form_fields(); ?>   
	        <button id="submit-add-receiver" class="button button-primary button-large button-wpcargo"><?php esc_html_e('Add Receiver', 'wpcargo-multi-receiver-popup'); ?></button>        
	    </div>
	</div>
	<?php
}
// Common Functions
// Format receiver fields copy to avoid jquery conflict with the id and class attribute
function wpcmr_registered_fields(){
	$formatted_fields 	= array();
	$key_prefix 	 	= 'wpcmr-';
	$fields	= wpc_get_multi_receiver_popup_form_fields( 'ARRAY_A' );
	if( !empty( $fields ) ){
		foreach ($fields as $field ) {
			if( 'file' == $field['field_type'] ){
				continue;
			}
			$field['id'] 			= $key_prefix.$field['id'];
			$field['field_key'] 	= $key_prefix.$field['field_key'];
			$formatted_fields[] = $field;
		}
	}
	return $formatted_fields;
}
function wpcmr_registered_key_label_option(){
	$key_label 	= array();
	$key_prefix = 'wpcmr-';
	$fields	= wpc_get_multi_receiver_popup_form_fields( 'ARRAY_A' );
	if( !empty( $fields ) ){
		foreach ($fields as $field ) {
			if( 'file' == $field['field_type'] || 'url' == $field['field_type'] ){
				continue;
			}
			$key_label[$key_prefix.$field['field_key']] = array(
				'label' => $field['label'],
				'type' => $field['field_type'],
			);
		}
	}
	return $key_label;
}
// Print FM hooks
function wpcmr_frontend_print_template_hook( $shipmentDetails  ){
	$shipment_id = $shipmentDetails['shipmentID'];
	$multi_receiver = maybe_unserialize( get_post_meta( $shipment_id, 'multi-receiver', TRUE ) );
	if( empty( $multi_receiver ) || !is_array( $multi_receiver ) ){
		return false;
	}
	$counter = 1;
	?>
	<tr>
        <td colspan="2" class="space-topbottom">
			<p style="font-size:1.2rem;margin-bottom:18px;"><strong><?php echo wpc_mrpopup_additional_receiver_label(); ?></strong></p>
			<table style="width:100%;border-collapse: collapse;border: none;">		
				<?php foreach ($multi_receiver as $receiver): ?>
					<?php echo $counter % 2 ? '<tr style="border: none;">' : '' ; ?>
						<td style="width:50%;border: none;" class="space-topbottom">
							<?php foreach ($receiver as $key => $value  ): ?>
								<p><span class="label"><?php echo get_form_label( $key ); ?></span> : <span class="value"><?php echo $value; ?></span></p>
							<?php endforeach; ?>
						</td>
						<?php echo !( $counter % 2 ) ? '</tr>' : '' ;  $counter++; ?>
				<?php endforeach; ?>
			</table>
        </td>
    </tr>
	<?php
}


