<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}
use Gigneo\Notifications;
$order_id = isset( $args['order_id'] ) ? absint( $args['order_id'] ) : 0;

$active_profile_id    = gigneo_active_profile_id();
$profile_type         = gigneo_profile_type( $active_profile_id );
$job_title            = gigneo_post_meta( $order_id, '_gigneo_pm_job_title' );
$job_type             = gigneo_post_meta( $order_id, '_gigneo_pm_job_type' );
$job_id               = gigneo_post_meta( $order_id, '_gigneo_pm_job_id' );
$date_format          = get_option( 'date_format' );
$time_format          = get_option( 'time_format' );
$order_status_raw     = get_post_status( $order_id );
$order_status_raw     = ( 'publish' === $order_status_raw ) ? 'active' : $order_status_raw;
$order_status_class   = gigneo_status_class( $order_status_raw );
$order_status_label   = gigneo_status_label( $order_status_raw );
$subtotal_amount      = gigneo_post_meta( $order_id, '_gigneo_pm_subtotal' );
$total_amount         = gigneo_post_meta( $order_id, '_gigneo_pm_total' );
$subtotal_amount      = gigneo_price_format( $subtotal_amount ? $subtotal_amount : 0, true );
$total_amount         = gigneo_price_format( $total_amount ? $total_amount : 0, true );
$started_time         = gigneo_post_meta( $order_id, '_gigneo_pm_started_at_gmt' );
$started_time         = ! empty( $started_time ) ? gigneo_datetime( $started_time, $date_format ) : '';
$delivery_date        = gigneo_post_meta( $order_id, '_gigneo_pm_delivery_at_gmt' );
$delivery_date        = ! empty( $delivery_date ) ? gigneo_datetime( $delivery_date, $date_format ) : esc_html__( 'Not started', 'gigneo' );
$order_profile_id     = ( 'freelancer' === $profile_type ) ? gigneo_post_meta( $order_id, '_gigneo_pm_profile_id' ) : gigneo_post_meta( $order_id, '_gigneo_pm_freelancer_profile_id' );
$profile_name         = $order_profile_id ? gigneo_profile_name( $order_profile_id ) : '';
$profile_thumbnail_id = $order_profile_id ? get_post_thumbnail_id( $order_profile_id ) : 0;
$profile_slug         = $order_profile_id ? get_post_field( 'post_name', $order_profile_id ) : '';
$job_thumbnail_id     = $job_id ? get_post_thumbnail_id( $job_id ) : 0;
$order_link           = add_query_arg( array( 'p_id' => $order_id ), gigneo_dashboard_url( 'order', 'job-detail' ) );
$unread_count         = Notifications::get_unread_order_messages_count( $active_profile_id, $order_id );
$message_link         = add_query_arg(
	array(
		'p_id' => $order_id,
		'tab'  => 'messages',
	),
	gigneo_dashboard_url( 'order', 'job-detail' )
);
?>
<tr class="gn-order-item-row">
	<td class="gn-order-item-cell gn-details">
		<div class="gn-order-details">
			<div class="gn-order-details-info">
				<h5 class="gn-title">
					<?php echo esc_html( $job_title ); ?>
				</h5>
				<div class="gn-order-meta">
					<?php if ( ! empty( $started_time ) ) { ?>
						<span class="gn-date">
							<i class="hi hi-calendar" aria-hidden="true"></i>
							<?php
							printf(
							// translators: 1. Started time.
								esc_html__( 'Started at %s', 'gigneo' ),
								esc_html( $started_time )
							);
							?>
						</span>
					<?php } else { ?>
						<span class="gn-date">
							<i class="hi hi-calendar" aria-hidden="true"></i>
							<?php echo esc_html__( 'Not started yet', 'gigneo' ); ?>
						</span>
					<?php } ?>
					<?php if ( ! empty( $job_type ) ) { ?>
						<span class="gn-type">
							<i class="hi hi-map-pin" aria-hidden="true"></i>
							<?php echo esc_html( 'fixed' === $job_type ? esc_html__( 'Remote', 'gigneo' ) : esc_html__( 'Onsite', 'gigneo' ) ); ?>
						</span>
					<?php } ?>
				</div>
			</div>
		</div>
	</td>
	<td class="gn-order-item-cell gn-user">
		<div class="gn-order-user">
			<figure class="gn-avatar">
				<?php
				gigneo_attachment_image_src( $profile_thumbnail_id ?? 0, 'thumbnail', 'profile' );
				?>
			</figure>
			<div class="gn-order-user-info">
				<span><?php echo esc_html( $profile_name ); ?></span>

				<?php if ( ! empty( $profile_slug ) ) : ?>
					<a href="<?php echo esc_url( $message_link ); ?>" class="gn-link">
						<?php echo esc_html__( 'Message', 'gigneo' ); ?>
						<span class="gn-count"
								data-gn-count="<?php echo esc_attr( $unread_count ); ?>"><?php echo esc_html( $unread_count ); ?></span>
					</a>
				<?php endif; ?>
			</div>
		</div>
	</td>
	<td class="gn-order-item-cell gn-amount">
		<div class="gn-order-amount">
			<span class="gn-amount">
				<?php
				echo esc_html(
					( 'employer' === $profile_type )
								? $total_amount
								: $subtotal_amount
				);
				?>
			</span>
		</div>
	</td>
	<td class="gn-order-item-cell gn-status">
		<div class="gn-status-wrapper">
			<div class="gn-status">
				<span class="gn-status-tag <?php echo esc_attr( $order_status_class ); ?>">
					<?php echo esc_html( $order_status_label ); ?>
				</span>
			</div>
			<div class="gn-delivery">
				<span class="gn-delivery-label">
					<?php echo esc_html__( 'Due on', 'gigneo' ); ?>
				</span>
				<span class="gn-delivery-date">
					<?php echo esc_html( $delivery_date ); ?>
				</span>
			</div>
		</div>
	</td>
	<td class="gn-order-item-cell gn-action">
		<div class="gn-dropdown-container">
			<a href="<?php echo esc_url( $order_link ); ?>" class="gn-btn gn-btn-sm gn-btn-outline-grey">
				<i class="hi hi-eye" aria-hidden="true"></i>
			</a>
		</div>
	</td>
</tr>
