<?php
    // Prepare Varaibles
    $origin_id          = (int)$_POST['shipment-from'];
    $origin             = get_wpcsr_loc_data( $origin_id, 'location' );
    $orig_postcode      = isset( $_POST['origin_postcode'] ) ? $_POST['origin_postcode'] : '';
    $destination_id     = (int)$_POST['shipment-to'];
    $destination        = get_wpcsr_loc_data( $destination_id, 'location' );
    $dest_postcode      = isset( $_POST['destination_postcode'] ) ? $_POST['destination_postcode']  : '';
    $packages           = $_POST['wpc-multiple-package'];
    $package_fields     = wpcargo_package_fields();
    unset( $package_fields['wpc-pm-description'] );
    unset( $package_fields['wpc-pm-piece-type'] );
    $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 );
?>

<div class="wpcargo-container">
    <section class="wpcargo-row mt-4 border-top">
        <div class="wpcargo-col-md-8 border-right">
            <h2 class="h3 text-center mt-4">Freight Information</h2>
            <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>
            <h2 class="h5 my-4">Pallets List</h2>
            <table class="table table-hover table-sm">
                <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 ) : ?>
                            <?php for ($i = 0; $i < $package['wpc-pm-qty']; $i++): $_package = array(); ?>
                                <?php 
                                    $_package['wpc-pm-piece-type'] = 'Pallet'; 
                                    $_package['wpc-pm-description'] = ''; 
                                ?>
                                <tr>
                                    <?php foreach ( $package_fields as $key => $field_value): ?>
                                        <?php 
                                            $value = $key == 'wpc-pm-qty' ? 1 : $package[$key] ; 
                                            // Create New Package Data
                                            $_package[$key] = $value;
                                        ?>
                                        <td><?php echo $value; ?></td>
                                    <?php endforeach; ?>
                                </tr>
                                <?php $package_breakdown[] = $_package; ?>
                            <?php endfor; ?>
                        <?php endforeach; ?>
                    <?php endif; ?>   
                </tbody>
            </table>
        </div>
        <div class="wpcargo-col-md-4">
            <h2 class="h3 text-center mt-4">Freight Cost</h2>
            <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>
            <?php if( is_user_logged_in() ): ?>
                <form class="wpcsr-inline mt-4" action="" method="POST">
                    <?php wp_nonce_field( 'wpcsr_create_order_action', 'wpcsr_create_order_step2' ); ?>
                    <input type="hidden" name="_wpcsr" value="8">
                    <input type="hidden" name="wpc-multiple-package" value="<?php echo base64_encode( serialize( $package_breakdown ) ); ?>">
                    <input type="hidden" name="wpc-pm-piece-type" value="<?php echo $package_type; ?>">
                    <input type="hidden" name="origin" value="<?php echo $origin_id; ?>">
                    <input type="hidden" name="origin_postcode" value="<?php echo $orig_postcode; ?>">
                    <input type="hidden" name="destination" value="<?php echo $destination_id; ?>">
                    <input type="hidden" name="destination_postcode" value="<?php echo $dest_postcode; ?>">
                    <input class="wpcargo-btn wpcargo-btn-primary wpcsr-purchase-btn w-100" type="submit" value="<?php echo wpcsr_book_now_label(); ?>">
                </form>
            <?php else: ?>
                <a href="<?php echo get_the_permalink( wpcfe_admin_page() ); ?>" class="wpcargo-btn wpcargo-btn-primary wpcsr-purchase-btn w-100" >LOGIN</a>
            <?php endif; ?>
        </div>
    </section>
</div>