<?php
/**
 * My Account — address book.
 *
 * This template can be overridden by copying it to
 * yourtheme/vanquish-multiple-addresses/my-account/address-book.php
 *
 * @package Vanquish\MultipleAddresses
 * @var array $addresses Saved addresses keyed by id.
 * @var array $fields    Address form fields keyed by plain name.
 */

use Vanquish\MultipleAddresses\Com\Access;
use Vanquish\MultipleAddresses\Com\AddressForm;
use Vanquish\MultipleAddresses\Com\Template;

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

// A customer may be allowed to use the book without being allowed to change it,
// in which case the list is shown but every control that alters it is not. And
// they may be allowed to change it while having no room for another address, in
// which case editing stays and adding goes.
$vanmuad_can_manage = Access::can_manage();
$vanmuad_can_add    = Access::can_add();
?>

<div class="vanmuad-book" data-vanmuad-book>

	<div class="vanmuad-notice" data-vanmuad-notice hidden></div>

	<header class="vanmuad-section__header">
		<h3 class="vanmuad-section__title"><?php esc_html_e( 'Your addresses', 'vanquish-multiple-addresses-for-woocommerce' ); ?></h3>
		<?php if ( $vanmuad_can_add ) : ?>
			<button type="button" class="vanmuad-btn" data-vanmuad-add>
				<?php esc_html_e( 'Add address', 'vanquish-multiple-addresses-for-woocommerce' ); ?>
			</button>
		<?php endif; ?>
	</header>

	<?php
	/**
	 * Extension point above the list — the premium management toolbar hangs here
	 * (search, select all, bulk delete). Nothing listens in the free build.
	 *
	 * @param array $addresses The customer's addresses.
	 */
	do_action( 'vanmuad_address_book_toolbar', $addresses );
	?>

	<ul class="vanmuad-list" data-vanmuad-list>
		<?php if ( empty( $addresses ) ) : ?>
			<li class="vanmuad-empty" data-vanmuad-empty>
				<?php esc_html_e( 'You have not saved any address yet.', 'vanquish-multiple-addresses-for-woocommerce' ); ?>
			</li>
		<?php else : ?>
			<?php foreach ( $addresses as $vanmuad_address ) : ?>
				<li class="vanmuad-card"
					data-vanmuad-card
					data-address-id="<?php echo esc_attr( $vanmuad_address['id'] ); ?>"
					data-address='<?php echo esc_attr( wp_json_encode( $vanmuad_address ) ); ?>'>

					<div class="vanmuad-card__head">
						<?php
						/**
						 * Extension point at the start of a card — the premium
						 * selection checkbox hangs here.
						 *
						 * @param array $vanmuad_address The address.
						 */
						do_action( 'vanmuad_address_book_card_start', $vanmuad_address );
						?>

						<span class="vanmuad-card__name">
							<?php
							echo esc_html(
								'' !== $vanmuad_address['internal_name']
									? $vanmuad_address['internal_name']
									: __( 'Address', 'vanquish-multiple-addresses-for-woocommerce' )
							);
							?>
						</span>

						<?php if ( ! empty( $vanmuad_address['is_default_billing'] ) ) : ?>
							<span class="vanmuad-badge"><?php esc_html_e( 'Billing', 'vanquish-multiple-addresses-for-woocommerce' ); ?></span>
						<?php endif; ?>

						<?php if ( ! empty( $vanmuad_address['is_default_shipping'] ) ) : ?>
							<span class="vanmuad-badge"><?php esc_html_e( 'Shipping', 'vanquish-multiple-addresses-for-woocommerce' ); ?></span>
						<?php endif; ?>
					</div>

					<address class="vanmuad-card__address">
						<?php
						// WooCommerce escapes and formats the address itself.
						echo wp_kses_post( AddressForm::format( $vanmuad_address['fields'] ) );
						?>
					</address>

					<?php
					/**
					 * Extension point under the address — premium shows the
					 * delivery notes here, which WooCommerce's address formatter
					 * knows nothing about.
					 *
					 * @param array $vanmuad_address The address.
					 */
					do_action( 'vanmuad_address_book_card_after_address', $vanmuad_address );
					?>

					<div class="vanmuad-card__actions">
						<?php if ( ! $vanmuad_can_manage ) : ?>
							<?php // Read-only: the merchant maintains this list. ?>
						<?php else : ?>
						<button type="button" class="vanmuad-btn vanmuad-btn--small" data-vanmuad-edit>
							<?php esc_html_e( 'Edit', 'vanquish-multiple-addresses-for-woocommerce' ); ?>
						</button>

						<?php
						// Quick way to move a default without opening the editor.
						// Only offered for the purpose the address is not already
						// the default for.
						?>
						<?php if ( empty( $vanmuad_address['is_default_billing'] ) ) : ?>
							<button type="button" class="vanmuad-btn vanmuad-btn--small" data-vanmuad-default="billing">
								<?php esc_html_e( 'Use for billing', 'vanquish-multiple-addresses-for-woocommerce' ); ?>
							</button>
						<?php endif; ?>

						<?php if ( empty( $vanmuad_address['is_default_shipping'] ) ) : ?>
							<button type="button" class="vanmuad-btn vanmuad-btn--small" data-vanmuad-default="shipping">
								<?php esc_html_e( 'Use for shipping', 'vanquish-multiple-addresses-for-woocommerce' ); ?>
							</button>
						<?php endif; ?>

						<?php
						/**
						 * Extension point in a card's actions — the premium
						 * "Duplicate" button hangs here.
						 *
						 * @param array $vanmuad_address The address.
						 */
						do_action( 'vanmuad_address_book_card_actions', $vanmuad_address );
						?>

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

	<?php
	// No editor at all when the book is read-only — there is nothing that could
	// open it.
	//
	// `$with_editor` lets a caller render the book here and the editor somewhere
	// else entirely. The admin profile screen needs that: WordPress prints the
	// book inside <form id="your-profile">, and the editor's inputs are the only
	// ones the plugin renders that carry a `name`, so they are the only ones that
	// would land in that form's serialization.
	if ( $vanmuad_can_manage && ( ! isset( $with_editor ) || $with_editor ) ) {
		Template::render(
			'my-account/address-form.php',
			array(
				'fields' => $fields,
			)
		);
	}
	?>
</div>
