<?php

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

use Gigneo\Modules;

$job_id            = $args['p_id'] ?? get_the_ID();
$job_location      = gigneo_post_meta( $job_id, '_gigneo_pm_location' );
$job_location      = is_array( $job_location ) ? $job_location : array();
$city              = gigneo_post_meta( $job_id, '_gigneo_pm_city' ) ?? '';
$friendly_address  = gigneo_post_meta( $job_id, '_gigneo_pm_friendly_address' ) ?? '';
$area              = gigneo_post_meta( $job_id, '_gigneo_pm_area' ) ?? '';
$zip_code          = gigneo_post_meta( $job_id, '_gigneo_pm_zip_code' ) ?? '';
$work_mode         = gigneo_post_meta( $job_id, '_gigneo_pm_type' ) ?? '';
$country_state_raw = gigneo_post_meta( $job_id, '_gigneo_pm_country_state' ) ?? '';

$country_state = explode( ':', $country_state_raw, 2 );
$country       = $country_state[0] ?? '';
$state_key     = $country_state[1] ?? '';

$city_term = get_term_by( 'slug', $city, 'gigneo_city' );
$city_name = ( $city_term && ! is_wp_error( $city_term ) ) ? $city_term->name : '';

$countries    = gigneo_allowed_countries();
$states       = gigneo_allowed_states();
$state_name   = $states[ $state_key ] ?? '';
$country_name = $countries[ $country ] ?? '';
$address      = $friendly_address ?? ( $job_location['address'] ?? '' );

?>
<div class="gn-section gn-locations-section">
	<div class="gn-head">
		<h3 class="gn-title"><?php echo esc_html__( 'Location', 'gigneo' ); ?></h3>
		<span class="gn-type"><?php echo esc_html( ucfirst( $work_mode ) ); ?></span>
	</div>
	<div class="gn-body gn-locations">
		<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
					$location_parts = array_filter(
						array(
							$city_name,
							$state_name,
							$country_name,
						)
					);
					echo esc_html( implode( ', ', $location_parts ) );
					?>
				</h6>
				<p class="gn-address">
					<?php
					$address_parts = array_filter(
						array(
							$area,
							$zip_code,
							$address,
						)
					);
					echo esc_html( implode( ', ', $address_parts ) );
					?>
				</p>
			</div>
		</div>
	</div>
</div>