<?php
	$orig_id 		= isset( $_POST['shipment-from'] ) ? (int)$_POST['shipment-from'] : 0;
    $dest_id 		= isset( $_POST['shipment-to'] ) ? (int)$_POST['shipment-to'] : 0;
	$orig_postcode 	= '';
    $dest_postcode 	= '';
	$orig_options 	= array();
	$dest_options 	= array();
	$packages 		= array();
	$total_qty 		= 0;
	$total_weight 	= 0;
    if ( isset( $_POST['quote_request_nonce_field'] ) && wp_verify_nonce( $_POST['quote_request_nonce_field'], 'quote_request_action' ) ):
        $orig_postcode = isset($_POST['origin_postcode']) ? $_POST['origin_postcode'] : '';
        $dest_postcode = isset($_POST['destination_postcode']) ? $_POST['destination_postcode'] : '';
		$orig_options  = wpcstl_opt_location( wpcstl_map_location( $orig_id ) );
		$dest_options  = wpcstl_opt_location( wpcstl_map_location( $dest_id ) );
		$packages  	   = isset($_POST['wpc-multiple-package']) ? $_POST['wpc-multiple-package'] : array();
		$total_qty     = wpcstl_get_pallet_number( $packages );
		$total_weight  = wpcstl_get_total_weight( $packages );
    endif;
?>
<div class="wpcsr-search-box single-search">
	<form id="wpcsr-findrate" class="wpcsr-quote-book mb-0" action="<?php echo $result_page_url; ?>" method="post">
		<?php wp_nonce_field( 'quote_request_action', 'quote_request_nonce_field' ); ?>
		<input type="hidden" name="search-type" value="0">
		<div id="wpcsr-srequest-wrapper" class="wpcargo-container">
			<div id="rate-container">
				<div class="location-information-section wpcargo-row">
					<div class="wpcargo-col-lg-5 p-lg-0">
						<div class="collection-from-section">
							<section class="wpcsr-sc-label">
								<span class="request-label label-from"><?php echo wpcsr_collection_form_label(); ?>:</span>
							</section>
							<section class="wpcsr-sc-content">
								<div class="content-fields wpcstl-option-wrapper">
									<section class="field-section origin-location">
										<select class="form-control browser-default custom-select wpcstl-location wpcstl-origin" name="shipment-from" required="required">
											<?php if( !empty( get_wpcsr_location() ) ): ?>
												<option value=""><i>Origin</i></option>
												<?php foreach(get_wpcsr_location() as $location ): ?>
													<option value="<?php echo $location->location_id; ?>" <?php echo selected( $orig_id, $location->location_id ); ?>><?php echo $location->location;?></option>
												<?php endforeach; ?>
											<?php endif; ?>
										</select>
									</section>
									<section class="field-section origin-postcode">
                                        <select id="origin_postcode" class="form-control browser-default custom-select wpcstl-postcode" name="origin_postcode" >
											<?php if( !empty( $orig_options ) ): ?>
												<?php foreach( $orig_options as $orig_ovalue ): ?>
                                            		<option value="<?php echo $orig_ovalue; ?>" <?php echo selected( $orig_postcode, $orig_ovalue ); ?>><?php echo $orig_ovalue; ?></option>
												<?php endforeach; ?>
											<?php else: ?>
												<option value=""><i><?php echo wpcsr_postcode_form_label(); ?></i></option>
											<?php endif ?>
                                        </select>
									</section>
								</div>
							</section>
						</div>
					</div>
					<div class="wpcargo-col-lg-2 p-lg-0 text-center" style="margin: auto">
						<i class="fa fa-chevron-circle-right"></i>
					</div>
					<div class="wpcargo-col-lg-5 p-lg-0">
						<div class="deliver-to-section">
							<section class="wpcsr-sc-label">
								<span class="request-label label-to"><?php echo wpcsr_delivery_to_form_label(); ?>:</span>
							</section>
							<section class="wpcsr-sc-content">
								<div class="content-fields wpcstl-option-wrapper">
									<section class="field-section destination-location">
										<select class="form-control browser-default custom-select wpcstl-location wpcstl-destination" name="shipment-to" required="required">
											<option value=""><i>Destination</i></option>
											<?php if( !empty( get_wpcsr_location() ) ): ?>
												<?php foreach(get_wpcsr_location() as $location ): ?>
													<option value="<?php echo $location->location_id; ?>" <?php echo selected( $dest_id, $location->location_id ); ?>><?php echo $location->location;?></option>
												<?php endforeach; ?>
											<?php endif; ?>
										</select>
									</section>
									<section class="field-section destination-postcode">
                                        <select id="destination_postcode" class="form-control browser-default custom-select wpcstl-postcode" name="destination_postcode" >
											<?php if( !empty( $dest_options ) ): ?>
												<?php foreach( $dest_options as $dest_ovalue ): ?>
                                            		<option value="<?php echo $dest_ovalue; ?>" <?php echo selected( $dest_postcode, $dest_ovalue ); ?>><?php echo $dest_ovalue; ?></option>
												<?php endforeach; ?>
											<?php else: ?>
												<option value=""><i><?php echo wpcsr_postcode_form_label(); ?></i></option>
											<?php endif ?>
                                        </select>
									</section>
								</div>
							</section>
						</div>
					</div>
				</div>
				<div id="parcel-dimension">
					<section>
						<span class="request-label"><?php echo wpcsr_enter_parcel_form_label(); ?></span>
					</section>
					<section id="wpcsr-parcels">
						<div class="table-responsive">
							<table id="wpcstl-package-table" class="table wpcargo-table-wrapper wpcsr-parcel-wrapper wpcargo-container" data-repeater-list="wpc-multiple-package">
								<thead>
									<tr>
										<th>Pallet</th>
										<th>Unit <?php echo wpcsr_weight_label(); ?></th>
										<th>Unit <?php echo wpcsr_length_label(); ?></th>
										<th>Unit <?php echo wpcsr_width_label(); ?></th>
										<th>Unit <?php echo wpcsr_height_label(); ?></th>
										<th>Unit Value</th>
										<?php if( $param['switch'] == 1 ): ?>
											<th>&nbsp;</th>
										<?php endif; ?>
									</tr>
								</thead>
								<tbody>
									<?php if( !empty( $packages ) ): ?>
										<?php foreach( $packages as $package ): ?>
											<tr data-repeater-item>
												<td>
													<div class="wpcsr-package-input">
														<input type="text" class="form-control number wpstl-qty" name="wpc-pm-qty" min="1" value="<?php echo $package['wpc-pm-qty']; ?>">
													</div>
													<div class="wpcsr-unit">
														<span><?php echo wpcsr_pcs_label(); ?></span>
													</div>
												</td>
												<td>
													<div class="wpcsr-package-input">
														<input type="text" class="form-control number wpstl-weight" name="wpc-pm-weight" value="<?php echo $package['wpc-pm-weight']; ?>" required="required">
													</div>
													<div class="wpcsr-unit">
														<span><?php _e( wpcargo_package_settings()->weight_unit, 'wpcargo-shipment-rate' );?></span>
													</div>
												</td>
												<?php if( wpcargo_package_settings()->dim_unit_enable ): ?>
													<td>
														<div class="wpcsr-package-input">
															<input type="text" data-label="Length" class="form-control number wpstl-length" name="wpc-pm-length" value="<?php echo $package['wpc-pm-length']; ?>" min="1" required="required">
														</div>
														<div class="wpcsr-unit">
															<span><?php _e( wpcargo_package_settings()->dim_unit, 'wpcargo-shipment-rate' );?></span>
														</div>
													</td>
													<td>
														<div class="wpcsr-package-input">
															<input type="text" data-label="Width" class="form-control number wpstl-width" name="wpc-pm-width" value="<?php echo $package['wpc-pm-width']; ?>" min="1" required="required">
														</div>
														<div class="wpcsr-unit">
															<span><?php _e( wpcargo_package_settings()->dim_unit, 'wpcargo-shipment-rate' );?></span>
														</div>
													</td>
													<td>
														<div class="wpcsr-package-input">
															<input type="text" data-label="Height" class="form-control number wpstl-height" name="wpc-pm-height" value="<?php echo $package['wpc-pm-height']; ?>" min="1" required="required">
														</div>
														<div class="wpcsr-unit">
															<span><?php _e( wpcargo_package_settings()->dim_unit, 'wpcargo-shipment-rate' );?></span>
														</div>
													</td>
													<td>
														<div class="wpcsr-package-input">
															<input type="text" data-label="Value" class="form-control number wpstl-value" name="wpc-pm-value" value="<?php echo $package['wpc-pm-value']; ?>" min="1" required="required">
														</div>
														<div class="wpcsr-unit">
															<?php if( function_exists('wpcsr_currency') ): ?>
																<span><?php echo wpcsr_currency();?></span>
															<?php endif; ?>
														</div>
													</td>
												<?php endif; ?>
												<td>
													<span class="dashicons dashicons-plus repeater-add" alt="<?php echo wpcsr_add_label(); ?>" data-repeater-create></span>
													<span class="dashicons dashicons-minus repeater-delete" alt="<?php echo wpcsr_delete_label(); ?>" data-repeater-delete></span>
												</td>
											</tr>
										<?php endforeach; ?>
									<?php else: ?>
										<tr data-repeater-item>
											<td>
												<div class="wpcsr-package-input">
													<input type="text" class="form-control number wpstl-qty" name="wpc-pm-qty" min="1">
												</div>
												<div class="wpcsr-unit">
													<span><?php echo wpcsr_pcs_label(); ?></span>
												</div>
											</td>
											<td>
												<div class="wpcsr-package-input">
													<input type="text" class="form-control number wpstl-weight" name="wpc-pm-weight" value="" required="required">
												</div>
												<div class="wpcsr-unit">
													<span><?php _e( wpcargo_package_settings()->weight_unit, 'wpcargo-shipment-rate' );?></span>
												</div>
											</td>
											<?php if( wpcargo_package_settings()->dim_unit_enable ): ?>
												<td>
													<div class="wpcsr-package-input">
														<input type="text" data-label="Length" class="form-control number wpstl-length" name="wpc-pm-length" value="" min="1" required="required">
													</div>
													<div class="wpcsr-unit">
														<span><?php _e( wpcargo_package_settings()->dim_unit, 'wpcargo-shipment-rate' );?></span>
													</div>
												</td>
												<td>
													<div class="wpcsr-package-input">
														<input type="text" data-label="Width" class="form-control number wpstl-width" name="wpc-pm-width" value="" min="1" required="required">
													</div>
													<div class="wpcsr-unit">
														<span><?php _e( wpcargo_package_settings()->dim_unit, 'wpcargo-shipment-rate' );?></span>
													</div>
												</td>
												<td>
													<div class="wpcsr-package-input">
														<input type="text" data-label="Height" class="form-control number wpstl-height" name="wpc-pm-height" value="" min="1" required="required">
													</div>
													<div class="wpcsr-unit">
														<span><?php _e( wpcargo_package_settings()->dim_unit, 'wpcargo-shipment-rate' );?></span>
													</div>
												</td>
												<td>
													<div class="wpcsr-package-input">
														<input type="text" data-label="Value" class="form-control number wpstl-value" name="wpc-pm-value" value="0" min="1" required="required">
													</div>
													<div class="wpcsr-unit">
														<?php if( function_exists('wpcsr_currency') ): ?>
															<span><?php echo wpcsr_currency();?></span>
														<?php endif; ?>
													</div>
												</td>
											<?php endif; ?>
											<td>
												<span class="dashicons dashicons-plus repeater-add" alt="<?php echo wpcsr_add_label(); ?>" data-repeater-create></span>
												<span class="dashicons dashicons-minus repeater-delete" alt="<?php echo wpcsr_delete_label(); ?>" data-repeater-delete></span>
											</td>
										</tr>
									<?php endif; ?>
								</tbody>
								<tfoot>
									<tr>
										<td>
											<div class="wpcsr-package-input">
												<input id="wpcstl-total-qty" type="text" class="form-control number" value="<?php echo $total_qty; ?>" min="1" disabled>
											</div>
											<div class="wpcsr-unit">
												<span><?php echo wpcsr_pcs_label(); ?></span>
											</div>
										</td>
										<td>
											<div class="wpcsr-package-input">
												<input id="wpcstl-total-weight" type="text" class="form-control number" value="<?php echo $total_weight; ?>" disabled>
											</div>
											<div class="wpcsr-unit">
												<span><?php _e( wpcargo_package_settings()->weight_unit, 'wpcargo-shipment-rate' );?></span>
											</div>
										</td>
										<td>&nbsp;</td>
										<td>&nbsp;</td>
										<td>&nbsp;</td>
										<td>&nbsp;</td>
									</tr>
								</tfoot>
							</table>
						</div>
					</section>
					<div class="quote_book_wrap text-center">			
						<input type="submit" class="wpcargo-btn wpcargo-btn-primary quote_book" value="<?php echo wpcsr_quote_book_label(); ?>" />	
					</div>
				</div>
			</div>	
		</div>
	</form>
</div>