<?php
/**
 * Time To Deliver functionality for FoodOrd Delivery.
 *
 * @package FoodOrd
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Class Foodord_Time_To_Deliver
 */
class Foodord_Time_To_Deliver {

	/**
	 * Initialize the class.
	 */
	public function __construct() {
		add_action( 'admin_init', array( $this, 'register_settings' ) );
	}

	/**
	 * Register settings for time to deliver.
	 */
	public function register_settings() {
		register_setting(
			'foodord_delivery_hours',
			'foodord_time_to_deliver_enabled',
			array(
				'type'              => 'string',
				'sanitize_callback' => array( $this, 'sanitize_yes_no_option' ),
			)
		);
		register_setting(
			'foodord_delivery_hours',
			'foodord_time_to_deliver_breakdown',
			array(
				'type'              => 'string',
				'sanitize_callback' => array( $this, 'sanitize_time_to_deliver_breakdown_option' ),
			)
		);
	}

	/**
	 * Sanitize yes/no option.
	 *
	 * @param mixed $value Value to sanitize.
	 */
	public function sanitize_yes_no_option( $value ): string {
		return 'yes' === $value ? 'yes' : 'no';
	}

	/**
	 * Sanitize time-to-deliver breakdown option.
	 *
	 * @param mixed $value Value to sanitize.
	 */
	public function sanitize_time_to_deliver_breakdown_option( $value ): string {
		$allowed  = array( '5', '10', '15', '20', '30', '45', '60', '90' );
		$selected = sanitize_text_field( (string) $value );
		return in_array( $selected, $allowed, true ) ? $selected : '30';
	}

	/**
	 * Render the Time To Deliver container.
	 */
	public function render_time_to_deliver_container() {
		$time_to_deliver_enabled = 'yes' === get_option( 'foodord_time_to_deliver_enabled', 'no' );
		$breakdown_value         = get_option( 'foodord_time_to_deliver_breakdown', '30' );

		$scheduled_date_enabled = 'yes' === get_option( 'foodord_scheduled_date_enabled_delivery', 'no' );
		$max_days_ahead         = absint( get_option( 'foodord_scheduled_date_max_days_delivery', 7 ) );
		if ( $max_days_ahead < 1 || $max_days_ahead > 365 ) {
			$max_days_ahead = 7;
		}
		$scheduled_days_class = $scheduled_date_enabled ? '' : 'wrc-field--preorder-hidden';
		?>
		<div class="wrc-time-to-deliver-container">
			<div class="wrc-time-to-deliver-header">
				<h2 class="wrc-section-title"><?php esc_html_e( 'Time To Deliver', 'foodord' ); ?></h2>
			</div>
			
			<div class="wrc-time-to-deliver-content">
				<div class="wrc-time-deliver-grid">
					<div class="wrc-time-deliver-group">
						<div class="wrc-time-deliver-cell">
							<label class="wrc-field-label" for="wrc_time_to_deliver_enabled"><?php esc_html_e( 'Enable Time To Deliver', 'foodord' ); ?></label>
							<div class="wrc-control">
								<div class="foodord-toggle-row">
									<label class="foodord-toggle-switch" for="wrc_time_to_deliver_enabled">
										<input type="checkbox" class="wrc-input" id="wrc_time_to_deliver_enabled" name="wrc_time_to_deliver_enabled" value="1" <?php checked( $time_to_deliver_enabled, true ); ?> />
										<span class="foodord-toggle-slider"></span>
									</label>
									<span class="foodord-toggle-label"><?php esc_html_e( 'Show estimated delivery time to customers.', 'foodord' ); ?></span>
								</div>
							</div>
						</div>
						<div class="wrc-time-deliver-cell">
							<label class="wrc-field-label" for="wrc_time_to_deliver_breakdown"><?php esc_html_e( 'Break Down', 'foodord' ); ?></label>
							<div class="wrc-control">
								<select id="wrc_time_to_deliver_breakdown" name="wrc_time_to_deliver_breakdown" class="wrc-input">
									<option value="5" <?php selected( $breakdown_value, '5' ); ?>><?php esc_html_e( '5 minutes', 'foodord' ); ?></option>
									<option value="10" <?php selected( $breakdown_value, '10' ); ?>><?php esc_html_e( '10 minutes', 'foodord' ); ?></option>
									<option value="15" <?php selected( $breakdown_value, '15' ); ?>><?php esc_html_e( '15 minutes', 'foodord' ); ?></option>
									<option value="20" <?php selected( $breakdown_value, '20' ); ?>><?php esc_html_e( '20 minutes', 'foodord' ); ?></option>
									<option value="30" <?php selected( $breakdown_value, '30' ); ?>><?php esc_html_e( '30 minutes', 'foodord' ); ?></option>
									<option value="45" <?php selected( $breakdown_value, '45' ); ?>><?php esc_html_e( '45 minutes', 'foodord' ); ?></option>
									<option value="60" <?php selected( $breakdown_value, '60' ); ?>><?php esc_html_e( '60 minutes', 'foodord' ); ?></option>
									<option value="90" <?php selected( $breakdown_value, '90' ); ?>><?php esc_html_e( '90 minutes', 'foodord' ); ?></option>
								</select>
							</div>
						</div>
					</div>

					<div class="wrc-time-deliver-group">
						<div class="wrc-time-deliver-cell">
							<label class="wrc-field-label" for="wrc_scheduled_date_enabled_delivery"><?php esc_html_e( 'Enable scheduled dates', 'foodord' ); ?></label>
							<div class="wrc-control">
								<div class="foodord-toggle-row">
									<label class="foodord-toggle-switch" for="wrc_scheduled_date_enabled_delivery">
										<input type="checkbox" class="wrc-input" id="wrc_scheduled_date_enabled_delivery" name="wrc_scheduled_date_enabled_delivery" value="1" <?php checked( $scheduled_date_enabled, true ); ?> />
										<span class="foodord-toggle-slider"></span>
									</label>
									<span class="foodord-toggle-label"><?php esc_html_e( 'Allow customers to select a delivery date.', 'foodord' ); ?></span>
								</div>
							</div>
						</div>
						<div class="wrc-time-deliver-cell <?php echo esc_attr( $scheduled_days_class ); ?>" id="wrc_scheduled_date_max_days_wrapper_delivery">
							<label class="wrc-field-label" for="wrc_scheduled_date_max_days_delivery"><?php esc_html_e( 'Max days ahead', 'foodord' ); ?></label>
							<div class="wrc-control">
								<input type="number" id="wrc_scheduled_date_max_days_delivery" name="wrc_scheduled_date_max_days_delivery" class="wrc-input" value="<?php echo esc_attr( $max_days_ahead ); ?>" min="1" max="365" step="1" />
								<p class="wrc-help"><?php esc_html_e( 'Customers can schedule up to this many days ahead.', 'foodord' ); ?></p>
							</div>
						</div>
					</div>
				</div>
			</div>
		</div>
		<?php
	}
}
