<?php
if (!defined('ABSPATH')){
	exit; // Exit if accessed directly
}
define( 'WPCARGO_PACKAGE_POSTMETA', apply_filters( 'wpcargo_package_postmeta', 'wpc-multiple-package' ) );
add_action('wpcargo_after_package_details', 'wpcargo_multiple_package_after_track_details', 5, 1 );
function wpcargo_multiple_package_after_track_details( $shipment ){
	if( multiple_package_status() ) {
		$template = wpcargo_include_template( 'package.tpl' );
		include_once( $template );
	}
}
add_action('wpcargo_after_package_totals', 'wpcargo_after_package_details_callback', 10, 1 );
function wpcargo_after_package_details_callback( $shipment ){
    $class = is_admin() ? 'one-third' : 'wpcargo-col-md-4' ;
    $style = is_admin() ? 'style="display:block;overflow:hidden;margin-bottom:36px"' : '' ;
	$shipment_id = (!empty ( $shipment ) ) ? $shipment->ID : '';
	$package_volumetric = (!empty ( $shipment ) ) ? wpcargo_package_volumetric( $shipment->ID ) : '0';
	$package_actual_weight = (!empty ( $shipment ) ) ? wpcargo_package_actual_weight( $shipment->ID ) : '0';
	$package_cubicmetric = (!empty ( $shipment ) ) ? wpcargo_package_cubicmetric( $shipment->ID ) : '0.00';
	$package_actual_weight = (!empty ( $shipment ) ) ? wpcargo_package_actual_weight( $shipment->ID ) : '0';
	$package_actual_koli = (!empty ( $shipment ) ) ? wpcargo_package_actual_koli( $shipment->ID ) : '0';
	?>
	<div id="package-weight-info" class="table-responsive" <?php echo $style; ?>>
		<table class="table table-hover table-sm">
			<thead>
				<tr class="text-center">
					<th><?php echo apply_filters( 'wpcargo_package_actual_koli_label', esc_html__('Koli', 'wpcargo') ); ?></th>
					<th><?php echo apply_filters( 'wpcargo_package_actual_weight_label', esc_html__('Berat', 'wpcargo') ); ?></th>
					<th><?php echo apply_filters( 'wpcargo_package_cubicmetric_label', esc_html__('Kubikasi', 'wpcargo') ); ?></th>
					<th><?php echo apply_filters( 'wpcargo_package_volumetric_label', esc_html__('Volume', 'wpcargo') ); ?></th>
				</tr>
			</thead>
			<tbody>
				<tr class="text-center">
					<td><?php echo $package_actual_koli; ?></td>
					<td><span id="package_actual_weight"><?php echo $package_actual_weight.'</span> '.wpcargo_package_settings()->weight_unit; ?></td>
					<td><span id="package_cubicmetric"><?php echo $package_cubicmetric.'</span> '.wpcargo_package_settings()->weight_unit; ?></td>
					<td><span id="package_volumetric"><?php echo $package_volumetric.'</span> '.wpcargo_package_settings()->weight_unit; ?></td>
				<tr>
			</tbody>
			<?php do_action('wpcargo_after_package_totals_section', $shipment_id); ?>
		</table>
	</div>
	<?php
	do_action('wpcargo_after_package_details_script', $shipment);
}
add_action('wpcargo_after_package_details_script', 'wpcargo_after_package_details_script_callback', 10, 1 );
function wpcargo_after_package_details_script_callback( $shipment ){
	$dim_meta   = wpcargo_package_dim_meta();
	$qty_meta   = wpcargo_package_qty_meta();
	$weight_meta = wpcargo_package_weight_meta();
	$divisor    = wpcargo_package_settings()->divisor ? wpcargo_package_settings()->divisor : 1;
	$divisor_cubic    = wpcargo_package_settings()->divisor_cubic ? wpcargo_package_settings()->divisor_cubic : 1;
	$dim_meta   = json_encode( $dim_meta );
	?>
	<script>
		var mainContainer   = 'table tbody[data-repeater-list="<?php echo WPCARGO_PACKAGE_POSTMETA; ?>"]';
		var divisor         = <?php echo $divisor ?>;
		var divisor_cubic         = <?php echo $divisor_cubic ?>;
		var dimMeta         = <?php echo $dim_meta; ?>;
		var qtyMeta         = "<?php echo $qty_meta; ?>";
		var weightMeta      = "<?php echo $weight_meta; ?>";    
		jQuery(document).ready(function($){
			if( mainContainer.length > 0 ){
				$( mainContainer ).on( 'change keyup', 'input', function(){
					var totalQTY        = 0;
					var totalWeight     = 0;
					var totalVolumetric = 0;
					var totalVolume		= 0;
					var totalCubicmetric = 0;
					var totalCubic		= 0;
				 
					$( mainContainer + ' tr' ).each(function(){
						var currentVolumetric = 1; 
						var currentCubicmetric = 1;
						var currentQTY        = 0;
						var packageWeight     = 0;
						$(this).find('input').each(function(){
							var currentField    = $(this);
							var className       = $( currentField ).attr('name');
							// Exclude in the loop field without name attribute
							if ( typeof className === "undefined" ){
									return;
							}
							// Get the QTY
							if ( className.indexOf(qtyMeta) > -1 ){
								var pQty = $( currentField ).val() == '' ? 0 : $( currentField ).val() ;
								totalQTY += parseFloat( pQty );
								currentQTY = parseFloat( pQty );
							}
							// Get the weight
							if ( className.indexOf(weightMeta) > -1 ){
								var pWeight = $( currentField ).val() == '' ? 0 : $( currentField ).val() ;
								packageWeight += parseFloat( pWeight );
							}
							
							// Calculate the volumetric                       
							$.each( dimMeta, function( index, value ){   
												  
								if ( className.indexOf(value) == -1 ){
									return;
								}
								currentVolumetric *= $( currentField ).val();
							} );

							// Calculate the cubicmetric                       
							$.each( dimMeta, function( index, value ){   

								if ( className.indexOf(value) == -1 ){
									return;
								}
								currentCubicmetric *= $( currentField ).val();
							} );
						});
						totalVolumetric += currentQTY * ( currentVolumetric / divisor );
						totalCubicmetric += currentQTY * ( currentCubicmetric / divisor_cubic );
						totalWeight     += currentQTY * packageWeight;
						totalVolume		+= currentQTY * currentVolumetric;
						totalCubic		+= currentQTY * currentCubicmetric;
					});
					$('#package-weight-info #total_volume_metric_output').text( totalVolume.toFixed(2) );
					$('#package-weight-info #package_volumetric').text( totalVolumetric.toFixed(2) );
					$('#package-weight-info #total_cubic_metric_output').text( totalCubic.toFixed(2) );
					$('#package-weight-info #package_cubicmetric').text( totalCubicmetric.toFixed(2) );
					$('#package-weight-info #package_actual_weight').text( totalWeight.toFixed(2) );
					$('#package-weight-info #package_actual_koli').text( totalQTY.toFixed(2) );
				});
			}
		});
	</script>
    <?php
}
function wpcargo_package_settings(){
	$options                        = get_option( 'wpc_mp_settings' );
	$woointeg_dim_divisor           = get_option( 'woointeg_dim_divisor' ) ? floatval( get_option( 'woointeg_dim_divisor' ) ) : 5000 ;
	$woointeg_dim_divisor_cubic           = get_option( 'woointeg_dim_divisor_cubic' ) ? floatval( get_option( 'woointeg_dim_divisor_cubic' ) ) : 1000000 ;
	$woointeg_dim_divisor 			= apply_filters( 'wpcargo_package_divisor_cm', $woointeg_dim_divisor );
	$woointeg_dim_divisor_cubic 			= apply_filters( 'wpcargo_package_divisor_cubic_cm', $woointeg_dim_divisor_cubic );
	$woointeg_dim_divisor_inc       = get_option( 'woointeg_dim_divisor_inc' ) ? floatval( get_option( 'woointeg_dim_divisor_inc' ) ) : 138.4 ;
	$woointeg_dim_divisor_inc 		= apply_filters( 'wpcargo_package_divisor_inc', $woointeg_dim_divisor_inc );

    $wpc_mp_dimension_unit          = !empty($options) && array_key_exists( 'wpc_mp_dimension_unit', $options ) ? $options['wpc_mp_dimension_unit'] : 'cm';
    $wpc_mp_peice_type              = !empty($options) && array_key_exists( 'wpc_mp_piece_type', $options ) ? array_filter( array_map('trim', explode(",", $options['wpc_mp_piece_type']) ) ) : array();
    $wpc_mp_weight_unit             = !empty($options) && array_key_exists( 'wpc_mp_weight_unit', $options ) ? $options['wpc_mp_weight_unit'] : 'lbs';
    $wpc_mp_enable_dimension_unit   = !empty($options) && array_key_exists( 'wpc_mp_enable_dimension_unit', $options ) ? $options['wpc_mp_enable_dimension_unit'] : false;
    $wpc_mp_enable_admin  			= !empty($options) && array_key_exists( 'wpc_mp_enable_admin', $options ) ? $options['wpc_mp_enable_admin'] : false;
    $wpc_mp_enable_frontend  		= !empty($options) && array_key_exists( 'wpc_mp_enable_frontend', $options ) ? $options['wpc_mp_enable_frontend'] : false;
    $package_settings                    = new stdClass();
    $package_settings->dim_unit          = $wpc_mp_dimension_unit;
    $package_settings->peice_types       = $wpc_mp_peice_type;
    $package_settings->weight_unit       = $wpc_mp_weight_unit;
    $package_settings->dim_unit_enable   = $wpc_mp_enable_dimension_unit;
    $package_settings->admin_enable   	 = apply_filters( 'wpcargo_package_admin_enable', $wpc_mp_enable_admin );
    $package_settings->frontend_enable   = apply_filters( 'wpcargo_package_frontend_enable', $wpc_mp_enable_frontend );
    $package_settings->divisor           = $wpc_mp_dimension_unit == 'cm' ? $woointeg_dim_divisor : $woointeg_dim_divisor_inc;
    $package_settings->divisor_cubic           = $wpc_mp_dimension_unit == 'cm' ? $woointeg_dim_divisor_cubic : $woointeg_dim_divisor_inc;
    $package_settings->volume_unit       = $wpc_mp_dimension_unit == 'cm' ? apply_filters( 'wpcargo_package_cm_volume_unit', 'kg.' ) : apply_filters( 'wpcargo_package_inc_volume_unit', 'lbs.' );
    $package_settings->cubic_unit       = $wpc_mp_dimension_unit == 'cm' ? apply_filters( 'wpcargo_package_cm_cubic_unit', 'kg.' ) : apply_filters( 'wpcargo_package_inc_cubic_unit', 'lbs.' );
    return $package_settings;
}
function wpcargo_package_dim_meta( ){
	$dim_meta = array( 'wpc-pm-length', 'wpc-pm-width', 'wpc-pm-height' );
	return apply_filters( 'wpcargo_package_dim_meta', $dim_meta );
}
function wpcargo_package_qty_meta( ){
	return apply_filters( 'wpcargo_package_qty_meta', 'wpc-pm-qty' );
}
function wpcargo_package_weight_meta( ){
	return apply_filters( 'wpcargo_package_weight_meta', 'wpc-pm-weight' );
}
function wpcargo_package_fields(){
    $package_fields = array(
        'wpc-pm-qty' => array(
            'label' => esc_html__('Koli', 'wpcargo'),
            'field' => 'text',
            'required' => false,
            'options' => array()
		),
		'wpc-pm-description' => array(
            'label' => esc_html__('Deskripsi', 'wpcargo'),
            'field' => 'textarea',
            'required' => false,
            'options' => array()
        ),
        'wpc-pm-piece-type' => array(
            'label' => esc_html__('Packaging', 'wpcargo'),
            'field' => 'select',
            'required' => false,
            'options' => wpcargo_package_settings()->peice_types
        ),
	);
	$package_fields['wpc-pm-weight'] = array(
		'label' => esc_html__('Berat', 'wpcargo').'('.wpcargo_package_settings()->weight_unit.')',
		'field' => 'text',
		'required' => false,
		'options' => array()
	);
	if( wpcargo_package_settings()->dim_unit_enable ){
		$package_fields['wpc-pm-length'] = array(
            'label' => esc_html__('Panjang', 'wpcargo').'('.wpcargo_package_settings()->dim_unit.')',
            'field' => 'text',
            'required' => false,
            'options' => array()
        );
        $package_fields['wpc-pm-width'] = array(
            'label' => esc_html__('Lebar', 'wpcargo').'('.wpcargo_package_settings()->dim_unit.')',
            'field' => 'text',
            'required' => false,
            'options' => array()
        );
        $package_fields['wpc-pm-height'] = array(
            'label' => esc_html__('Tinggi', 'wpcargo').'('.wpcargo_package_settings()->dim_unit.')',
            'field' => 'text',
            'required' => false,
            'options' => array()
        );
	}
    return apply_filters( 'wpcargo_package_fields', $package_fields );
}
function wpcargo_get_package_data( $shipment_id, $meta_key = WPCARGO_PACKAGE_POSTMETA  ){
    $packages = get_post_meta( (int)$shipment_id, $meta_key, true) ? maybe_unserialize( get_post_meta( (int)$shipment_id, $meta_key, true) ) : array();
    return apply_filters( 'wpcargo_get_package_data', $packages, $shipment_id, $meta_key );
}
function wpcargo_package_volumetric( $shipment_id ){
	$volumetric = 0;
	$divisor    = wpcargo_package_settings()->divisor;
	$unit    	= wpcargo_package_settings()->volume_unit;
	$packages 	= wpcargo_get_package_data( $shipment_id );
	
	if( !empty( $packages ) ){	
		foreach ($packages as $key => $value) {
			$multiplier = 1;
			$qty = array_key_exists( wpcargo_package_qty_meta(), $value ) ? $value[wpcargo_package_qty_meta()] : 0 ;
			foreach ( wpcargo_package_dim_meta() as $dim_meta ) {
				if( !array_key_exists( $dim_meta, $value ) ){
					continue;
				}
				$multiplier *=  floatval($value[$dim_meta]);
			}
			$volumetric += ( floatval($multiplier) / floatval($divisor) ) * floatval($qty);
		}		
	}
	return apply_filters( 'wpcargo_package_volumetric', number_format( $volumetric, 0, '.', ''), $shipment_id );
}
function wpcargo_package_cubicmetric( $shipment_id ){
	$cubicmetric = 0;
	$divisor_cubic    = wpcargo_package_settings()->divisor_cubic;
	$unit    	= wpcargo_package_settings()->cubic_unit;
	$packages 	= wpcargo_get_package_data( $shipment_id );
	
	if( !empty( $packages ) ){	
		foreach ($packages as $key => $value) {
			$multiplier = 1;
			$qty = array_key_exists( wpcargo_package_qty_meta(), $value ) ? $value[wpcargo_package_qty_meta()] : 0 ;
			foreach ( wpcargo_package_dim_meta() as $dim_meta ) {
				if( !array_key_exists( $dim_meta, $value ) ){
					continue;
				}
				$multiplier *=  floatval($value[$dim_meta]);
			}
			$cubicmetric += ( floatval($multiplier) / floatval($divisor_cubic) ) * floatval($qty);
		}		
	}
	return apply_filters( 'wpcargo_package_cubicmetric', number_format( $cubicmetric, 3, '.', ''), $shipment_id );
}
function wpcargo_package_actual_weight( $shipment_id ){
	$weight = 0;
	$packages = wpcargo_get_package_data( $shipment_id );
	if( !empty( $packages ) ){	
		foreach ($packages as $key => $value) {
			$qty 			= array_key_exists( wpcargo_package_qty_meta(), $value ) ? $value[wpcargo_package_qty_meta()] : 0 ;
			$weight_meta 	= array_key_exists( wpcargo_package_weight_meta(), $value ) ? $value[wpcargo_package_weight_meta()] : 0 ;
			$weight += floatval( $weight_meta ) * floatval( $qty );
		}		
	}
	return apply_filters( 'wpcargo_package_actual_weight', number_format( $weight, 0, '.', ''), $shipment_id );
}
function wpcargo_package_actual_koli( $shipment_id ){
	$koli = 0;
	$packages = wpcargo_get_package_data( $shipment_id );
	if( !empty( $packages ) ){	
		foreach ($packages as $key => $value) {
			$qty 			= array_key_exists( wpcargo_package_qty_meta(), $value ) ? $value[wpcargo_package_qty_meta()] : 0 ;
			$qty_meta 	= array_key_exists( wpcargo_package_qty_meta(), $value ) ? $value[wpcargo_package_qty_meta()] : 0 ;
			$koli += floatval( $qty_meta );
		}		
	}
	return apply_filters( 'wpcargo_package_actual_koli', number_format( $koli, 0, '.', ''), $shipment_id );
}
function multiple_package_status(){
	$status = false;
	if( wpcargo_package_settings()->frontend_enable && !empty( wpcargo_package_fields() ) ) {
		$status = true;
	}
	return apply_filters( 'multiple_package_status', $status );
}
