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

global $current_user;

$platform_type         = gigneo_platform_type();
$profile_id            = gigneo_active_profile_id( $current_user->ID );
$services_status_count = gigneo_posts_status_count( 'gigneo_service', $profile_id );
$date_format           = get_option( 'date_format' );
$featured_limit        = gigneo_option( '_gigneo_ts_service_featured_limit', '0' );
$order_status_count    = gigneo_posts_status_count( 'gigneo_order', $profile_id );
$p_type                = 'job' === $platform_type ? 'job-list' : 'service-list';
?>

<div class="gn-dashboard-section gn-section-intro">
	<div class="gn-left-area">
		<div class="gn-info">
			<h2 class="gn-title"><?php echo esc_html__( 'Overview', 'gigneo' ); ?></h2>
			<p class="gn-description"><?php echo esc_html__( 'Here’s a quick overview of your services', 'gigneo' ); ?></p>
		</div>
		<div class="gn-actions">
			<a href="<?php echo esc_url( gigneo_dashboard_url( 'service', 'edit' ) ); ?>" class="gn-btn gn-btn-md gn-btn-primary">
				<i class="hi hi-plus" aria-hidden="true"></i>
				<?php echo esc_html__( 'Create service', 'gigneo' ); ?>
			</a>
			<a href="<?php echo esc_url( gigneo_dashboard_url( 'order', 'service-list' ) ); ?>" class="gn-btn gn-btn-md gn-btn-outline-grey">
				<?php echo esc_html__( 'View orders', 'gigneo' ); ?>
			</a>
		</div>
	</div>
	<div class="gn-right-area">
		<div class="gn-stat-cards">
			<div class="gn-stat-card">
				<div class="gn-head">
					<span class="gn-icon">
						<i class="hi hi-briefcase" aria-hidden="true"></i>
					</span>
				</div>
				<h3 class="gn-value"><?php echo esc_html( $services_status_count['publish'] ?? 0 ); ?></h3>
				<h6 class="gn-label"><?php echo esc_html__( 'Active services', 'gigneo' ); ?></h6>
			</div>
			<div class="gn-stat-card">
				<div class="gn-head">
					<span class="gn-icon">
						<i class="hi hi-clock" aria-hidden="true"></i>
					</span>
					<a href="<?php echo esc_url( gigneo_dashboard_url( 'order', $p_type ) ); ?>" class="gn-link">
						<i class="hi hi-arrow-up-right" aria-hidden="true"></i>
					</a>
				</div>
				<h3 class="gn-value"><?php echo esc_html( $order_status_count['publish'] ?? 0 ); ?></h3>
				<h6 class="gn-label"><?php echo esc_html__( 'Active orders', 'gigneo' ); ?></h6>
			</div>
			<div class="gn-stat-card">
				<div class="gn-head">
					<span class="gn-icon">
						<i class="hi hi-document-text" aria-hidden="true"></i>
					</span>
					<a href="<?php echo esc_url( gigneo_dashboard_url( 'order', $p_type ) ); ?>" class="gn-link">
						<i class="hi hi-arrow-up-right" aria-hidden="true"></i>
					</a>
				</div>
				<h3 class="gn-value"> <?php echo esc_html( $order_status_count['all'] ?? 0 ); ?></h3>
				<h6 class="gn-label"><?php echo esc_html__( 'Total orders', 'gigneo' ); ?></h6>
			</div>
		</div>
	</div>
</div>

<div class="gn-dashboard-section gn-service-listing-wrapper">
	<div class="gn-head">
		<h2 class="gn-title"><?php echo esc_html__( 'My services', 'gigneo' ); ?></h2>
		<div class="gn-listing-actions-wrapper">
			<ul id="gn-service-filters" class="gn-filters gn-service-filters">
				<li class="gn-filter gn-active" data-gn-filter="*">
					<span class="gn-title"><?php echo esc_html__( 'All', 'gigneo' ); ?>
					<?php if ( ! empty( $services_status_count['all'] ) ) { ?>
						<span class="gn-count"><?php echo esc_html( $services_status_count['all'] ); ?></span>
					<?php } ?>
					</span>
				</li>
				<li class="gn-filter" data-gn-filter="gn-publish">
					<span class="gn-title"><?php echo esc_html__( 'Active', 'gigneo' ); ?>
					<?php if ( ! empty( $services_status_count['publish'] ) ) { ?>
						<span class="gn-count"><?php echo esc_html( $services_status_count['publish'] ); ?></span>
					<?php } ?>
					</span>
				</li>
				<li class="gn-filter" data-gn-filter="gn-pending">
					<span class="gn-title"><?php echo esc_html__( 'Pending', 'gigneo' ); ?>
					<?php if ( ! empty( $services_status_count['pending'] ) ) { ?>
						<span class="gn-count"><?php echo esc_html( $services_status_count['pending'] ); ?></span>
					<?php } ?>
					</span>
				</li>
				<li class="gn-filter" data-gn-filter="gn-rejected">
					<span class="gn-title"><?php echo esc_html__( 'Rejected', 'gigneo' ); ?>
					<?php if ( ! empty( $services_status_count['rejected'] ) ) { ?>
						<span class="gn-count"><?php echo esc_html( $services_status_count['rejected'] ); ?></span>
					<?php } ?>
					</span>
				</li>
				<li class="gn-filter" data-gn-filter="gn-paused">
					<span class="gn-title"><?php echo esc_html__( 'Paused', 'gigneo' ); ?>
					<?php if ( ! empty( $services_status_count['paused'] ) ) { ?>
						<span class="gn-count"><?php echo esc_html( $services_status_count['paused'] ); ?></span>
					<?php } ?>
					</span>
				</li>
				<li class="gn-filter" data-gn-filter="gn-draft">
					<span class="gn-title"><?php echo esc_html__( 'Draft', 'gigneo' ); ?>
					<?php if ( ! empty( $services_status_count['draft'] ) ) { ?>
						<span class="gn-count"><?php echo esc_html( $services_status_count['draft'] ); ?></span>
					<?php } ?>
					</span>
				</li>
			</ul>
			<div class="gn-actions">
				<div class="gn-search-wrapper">
					<div class="gn-search">
						<input type="hidden" name="source" value="service">
						<input type="hidden" name="ref" value="list">
						<label for="gn-search-service-input">
							<input id="gn-search-service-input" type="search" name="search" class="gn-search-input" placeholder="<?php echo esc_html__( 'Search...', 'gigneo' ); ?>" value=""/>
						</label>
						<button type="submit" class="gn-search-icon">
							<i class="hi hi-magnifying-glass" aria-hidden="true"></i>
						</button>
					</div>
				</div>
			</div>
		</div>
	</div>
	<?php
	$args = array(
		'post_type'      => 'gigneo_service',
		'post_status'    => array( 'publish', 'paused', 'pending', 'denied', 'draft', 'rejected' ),
		'author'         => $current_user->ID,
		'posts_per_page' => - 1,
		'orderby'        => 'date',
		'order'          => 'DESC',
		'fields'         => 'ids',
	);

	$services      = get_posts( $args );
	$dashboard_url = gigneo_dashboard_url( 'service', 'edit' );
	?>
	<div class="gn-service-listing <?php echo esc_attr( empty( $services ) ? ' gn-no-services' : '' ); ?>">
		<?php if ( ! empty( $services ) ) { ?>
			<div class="row gy-4">
				<?php
				foreach ( $services as $service_id ) {
					$service_status = get_post_status( $service_id );
					?>
					<div class="col-md-6 col-xl-4 gn-service-listing-item gn-active gn-<?php echo esc_attr( $service_status ); ?>">
						<?php
						//Service Card
						gigneo_load_template(
							'dashboard/service/parts/list/card',
							false,
							array(
								'service_id'     => $service_id,
								'dashboard_url'  => $dashboard_url,
								'featured_limit' => $featured_limit,
								'date_format'    => $date_format,
							)
						);
						?>
					</div>
					<?php
				}
				?>
			</div>
		<?php } ?>
	</div>
	<?php
	$not_found_args = array(
		'title' => esc_html__( 'No service found', 'gigneo' ),
	);
	gigneo_not_found( 'service_list', $not_found_args );
	?>
</div>