<?php
    $package_fields     = wpcargo_package_fields();
    $packages           = maybe_unserialize( get_post_meta( $shipment_id, 'wpc-multiple-package', true ) );
    $total_cost         = get_post_meta( $shipment_id, '__wpcsr_package_cost', true );
    $origin             = get_post_meta( $shipment_id, '__wpcsr_origin', true );
    $orig_postcode      = get_post_meta( $shipment_id, '__wpcsr_origin_postcode', true );
    $destination        = get_post_meta( $shipment_id, '__wpcsr_destination', true );
    $dest_postcode      = get_post_meta( $shipment_id, '__wpcsr_destination_postcode',true );
    $total_pallet       = wpcstl_get_pallet_number( $packages );
    $total_weight       = wpcstl_get_total_weight( $packages );
    $additional_cost    = wpcstl_get_gst( $total_cost );
    $shipment_number    = get_the_title( $shipment_id );
?>
<div class="wpcargo-row" style="margin-bottom:18px;">
    <section class="col-md-6">
        <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> -->
    </section>
    <section class="col-md-6">
        <p><span class="wpcstl-label">Total Pallet:</span> <span class="font-weight-bold"><?php echo $total_pallet.wpcsr_pcs_label(); ?></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><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">Subtotal:</span> <span class="font-weight-bold"><?php echo format_wpcstl_currency( $additional_cost['subtotal'], true ); ?></span></p>
    </section>
</div>
<table id="wpcstl-packages" class="table table-hover table-sm wpcargo-table">
    <thead>
        <tr>
            <?php foreach( $package_fields as $package_key => $package_value ): ?>
                <th><?php esc_html_e($package_value['label'], 'wpcargo-shipment-rate'); ?></th>
                <?php if( $package_key == 'wpc-pm-description' ): ?>
                    <th>Pallet Barcode</th>
                <?php endif; ?>
            <?php endforeach; ?>
        </tr>
    </thead>
    <tbody>
        <?php if( !empty( $packages ) ): $counter = 0; ?>
            <?php foreach ( $packages as $package ) : ?>
                <tr>
                    <?php foreach ( $package_fields as $package_key => $package_value ): ?>
                        <td>
                            <input type="hidden" name="wpc-multiple-package[<?php echo $counter; ?>][<?php echo $package_key; ?>]" value="<?php echo $package[$package_key]; ?>">
                            <?php echo $package[$package_key]; ?>
                        </td>
                        <?php if( $package_key == 'wpc-pm-description' ): ?>
                            <td><a href="#" class="wpcstl-print-pbarcode text-primary" data-id="<?php echo $shipment_id; ?>" data-pallet="<?php echo $counter + 1; ?>"><?php echo $shipment_number.'-TRK'.str_pad( $counter + 1, 3, "0", STR_PAD_LEFT ); ?></a></td>
                        <?php endif; ?>
                    <?php endforeach; ?>
                </tr>
                <?php $counter++; ?>
            <?php endforeach; ?>
        <?php endif; ?>   
    </tbody>
</table>