<?php

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

$job_id      = ! empty( $args['p_id'] ) ? absint( $args['p_id'] ) : 0;
$date_format = $args['date_format'] ?? '';
$time_format = $args['time_format'] ?? '';

$job_title        = get_the_title( $job_id );
$job_status       = get_post_status( $job_id );
$duration         = gigneo_post_meta( $job_id, '_gigneo_pm_duration' );
$duration_type    = gigneo_post_meta( $job_id, '_gigneo_pm_duration_type' );
$work_mode        = gigneo_post_meta( $job_id, '_gigneo_pm_type' );
$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 );
$is_featured      = gigneo_post_meta( $job_id, '_gigneo_pm_featured' );
$date_time_format = $date_format . ' ' . $time_format;
$job_permalink    = get_permalink( $job_id );

$posted_time = get_post_time( 'Y-m-d H:i:s', true, $job_id );
$time_diff   = gigneo_datetime( $posted_time, $date_time_format );
$budget      = gigneo_post_meta( $job_id, '_gigneo_pm_price' );
$deadline    = gigneo_post_meta( $job_id, '_gigneo_pm_application_deadline_gmt' );

?>
<div class="gn-dashboard-section gn-section-single-job">
	<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>
				<?php if ( ! empty( $is_featured ) ) { ?>
					<span class="gn-status-tag-fill gn-warning">
						<?php echo esc_html__( 'Featured', 'gigneo' ); ?>
					</span>
				<?php } ?>
				<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>
			</div>
			<div class="gn-info-wrapper">
				<div class="gn-meta">
					<?php if ( ! empty( $work_mode ) ) { ?>
						<div class="gn-meta-item gn-mode">
							<i class="hi hi-globe-alt" aria-hidden="true"></i>
							<span><?php echo esc_html( ucfirst( $work_mode ) ); ?></span>
						</div>
						<?php
					}
					if ( ! empty( $duration ) && ! empty( $duration_type ) ) {
						?>
						<div class="gn-meta-item gn-duration">
							<i class="hi hi-clock" aria-hidden="true"></i>
							<span><?php echo esc_html( gigneo_delivery_time( $duration, $duration_type ) ); ?></span>
						</div>
						<?php
					}
					if ( ! empty( $budget ) ) {
						?>
						<div class="gn-meta-item gn-budget">
							<i class="hi hi-banknotes" aria-hidden="true"></i>
							<span><?php echo esc_html( gigneo_price_format( $budget, true ) ); ?></span>
						</div>
						<?php
					}
					if ( ! empty( $deadline ) ) {
						?>
						<div class="gn-meta-item gn-deadline">
							<i class="hi hi-calendar" aria-hidden="true"></i>
							<span>
								<?php
								printf(
								/* translators: %s: application deadline date */
									esc_html__( 'Deadline: %s', 'gigneo' ),
									esc_html( gigneo_datetime( $deadline, 'd M Y' ) )
								);
								?>
							</span>
						</div>
						<?php
					}
					if ( ! empty( $time_diff ) ) {
						?>
						<div class="gn-meta-item gn-date">
							<i class="hi hi-calendar" aria-hidden="true"></i>
							<span>
								<?php
								printf(
								/* translators: %s: posted time */
									esc_html__( 'Posted: %s', 'gigneo' ),
									esc_html( $time_diff )
								);
								?>
							</span>
						</div>
					<?php } ?>
				</div>
			</div>
		</div>
		<div class="gn-actions">
			<?php if ( in_array( $job_status, array( 'publish', 'completed', 'in-progress' ), true ) ) { ?>
				<a href="<?php echo esc_url( $job_permalink ); ?>" target="_blank" rel="noopener noreferrer" class="gn-btn gn-btn-md gn-btn-outline-grey gn-view-job-btn">
					<i class="hi hi-eye" aria-hidden="true"></i>
					<span class="gn-title"><?php echo esc_html__( 'View job posting', 'gigneo' ); ?></span>
				</a>
			<?php } ?>
			<div class="gn-dropdown-container">
				<button type="button"
						class="gn-btn gn-btn-md gn-btn-outline-grey gn-dropdown-toggle gn-job-action gn-ellipsis-btn"
						data-gn-target="#gn-job-action-dropdown">
					<i class="hi hi-ellipsis-horizontal" aria-hidden="true"></i>
				</button>
				<div class="gn-dropdown-content gn-enter-bottom" id="gn-job-action-dropdown">
					<ul class="gn-dropdown-menu">
						<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>
						<li class="gn-dropdown-item">
							<?php if ( 'publish' === $job_status ) { ?>
								<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>
							<?php } elseif ( 'paused' === $job_status ) { ?>
								<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>
							<?php } ?>
						</li>
					</ul>
				</div>
			</div>
		</div>
	</div>
</div>