<?php
/**
 * My Addresses
 *
 * This template can be overridden by copying it to yourtheme/woocommerce/myaccount/my-address.php.
 *
 * HOWEVER, on occasion WooCommerce will need to update template files and you
 * (the theme developer) will need to copy the new files to your theme to
 * maintain compatibility. We try to do this as little as possible, but it does
 * happen. When this occurs the version of the template file will be bumped and
 * the readme will list any important changes.
 *
 * @see     https://woocommerce.com/document/template-structure/
 * @package WooCommerce\Templates
 * @version 9.3.0
 */

defined( 'ABSPATH' ) || exit;

$customer_id = get_current_user_id();

if ( ! wc_ship_to_billing_address_only() && wc_shipping_enabled() ) {
	$get_addresses = apply_filters(
		'woocommerce_my_account_get_addresses',
		array(
			'billing'  => __('Billing address','hello-wpstream'),
			'shipping' => __('Shipping address','hello-wpstream'),
		),
		$customer_id
	);
} else {
	$get_addresses = apply_filters(
		'woocommerce_my_account_get_addresses',
		array(
			'billing' => __('Billing address','hello-wpstream'),
		),
		$customer_id
	);
}

$oldcol = 1;
$col    = 1;

$user_id = get_current_user_id();
?>

<section class="wpstream-dashboard-address">
	<div class="wpstream-dashboard-address__header">
		<h1 class="wpstream-dashboard-address__title">
			<?php 
				echo esc_html__('Addresses','hello-wpstream'); 
			?>
		</h1>

		<p class="wpstream-dashboard-address__subtitle">
			<?php 
				echo apply_filters( 'woocommerce_my_account_my_address_description', esc_html__( 'The following addresses will be used on the checkout page by default.', 'hello-wpstream' ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped 
			?>
		</p>
	</div>

	<?php if ( ! wc_ship_to_billing_address_only() && wc_shipping_enabled() ) : ?>
	<div class="u-columns woocommerce-Addresses col2-set addresses">
		<?php endif; ?>

		<?php foreach ( $get_addresses as $name => $section_title ) :
			$address = wc_get_account_formatted_address( $name );
			?>

			<div class="wpstream-dashboard-address__address woocommerce-Address">
				<header class="wpstream-dashboard-address__address-header woocommerce-Address-title title">
					<h3 class="wpstream-dashboard-address__address-title">
						<?php 
						echo esc_html($section_title); //phpcs:ignore WordPress.WP.I18n.NonSingularStringLiteralText 
						?>
					</h3>

					<?php
					if ( 'billing' === $name ) :
						$address_fields_billing = WC()->countries->get_address_fields( WC()->customer->get_billing_country(), 'billing_' );
	

						$modal_id               = 'wpstream_edit_addr_modal';
						$button_id              = 'wpstream_edit_address_save';
						$title                  = esc_html__( 'Edit', 'hello-wpstream' ); //phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
						?>

						<?php
						echo wpstream_address_modal_display( $modal_id, $button_id, $title, $address_fields_billing, $user_id ); //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
						?>

						<?php
					elseif ( 'shipping' === $name ) :
						$address_fields_shipping = WC()->countries->get_address_fields( WC()->customer->get_billing_country(), 'shipping_' );
						$modal_id                = 'wpstream_edit_addr_modal_shipping';
						$button_id               = 'wpstream_edit_address_save_shipping';
						$title                   = esc_html__( 'Edit', 'hello-wpstream' ); //phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
						?>

						<?php
						echo wpstream_address_modal_display( $modal_id, $button_id, $title, $address_fields_shipping, $user_id ); //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
						?>

					<?php endif; ?>
				</header>

				<?php if ( $address ) : ?>
					<?php
					if ( 'billing' === $name ) :
						$address_fields_billing = WC()->countries->get_address_fields( WC()->customer->get_billing_country(), 'billing_' );
						$billing_fields         = $address_fields_billing;
						?>
						<div class="wpstream-dashboard-address__address-body">
							<?php
							foreach ( $billing_fields as $key => $field ) :
								$saved_value = get_user_meta( $user_id, $key, true );
								?>
								<div class="wpstream-dashboard-details wpstream-dashboard-address__address-item">
									<div class="wpstream-dashboard-details-header wpstream-dashboard-address__address-item-label">
										<?php echo esc_html( $field['label'] ); ?>
									</div>
									<div id="wpstream_display_<?php echo esc_attr( $key ); ?>"
										class="wpstream_account_details_value wpstream-dashboard-address__address-item-value">
										<?php echo esc_html( $saved_value ); ?>
									</div>
								</div>
							<?php endforeach; ?>
						</div>
					<?php endif; ?>

					<?php
					if ( 'shipping' === $name ) :
						$address_fields_shipping = WC()->countries->get_address_fields( WC()->customer->get_billing_country(), 'shipping_' );
						$billing_fields          = $address_fields_shipping;
						?>
						<div class="wpstream-dashboard-address__address-body">
							<?php
							foreach ( $billing_fields as $key => $field ) :
								$saved_value = get_user_meta( $user_id, $key, true );
								?>
								<div class="wpstream-dashboard-details wpstream-dashboard-address__address-item">
									<div class="wpstream-dashboard-details-header wpstream-dashboard-address__address-item-label">
										<?php echo esc_html( $field['label'] ); ?>
									</div>

									<div id="wpstream_display_<?php echo esc_attr( $key ); ?>"
										class="wpstream_account_details_value wpstream-dashboard-address__address-item-value">
										<?php echo esc_html( $saved_value ); ?>
									</div>
								</div>
							<?php endforeach; ?>
						</div>
					<?php endif; ?>
				<?php else : ?>
					<p class="wpstream-dashboard-address__no-shipping"><?php esc_html_e( 'You have not set up this type of address yet.', 'hello-wpstream' ); ?></p>
				<?php endif; ?>
			</div>

		<?php endforeach; ?>

		<?php if ( ! wc_ship_to_billing_address_only() && wc_shipping_enabled() ) : ?>
	</div>
<?php endif; ?>
</section>