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

$proposal_id                   = $args['p_id'] ?? '';
$date_format                   = $args['date_format'] ?? '';
$time_format                   = $args['time_format'] ?? '';
$author_id                     = get_post_field( 'post_author', $proposal_id );
$job_id                        = gigneo_post_meta( $proposal_id, '_gigneo_pm_job_id' );
$order_id                      = gigneo_post_meta( $proposal_id, '_gigneo_pm_order_id' );
$price                         = gigneo_post_meta( $proposal_id, '_gigneo_pm_budget' );
$cover_description             = gigneo_post_meta( $proposal_id, '_gigneo_pm_cover_letter' );
$skill_terms                   = get_the_terms( $job_id, 'gigneo_skill' );
$duration                      = gigneo_post_meta( $proposal_id, '_gigneo_pm_duration' );
$duration_type                 = gigneo_post_meta( $proposal_id, '_gigneo_pm_duration_type' );
$author_profile_id             = gigneo_profile_id( $author_id, 'freelancer' );
$author_avatar_id              = get_post_thumbnail_id( $author_profile_id ) ?? 0;
$online_status                 = gigneo_online_status( $author_profile_id );
$author_profile_average_rating = gigneo_profile_rating( $author_profile_id, 'average' );
$author_profile_reviews        = gigneo_profile_rating( $author_profile_id );
$date_time_format              = $date_format . ' ' . $time_format;

$posted_time = get_post_time( 'Y-m-d H:i:s', true, $proposal_id );
$time_diff   = gigneo_datetime( $posted_time, $date_time_format );

$job_author_id         = get_post_field( 'post_author', $job_id );
$job_author_profile_id = gigneo_profile_id( $job_author_id, 'employer' );
$user_profile_id       = gigneo_active_profile_id();

// Earnings
$earnings_data    = gigneo_profile_stat( 'total_earning', $author_profile_id );
$lifetime_earning = $earnings_data['lifetime'] ?? 0;

// Location
$country_state_list = gigneo_countries_with_states( 'active' );
$country_state_raw  = gigneo_post_meta( $author_profile_id, '_gigneo_pm_country_state' );
$country_label      = ! empty( $country_state_list[ $country_state_raw ] ) ? $country_state_list[ $country_state_raw ] : '';
$location_label     = $country_label;
$proposal_status    = get_post_status( $proposal_id );
$job_status         = get_post_status( $job_id );
?>

<div class="gn-proposal-archive-card" data-proposal-id="<?php echo intval( $proposal_id ); ?>">
	<div class="gn-head">
		<div class="gn-heading-wrapper">
			<div class="gn-profile">
				<figure class="gn-avatar gn-<?php echo esc_attr( $online_status ? 'online' : 'offline' ); ?>">
					<?php gigneo_attachment_image_src( $author_avatar_id, 'thumbnail', 'profile', '', '', $author_profile_id ); ?>
				</figure>
				<div class="gn-info">
					<div class="gn-name-wrapper">
						<a href="<?php echo esc_url( get_the_permalink( $author_profile_id ) ); ?>" target="_blank"
							class="gn-name"><?php echo esc_html( gigneo_profile_name( $author_profile_id ) ); ?></a>
							<?php gigneo_verification_badge( $author_profile_id, true ); ?>
					</div>
					<ul class="gn-meta">
						<li class="gn-ratings">
							<i class="hi hi-star-alt" aria-hidden="true"></i>
							<?php echo esc_html( $author_profile_average_rating ?? 0 ); ?>
							<span class="gn-count">(<?php echo esc_html( $author_profile_reviews ?? 0 ); ?>)</span>
						</li>
						<?php if ( $location_label ) { ?>
							<li class="gn-location">
								<i class="hi hi-map-pin" aria-hidden="true"></i>
								<span class="gn-text"><?php echo esc_html( $location_label ); ?></span>
							</li>
						<?php } ?>
						<li class="gn-earning">
							<i class="hi hi-banknotes" aria-hidden="true"></i>
							<span class="gn-text"><?php echo esc_html( gigneo_price_format( $lifetime_earning, true ) ); ?><?php esc_html_e( ' earned', 'gigneo' ); ?></span>
						</li>
					</ul>
				</div>
			</div>
			<div class="gn-proposal-info">
				<div class="gn-proposal-bid">
					<span class="gn-title"><?php echo esc_html__( 'Bid amount', 'gigneo' ); ?></span>
					<span class="gn-value"><?php echo esc_html( gigneo_price_format( $price, true ) ); ?></span>
				</div>
				<div class="gn-proposal-bid">
					<span class="gn-title"><?php echo esc_html__( 'Timeline', 'gigneo' ); ?></span>
					<span class="gn-value"><?php echo esc_html( gigneo_delivery_time( $duration, $duration_type ) ); ?></span>
				</div>
				<div class="gn-proposal-bid">
					<span class="gn-title"><?php echo esc_html__( 'Submitted at', 'gigneo' ); ?></span>
					<span class="gn-value"><?php echo esc_html( $time_diff ); ?></span>
				</div>
				<?php if ( 'publish' === $proposal_status || 'pending' === $proposal_status ) { ?>
					<span class="gn-status-tag-fill"><?php echo esc_html__( 'New', 'gigneo' ); ?></span>
				<?php } ?>
			</div>
		</div>
	</div>
	<div class="gn-body">
		<div class="gn-proposal-info">
			<div class="gn-cover-letter-head">
				<h4 class="gn-title"><?php echo esc_html__( 'Cover letter', 'gigneo' ); ?>:</h4>
			</div>
			<p class="gn-description">
				<?php echo esc_html( wp_trim_words( $cover_description, 25, '...' ) ); ?>
			</p>
			<a href="<?php echo esc_url( gigneo_dashboard_url( 'proposal', 'detail', array( 'p_id' => $proposal_id ) ) ); ?>" class="gn-link gn-link-primary">
				<?php echo esc_html__( 'View proposal', 'gigneo' ); ?>
				<i class="hi hi-arrow-right"></i>
			</a>
		</div>
	</div>
	<div class="gn-footer">
			<div class="gn-tags">
			<div class="gn-skills">
				<?php
				if ( ! empty( $skill_terms ) && ! is_wp_error( $skill_terms ) ) {
					foreach ( $skill_terms as $skill_term ) {
						?>
						<span class="gn-status-tag"><?php echo esc_html( $skill_term->name ); ?></span>
						<?php
					}
				}
				?>
			</div>
		</div>
		<div class="gn-actions">
			<?php
			if ( $job_author_profile_id === $user_profile_id && ! in_array( $proposal_status, array( 'hired', 'completed', 'cancelled' ), true ) && 'publish' === $job_status ) {
				$is_shortlisted = ( 'shortlisted' === $proposal_status );
				$shortlist_text = $is_shortlisted ? esc_html__( 'Shortlisted', 'gigneo' ) : esc_html__( 'Shortlist', 'gigneo' );
				?>
				<a href="#" class="gn-btn gn-btn-md gn-btn-outline-grey gn-shortlist-btn <?php echo $is_shortlisted ? 'gn-active' : ''; ?>" data-gn-id="<?php echo esc_attr( $proposal_id ); ?>">
					<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor">
						<path stroke-linecap="round" stroke-linejoin="round" d="M21 8.25c0-2.485-2.099-4.5-4.688-4.5-1.935 0-3.597 1.126-4.312 2.733-.715-1.607-2.377-2.733-4.313-2.733C5.1 3.75 3 5.765 3 8.25c0 7.22 9 12 9 12s9-4.78 9-12Z" />
					</svg>
					<span><?php echo esc_html( $shortlist_text ); ?></span>
				</a>
			<?php } elseif ( in_array( $proposal_status, array( 'hired', 'completed', 'cancelled' ), true ) ) { ?>
				<a href="<?php echo esc_url( gigneo_dashboard_url( 'order', 'job-detail', array( 'p_id' => $order_id ) ) ); ?>" class="gn-btn gn-btn-outline-grey gn-btn-md">
					<i class="hi hi-calendar" aria-hidden="true"></i>
					<?php echo esc_html__( 'View order', 'gigneo' ); ?>
				</a>
			<?php } ?>
			<div class="gn-dropdown-container">
				<button type="button" class="gn-btn gn-btn-md gn-btn-outline-grey gn-dropdown-toggle gn-proposal-action gn-ellipsis-btn" data-gn-target="#gn-proposal-action-dropdown-<?php echo esc_attr( $proposal_id ); ?>">
					<i class="hi hi-ellipsis-horizontal" aria-hidden="true"></i>
				</button>
				<div class="gn-dropdown-content gn-enter-top" id="gn-proposal-action-dropdown-<?php echo esc_attr( $proposal_id ); ?>">
					<ul class="gn-dropdown-menu">
						<li class="gn-dropdown-item">
							<a href="<?php echo esc_url( get_the_permalink( $job_id ) ); ?>" target="_blank">
								<i class="hi hi-arrow-up-right" aria-hidden="true"></i>
								<span class="gn-title"><?php echo esc_html__( 'View job', 'gigneo' ); ?></span>
							</a>
						</li>
					</ul>
				</div>
			</div>
		</div>
	</div>
</div>
