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

$proposal_id     = absint( $args['p_id'] ?? '' );
$job_id          = gigneo_post_meta( $proposal_id, '_gigneo_pm_job_id' );
$job_title       = get_the_title( $job_id );
$proposal_price  = gigneo_post_meta( $proposal_id, '_gigneo_pm_budget' );
$duration        = gigneo_post_meta( $proposal_id, '_gigneo_pm_duration' );
$duration_type   = gigneo_post_meta( $proposal_id, '_gigneo_pm_duration_type' );
$proposal_status = get_post_status( $proposal_id );
$proposal_status = 'publish' === $proposal_status ? 'pending' : $proposal_status;
$status_label    = gigneo_status_label( $proposal_status );
$job_status      = get_post_status( $job_id );

// Job Meta
$job_mode     = gigneo_post_meta( $job_id, '_gigneo_pm_type' );
$job_budget   = gigneo_post_meta( $job_id, '_gigneo_pm_price' );
$job_duration = gigneo_post_meta( $job_id, '_gigneo_pm_duration' );
$job_dur_type = gigneo_post_meta( $job_id, '_gigneo_pm_duration_type' );

// Status Styling logic matching job list
$status_class   = gigneo_status_class( $proposal_status );


$date_format      = get_option( 'date_format' );
$time_format      = get_option( 'time_format' );
$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 );

$edit_url = home_url( '/hire/?source=job&ref=fixed&p_id=' . $job_id . '&proposal_id=' . $proposal_id );
?>

<tr class="gn-job-item-row gn-proposal-item-row">
	<!-- Job Details -->
	<td class="gn-job-item-cell gn-details">
		<div class="gn-job-details">
			<h5 class="gn-title">
				<a href="<?php echo esc_url( gigneo_dashboard_url( 'proposal', 'detail', array( 'p_id' => $proposal_id ) ) ); ?>">
					<?php echo esc_html( $job_title ); ?>
				</a>
			</h5>
			<div class="gn-meta">
				<div class="gn-item">
					<i class="hi hi-globe-alt"></i>
					<span>
						<?php echo 'remote' === $job_mode ? esc_html__( 'Remote', 'gigneo' ) : esc_html__( 'Onsite', 'gigneo' ); ?>
					</span>
				</div>
				<div class="gn-item">
					<i class="hi hi-clock"></i>
					<span>
						<?php echo esc_html( gigneo_delivery_time( $job_duration, $job_dur_type ) ); ?>
					</span>
				</div>
				<div class="gn-item">
					<i class="hi hi-banknotes"></i>
					<span>
						<?php echo esc_html( gigneo_price_format( $job_budget, true ) ); ?>
					</span>
				</div>
			</div>
		</div>
	</td>

	<!-- Your Bid -->
	<td class="gn-job-item-cell gn-amount">
		<div class="gn-job-amount">
			<span class="gn-amount"><?php echo esc_html( gigneo_price_format( $proposal_price, true ) ); ?></span>
		</div>
	</td>

	<!-- Timeline -->
	<td class="gn-job-item-cell gn-timeline">
		<div class="gn-job-details">
			<span class="gn-job-timeline"><?php echo esc_html( gigneo_delivery_time( $duration, $duration_type ) ); ?></span>
		</div>
	</td>

	<!-- Submitted -->
	<td class="gn-job-item-cell gn-submitted text-muted">
		<span class="gn-date"><?php echo esc_html( $time_diff ); ?></span>
	</td>

	<!-- Status -->
	<td class="gn-job-item-cell gn-status">
		<div class="gn-status-wrapper">
			<span class="gn-status-tag <?php echo esc_attr( $status_class ); ?>">
				<?php echo esc_html( $status_label ); ?>
			</span>
		</div>
	</td>

	<!-- Action -->
	<td class="gn-job-item-cell gn-action">
		<div class="gn-dropdown-container">
			<button type="button" class="gn-btn gn-btn-sm gn-btn-outline-grey gn-dropdown-toggle gn-job-action" 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( gigneo_dashboard_url( 'proposal', 'detail', array( 'p_id' => $proposal_id ) ) ); ?>">
							<i class="hi hi-eye" aria-hidden="true"></i>
							<span class="gn-title"><?php echo esc_html__( 'View proposal', 'gigneo' ); ?></span>
						</a>
					</li>
					<?php if ( 'publish' === $job_status && in_array( $proposal_status, array( 'publish', 'pending' ), true ) ) { ?>
					<li class="gn-dropdown-item">
						<a href="<?php echo esc_url( $edit_url ); ?>">
							<i class="hi hi-pencil" aria-hidden="true"></i>
							<span class="gn-title"><?php echo esc_html__( 'Edit proposal', 'gigneo' ); ?></span>
						</a>
					</li>
						<?php
					}
					if ( in_array( $proposal_status, array( 'publish', 'pending', 'viewed' ), true ) ) {
						?>
						<li class="gn-dropdown-item">
							<a href="javascript:" class="gn-action-withdraw-proposal" data-gn-id="<?php echo esc_attr( $proposal_id ); ?>">
								<i class="hi hi-arrow-uturn-left" aria-hidden="true"></i>
								<span class="gn-title"><?php echo esc_html__( 'Withdraw', 'gigneo' ); ?></span>
							</a>
						</li>
							<?php
					}
					?>
				</ul>
			</div>
		</div>
	</td>
</tr>
