<?php

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}

$p_id                 = $args['p_id'] ?? '';
$current_step         = $args['current_step'] ?? '';
$country_state_list   = $args['country_state_list'] ?? array();
$country_state_cities = $args['country_state_cities'] ?? array();
$disable_cities       = $args['disable_cities'] ?? false;

$profile_id        = gigneo_active_profile_id();
$service_type_meta = gigneo_post_meta( $p_id, '_gigneo_pm_type' );

$locations      = gigneo_post_meta( $p_id, '_gigneo_pm_locations' );
$location_count = ! empty( $locations ) && is_array( $locations ) ? count( $locations ) : 0;
$delivery_type  = gigneo_post_meta( $p_id, '_gigneo_pm_delivery_type' );
$delivery_type  = ! empty( $delivery_type ) ? $delivery_type : 'both';

$max_locations = 1;

?>
<div id="gn-edit-service-locations" class="gn-edit-service-section">
	<div class="gn-head">
		<div class="gn-heading-area">
			<h3 class="gn-title"><?php echo esc_html__( 'My locations', 'gigneo' ); ?></h3>
			<p class="gn-description"><?php echo esc_html__( 'Specify your location to help clients find region-relevant services.', 'gigneo' ); ?></p>
		</div>
		<?php
		if ( $location_count < $max_locations ) {

			$profile_location = gigneo_post_meta( $profile_id, '_gigneo_pm_location' );

			$fields_data = array(
				'country_state'    => gigneo_post_meta( $profile_id, '_gigneo_pm_country_state' ) ?? '',
				'city'             => gigneo_post_meta( $profile_id, '_gigneo_pm_city' ) ?? '',
				'area'             => gigneo_post_meta( $profile_id, '_gigneo_pm_area' ) ?? '',
				'zip_code'         => gigneo_post_meta( $profile_id, '_gigneo_pm_zip_code' ) ?? '',
				'address'          => $profile_location['address'] ?? '',
				'location'         => $profile_location ?? array(),
				'friendly_address' => gigneo_post_meta( $profile_id, '_gigneo_pm_address' ) ?? '',
			);

			?>
			<div class="gn-actions">
				<button type="button" class="gn-link gn-link-primary gn-add-location" data-gn-modal-toggle="gn-edit-service-location" data-gn-fields="<?php echo esc_attr( wp_json_encode( $fields_data ) ); ?>">
					<i class="hi hi-plus" aria-hidden="true"></i>
					<?php echo esc_html__( 'Add location', 'gigneo' ); ?>
				</button>
			</div>
		<?php } ?>
	</div>
	<div class="gn-body">
		<div class="gn-locations">
			<?php
			if ( ! empty( $locations ) ) {
				foreach ( $locations as $a => $address ) {
					$country_state = $country_state_list[ $address['country_state'] ] ?? '';
					$city_term     = ! empty( $address['city'] ) ? get_term_by( 'slug', $address['city'], 'gigneo_city' ) : '';
					$city_name     = $city_term->name ?? '';
					$location      = $address['location'][0] ?? array();
					$address_info  = array();

					if ( ! empty( $city_name ) ) {
						$address_info['city'] = $city_name;
					}

					if ( $address['area'] ) {
						$address_info['area'] = $address['area'];
					}

					if ( $address['address'] ) {
						$address_info['address'] = $address['address'];
					}

					if ( $address['zip_code'] ) {
						$address_info['zip_code'] = $address['zip_code'];
					}

					$fields_data = array(
						'location_id'      => $a,
						'country_state'    => $address['country_state'] ?? '',
						'city'             => $address['city'] ?? '',
						'area'             => $address['area'] ?? '',
						'zip_code'         => $address['zip_code'] ?? '',
						'address'          => $location['address'] ?? '',
						'location'         => $location ?? array(),
						'friendly_address' => $address['address'] ?? '',
					);

					?>
					<div class="gn-location">
						<span class="gn-icon">
							<i class="hi hi-map-pin" aria-hidden="true"></i>
						</span>
						<div class="gn-info">
							<h6 class="gn-country-state">
								<?php
								echo esc_html( $country_state );
								?>
							</h6>
							<p class="gn-address"><?php echo esc_html( implode( ', ', $address_info ) ); ?></p>
						</div>
						<div class="gn-actions">
							<button type="button" class="gn-btn" data-gn-fields="<?php echo esc_attr( wp_json_encode( $fields_data ) ); ?>" data-gn-modal-toggle="gn-edit-service-location">
								<i class="hi hi-pencil-edit" aria-hidden="true"></i>
							</button>
							<button type="button" class="gn-btn gn-remove-location" data-gn-id="<?php echo esc_attr( $p_id ); ?>" data-gn-location-id="<?php echo esc_attr( $a ); ?>">
								<i class="hi hi-trash" aria-hidden="true"></i>
							</button>
						</div>
					</div>
					<?php
				}
			}
			?>
		</div>
	</div>
</div>

