<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit; // Prevent direct access to the script.
}

global $current_user;
$user_id            = $args['user_id'] ?? $current_user->ID;
$first_name         = get_user_meta( $user_id, 'billing_first_name', true );
$first_name         = ! empty( $first_name ) ? $first_name : $current_user->first_name;
$last_name          = get_user_meta( $user_id, 'billing_last_name', true );
$last_name          = ! empty( $last_name ) ? $last_name : $current_user->last_name;
$email_raw          = get_user_meta( $user_id, 'billing_email', true );
$email              = ! empty( $email_raw ) ? $email_raw : '';
$company_raw        = get_user_meta( $user_id, 'billing_company', true );
$phone_raw          = get_user_meta( $user_id, 'billing_phone', true );
$country_state_list = gigneo_countries_with_states( 'active' );
$country            = get_user_meta( $user_id, 'billing_country', true );
$state              = get_user_meta( $user_id, 'billing_state', true );
$country_state_raw  = ! empty( $state ) ? "$country:$state" : $country;
$country_state      = ! empty( $country_state_list[ $country_state_raw ] ) ? $country_state_list[ $country_state_raw ] : esc_html__( 'None', 'gigneo' );
$city_raw           = get_user_meta( $user_id, 'billing_city', true );
$city               = ! empty( $city_raw ) ? $city_raw : esc_html__( 'None', 'gigneo' );
$zip_code_raw       = get_user_meta( $user_id, 'billing_postcode', true );
$zip_code           = ! empty( $zip_code_raw ) ? $zip_code_raw : esc_html__( 'None', 'gigneo' );
$address_1_raw      = get_user_meta( $user_id, 'billing_address_1', true );
$address_1          = ! empty( $address_1_raw ) ? $address_1_raw : esc_html__( 'None', 'gigneo' );
?>
<div id="gn-billing-address" class="gn-dashboard-section gn-section-billing-address">
	<div class="gn-head">
		<div class="gn-heading-info">
			<h3 class="gn-title"><?php echo esc_html__( 'Billing address', 'gigneo' ); ?></h3>
			<p class="gn-description"><?php echo esc_html__( 'Manage or update your billing details.', 'gigneo' ); ?></p>
		</div>
		<button class="gn-link gn-link-primary" data-gn-modal-toggle="gn-edit-user-billing-address">
			<i class="hi hi-pencil-edit" aria-hidden="true"></i>
			<?php echo esc_html__( 'Edit', 'gigneo' ); ?>
		</button>
	</div>
	<div class="gn-body">
		<div class="gn-stat-cards gn-stat-cards-grid-3">
			<div class="gn-stat-card">
				<h3 class="gn-label">
					<i class="hi hi-user" aria-hidden="true"></i>
					<?php echo esc_html__( 'Name', 'gigneo' ); ?>
				</h3>
				<h5 class="gn-value"><?php echo esc_html( $first_name . ' ' . $last_name ); ?></h5>
			</div>
			<div class="gn-stat-card">
				<h3 class="gn-label">
					<i class="hi hi-envelope" aria-hidden="true"></i>
					<?php echo esc_html__( 'Email', 'gigneo' ); ?>
				</h3>
				<h5 class="gn-value"><?php echo esc_html( $email ); ?></h5>
			</div>
			<div class="gn-stat-card">
				<h3 class="gn-label">
					<i class="hi hi-globe-americas" aria-hidden="true"></i>
					<?php echo esc_html__( 'Country / State', 'gigneo' ); ?>
				</h3>
				<h5 class="gn-value"><?php echo esc_html( $country_state ); ?></h5>
			</div>
			<div class="gn-stat-card">
				<h3 class="gn-label">
					<i class="hi hi-home" aria-hidden="true"></i>
					<?php echo esc_html__( 'City', 'gigneo' ); ?>
				</h3>
				<h5 class="gn-value"><?php echo esc_html( $city ); ?></h5>
			</div>
			<div class="gn-stat-card">
				<h3 class="gn-label">
					<i class="hi hi-building-office" aria-hidden="true"></i>
					<?php echo esc_html__( 'Postcode / ZIP', 'gigneo' ); ?>
				</h3>
				<h5 class="gn-value"><?php echo esc_html( $zip_code ); ?></h5>
			</div>
			<div class="gn-stat-card">
				<h3 class="gn-label">
					<i class="hi hi-map" aria-hidden="true"></i>
					<?php echo esc_html__( 'Address Line 1', 'gigneo' ); ?>
				</h3>
				<h5 class="gn-value"><?php echo esc_html( $address_1 ); ?></h5>
			</div>
		</div>
	</div>
	<!--Edit User Info Modal-->
	<div id="gn-edit-user-billing-address" class="gn-modal-wrapper gn-user-setting-modal">
		<div class="gn-modal-dialog">
			<div class="gn-modal-content">
				<div class="gn-modal-info">
					<div class="gn-modal-header">
						<h4 class="gn-title"><?php echo esc_html__( 'Billing Address', 'gigneo' ); ?></h4>
						<p class="gn-description"><?php echo esc_html__( 'Manage or update your billing details.', 'gigneo' ); ?></p>
						<button type="button" data-gn-modal-close="gn-edit-user-billing-address" class="gn-modal-close">
							<i class="hi hi-x-mark" aria-hidden="true"></i>
						</button>
					</div>
					<form id="gn-save-user-billing-address-form" class="gn-theme-form gn-user-billing-address-setting">
						<div class="gn-modal-body gn-custom-scrollbar">
							<fieldset class="gn-form-fields">
								<div class="row g-4">
									<div class="col-md-6">
										<div class="gn-form-field">
											<label class="gn-required-field-label" for="gn-billing-first-name"><?php echo esc_html__( 'First name', 'gigneo' ); ?></label>
											<input id="gn-billing-first-name" class="gn-required-field" type="text" name="billing_first_name" autocomplete="off" value="<?php echo esc_attr( $first_name ); ?>" placeholder="<?php echo esc_attr__( 'Enter first name', 'gigneo' ); ?>">
										</div>
									</div>
									<div class="col-md-6">
										<div class="gn-form-field">
											<label class="gn-required-field-label" for="gn-billing-last-name"><?php echo esc_html__( 'Last name', 'gigneo' ); ?></label>
											<input id="gn-billing-last-name" class="gn-required-field" type="text" name="billing_last_name" autocomplete="off" value="<?php echo esc_attr( $last_name ); ?>" placeholder="<?php echo esc_attr__( 'Enter last name', 'gigneo' ); ?>">
										</div>
									</div>
									<div class="col-md-12">
										<div class="gn-form-field">
											<label class="gn-required-field-label" for="gn-billing-email"><?php echo esc_html__( 'Email', 'gigneo' ); ?></label>
											<input id="gn-billing-email" class="gn-required-field" type="email" name="billing_email" autocomplete="off" value="<?php echo esc_attr( $email ); ?>" placeholder="<?php echo esc_attr__( 'Enter email', 'gigneo' ); ?>">
										</div>
									</div>
									<div class="col-md-6">
										<div class="gn-form-field">
											<label for="gn-billing-phone"><?php echo esc_html__( 'Phone', 'gigneo' ); ?></label>
											<input id="gn-billing-phone" type="number" name="billing_phone" autocomplete="off" value="<?php echo esc_attr( $phone_raw ); ?>" placeholder="<?php echo esc_attr__( 'Enter phone number', 'gigneo' ); ?>">
										</div>
									</div>
									<div class="col-md-6">
										<div class="gn-form-field">
											<label class="gn-required-field-label" for="gn-billing-zip"><?php echo esc_html__( 'Postcode / ZIP', 'gigneo' ); ?></label>
											<input id="gn-billing-zip" class="gn-required-field" type="text" name="billing_postcode" autocomplete="off" value="<?php echo esc_attr( $zip_code_raw ); ?>" placeholder="<?php echo esc_attr__( 'Enter postcode or ZIP', 'gigneo' ); ?>">
										</div>
									</div>
									<div class="col-md-6">
										<div class="gn-form-field gn-select2">
											<label class="gn-required-field-label" for="gn-billing-country-state"><?php echo esc_html__( 'Country / State', 'gigneo' ); ?></label>
											<select id="gn-billing-country-state" name="billing_country_state" class="gn-required-field">
												<?php foreach ( $country_state_list as $key => $country_state ) { ?>
													<option<?php echo esc_attr( $key === $country_state_raw ? ' selected' : '' ); ?> value="<?php echo esc_attr( $key ); ?>"><?php echo esc_html( $country_state ); ?></option>
												<?php } ?>
											</select>
										</div>
									</div>
									<div class="col-md-6">
										<div class="gn-form-field">
											<label class="gn-required-field-label" for="gn-billing-city"><?php echo esc_html__( 'City', 'gigneo' ); ?></label>
											<input id="gn-billing-city" class="gn-required-field" type="text" name="billing_city" autocomplete="off" value="<?php echo esc_attr( $city_raw ); ?>" placeholder="<?php echo esc_attr__( 'Enter city', 'gigneo' ); ?>">
										</div>
									</div>
									<div class="col-md-12">
										<div class="gn-form-field">
											<label class="gn-required-field-label" for="gn-billing-address"><?php echo esc_html__( 'Address', 'gigneo' ); ?></label>
											<textarea id="gn-billing-address" class="gn-required-field gn-textarea-counter" name="billing_address" autocomplete="off" rows="5" placeholder="<?php echo esc_attr__( 'Enter address', 'gigneo' ); ?>"><?php echo esc_attr( $address_1_raw ); ?></textarea>
										</div>
									</div>
								</div>
							</fieldset>
						</div>
						<div class="gn-modal-footer">
							<button type="button" class="gn-btn gn-btn-outline-grey" data-gn-modal-close="gn-edit-user-billing-address"><?php echo esc_html__( 'Close', 'gigneo' ); ?></button>
							<button type="submit" class="gn-btn gn-btn-primary"><?php echo esc_html__( 'Save', 'gigneo' ); ?></button>
						</div>
					</form>
				</div>
			</div>
		</div>
	</div>
</div>