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

$profile_id = $args['profile_id'] ?? gigneo_active_profile_id();

$country_state_list   = gigneo_countries_with_states( 'active' );
$disable_cities       = gigneo_option( '_gigneo_ts_disable_cities', false );
$country_state_raw    = gigneo_post_meta( $profile_id, '_gigneo_pm_country_state' );
$country_state        = ! empty( $country_state_list[ $country_state_raw ] ) ? $country_state_list[ $country_state_raw ] : esc_html__( 'None', 'gigneo' );
$city_raw             = gigneo_post_meta( $profile_id, '_gigneo_pm_city' );
$city                 = get_term_by( 'slug', $city_raw, 'gigneo_city' );
$city                 = ! empty( $city ) ? $city->name : esc_html__( 'None', 'gigneo' );
$area_raw             = gigneo_post_meta( $profile_id, '_gigneo_pm_area' );
$area                 = ! empty( $area_raw ) ? $area_raw : esc_html__( 'None', 'gigneo' );
$address_raw          = gigneo_post_meta( $profile_id, '_gigneo_pm_address' );
$address              = ! empty( $address_raw ) ? $address_raw : esc_html__( 'None', 'gigneo' );
$zip_code_raw         = gigneo_post_meta( $profile_id, '_gigneo_pm_zip_code' );
$zip_code             = ! empty( $zip_code_raw ) ? $zip_code_raw : esc_html__( 'None', 'gigneo' );
$country_state_cities = ! empty( $country_state_raw ) ? gigneo_country_state_cities( $country_state_raw ) : array();

?>
<div id="gn-profile-address" class="gn-setting-section">
	<div class="gn-head">
		<h3 class="gn-title"><?php echo esc_html__( 'Location', 'gigneo' ); ?></h3>
		<button class="gn-link gn-link-primary" data-gn-modal-toggle="gn-edit-profile-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="row g-4">
			<div class="col-sm-6">
				<div class="gn-info-grid">
					<span class="gn-icon">
						<i class="hi hi-globe-americas" aria-hidden="true"></i>
					</span>
					<div class="gn-info">
						<h5 class="gn-title"><?php echo esc_html( $country_state ); ?></h5>
						<p class="gn-label"><?php echo esc_html__( 'Country / State', 'gigneo' ); ?></p>
					</div>
				</div>
			</div>
			<?php
			if ( ! $disable_cities ) { ?>
			<div class="col-sm-6">
				<div class="gn-info-grid">
					<span class="gn-icon">
						<i class="hi hi-map" aria-hidden="true"></i>
					</span>
					<div class="gn-info">
						<h5 class="gn-title"><?php echo esc_html( $city ); ?></h5>
						<p class="gn-label"><?php echo esc_html__( 'City', 'gigneo' ); ?></p>
					</div>
				</div>
			</div>
			<?php } ?>
			<div class="col-sm-6">
				<div class="gn-info-grid">
					<span class="gn-icon">
						<i class="hi hi-flag" aria-hidden="true"></i>
					</span>
					<div class="gn-info">
						<h5 class="gn-title"><?php echo esc_html( $area ); ?></h5>
						<p class="gn-label"><?php echo esc_html__( 'Area', 'gigneo' ); ?></p>
					</div>
				</div>
			</div>
			<div class="col-sm-6">
				<div class="gn-info-grid">
					<span class="gn-icon">
						<i class="hi hi-flag" aria-hidden="true"></i>
					</span>
					<div class="gn-info">
						<h5 class="gn-title"><?php echo esc_html( $zip_code ); ?></h5>
						<p class="gn-label"><?php echo esc_html__( 'Postcode / ZIP', 'gigneo' ); ?></p>
					</div>
				</div>
			</div>
			<div class="col-md-12">
				<div class="gn-info-grid">
					<span class="gn-icon">
						<i class="hi hi-home" aria-hidden="true"></i>
					</span>
					<div class="gn-info">
						<h5 class="gn-title"><?php echo esc_html( $address ); ?></h5>
						<p class="gn-label"><?php echo esc_html__( 'Address', 'gigneo' ); ?></p>
					</div>
				</div>
			</div>
		</div>
	</div>
	<!--Edit Personal Info Modal-->
	<div id="gn-edit-profile-address" class="gn-modal-wrapper gn-profile-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__( 'Location', 'gigneo' ); ?></h4>
						<p class="gn-description"><?php echo esc_html__( 'Manage or update your address and location details.', 'gigneo' ); ?></p>
						<button type="button" data-gn-modal-close="gn-edit-profile-address" class="gn-modal-close">
							<i class="hi hi-x-mark" aria-hidden="true"></i>
						</button>
					</div>
					<form id="gn-save-profile-setting-form" class="gn-theme-form gn-save-profile-setting">
						<div class="gn-modal-body gn-custom-scrollbar">
							<fieldset class="gn-form-fields">
								<div class="row g-4">
									<div class="col-sm-6">
										<div class="gn-form-field gn-select2">
											<label for="gn-country-state" class="gn-required-field-label"><?php echo esc_html__( 'Country / State', 'gigneo' ); ?></label>
											<select id="gn-country-state" name="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>
									<?php
									if ( ! $disable_cities ) {
										?>
										<div class="col-sm-6">
											<div class="gn-form-field gn-select2">
												<label for="gn-city"><?php echo esc_html__( 'City', 'gigneo' ); ?></label>
												<select id="gn-city" name="city" placeholder="<?php echo esc_html__( 'None', 'gigneo' ); ?>">
													<?php foreach ( $country_state_cities as $key => $city_name ) { ?>
														<option<?php echo esc_attr( $key === $city_raw ? ' selected' : '' ); ?> value="<?php echo esc_attr( $key ); ?>"><?php echo esc_html( $city_name ); ?></option>
													<?php } ?>
												</select>
											</div>
										</div>
									<?php } ?>
									<div class="col-sm-6">
										<div class="gn-form-field">
											<label for="gn-area"><?php echo esc_html__( 'Area', 'gigneo' ); ?></label>
											<input id="gn-area" type="text" name="area" autocomplete="off" value="<?php echo esc_attr( $area_raw ); ?>" placeholder="<?php echo esc_attr__( 'Block / Sector / Town', 'gigneo' ); ?>">
										</div>
									</div>
									<div class="col-md-<?php echo esc_attr( ! $disable_cities ? '6' : '12' ); ?>">
										<div class="gn-form-field">
											<label for="gn-zip-code" class="gn-required-field-label"><?php echo esc_html__( 'Postcode / ZIP', 'gigneo' ); ?></label>
											<input id="gn-zip-code" type="text" class="gn-required-field" name="zip_code" 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-12">
										<div class="gn-form-field">
											<label for="gn-address" class="gn-required-field-label"><?php echo esc_html__( 'Address', 'gigneo' ); ?></label>
											<textarea id="gn-address" class="gn-required-field" name="address" autocomplete="off" rows="5" placeholder="<?php echo esc_attr__( 'Enter address', 'gigneo' ); ?>"><?php echo esc_attr( $address_raw ); ?></textarea>
										</div>
									</div>
								</div>
							</fieldset>
						</div>
						<div class="gn-modal-footer">
							<input type="hidden" name="type" value="address">
							<button type="button" class="gn-btn gn-btn-outline-grey" data-gn-modal-close="gn-edit-profile-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>
