<?php
/**
 * Address editor fields — shared by the My Account book and the checkout selector.
 *
 * Deliberately NOT wrapped in a <form>: at checkout this markup lives inside
 * WooCommerce's checkout form, and nested forms are invalid HTML. The save
 * button is a plain button and the script posts the values over AJAX.
 *
 * This template can be overridden by copying it to
 * yourtheme/vanquish-multiple-addresses/address-fields.php
 *
 * @package Vanquish\MultipleAddresses
 * @var array $fields WooCommerce address fields keyed by plain name.
 */

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

<h4 class="vanmuad-form__title" data-vanmuad-form-title>
	<?php esc_html_e( 'Add address', 'vanquish-multiple-addresses-for-woocommerce' ); ?>
</h4>

<input type="hidden" value="" data-vanmuad-field-id />

<p class="form-row form-row-wide">
	<label for="vanmuad-address-name">
		<?php esc_html_e( 'Address name', 'vanquish-multiple-addresses-for-woocommerce' ); ?>
		<span class="vanmuad-hint">(<?php esc_html_e( 'e.g. Home, Office', 'vanquish-multiple-addresses-for-woocommerce' ); ?>)</span>
	</label>
	<input type="text" class="input-text" id="vanmuad-address-name" data-vanmuad-field-name />
</p>

<?php
foreach ( $fields as $vanmuad_name => $vanmuad_field ) {
	// WooCommerce renders the field, including its country/state behaviour.
	$vanmuad_field['id'] = 'vanmuad-field-' . $vanmuad_name;

	// Tag each input with its plain field name so the script can collect the
	// values without parsing the name attribute.
	$vanmuad_field['custom_attributes'] = isset( $vanmuad_field['custom_attributes'] ) ? (array) $vanmuad_field['custom_attributes'] : array();
	$vanmuad_field['custom_attributes']['data-vanmuad-field'] = $vanmuad_name;

	woocommerce_form_field( 'vanmuad_fields[' . $vanmuad_name . ']', $vanmuad_field, '' );
}
?>

<?php
// One address, two independent uses. Both may be ticked — billing and shipping
// to the same place is the common case, and the whole point of not typing the
// address itself is that it no longer has to be saved twice for that.
?>
<p class="form-row form-row-wide">
	<label class="vanmuad-checkbox">
		<input type="checkbox" value="1" data-vanmuad-field-default-billing />
		<span><?php esc_html_e( 'Use as my default billing address', 'vanquish-multiple-addresses-for-woocommerce' ); ?></span>
	</label>
</p>

<p class="form-row form-row-wide">
	<label class="vanmuad-checkbox">
		<input type="checkbox" value="1" data-vanmuad-field-default-shipping />
		<span><?php esc_html_e( 'Use as my default shipping address', 'vanquish-multiple-addresses-for-woocommerce' ); ?></span>
	</label>
</p>

<p class="form-row vanmuad-form__actions">
	<button type="button" class="vanmuad-btn vanmuad-btn--primary" data-vanmuad-save>
		<?php esc_html_e( 'Save address', 'vanquish-multiple-addresses-for-woocommerce' ); ?>
	</button>
	<button type="button" class="vanmuad-btn" data-vanmuad-cancel>
		<?php esc_html_e( 'Cancel', 'vanquish-multiple-addresses-for-woocommerce' ); ?>
	</button>
</p>
