<?php
/**
 * Snooze Form Template
 *
 * Inline form displayed when a subscriber clicks the Snooze action.
 * Shows duration options and a confirmation button.
 *
 * This template can be overridden by copying it to yourtheme/autoship-cloud/templates/scheduled-orders/snooze-form-template.php
 *
 * @package Autoship
 * @subpackage Templates
 *
 * @var int   $order_number   The scheduled order ID.
 * @var array $snooze_options The available snooze duration options.
 * @var bool  $is_snoozed     Whether the order is currently snoozed (re-snooze flow).
 */

defined( 'ABSPATH' ) || exit;

$snooze_label      = autoship_get_snooze_action_label();
$schedule_label    = autoship_translate_text( 'Scheduled Order' );
$allow_custom_date = autoship_is_snooze_custom_date_allowed();
$max_duration_days = autoship_get_snooze_max_duration_days();
$max_date_attr     = '';
$is_snoozed        = ! empty( $is_snoozed );

if ( ! empty( $max_duration_days ) ) {
	$max_date_attr = gmdate( 'Y-m-d', strtotime( '+' . absint( $max_duration_days ) . ' days' ) );
}

$min_date_attr = gmdate( 'Y-m-d', strtotime( '+1 day' ) );
?>

<div class="autoship-snooze-form"
	id="autoship-snooze-form-<?php echo esc_attr( $order_number ); ?>"
	data-order-id="<?php echo esc_attr( $order_number ); ?>"
	data-is-snoozed="<?php echo $is_snoozed ? 'true' : 'false'; ?>">

	<div class="autoship-snooze-form-inner">

		<h4 class="autoship-snooze-form-title">
			<?php
			echo esc_html(
				sprintf(
					/* translators: %1$s is the snooze action label (e.g. "Snooze"), %2$s is the Scheduled Order label */
					__( '%1$s this %2$s', 'autoship' ),
					$snooze_label,
					$schedule_label
				)
			);
			?>
		</h4>

		<p class="autoship-snooze-form-subtitle">
			<?php
			echo wp_kses(
				__( 'Choose how long to pause. Your order will <strong>automatically resume</strong> on the date you select — no action needed.', 'autoship' ),
				array( 'strong' => array() )
			);
			?>
		</p>

		<div class="autoship-snooze-options">
			<label class="autoship-snooze-select-label" for="autoship-snooze-duration-<?php echo esc_attr( $order_number ); ?>">
				<?php echo esc_html( __( 'SNOOZE DURATION', 'autoship' ) ); ?>
			</label>
			<select class="autoship-snooze-duration-select"
					id="autoship-snooze-duration-<?php echo esc_attr( $order_number ); ?>"
					name="autoship_snooze_duration_<?php echo esc_attr( $order_number ); ?>">
				<option value="" data-duration-type=""><?php echo esc_html( __( '— Select a duration —', 'autoship' ) ); ?></option>
				<?php
				foreach ( $snooze_options as $option ) :
					$option_label = ! empty( $option['Label'] ) ? $option['Label'] : autoship_format_snooze_duration_label( $option['Duration'], $option['DurationType'] );
					?>
					<option value="<?php echo esc_attr( $option['Duration'] ); ?>"
							data-duration-type="<?php echo esc_attr( $option['DurationType'] ); ?>">
						<?php echo esc_html( $option_label ); ?>
					</option>
				<?php endforeach; ?>
				<?php if ( $allow_custom_date ) : ?>
					<option value="custom" data-duration-type="Custom"><?php echo esc_html( __( 'Custom date', 'autoship' ) ); ?></option>
				<?php endif; ?>
			</select>
		</div>

		<?php if ( $allow_custom_date ) : ?>
			<div class="autoship-snooze-custom-date" id="autoship-snooze-custom-date-<?php echo esc_attr( $order_number ); ?>" style="display:none;">
				<label class="autoship-snooze-select-label" for="autoship-snooze-custom-date-input-<?php echo esc_attr( $order_number ); ?>">
					<?php echo esc_html( __( 'RESUME DATE', 'autoship' ) ); ?>
				</label>
				<input type="date"
					id="autoship-snooze-custom-date-input-<?php echo esc_attr( $order_number ); ?>"
					class="autoship-snooze-custom-date-input"
					min="<?php echo esc_attr( $min_date_attr ); ?>"
					<?php
					if ( $max_date_attr ) :
						?>
					max="<?php echo esc_attr( $max_date_attr ); ?>"<?php endif; ?> />
			</div>
		<?php endif; ?>

		<p class="autoship-snooze-resume-date" id="autoship-snooze-resume-date-<?php echo esc_attr( $order_number ); ?>"></p>

		<div class="autoship-snooze-message" id="autoship-snooze-message-<?php echo esc_attr( $order_number ); ?>" style="display:none;"></div>

		<div class="autoship-snooze-form-actions">
			<button type="button"
					class="autoship-snooze-confirm button"
					data-order-id="<?php echo esc_attr( $order_number ); ?>">
				<?php
				echo esc_html(
					sprintf(
						/* translators: %s is the snooze action label */
						__( 'Confirm %s', 'autoship' ),
						$snooze_label
					)
				);
				?>
			</button>
			<button type="button"
					class="autoship-snooze-cancel button"
					data-order-id="<?php echo esc_attr( $order_number ); ?>">
				<?php echo esc_html( __( 'Cancel', 'autoship' ) ); ?>
			</button>
		</div>

	</div>

</div>
