<?php
	global $WPCCF_Fields;
    // Prepare Varaibles
    $origin_id          = (int)$_POST['origin'];
    $origin             = get_wpcsr_loc_data( $origin_id, 'location' );
    $orig_postcode      = $_POST['origin_postcode'];
    $destination_id     = (int)$_POST['destination'];
    $destination        = get_wpcsr_loc_data( $destination_id, 'location' );
    $dest_postcode      = $_POST['destination_postcode'];
    $packages 		    = isset( $_POST['wpc-multiple-package'] ) ? unserialize( base64_decode( $_POST['wpc-multiple-package'] ) ) : array();
    $package_fields     = wpcargo_package_fields();
    $package_breakdown  = array();
    $total_pallet       = wpcstl_get_pallet_number( $packages );
    $total_weight       = wpcstl_get_total_weight( $packages );
    $total_cost         = wpcstl_get_pallet_cost( $origin_id, $destination_id, $packages );
	$additional_cost    = wpcstl_get_gst( $total_cost );

	// Format fields 
	// Shipper Fields
	$formatted_fshipper = array_filter( $shipper_fields, function( $arr ){
		$excluded = array( WPCSTL_SHIPPER_POSTCODE_METAKEY, WPCSTL_SHIPPER_EMAIL_METAKEY, WPCSTL_SHIPPER_PHONE_METAKEY);
		return !in_array( $arr['field_key'], $excluded );
	} );
	$formatted_efshipper = array_filter( $shipper_fields, function( $arr ){
		return $arr['field_key'] == WPCSTL_SHIPPER_EMAIL_METAKEY;
	} );
	$formatted_efsphone = array_filter( $shipper_fields, function( $arr ){
		return $arr['field_key'] == WPCSTL_SHIPPER_PHONE_METAKEY;
	} );
	// Receiver Fields
	$formatted_freceiver = array_filter( $receiver_fields, function( $arr ){
		$excluded = array( WPCSTL_RECEIVER_POSTCODE_METAKEY, WPCSTL_RECEIVER_EMAIL_METAKEY, WPCSTL_RECEIVER_PHONE_METAKEY);
		return !in_array( $arr['field_key'], $excluded );
	} );
	$formatted_efreceiver = array_filter( $receiver_fields, function( $arr ){
		return $arr['field_key'] == WPCSTL_RECEIVER_EMAIL_METAKEY;
	} );
	$formatted_efrphone = array_filter( $receiver_fields, function( $arr ){
		return $arr['field_key'] == WPCSTL_RECEIVER_PHONE_METAKEY;
	} );

	$formatted_efsphone = array_shift($formatted_efsphone);
	$formatted_efrphone = array_shift($formatted_efrphone);

?>
<section class="wpcargo-container">
	<div class="wpcargo-row">
		<div class="wpcargo-col-md-12">
			<div id="parcel-rate" class="wpcsr-card">
				<div class="wpcsr-card-header">
					<h2 class="h3 text-center mt-4">Freight Information</h2>               
				</div>
				<div class="wpcsr-card-body wpcsr-rate-list">
					<div class="wpcargo-row">
						<div class="wpcargo-col-md-4 wpcsr-details">
							<p><span class="wpcstl-label">Origin:</span> <span class="font-weight-bold"><?php echo $origin; ?></span></p>
							<!-- <p><span class="wpcstl-label">Postcode:</span> <span class="font-weight-bold"><?php //echo $orig_postcode; ?></span></p> -->
            				<p><span class="wpcstl-label">Detination:</span> <span class="font-weight-bold"><?php echo $destination; ?></span></p>
            				<!-- <p><span class="wpcstl-label">Postcode:</span> <span class="font-weight-bold"><?php //echo $dest_postcode; ?></span></p> -->
						</div>
						<div class="wpcargo-col-md-4 wpcsr-payment-details">
							<p><span class="wpcstl-label">Total Pallet:</span> <span class="font-weight-bold"><?php echo $total_pallet; ?></span></p>
							<p><span class="wpcstl-label">Total Weight:</span> <span class="font-weight-bold"><?php echo $total_weight.' '.wpcargo_package_settings()->weight_unit; ?></span></p>
							<p><span class="wpcstl-label">Freight Cost:</span> <span class="font-weight-bold"><?php echo format_wpcstl_currency( $total_cost, true ); ?></span></p>
							<p><span class="wpcstl-label">Insurance:</span> <span class="font-weight-bold"><?php echo format_wpcstl_currency( $additional_cost['insurance'], true ); ?></span></p>
							<p style="margin-bottom:18px;"><span class="wpcstl-label">GST:</span> <span class="font-weight-bold"><?php echo format_wpcstl_currency( $additional_cost['gst'], true ); ?></span></p>
							<p><span class="wpcstl-label">Total Cost:</span> <span class="font-weight-bold"><?php echo format_wpcstl_currency( $additional_cost['subtotal'], true ); ?></span></p>
						</div>
					</div>
				</div>
			</div>
		</div>
	</div>
</section>
<form id="wpcsr-create-order" method="post" action="" enctype="multipart/form-data" >
	<?php wp_nonce_field( 'wpcstl_create_order_action', 'wpcstl_create_order_nonce' ); ?>
	<?php do_action('wpcsr_create_order_before_form_wrapper'); ?>
	<input type="hidden" name="wpcsr_rate" value="1">
	<input type="hidden" name="wpcsr_origin" value="<?php echo $origin_id; ?>">
	<input type="hidden" name="wpcsr_origin_postcode" value="<?php echo $orig_postcode; ?>">
	<input type="hidden" name="wpcsr_destination" value="<?php echo $destination_id; ?>">
	<input type="hidden" name="wpcsr_destination_postcode" value="<?php echo $dest_postcode; ?>">
	<div class="wpcargo-container">
		<div id="parcel-container" class="wpcargo-row">
			<div id="shipper-info" class="section wpcargo-col-md-6">
				<h4><?php echo wpcsr_shipper_information_label(); ?></h4>
				<?php $WPCCF_Fields->convert_to_form_fields( $formatted_fshipper ); ?>
				<div class="form-group">
			        <label ><?php echo wpcstl_key_metakey_label(WPCSTL_SHIPPER_POSTCODE_METAKEY); ?></label>
			        <input type="text" class="form-control "value="<?php echo $orig_postcode; ?>" disabled>
			    </div>
				<?php $WPCCF_Fields->convert_to_form_fields( $formatted_efshipper ); ?>
				<?php //$WPCCF_Fields->convert_to_form_fields( $formatted_efphone ); ?>
				<div class="form-group">
					<label for="<?php echo $formatted_efsphone['field_key']; ?>" class="d-block"><?php echo $formatted_efsphone['label']; ?></label>
					<input type="tel" class="form-control" id="<?php echo $formatted_efsphone['field_key']; ?>" name="<?php echo $formatted_efsphone['field_key']; ?>" value="" required>
					<span class="valid-msg text-success d-none">✓ Valid</span>
					<span class="error-msg text-danger d-none"></span>
				</div>
			</div>
			<div id="receiver-info" class="section wpcargo-col-md-6" >
				<h4><?php echo wpcsr_receiver_information_label(); ?></h4>
				<?php $WPCCF_Fields->convert_to_form_fields( $formatted_freceiver ); ?>
				<div class="form-group">
			        <label ><?php echo wpcstl_key_metakey_label(WPCSTL_RECEIVER_POSTCODE_METAKEY); ?></label>
			        <input type="text" class="form-control "value="<?php echo $dest_postcode; ?>" disabled>
			    </div>
				<?php $WPCCF_Fields->convert_to_form_fields( $formatted_efreceiver ); ?>
				<?php //$WPCCF_Fields->convert_to_form_fields( $formatted_efphone ); ?>
				<div class="form-group">
					<label for="<?php echo $formatted_efrphone['field_key']; ?>" class="d-block"><?php echo $formatted_efrphone['label']; ?></label>
					<input type="tel" class="form-control" id="<?php echo $formatted_efrphone['field_key']; ?>" name="<?php echo $formatted_efrphone['field_key']; ?>" value="" required>
					<span class="valid-msg text-success d-none">✓ Valid</span>
					<span class="error-msg text-danger d-none"></span>
				</div>
			</div>
			<?php do_action('wpcsr_after_receiver_info', $rate_id); ?>
			<div id="shipment_info" class="section wpcargo-col-md-12" style="display:block; clear:both;" >
				<h4><?php echo wpcsr_shipment_information_label(); ?></h4>
				<div class="row">
					<?php $WPCCF_Fields->convert_to_form_fields( $shipment_fields, '', 'col-md-6' ); ?>
				</div>
			</div>
			<div id="parcel-info" class="section wpcargo-col-md-12">
				<h4><?php echo wpcsr_parcel_details_label(); ?></h4>
				<div id="wpcsr-parcels" class="table-responsive">
					<table class="table wpcargo-table-wrapper wpcsr-parcel-wrapper wpcargo-container" data-repeater-list="wpc-multiple-package">
						<thead>
							<tr>
								<?php foreach( $package_fields as $package_key => $package_value ): ?>
									<th><?php esc_html_e($package_value['label'], 'wpcargo-shipment-rate'); ?></th>
								<?php endforeach; ?>
							</tr>
						</thead>
						<tbody>
							<?php if( !empty( $packages ) ): ?>
								<?php foreach ( $packages as $package ) : ?>
									<tr data-repeater-item>
										<?php foreach ( $package_fields as $key => $field_value): ?>
											<?php $value = array_key_exists( $key, $package ) ? $package[$key] : '' ; ?>
											<td class="<?php echo $key == 'wpc-pm-description' ? 'p-1' : ''; ?>">
												<?php if( $key == 'wpc-pm-description' ): ?>
													<textarea id="<?php echo $key; ?>" class="form-control  " name="<?php echo $key; ?>"><?php echo $value; ?></textarea>
												<?php else: ?>
													<input type="hidden" name="<?php echo $key; ?>" value="<?php echo $value; ?>">
													<?php echo $value; ?>
												<?php endif; ?>
											</td>
										<?php endforeach; ?>
									</tr>
								<?php endforeach; ?>
							<?php else: ?>
								<i></i>
							<?php endif; ?>   
						</tbody>
					</table>
				</div>
			</div>
			<div class="submit-wrapper form-check wpcargo-col-md-12">    
                <input id="submit-order" class="wpcargo-btn wpcargo-btn-md wpcargo-btn-primary" type="submit" name="submit" value="<?php echo wpcsr_submit_order_label(); ?>" />
			</div>
		</div>
	</div> <!-- wpcargo-container -->
</form>