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

$proposal_id                   = $args['p_id'] ?? '';
$author_id                     = $args['author_id'] ?? get_post_field( 'post_author', $proposal_id );
$job_id                        = $args['job_id'] ?? gigneo_post_meta( $proposal_id, '_gigneo_pm_job_id' );
$price                         = $args['price'] ?? gigneo_post_meta( $proposal_id, '_gigneo_pm_budget' );
$duration                      = $args['duration'] ?? gigneo_post_meta( $proposal_id, '_gigneo_pm_duration' );
$duration_type                 = $args['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' );
$user_profile_id               = gigneo_active_profile_id();

// Location
$city               = gigneo_post_meta( $author_profile_id, '_gigneo_pm_city' );
$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 ] : '';
$city_term          = ! empty( $city ) ? get_term_by( 'slug', $city, 'gigneo_city' ) : false;
$city_label         = ( $city_term && ! is_wp_error( $city_term ) ) ? $city_term->name : '';
$location_label     = implode( ', ', array_filter( array( $city_label, $country_label ) ) );

// Job Info
$job_title         = get_the_title( $job_id );
$job_duration      = gigneo_post_meta( $job_id, '_gigneo_pm_duration' );
$job_duration_type = gigneo_post_meta( $job_id, '_gigneo_pm_duration_type' );
$job_work_mode     = gigneo_post_meta( $job_id, '_gigneo_pm_type' );
$job_budget        = gigneo_post_meta( $job_id, '_gigneo_pm_price' );

$platform_commissions      = gigneo_commission( $price, 'job', $user_profile_id );
$platform_commission       = ! empty( $platform_commissions['total'] ) ? $platform_commissions['total'] : 0;
$platform_commission_title = gigneo_option( '_gigneo_ts_platform_fee_name', esc_html__( 'Platform fee', 'gigneo' ) );
if ( ! empty( $platform_commissions['fixed_amount'] ) && ! empty( $platform_commissions['percentage'] ) ) {
	$platform_commission_title = sprintf(
		// translators: 1. Fee name, 2. Fixed amount, 3. Percentage amount.
		esc_html__( '%1$s (%2$s + %3$s%%)', 'gigneo' ),
		$platform_commission_title,
		$platform_commissions['fixed_amount'],
		$platform_commissions['percentage']
	);
} elseif ( ! empty( $platform_commissions['percentage'] ) ) {
	$platform_commission_title = sprintf(
		// translators: 1: Fee name 2: Percentage amount.
		esc_html__( '%1$s %2$s%%', 'gigneo' ),
		$platform_commission_title,
		$platform_commissions['percentage']
	);
}
$total_amount  = $platform_commission + $price;
$total_amount  = gigneo_price_format( $total_amount, true );
$tagline_raw   = gigneo_post_meta( $author_profile_id, '_gigneo_pm_tagline' );
$earnings_data = gigneo_profile_stat( 'total_earning', $author_profile_id );
?>
<!--Buy Funds Modal-->
<div id="gn-proposal-hire" class="gn-modal-wrapper gn-confirm-hire-popup">
	<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__( 'Confirm hire', 'gigneo' ); ?></h4>
					<button type="button" data-gn-modal-close="gn-proposal-hire" class="gn-modal-close">
						<i class="hi hi-x-mark" aria-hidden="true"></i>
					</button>
				</div>
				<form id="gn-proposal-hire-form" class="gn-theme-form gn-proposal-hire-form">
					<div class="gn-modal-body gn-custom-scrollbar">
						<div class="gn-dashboard-section gn-section-single-proposal">
							<div class="gn-head">
								<div class="gn-heading-wrapper">
									<div class="gn-title-wrapper">
										<h2 class="gn-title"><?php echo esc_html( $job_title ); ?></h2>
									</div>
									<div class="gn-info-wrapper">
										<div class="gn-meta">
											<?php if ( $job_work_mode ) { ?>
												<div class="gn-mode">
													<i class="hi hi-map-pin" aria-hidden="true"></i>
														<?php echo esc_html( ucfirst( $job_work_mode ) ); ?>
												</div>
											<?php } ?>
											<?php

											if ( ! empty( $job_duration ) && ! empty( $job_duration_type ) ) {
												?>
												<div class="gn-duration">
													<i class="hi hi-clock" aria-hidden="true"></i>
													<?php echo esc_html( gigneo_delivery_time( $job_duration, $job_duration_type ) ); ?>
												</div>
											<?php } ?>
											<?php if ( ! empty( $job_budget ) ) { ?>
												<div class="gn-budget">
													<i class="hi hi-banknotes" aria-hidden="true"></i>
													<?php echo esc_html( gigneo_price_format( $job_budget, true ) ); ?>
												</div>
											<?php } ?>
										</div>
									</div>
								</div>
							</div>
						</div>
						<div class="gn-form-summary">
							<h6 class="gn-title"><?php echo esc_html__( 'Order summary', 'gigneo' ); ?></h6>
							<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' ); ?>
								</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>
									</div>
									<?php if ( ! empty( $tagline_raw ) ) { ?>
										<p class="gn-tagline"><?php echo esc_html( $tagline_raw ); ?></p>
									<?php } ?>
									<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( gigneo_profile_rating( $author_profile_id ) ); ?>)</span>
										</li>
										<li>
											<i class="hi hi-banknotes" aria-hidden="true"></i><?php echo esc_html( gigneo_price_format( $earnings_data['lifetime'], true ) ); ?>
										</li>
										<li>
											<i class="hi hi-map" aria-hidden="true"></i><span class="gn-value"><?php echo esc_html( $location_label ); ?></span>
										</li>
										
									</ul>
								</div>
							</div>
							<div class="gn-summary-info">
								<div class="gn-info gn-total-timeline">
									<span class="gn-label"><?php echo esc_html__( 'Estimated timeline', 'gigneo' ); ?></span>
									<span class="gn-value"><?php echo esc_html( gigneo_delivery_time( $duration, $duration_type ) ); ?></span>
								</div>
								<div class="gn-info gn-proposal-bit">
									<span class="gn-label"><?php echo esc_html__( 'Bid amount', 'gigneo' ); ?></span>
									<span class="gn-value"><?php echo esc_html( gigneo_price_format( $price, true ) ); ?></span>
								</div>
								<?php if ( ! empty( $platform_commission ) ) { ?>
									<div class="gn-info gn-total-amount">
										<span class="gn-label"><?php echo esc_html( $platform_commission_title ); ?></span>
										<span id="gn-total-amount" class="gn-value"><?php echo esc_html( gigneo_price_format( $platform_commission, true ) ); ?></span>
									</div>
								<?php } ?>
								<div class="gn-info gn-total">
									<strong class="gn-label"><?php echo esc_html__( 'Total', 'gigneo' ); ?></strong>
									<strong id="gn-total-amount" class="gn-value"><?php echo esc_html( $total_amount ); ?></strong>
								</div>
							</div>
						</div>
						<div class="gn-notices-wrapper">
							<div class="gn-notice gn-notice-info">
								<i class="hi hi-lock-closed" aria-hidden="true"></i>
								<div class="gn-notice-info">
									<h3 class="gn-title">
										<?php
										printf(
											// translators: %s: Total amount
											esc_html__( 'Confirming will instantly start this order and deduct %s from your wallet.', 'gigneo' ),
											esc_html( $total_amount )
										);
										?>
									</h3>
									<p class="gn-description"><?php echo esc_html__( 'Funds are held securely in escrow and only released when you approve the completed work.', 'gigneo' ); ?></p>
								</div>
							</div>
						</div>
					</div>
					<div class="gn-modal-footer">
						<button type="button" class="gn-btn gn-btn-outline-grey" data-gn-modal-close="gn-proposal-hire"><?php echo esc_html__( 'Cancel', 'gigneo' ); ?></button>
						<button type="submit" class="gn-btn gn-btn-primary"><?php echo esc_html__( 'Confirm & hire', 'gigneo' ); ?></button>
					</div>
				</form>
			</div>
		</div>
	</div>
</div>