<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly
}

/**
 * @var array $data
 * @var array $dependencies
 */
if ( 'bulk' === $data['type'] ) {
	$deal_title = __( 'Bulk deal', 'advanced-dynamic-pricing-for-woocommerce' );
} elseif ( 'tier' === $data['type'] ) {
	$deal_title = __( 'Tier deal', 'advanced-dynamic-pricing-for-woocommerce' );
} else {
	return;
}

if ( in_array( $data['discount'], array( 'discount__amount', 'discount__percentage' ) ) ) {
	$amount_title = __( 'Discount', 'advanced-dynamic-pricing-for-woocommerce' );
} elseif ( 'price__fixed' === $data['discount'] ) {
	$amount_title = __( 'Price', 'advanced-dynamic-pricing-for-woocommerce' );
} else {
	return;
}
?>
<div class='clear'></div>

<div class="bulk_table">
<div class="wdp_pricing_table_caption"><?php echo $deal_title; ?></div>
<table class="wdp_pricing_table">
    <thead>
    <tr>
        <td><?php _e( 'Quantity', 'advanced-dynamic-pricing-for-woocommerce' ) ?></td>
        <td><?php echo $amount_title ?></td>
    </tr>
    </thead>

    <tbody>
	<?php foreach ( $data['ranges'] as $line ): ?>
        <tr>
            <td>
				<?php echo $line['from'] . '-' . $line['to'] ?>
            </td>
            <td>
				<?php
				if ( in_array( $data['discount'], array( 'price__fixed', 'discount__amount' ) ) ) {
					echo wc_price( $line['value'] );
				} elseif ( 'discount__percentage' === $data['discount'] ) {
					echo "{$line['value']}%";
				}
				?>
            </td>
        </tr>
	<?php endforeach; ?>
    </tbody>
</table>
<?php if ( ! empty( $data['table_message'] ) ): ?>
    <p><?php echo esc_html($data['table_message']) ?></p>
<?php else: ?>
    <div><?php _e( 'Bulk pricing will be applied to package:', 'advanced-dynamic-pricing-for-woocommerce' ); ?></div>
    <ul>
		<?php foreach ( $dependencies as $dependency ): ?>
            <li>
				<?php
				if ( 'any' === $dependency['method'] ) {
					echo sprintf(
						'<a href="%s">%s</a>',
						get_permalink( wc_get_page_id( 'shop' ) ),
						sprintf( __( '%d of any product(s)', 'advanced-dynamic-pricing-for-woocommerce' ), $dependency['qty'] )
					);
					continue;
				}

				$items = array();
				foreach ( $dependency['values'] as $i => $value ) {
					$name = $dependency['titles'][ $i ];
					$link = $dependency['links'][ $i ];
					if ( ! empty( $link ) ) {
						$items[] = "<a href='{$link}'>{$name}</a>";
					} else {
						$items[] = "'{$name}'";
					}
				}
				$items = implode( ', ', $items );
				if ( 'products' === $dependency['type'] ) {
					if ( 'in_list' === $dependency['method'] ) {
						echo sprintf(
							__( '%d product(s) from list: %s', 'advanced-dynamic-pricing-for-woocommerce' ),
							$dependency['qty'],
							$items
						);
					} elseif ( 'not_in_list' === $dependency['method'] ) {
						echo sprintf(
							__( '%d product(s) not from list: %s', 'advanced-dynamic-pricing-for-woocommerce' ),
							$dependency['qty'],
							$items
						);
					}
				} elseif ( 'product_categories' === $dependency['type'] ) {
					if ( 'in_list' === $dependency['method'] ) {
						echo sprintf(
							__( '%d product(s) from categories: %s', 'advanced-dynamic-pricing-for-woocommerce' ),
							$dependency['qty'],
							$items
						);
					} elseif ( 'not_in_list' === $dependency['method'] ) {
						echo sprintf(
							__( '%d product(s) not from categories: %s', 'advanced-dynamic-pricing-for-woocommerce' ),
							$dependency['qty'],
							$items
						);
					}
				} elseif ( 'product_tags' === $dependency['type'] ) {
					if ( 'in_list' === $dependency['method'] ) {
						echo sprintf(
							__( '%d product(s) with tags from list: %s', 'advanced-dynamic-pricing-for-woocommerce' ),
							$dependency['qty'],
							$items
						);
					} elseif ( 'not_in_list' === $dependency['method'] ) {
						echo sprintf(
							__( '%d product(s) with tags not from list: %s', 'advanced-dynamic-pricing-for-woocommerce' ),
							$dependency['qty'],
							$items
						);
					}
				} elseif ( 'product_attributes' === $dependency['type'] ) {
					if ( 'in_list' === $dependency['method'] ) {
						echo sprintf(
							__( '%d product(s) with attributes from list: %s', 'advanced-dynamic-pricing-for-woocommerce' ),
							$dependency['qty'],
							$items
						);
					} elseif ( 'not_in_list' === $dependency['method'] ) {
						echo sprintf(
							__( '%d product(s) with attributes not from list: %s', 'advanced-dynamic-pricing-for-woocommerce' ),
							$dependency['qty'],
							$items
						);
					}
				}
				?>
            </li>
		<?php endforeach; ?>
    </ul>
<?php endif; ?>
<br>
</div>