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

$profile_id    = ! empty( $args['profile_id'] ) ? $args['profile_id'] : '';
$profile_type  = ! empty( $args['profile_type'] ) ? $args['profile_type'] : '';
$platform_type = ! empty( $args['platform_type'] ) ? $args['platform_type'] : '';
$credit_system = ! empty( $args['credit_system'] ) ? $args['credit_system'] : '';

$date_format = get_option( 'date_format' );

$participant_singular = 'freelancer' === $profile_type ? gigneo_employer_singular() : gigneo_freelancer_singular();

$profile_key = ( 'freelancer' === $profile_type )
		? '_gigneo_pm_freelancer_profile_id'
		: '_gigneo_pm_profile_id';

$service_order_args = array(
	'post_type'      => 'gigneo_order',
	'posts_per_page' => 5,
	'post_status'    => array( 'publish', 'disputed' ),
	'paged'          => 1,
	'order'          => 'DESC',
	'orderby'        => 'date',
	'fields'         => 'ids',
	// phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query
	'meta_query'     => array(
		'relation' => 'AND',
		array(
			'key'     => $profile_key,
			'value'   => $profile_id,
			'compare' => '=',
		),
		array(
			'key'     => '_gigneo_pm_order_type',
			'value'   => 'service',
			'compare' => '=',
		),
	),
);

$service_orders_query = new WP_Query( $service_order_args );
$total_service_orders = $service_orders_query->found_posts;

$job_order_args = array(
	'post_type'      => 'gigneo_order',
	'posts_per_page' => 5,
	'post_status'    => array( 'publish', 'disputed' ),
	'paged'          => 1,
	'order'          => 'DESC',
	'orderby'        => 'date',
	'fields'         => 'ids',
	// phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query
	'meta_query'     => array(
		'relation' => 'AND',
		array(
			'key'     => $profile_key,
			'value'   => $profile_id,
			'compare' => '=',
		),
		array(
			'key'     => '_gigneo_pm_order_type',
			'value'   => 'job',
			'compare' => '=',
		),
	),
);

$job_orders_query = new WP_Query( $job_order_args );
$total_job_orders = $job_orders_query->found_posts;
$delivery_types   = array(
	'remote' => esc_html__( 'Remote', 'gigneo' ),
	'onsite' => esc_html__( 'Onsite', 'gigneo' ),
);
?>
<div id="gn-account-overview-active-orders" class="gn-dashboard-section gn-account-overview-active-orders">
	<div class="gn-head">
		<h2 class="gn-title">
			<?php echo esc_html__( 'Active orders', 'gigneo' ); ?>
		</h2>
		<a href="<?php echo esc_url( gigneo_dashboard_url( 'order', 'service-list' ) ); ?>" class="gn-btn gn-btn-outline-grey gn-btn-md">
			<?php echo esc_html__( 'View all orders', 'gigneo' ); ?>
			<i class="hi hi-arrow-up-right" aria-hidden="true"></i>
		</a>
	</div>
	<div class="gn-body">
			<div class="gn-tabs-wrapper">
				<?php if ( 'both' === $platform_type ) { ?>
				<ul class="gn-tabs">
					<li class="gn-tab gn-active" data-gn-target="services">
						<?php echo esc_html__( 'Service', 'gigneo' ); ?>
						<?php if ( $total_service_orders > 0 ) { ?>
							<span class="gn-count"><?php echo esc_html( $total_service_orders ); ?></span>
						<?php } ?></li>
					<li class="gn-tab" data-gn-target="jobs">
						<?php echo esc_html__( 'Jobs', 'gigneo' ); ?>
						<?php if ( $total_job_orders > 0 ) { ?>
						<span class="gn-count"><?php echo esc_html( $total_job_orders ); ?></span>
						<?php } ?>
					</li>
				</ul>
				<?php } ?>
				<div class="gn-tabs-content">
					<?php if ( 'jobs' !== $platform_type ) { ?>
						<div id="gn-tab-content-services" class="gn-tab-content gn-active">
							<?php
							if ( $service_orders_query->have_posts() ) {
								?>
							<div class="gn-account-overview-service-orders">
								<?php
								while ( $service_orders_query->have_posts() ) {
									$service_orders_query->the_post();

									$order_id                         = get_the_ID();
									$service_id                       = gigneo_post_meta( $order_id, '_gigneo_pm_service_id' );
									$service_title                    = gigneo_post_meta( $order_id, '_gigneo_pm_service_title' );
									$service_type                     = gigneo_post_meta( $order_id, '_gigneo_pm_service_type' );
									$service_type                     = ! empty( $service_type ) ? ucfirst( $service_type ) : '';
									$order_status                     = get_post_status( $order_id );
									$order_status                     = ( 'publish' === $order_status ) ? 'active' : $order_status;
									$order_status_class               = gigneo_status_class( $order_status );
									$order_status                     = gigneo_status_label( $order_status );
									$subtotal_amount                  = gigneo_post_meta( $order_id, '_gigneo_pm_subtotal' );
									$subtotal_amount                  = gigneo_price_format( $subtotal_amount ?? 0, true );
									$total_amount                     = gigneo_post_meta( $order_id, '_gigneo_pm_total' );
									$total_amount                     = gigneo_price_format( $total_amount ?? 0, true );
									$participant_profile_id           = 'freelancer' === $profile_type ? gigneo_post_meta( $order_id, '_gigneo_pm_profile_id' ) : gigneo_post_meta( $order_id, '_gigneo_pm_freelancer_profile_id' );
									$participant_profile_name         = gigneo_profile_name( $profile_id );
									$service_thumbnail_id             = get_post_thumbnail_id( $service_id );
									$profile_thumbnail_id             = get_post_thumbnail_id( $profile_id );
									$delivery_type                    = gigneo_post_meta( $order_id, '_gigneo_pm_delivery_type' );
									$delivery_type                    = ! empty( $delivery_type ) ? $delivery_type : 'remote';
									$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' );
									$subtotal_amount                  = gigneo_post_meta( $order_id, '_gigneo_pm_subtotal' );
									$subtotal_amount                  = gigneo_price_format( $subtotal_amount ?? 0, true );
									$total_amount                     = gigneo_post_meta( $order_id, '_gigneo_pm_total' );
									$total_amount                     = gigneo_price_format( $total_amount ?? 0, true );
									$participant_profile_id           = gigneo_post_meta( $order_id, $profile_key );
									$participant_profile_thumbnail_id = get_post_thumbnail_id( $participant_profile_id );
									?>
									<div class="gn-order-item">
										<figure class="gn-thumbnail">
											<?php gigneo_attachment_image_src( $service_thumbnail_id ?? 0, 'small' ); ?>
										</figure>
										<div class="gn-info">
											<div class="gn-head">
												<a href="<?php echo esc_url( gigneo_dashboard_url( 'order', 'service-detail', array( 'p_id' => $order_id ) ) ); ?>" class="gn-title">
													<?php echo esc_html( $service_title ); ?>
												</a>
												<span class="gn-status-tag <?php echo esc_attr( $order_status_class ); ?>"><?php echo esc_html( $order_status ); ?></span>
											</div>
											<div class="gn-body">
												<ul class="gn-meta-wrapper">
													<li class="gn-meta gn-delivery-type">
														<i class="hi hi-map-pin" aria-hidden="true"></i>
														<h5 class="gn-label"><?php echo esc_html( $delivery_types[ $delivery_type ] ); ?></h5>
													</li>
													<li class="gn-meta gn-delivery-date">
														<i class="hi hi-clock" aria-hidden="true"></i>
														<h5 class="gn-label">
															<?php
															printf(
																// translators: %s is delivery date
																esc_html__( 'Due on %s', 'gigneo' ),
																esc_html( $delivery_date )
															);
															?>
														</h5>
													</li>
													<li class="gn-meta gn-amount">
														<i class="hi hi-currency-dollar" aria-hidden="true"></i>
														<h5 class="gn-label"><?php echo esc_html( 'employer' === $profile_type ? $total_amount : $subtotal_amount ); ?></h5>
													</li>
													<li class="gn-meta gn-participent">
														<i class="hi hi-user" aria-hidden="true"></i>
														<h5 class="gn-name"><?php echo esc_html( gigneo_profile_name( $participant_profile_id ) ); ?></h5>
													</li>
												</ul>
											</div>
										</div>
										<div class="gn-actions">
											<a href="<?php echo esc_url( gigneo_dashboard_url( 'order', 'service-detail', array( 'p_id' => $order_id ) ) ); ?>" class="gn-btn gn-btn-outline-grey">
												<i class="hi hi-arrow-up-right" aria-hidden="true"></i>
											</a>
										</div>
									</div>
									<?php
								}
								wp_reset_postdata();
								?>
							</div>
								<?php
							} else {
								$not_found_args = array(
									'title'       => esc_html__( 'No active orders', 'gigneo' ),
									'description' => esc_html__( 'When a service is hired, your active order will appear here.', 'gigneo' ),
								);
								gigneo_not_found( 'account_overview_service_orders', $not_found_args );
							}
							?>
						</div>
						<?php
					}
					if ( 'services' !== $platform_type ) {
						?>
						<div id="gn-tab-content-jobs" class="gn-tab-content<?php echo esc_attr( 'jobs' === $platform_type ? ' gn-active' : '' ); ?>">
							<?php if ( $job_orders_query->have_posts() ) { ?>
							<div class="gn-account-overview-job-orders">
								<?php
								while ( $job_orders_query->have_posts() ) {
									$job_orders_query->the_post();

									$order_id                         = get_the_ID();
									$job_id                           = gigneo_post_meta( $order_id, '_gigneo_pm_job_id' );
									$job_title                        = gigneo_post_meta( $order_id, '_gigneo_pm_job_title' );
									$job_type                         = gigneo_post_meta( $order_id, '_gigneo_pm_job_type' );
									$job_type                         = ! empty( $job_type ) ? ucfirst( $job_type ) : '';
									$order_status                     = get_post_status( $order_id );
									$order_status                     = ( 'publish' === $order_status ) ? 'active' : $order_status;
									$order_status_class               = gigneo_status_class( $order_status );
									$order_status                     = gigneo_status_label( $order_status );
									$subtotal_amount                  = gigneo_post_meta( $order_id, '_gigneo_pm_subtotal' );
									$subtotal_amount                  = gigneo_price_format( $subtotal_amount ?? 0, true );
									$total_amount                     = gigneo_post_meta( $order_id, '_gigneo_pm_total' );
									$total_amount                     = gigneo_price_format( $total_amount ?? 0, true );
									$participant_profile_id           = 'freelancer' === $profile_type ? gigneo_post_meta( $order_id, '_gigneo_pm_profile_id' ) : gigneo_post_meta( $order_id, '_gigneo_pm_freelancer_profile_id' );
									$participant_profile_name         = gigneo_profile_name( $profile_id );
									$job_thumbnail_id                 = get_post_thumbnail_id( $job_id );
									$profile_thumbnail_id             = get_post_thumbnail_id( $profile_id );
									$delivery_type                    = gigneo_post_meta( $order_id, '_gigneo_pm_delivery_type' );
									$delivery_type                    = ! empty( $delivery_type ) ? $delivery_type : 'remote';
									$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' );
									$subtotal_amount                  = gigneo_post_meta( $order_id, '_gigneo_pm_subtotal' );
									$subtotal_amount                  = gigneo_price_format( $subtotal_amount ?? 0, true );
									$total_amount                     = gigneo_post_meta( $order_id, '_gigneo_pm_total' );
									$total_amount                     = gigneo_price_format( $total_amount ?? 0, true );
									$participant_profile_id           = gigneo_post_meta( $order_id, $profile_key );
									$participant_profile_thumbnail_id = get_post_thumbnail_id( $participant_profile_id );
									?>
									<div class="gn-order-item">
										<div class="gn-info">
											<div class="gn-head">
												<a href="<?php echo esc_url( gigneo_dashboard_url( 'order', 'details', array( 'p_id' => $order_id ) ) ); ?>" class="gn-title">
													<?php echo esc_html( $job_title ); ?>
												</a>
												<span class="gn-status-tag <?php echo esc_attr( $order_status_class ); ?>"><?php echo esc_html( $order_status ); ?></span>
											</div>
											<div class="gn-body">
												<ul class="gn-meta-wrapper">
													<li class="gn-meta gn-delivery-type">
														<i class="hi hi-map-pin" aria-hidden="true"></i>
														<h5 class="gn-label"><?php echo esc_html( $delivery_types[ $delivery_type ] ); ?></h5>
													</li>
													<li class="gn-meta gn-delivery-date">
														<i class="hi hi-clock" aria-hidden="true"></i>
														<h5 class="gn-label">
															<?php
															printf(
																// translators: %s is delivery date
																esc_html__( 'Due on %s', 'gigneo' ),
																esc_html( $delivery_date )
															);
															?>
														</h5>
													</li>
													<li class="gn-meta gn-amount">
														<i class="hi hi-banknotes" aria-hidden="true"></i>
														<h5 class="gn-label"><?php echo esc_html( 'employer' === $profile_type ? $total_amount : $subtotal_amount ); ?></h5>
													</li>
													<li class="gn-meta gn-participent">
														<i class="hi hi-user" aria-hidden="true"></i>
														<h5 class="gn-name"><?php echo esc_html( gigneo_profile_name( $participant_profile_id ) ); ?></h5>
													</li>
												</ul>
											</div>
										</div>
										<div class="gn-actions">
											<a href="<?php echo esc_url( gigneo_dashboard_url( 'order', 'job-detail', array( 'p_id' => $order_id ) ) ); ?>" class="gn-btn gn-btn-outline-grey">
												<i class="hi hi-arrow-up-right" aria-hidden="true"></i>
											</a>
										</div>
									</div>
									<?php
								}
								wp_reset_postdata();
								?>
							</div>
								<?php
							} else {
								$not_found_args = array(
									'title'       => esc_html__( 'No active orders', 'gigneo' ),
									'description' => esc_html__( 'When a job is hired, your active order will appear here.', 'gigneo' ),
								);
								gigneo_not_found( 'account_overview_job_orders', $not_found_args );
							}
							?>
						</div>
					<?php } ?>
				</div>
			</div>
		
	</div>
</div>
