<?php
/**
 * Filters for one destination.
 *
 * @package Ordsync
 *
 * @var string $tab    Destination type.
 * @var int    $ordsync_slot_i Slot index.
 * @var array  $ordsync_slot   Slot settings, including its own rules.
 */

defined( 'ABSPATH' ) || exit;

// Rules belong to the individual slot, not to the type.
$ordsync_rules    = isset( $ordsync_slot['conditions'] ) ? (array) $ordsync_slot['conditions'] : array();
$ordsync_match    = isset( $ordsync_slot['condition_match'] ) ? $ordsync_slot['condition_match'] : 'all';
$ordsync_prefix   = 'ordsync[' . $tab . '][' . $ordsync_slot_i . ']';
$ordsync_unlocked = class_exists( 'Ordsync_Conditions' ) && function_exists( 'ordsync_is_premium' ) && ordsync_is_premium();

/*
 * Most shops want every order to go, so this stays folded. It springs open
 * only when rules already exist -- a rule you cannot see is a rule you will
 * blame the plugin for.
 */
$ordsync_rules_open = ( $ordsync_unlocked && ! empty( $ordsync_rules ) );

/*
 * Name the thing being filtered in the user's own words. This partial is
 * shared by every tab, so it derives the noun from $tab rather than reading
 * one the including view happens to define.
 */
$ordsync_filter_intro = array(
	'sheets'  => __( 'Send to this sheet only when an order matches specific conditions.', 'ordercarry-order-automation-for-woocommerce' ),
	'webhook' => __( 'Send to this webhook only when an order matches specific conditions.', 'ordercarry-order-automation-for-woocommerce' ),
	'email'   => __( 'Send this email only when an order matches specific conditions.', 'ordercarry-order-automation-for-woocommerce' ),
	'pdf'     => __( 'Send this invoice only when an order matches specific conditions.', 'ordercarry-order-automation-for-woocommerce' ),
);

$ordsync_intro_line = isset( $ordsync_filter_intro[ $tab ] )
	? $ordsync_filter_intro[ $tab ]
	: __( 'Send to this destination only when an order matches specific conditions.', 'ordercarry-order-automation-for-woocommerce' );
?>

<details class="ordsync-conditions ordsync-disclosure" <?php echo esc_attr( $ordsync_rules_open ? 'open' : '' ); ?>>

<summary>
	<?php esc_html_e( 'Filters', 'ordercarry-order-automation-for-woocommerce' ); ?>
	<?php if ( ! $ordsync_unlocked ) : ?>
		<span class="ordsync-pro-badge"><?php esc_html_e( 'Pro', 'ordercarry-order-automation-for-woocommerce' ); ?></span>
	<?php elseif ( ! empty( $ordsync_rules ) ) : ?>
		<span class="ordsync-count"><?php echo esc_html( count( $ordsync_rules ) ); ?></span>
	<?php endif; ?>
</summary>

<div class="ordsync-disclosure-body">

	<p class="ordsync-filter-intro"><?php echo esc_html( $ordsync_intro_line ); ?></p>

<?php if ( ! $ordsync_unlocked ) : ?>

	<p class="description">
		<?php esc_html_e( 'Without filters every order is sent. Pro can narrow it down — for example only orders over 100, only Germany, Austria and Switzerland, or only orders containing a certain product.', 'ordercarry-order-automation-for-woocommerce' ); ?>
	</p>

<?php else : ?>

	<?php
	$ordsync_fields    = Ordsync_Conditions::fields();
	$ordsync_operators = Ordsync_Conditions::operators();

	/**
	 * Render one rule row.
	 *
	 * @param string $tab       Destination key.
	 * @param string $index     Row index, or the __i__ placeholder.
	 * @param array  $rule      Existing values.
	 * @param array  $fields    Field choices.
	 * @param array  $operators Operator choices.
	 * @param bool   $template  Whether this is the hidden template row.
	 * @return void
	 */
	$ordsync_row = function ( $prefix, $index, $rule, $fields, $operators, $template = false ) {
		$name  = $prefix . '[conditions][' . $index . ']';
		$field = isset( $rule['field'] ) ? $rule['field'] : '';
		$op    = isset( $rule['operator'] ) ? $rule['operator'] : '';
		$value = isset( $rule['value'] ) ? $rule['value'] : '';
		?>
		<div class="ordsync-rule<?php echo esc_attr( $template ? ' ordsync-rule-template' : '' ); ?>">

			<select <?php echo esc_attr( $template ? 'data-name' : 'name' ); ?>="<?php echo esc_attr( $name . '[field]' ); ?>"
				aria-label="<?php esc_attr_e( 'Field', 'ordercarry-order-automation-for-woocommerce' ); ?>">
				<?php foreach ( $fields as $key => $label ) : ?>
					<option value="<?php echo esc_attr( $key ); ?>" <?php selected( $key, $field ); ?>>
						<?php echo esc_html( $label ); ?>
					</option>
				<?php endforeach; ?>
			</select>

			<select <?php echo esc_attr( $template ? 'data-name' : 'name' ); ?>="<?php echo esc_attr( $name . '[operator]' ); ?>"
				aria-label="<?php esc_attr_e( 'Comparison', 'ordercarry-order-automation-for-woocommerce' ); ?>">
				<?php foreach ( $operators as $key => $label ) : ?>
					<option value="<?php echo esc_attr( $key ); ?>" <?php selected( $key, $op ); ?>>
						<?php echo esc_html( $label ); ?>
					</option>
				<?php endforeach; ?>
			</select>

			<input type="text"
				<?php echo esc_attr( $template ? 'data-name' : 'name' ); ?>="<?php echo esc_attr( $name . '[value]' ); ?>"
				value="<?php echo esc_attr( $value ); ?>"
				placeholder="<?php esc_attr_e( 'value', 'ordercarry-order-automation-for-woocommerce' ); ?>"
				aria-label="<?php esc_attr_e( 'Value', 'ordercarry-order-automation-for-woocommerce' ); ?>" />

			<button type="button" class="button-link ordsync-remove-rule"
				aria-label="<?php esc_attr_e( 'Remove this filter', 'ordercarry-order-automation-for-woocommerce' ); ?>">
				<?php esc_html_e( 'Remove', 'ordercarry-order-automation-for-woocommerce' ); ?>
			</button>
		</div>
		<?php
	};
	?>

	<div class="ordsync-field">
		<label for="ordsync-match-<?php echo esc_attr( $tab . '-' . $ordsync_slot_i ); ?>">
			<?php esc_html_e( 'Send the order when', 'ordercarry-order-automation-for-woocommerce' ); ?>
		</label>
		<select id="ordsync-match-<?php echo esc_attr( $tab . '-' . $ordsync_slot_i ); ?>"
			name="<?php echo esc_attr( $ordsync_prefix ); ?>[condition_match]">
			<option value="all" <?php selected( 'all', $ordsync_match ); ?>>
				<?php esc_html_e( 'every filter below matches', 'ordercarry-order-automation-for-woocommerce' ); ?>
			</option>
			<option value="any" <?php selected( 'any', $ordsync_match ); ?>>
				<?php esc_html_e( 'any one filter below matches', 'ordercarry-order-automation-for-woocommerce' ); ?>
			</option>
		</select>
	</div>

	<div class="ordsync-rules">
		<?php foreach ( array_values( $ordsync_rules ) as $ordsync_i => $ordsync_rule ) : ?>
			<?php $ordsync_row( $ordsync_prefix, $ordsync_i, $ordsync_rule, $ordsync_fields, $ordsync_operators ); ?>
		<?php endforeach; ?>
	</div>

	<?php // Cloned by admin.js. Inputs carry data-name so they never submit. ?>
	<?php $ordsync_row( $ordsync_prefix, '__i__', array(), $ordsync_fields, $ordsync_operators, true ); ?>

	<p>
		<button type="button" class="button ordsync-add-rule"
			data-start="<?php echo esc_attr( count( $ordsync_rules ) ); ?>">
			<?php esc_html_e( '+ Add filter', 'ordercarry-order-automation-for-woocommerce' ); ?>
		</button>
		<span class="description">
			<?php esc_html_e( 'No filters means every order is sent.', 'ordercarry-order-automation-for-woocommerce' ); ?>
		</span>
	</p>

<?php endif; ?>

</div><!-- .ordsync-disclosure-body -->
</details><!-- .ordsync-conditions -->
