<?php

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

use Gigneo\Modules;

$job_id          = get_the_ID();
$profile_id      = gigneo_active_profile_id();
$author_id       = get_post_field( 'post_author', $job_id );
$user_type       = gigneo_profile_type( $profile_id );
$wp_current_user = wp_get_current_user();
$open_positions  = gigneo_post_meta( $job_id, '_gigneo_pm_open_positions' );
$open_positions  = ! empty( $open_positions ) ? $open_positions : 1;
$apply_type      = gigneo_option( '_gigneo_ts_job_apply_type' );
$apply_credits   = gigneo_option( '_gigneo_ts_job_apply_credits' );
$job_status      = ! empty( $job_id ) ? get_post_status( $job_id ) : '';
$hire_page_id    = gigneo_option( '_gigneo_ts_template_hire' );
$hire_page_url   = $hire_page_id ? get_permalink( $hire_page_id ) : home_url( '/hire/' );
$hire_page_url   = add_query_arg(
	array(
		'source' => 'job',
		'ref'    => 'fixed',
		'p_id'   => $job_id,
	),
	$hire_page_url
);

//profile fields
$pref_location        = gigneo_post_meta( $job_id, '_gigneo_pm_preferred_location' );
$pref_timezone        = gigneo_post_meta( $job_id, '_gigneo_pm_preferred_timezone' );
$pref_language        = gigneo_post_meta( $job_id, '_gigneo_pm_preferred_language' );
$pref_language        = ! empty( $pref_language ) ? get_term( $pref_language, 'gigneo_language' ) : '';
$language_proficiency = gigneo_post_meta( $job_id, '_gigneo_pm_language_proficiency' );

$pref_country  = is_array( $pref_location ) ? ( $pref_location[0] ?? '' ) : $pref_location;
$countries     = gigneo_allowed_countries();
$pref_location = $countries[ $pref_country ] ?? $pref_country;

//Activity
$proposal_count     = gigneo_count_job_proposals( $job_id );
$interviewing_count = gigneo_count_job_viewed( $job_id );
$date_format        = get_option( 'date_format' );
$time_format        = get_option( 'time_format' );
$date_time_format   = $date_format . ' ' . $time_format;
$last_activity      = gigneo_job_last_activity( $job_id );
$time_diff          = gigneo_datetime( $last_activity, $date_time_format );

$submit_proposal_class = '';
$submit_proposal_msg   = '';
$submit_proposal_text  = esc_html__( 'Submit proposal', 'gigneo' );
if ( empty( $wp_current_user->ID ) ) {
	$submit_proposal_msg   = 'proposal_login_message';
	$submit_proposal_class = 'gn-job-error';
} elseif ( $wp_current_user->ID === $author_id ) {
	$submit_proposal_msg   = 'proposal_author_message';
	$submit_proposal_class = 'gn-job-error';
} elseif ( 'freelancer' !== $user_type ) {
	$submit_proposal_msg   = 'proposal_employer_message';
	$submit_proposal_class = 'gn-job-error';
} else {
	$existing_proposal = get_posts(
		array(
			'post_type'      => 'gigneo_proposal',
			'posts_per_page' => 1,
			'fields'         => 'ids',
			'no_found_rows'  => true,
			// phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query
			'meta_query'     => array(
				'relation' => 'AND',
				array(
					'key'   => '_gigneo_pm_job_id',
					'value' => $job_id,
				),
				array(
					'key'   => '_gigneo_pm_freelancer_profile_id',
					'value' => $profile_id,
				),
			),
		)
	);
	if ( ! empty( $existing_proposal ) ) {
		$dashboard_page_id = gigneo_option( '_gigneo_ts_template_dashboard', '' );
		$redirect_url      = ! empty( $dashboard_page_id ) ? get_permalink( $dashboard_page_id ) : home_url();
		$proposal_id       = ! empty( $existing_proposal[0] ) ? $existing_proposal[0] : 0;
		$order_data        = array(
			'source' => 'proposal',
			'ref'    => 'detail',
			'p_id'   => $proposal_id,
		);

		$hire_page_url        = add_query_arg( $order_data, $redirect_url );
		$submit_proposal_msg  = 'proposal_sent_message';
		$submit_proposal_text = esc_html__( 'View proposal', 'gigneo' );
	}
}
if ( 'paused' === $job_status ) { ?>
	<div class="gn-notices-wrapper">
		<div class="gn-notice gn-notice-danger">
			<div class="gn-notice-icon">
				<i class="hi hi-exclamation-circle" aria-hidden="true"></i>
			</div>
			<div class="gn-notice-info">
				<h3 class="gn-title"><?php echo esc_html__( 'Paused', 'gigneo' ); ?></h3>
				<p class="gn-description"><?php echo esc_html__( 'This job is currently paused by the author and is not accepting new proposals.', 'gigneo' ); ?></p>
			</div>
		</div>
	</div>
	<?php
}
?>
<div class="gn-section gn-job-info-section">
	<?php if ( ! empty( $pref_location ) || ! empty( $pref_timezone ) || ! empty( $pref_language ) ) { ?>
	<div class="gn-sub-section gn-preferences">
			<div class="gn-head">
				<h2 class="gn-title"><?php echo esc_html__( 'Preferences', 'gigneo' ); ?></h2>
			</div>
		<div class="gn-body">
			<ul class="gn-preferences">
					<?php if ( ! empty( $pref_language ) ) { ?>
						<li class="gn-info gn-language">
							<span class="gn-title"><?php echo esc_html__( 'Language', 'gigneo' ); ?></span>
							<?php
							$active_class = '';
							if ( 'freelancer' === $user_type ) {
								$active_language = 'hi-exclamation-circle';
								$active_class    = 'gn-value-exclamation';
								if ( has_term( $pref_language->term_id, 'gigneo_language', $profile_id ) ) {
									$active_language = 'hi-check-circle';
									$active_class    = '';
									if ( ! empty( $language_proficiency ) ) {
										$profile_languages = gigneo_post_meta( $profile_id, '_gigneo_pm_languages' );
										if ( ! empty( $profile_languages ) && is_array( $profile_languages ) ) {
											foreach ( $profile_languages as $lang ) {
												if ( is_array( $lang ) && isset( $lang['name'] ) && strtolower( $lang['name'] ) === strtolower( $pref_language->name ) && isset( $lang['level'] ) && strtolower( $lang['level'] ) === strtolower( $language_proficiency ) ) {
													$active_language = 'hi-check-circle';
													$active_class    = '';
													break;
												} else {
													$active_language = 'hi-exclamation-circle';
													$active_class    = 'gn-value-exclamation';
												}
											}
										}
									}
								}
							}
							?>
							<span class="gn-value <?php echo esc_attr( $active_class ); ?>">
								<?php
								echo esc_html( $pref_language->name ?? '' );
								if ( ! empty( $language_proficiency ) ) {
									echo ' (' . esc_html( $language_proficiency ) . ')';
								}
								?>
								<?php if ( 'freelancer' === $user_type ) { ?>
									<i class="hi <?php echo esc_attr( $active_language ); ?>" aria-hidden="true"></i>
								<?php } ?>
							</span>
						</li>
					<?php } ?>
					<?php if ( ! empty( $pref_timezone && in_array( 'timezone', gigneo_active_modules(), true ) ) ) { ?>
						<li class="gn-info gn-timezone">
							<span class="gn-title"><?php echo esc_html__( 'Timezone', 'gigneo' ); ?></span>
							<?php
							$active_class = '';
							if ( 'freelancer' === $user_type ) {
								$profile_timezone = gigneo_timezone( $profile_id );
								$active_timezone  = $profile_timezone === $pref_timezone ? 'hi-check-circle' : 'hi-exclamation-circle';
								$active_class     = $profile_timezone === $pref_timezone ? '' : 'gn-value-exclamation';
							}
							?>
							<span class="gn-value <?php echo esc_attr( $active_class ); ?>">
								<?php echo esc_html( $pref_timezone ?? '' ); ?>
								<?php if ( 'freelancer' === $user_type ) { ?>
									<i class="hi <?php echo esc_attr( $active_timezone ); ?>" aria-hidden="true"></i>
								<?php } ?>
							</span>
						</li>
					<?php } ?>
					<?php if ( ! empty( $pref_location ) ) { ?>
						<li class="gn-info gn-location">
							<span class="gn-title"><?php echo esc_html__( 'Location', 'gigneo' ); ?></span>
							<?php
							$active_class = '';
							if ( 'freelancer' === $user_type ) {
								$profile_location = gigneo_post_meta( $profile_id, '_gigneo_pm_country_state' );
								$country_state    = ! empty( $profile_location ) ? explode( ':', (string) $profile_location, 2 ) : array();
								$country_state    = ! empty( $country_state[0] ) ? $country_state[0] : '';
								$active_location  = ! empty( $pref_country ) && $country_state === $pref_country ? 'hi-check-circle' : 'hi-exclamation-circle';
								$active_class     = ! empty( $pref_country ) && $country_state === $pref_country ? '' : 'gn-value-exclamation';
							}
							?>
							<span class="gn-value <?php echo esc_attr( $active_class ); ?>">
								<?php echo esc_html( $pref_location ?? '' ); ?>
								<?php if ( 'freelancer' === $user_type ) { ?>
									<i class="hi <?php echo esc_attr( $active_location ); ?>" aria-hidden="true"></i>
								<?php } ?>
							</span>
						</li>
					<?php } ?>

			</ul>
		</div>
	</div>
	<?php } ?>
	<?php
	if ( ! empty( $proposal_count ) || ! empty( $interviewing_count ) ) {
		?>
	<div class="gn-sub-section gn-activity">
		<div class="gn-head">
			<h2 class="gn-title"><?php echo esc_html__( 'Activity', 'gigneo' ); ?></h2>
		</div>
		<div class="gn-body">
			<ul class="gn-activity-info">
				<li class="gn-info">
					<span class="gn-title"><?php echo esc_html__( 'Proposals', 'gigneo' ); ?></span>
					<span class="gn-value"><?php echo esc_html( $proposal_count ); ?></span>
				</li>
				<li class="gn-info">
					<span class="gn-title"><?php echo esc_html__( 'Interviewing', 'gigneo' ); ?></span>
					<span class="gn-value"><?php echo esc_html( $interviewing_count ); ?></span>
				</li>
				<li class="gn-info">
					<span class="gn-title"><?php echo esc_html__( 'Last activity ', 'gigneo' ); ?></span>
					<span class="gn-value"><?php echo esc_html( $time_diff ); ?></span>
				</li>
			</ul>

		</div>

	</div>
	<?php } ?>
	<div class="gn-sub-section gn-basic-info">
		<div class="gn-body">
			<ul class="gn-activity-info">
				<li class="gn-info">
					<span class="gn-title"><?php echo esc_html__( 'Open positions', 'gigneo' ); ?></span>
					<span class="gn-value"><?php echo esc_html( $open_positions ); ?></span>
				</li>
				<?php
				if ( 'paid' === $apply_type && ! empty( $apply_credits ) ) {
					?>
					<li class="gn-info">
						<span class="gn-title"><?php echo esc_html__( 'Credits required', 'gigneo' ); ?></span>
						<span class="gn-value">
						<?php
						printf(
						// translators: 1. credits number
							esc_html__( '%s credits', 'gigneo' ),
							esc_html( $apply_credits )
						);
						?>

					</span>
					</li>
				<?php } ?>
			</ul>
		</div>

	</div>
	<?php if ( 'publish' === $job_status ) { ?>
		<div class="gn-footer">
			<div class="gn-actions">
				<input type="hidden" name="source" value="<?php echo esc_attr( 'hire' ); ?>">
				<input type="hidden" name="ref" value="<?php echo esc_attr( 'fixed' ); ?>">
				<input type="hidden" name="p_id" value="<?php echo esc_attr( $job_id ); ?>">
				<a href="<?php echo esc_url( $hire_page_url ); ?>" data-gn-message="<?php echo esc_attr( $submit_proposal_msg ); ?>"
					class="gn-btn gn-btn-primary gn-btn-md <?php echo esc_attr( $submit_proposal_class ); ?>">
					<?php echo esc_html( $submit_proposal_text ); ?>
				</a>
			</div>
		</div>
	<?php } ?>
</div>










			