<?php

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}
$job_id     = $args['job_id'] ?? 0;
$job_title  = get_the_title( $job_id );
$job_status = get_post_status( $job_id );
$price      = gigneo_post_meta( $job_id, '_gigneo_pm_price' );

$featured_limit   = gigneo_option( '_gigneo_ts_job_featured_limit', '0' );
$proposals        = gigneo_count_job_proposals( $job_id );
$new_proposals    = gigneo_count_job_proposals( $job_id, 'publish' );
$status_class     = gigneo_status_class( $job_status );
$job_status_key   = 'publish' === $job_status ? 'active' : $job_status;
$job_status_label = gigneo_status_label( $job_status_key );

$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, $job_id );
$time_diff     = gigneo_datetime( $posted_time, $date_time_format );
$job_type      = gigneo_post_meta( $job_id, '_gigneo_pm_job_type' );
$job_type      = ! empty( $job_type ) ? ucfirst( $job_type ) : '';
$activity      = gigneo_job_last_activity( $job_id );
$last_activity = gigneo_datetime( $activity, $date_time_format );
$is_featured   = gigneo_post_meta( $job_id, '_gigneo_pm_featured' );

$proposal_status_count = gigneo_get_job_proposals_status_count( $job_id );
$step_title            = '';
$active_step           = 0;
if ( ! empty( $proposal_status_count['all'] ) ) {
	$step_title = esc_html__( 'Screening', 'gigneo' );
	++$active_step;
}
if ( ! empty( $proposal_status_count['reviewed'] ) || ! empty( $proposal_status_count['shortlisted'] ) ) {
	$step_title = esc_html__( 'Interviewing', 'gigneo' );
	++$active_step;
}

if ( ! empty( $proposal_status_count['hired'] ) || ! empty( $proposal_status_count['completed'] ) || ! empty( $proposal_status_count['cancelled'] ) ) {
	$step_title = esc_html__( 'Hired', 'gigneo' );
	++$active_step;
}
$enabled_job_removed = false;
if ( in_array( $job_status, array( 'publish', 'draft', 'pending' ), true ) ) {
	$enabled_job_removed = true;
	if ( 'publish' === $job_status && ! empty( $proposal_status_count['all'] ) ) {
		$enabled_job_removed = false;
	}
}
?>

<tr class="gn-job-item-row">
	<td class="gn-job-item-cell gn-details">
		<div class="gn-job-details">
			<h5 class="gn-title"><span><?php echo esc_html( $job_title ); ?></span>
				<?php
				if ( $is_featured ) {
					?>
					<span class="gn-status-tag-fill gn-warning">
					<?php echo esc_html__( 'Featured', 'gigneo' ); ?>
				</span>
					<?php
				}
				?>
				</h5>

			<div class="gn-job-meta">
				<span
					class="gn-date">
					<i class="hi hi-calendar" aria-hidden="true"></i>
					<?php
					printf(
					// translators: %s is the time difference
						esc_html__( 'Posted %s', 'gigneo' ),
						esc_html( $time_diff )
					);
					?>
				</span>
				<span class="gn-type">
					<i class="hi hi-banknotes" aria-hidden="true"></i>
					<?php echo esc_html( $job_type ); ?>
				</span>
			</div>
		</div>
	</td>
	<td class="gn-job-item-cell gn-applications">
		<div class="gn-job-applications">
			<div class="gn-appllicants">
				<span class="gn-number"><?php echo esc_html( $proposals ); ?></span>
				<?php if ( $new_proposals > 0 ) { ?>
					<span class="gn-notification-count">
						<?php
						printf(
								// translators: %d is the number of new proposals
							esc_html__( '+%d New', 'gigneo' ),
							absint( $new_proposals )
						);
						?>
					</span>
				<?php } ?>
			</div>
			<div class="gn-applicant-activity">
				<span class="gn-label"><?php echo esc_html__( 'last activity:', 'gigneo' ); ?></span>
				<span class="gn-activity">
					<?php echo esc_html( $last_activity ); ?>
				</span>
			</div>
		</div>
	</td>
	<td class="gn-job-item-cell gn-progress">
		<div class="gn-job-progress">
			<div class="gn-progress-pills">
				<?php for ( $i = 0; $i < 3; $i++ ) { ?>
					<span class="gn-pill <?php echo $i < $active_step ? 'gn-active' : ''; ?>"></span>
				<?php } ?>
			</div>
			<?php if ( ! empty( $step_title ) ) { ?>
				<div class="gn-progress-label">
					<span class="gn-label"><?php echo esc_html( $step_title ); ?></span>
				</div>
			<?php } ?>
		</div>
	</td>
	<td class="gn-job-item-cell gn-amount">
		<div class="gn-job-amount">
			<span class="gn-amount"><?php echo esc_html( gigneo_price_format( $price, true ) ); ?></span>
		</div>
	</td>
	<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( $job_status_label ); ?>
			</span>
		</div>
	</td>
	<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-job-action-dropdown-<?php echo esc_attr( $job_id ); ?>">
				<i class="hi hi-ellipsis-horizontal" aria-hidden="true"></i>
			</button>
			<div class="gn-dropdown-content gn-enter-top"
				id="gn-job-action-dropdown-<?php echo esc_attr( $job_id ); ?>">
				<ul class="gn-dropdown-menu">
					<?php if ( in_array( $job_status, array( 'publish', 'completed', 'in-progress' ), true ) ) { ?>
					<li class="gn-dropdown-item">
						<a href="<?php echo esc_url( gigneo_dashboard_url( 'job', 'proposal', array( 'p_id' => $job_id ) ) ); ?>"
							target="_blank">
							<i class="hi hi-eye" aria-hidden="true"></i>
							<span class="gn-title"><?php echo esc_html__( 'View proposals', 'gigneo' ); ?></span>
						</a>
					</li>
						<?php
					}
					if ( in_array( $job_status, array( 'publish', 'completed', 'in-progress' ), true ) ) {
						?>
					<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>
						<?php
					}
					if ( in_array( $job_status, array( 'publish', 'paused' ) ) ) {
						if ( $is_featured || $featured_limit > 0 ) {
							?>
						<li class="gn-dropdown-item">
							<span class="gn-featured-job"
								data-gn-featured="<?php echo ! esc_attr( $is_featured ); ?>"
								data-gn-id="<?php echo esc_attr( $job_id ); ?>">
								<i class="hi hi-star" aria-hidden="true"></i>
								<span class="gn-title"><?php echo $is_featured ? esc_html__( 'Remove featured', 'gigneo' ) : esc_html__( 'Featured', 'gigneo' ); ?></span>
							</span>
						</li>
							<?php
						}
					}
					if ( in_array( $job_status, array( 'pending', 'draft', 'rejected', 'publish' ), true ) ) {
						?>
					<li class="gn-dropdown-item">
						<a class="gn-edit-job" data-id="<?php echo esc_attr( $job_id ); ?>"
							href="<?php echo esc_url( gigneo_dashboard_url( 'job', 'edit', array( 'p_id' => $job_id ) ) ); ?>">
							<i class="hi hi-pencil-edit" aria-hidden="true"></i>
							<span class="gn-title"><?php echo esc_html__( 'Edit job', 'gigneo' ); ?></span>
						</a>
					</li>
						<?php
					}
					if ( in_array( $job_status, array( 'publish', 'draft' ), true ) ) {
						?>
						<li class="gn-dropdown-item">
							<span class="gn-update-job-status" data-gn-id="<?php echo esc_attr( $job_id ); ?>"
								data-gn-status="<?php echo esc_attr( ( 'paused' ) ); ?>">
								<i class="hi hi-pause" aria-hidden="true"></i>
								<span class="gn-title"><?php echo esc_html__( 'Pause job', 'gigneo' ); ?></span>
							</span>
						</li>
						<?php
					} elseif ( 'paused' === $job_status ) {
						?>
						<li class="gn-dropdown-item">
							<span class="gn-update-job-status" data-gn-id="<?php echo esc_attr( $job_id ); ?>"
								data-gn-status="<?php echo esc_attr( 'publish' ); ?>">
								<i class="hi hi-play" aria-hidden="true"></i>
								<span class="gn-title"><?php echo esc_html__( 'Active job', 'gigneo' ); ?></span>
							</span>
						</li>
						<?php
					}
					if ( 'publish' === $job_status ) {
						?>
						<li class="gn-dropdown-item">
							<span class="gn-draft-job" data-gn-id="<?php echo esc_attr( $job_id ); ?>">
								<i class="hi hi-arrow-right" aria-hidden="true"></i>
								<span class="gn-title"><?php echo esc_html__( 'Move to drafts', 'gigneo' ); ?></span>
							</span>
						</li>
					<?php } ?>
					<li class="gn-dropdown-item">
						<span class="gn-clone-job" data-gn-id="<?php echo esc_attr( $job_id ); ?>">
							<i class="hi hi-document" aria-hidden="true"></i>
							<span class="gn-title"><?php echo esc_html__( 'Use as template', 'gigneo' ); ?></span>
						</span>
					</li>
					<?php if ( ( 'publish' === $job_status || 'draft' === $job_status ) && $enabled_job_removed ) { ?>
						<li class="gn-dropdown-item">
							<span class="gn-remove-job" data-gn-id="<?php echo esc_attr( $job_id ); ?>">
								<i class="hi hi-trash" aria-hidden="true"></i>
								<span class="gn-title"><?php echo esc_html__( 'Delete job', 'gigneo' ); ?></span>
							</span>
						</li>
					<?php } ?>
				</ul>
			</div>
		</div>
	</td>
</tr>