<?php
/**
 * Display meta-information/filters for pickup shipping.
 *
 * @package flo-sync
 * @var array                $params             Array view parameters.
 * @var Hub_Account_Model[]  $accounts           Array of accounts to choose from.
 * @var string               $select_id          ID of the select element.
 * @var string               $shipping_method_id ID of the shipping method.
 * @var string               $chosen_store_id    ID of the chosen store.
 */

use Northmill_Online_Shared_Sync\Northmill_Online_Shared\Abstracts\Hub_API\Hub_Accounts\Models\Hub_Account_Model;

defined( 'ABSPATH' ) || exit;

$accounts           = $params['accounts'];
$select_id          = $params['select_id'];
$shipping_method_id = $params['shipping_method_id'];
$chosen_store_id    = $params['chosen_store_id'];

?>
<div class="nmf-mas-store-select">
	<?php if ( empty( $accounts ) ) : ?>
		<p class="woocommerce-error">
			<?php esc_html_e( 'All items in the cart must be available from the same store, please update your cart.', 'flo-sync' ); ?>
		</p>
	<?php elseif ( count( $accounts ) > 1 ) : ?>
		<p>
			<span class="woocommerce-input-wrapper">
				<select class="wc-enhanced-select nmf-backoffice-account-select" id="<?php echo esc_attr( $select_id ); ?>" name="<?php echo esc_attr( $select_id ); ?>" data-shipping-method="<?php echo esc_attr( $shipping_method_id ); ?>">
					<option value=""><?php esc_html_e( 'Choose store', 'flo-sync' ); ?></option>
					<?php foreach ( $accounts as $account ) { ?>
						<option value="<?php echo esc_attr( $account->wp_id ); ?>" <?php selected( $chosen_store_id, $account->wp_id ); ?> >
							<?php echo esc_html( $account->get_display_name() ); ?>
						</option>
					<?php } ?>
				</select>
			</span>
			<small>
				<?php esc_html_e( 'Only stores with all cart items in stock are shown in the list.', 'flo-sync' ); ?>
			</small>
		</p>
	<?php else : ?>
		<?php
		$account = $accounts[ array_key_first( $accounts ) ];
		// translators: %s is store name.
		echo esc_html( sprintf( __( 'Checking out from %s', 'flo-sync' ), esc_html( $account->get_display_name() ) ) );
		?>
		<input type="hidden" name="<?php echo esc_attr( $select_id ); ?>" value="<?php echo esc_attr( $account->wp_id ); ?>" />
	<?php endif; ?>
</div>
