<?php
/**
 * Checkout — saved address selector.
 *
 * Rendered inside WooCommerce's checkout form, above the billing/shipping
 * fields. It contains no <form> and its select has no name attribute, so
 * nothing here is posted with the checkout: the script applies the chosen
 * address to WooCommerce's own fields.
 *
 * Deliberately NOT using WooCommerce's .form-row classes: this is our own
 * component, and inheriting them meant every margin and padding reset had to
 * out-specify `.woocommerce form .form-row` — a fight that ties on specificity
 * and is then decided by whichever stylesheet the theme happens to load last.
 *
 * This template can be overridden by copying it to
 * yourtheme/vanquish-multiple-addresses/checkout/address-selector.php
 *
 * @package Vanquish\MultipleAddresses
 * @var string $purpose   'billing' or 'shipping' — which checkout form this sits on.
 * @var array  $addresses Saved addresses keyed by id.
 * @var string $selected    Id of the address to preselect ('' for none).
 * @var bool   $show_add    Whether to offer adding a new address.
 * @var bool   $show_manage Whether to offer editing and deleting.
 * @var array  $labels      Address id => dropdown label.
 */

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

<div class="vanmuad-selector" data-vanmuad-selector="<?php echo esc_attr( $purpose ); ?>">

	<div class="vanmuad-selector__field">
		<label class="vanmuad-selector__label" for="vanmuad-select-<?php echo esc_attr( $purpose ); ?>">
			<?php esc_html_e( 'Choose a saved address', 'vanquish-multiple-addresses-for-woocommerce' ); ?>
		</label>

		<select id="vanmuad-select-<?php echo esc_attr( $purpose ); ?>"
			class="vanmuad-select"
			data-vanmuad-select>

			<option value=""><?php esc_html_e( 'Use the address below', 'vanquish-multiple-addresses-for-woocommerce' ); ?></option>

			<?php foreach ( $addresses as $vanmuad_address ) : ?>
				<option value="<?php echo esc_attr( $vanmuad_address['id'] ); ?>"
					data-address='<?php echo esc_attr( wp_json_encode( $vanmuad_address ) ); ?>'
					<?php selected( $selected, $vanmuad_address['id'] ); ?>>
					<?php echo esc_html( $labels[ $vanmuad_address['id'] ] ); ?>
				</option>
			<?php endforeach; ?>
		</select>
	</div>

	<?php
	// type="button" on all three is load-bearing: this markup sits inside
	// WooCommerce's checkout form, and a default-type button would submit the order.
	?>
	<div class="vanmuad-selector__actions">
		<?php // Adding can be out of reach — an allowance used up — while editing what is already saved is not. ?>
		<?php if ( $show_add ) : ?>
			<button type="button" class="vanmuad-btn" data-vanmuad-add>
				<?php esc_html_e( 'Add a new address', 'vanquish-multiple-addresses-for-woocommerce' ); ?>
			</button>
		<?php endif; ?>

		<?php if ( $show_manage ) : ?>
			<button type="button" class="vanmuad-btn" data-vanmuad-edit hidden>
				<?php esc_html_e( 'Edit', 'vanquish-multiple-addresses-for-woocommerce' ); ?>
			</button>

			<button type="button" class="vanmuad-btn vanmuad-btn--danger" data-vanmuad-delete hidden>
				<?php esc_html_e( 'Delete', 'vanquish-multiple-addresses-for-woocommerce' ); ?>
			</button>
		<?php endif; ?>
	</div>
</div>
