<?php

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}
use Gigneo\Modules;
global $current_user;

$profile_id    = gigneo_active_profile_id( $current_user->ID );
$show_per_page = apply_filters( 'gigneo_job_per_page', 10 );

// phpcs:disable WordPress.Security.NonceVerification.Recommended -- Read-only parameters no nonce required.
$p_search = ! empty( $_GET['search'] ) ? sanitize_text_field( wp_unslash( $_GET['search'] ) ) : '';
$p_status = ! empty( $_GET['status'] ) ? sanitize_text_field( wp_unslash( $_GET['status'] ) ) : '';
$p_step   = ! empty( $_GET['step'] ) ? absint( $_GET['step'] ) : 1;
// phpcs:enable WordPress.Security.NonceVerification.Recommended -- Read-only parameters no nonce required.

$jobs_status_count     = gigneo_posts_status_count( 'gigneo_job', $profile_id );
$proposal_status_count = gigneo_posts_status_count( 'gigneo_proposal', $profile_id );
$order_status_count    = gigneo_posts_status_count( 'gigneo_order', $profile_id );
$platform_type         = gigneo_platform_type();
$p_type                = 'job' === $platform_type ? 'job-list' : 'service-list';
$post_statuses         = array( 'publish', 'pending', 'rejected', 'paused', 'draft', 'in-progress', 'completed', 'cancelled' );
?>

<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 jobs', 'gigneo' ); ?></p>
		</div>
		<div class="gn-actions">
			<a href="<?php echo esc_url( gigneo_dashboard_url( 'job', 'edit' ) ); ?>"
				class="gn-btn gn-btn-md gn-btn-primary">
				<i class="hi hi-plus" aria-hidden="true"></i>
				<?php echo esc_html__( 'Create job', 'gigneo' ); ?>
			</a>
			<a href="<?php echo esc_url( gigneo_dashboard_url( 'order', 'job-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-clipboard-document-check" aria-hidden="true"></i>
					</span>
				</div>
				<h3 class="gn-value"><?php echo esc_html( $proposal_status_count['publish'] ?? 0 ); ?></h3>
				<h6 class="gn-label"><?php echo esc_html__( 'New proposals', 'gigneo' ); ?></h6>
			</div>
			<div class="gn-stat-card">
				<div class="gn-head">
					<span class="gn-icon">
						<i class="hi hi-briefcase" aria-hidden="true"></i>
					</span>
					<a href="<?php echo esc_url( gigneo_dashboard_url( 'order', 'job-list' ) ); ?>" class="gn-link">
						<i class="hi hi-arrow-up-right" aria-hidden="true"></i>
					</a>
				</div>
				<h3 class="gn-value"><?php echo esc_html( $jobs_status_count['publish'] ?? 0 ); ?></h3>
				<h6 class="gn-label"><?php echo esc_html__( 'Active jobs', 'gigneo' ); ?></h6>
			</div>
			<div class="gn-stat-card">
				<div class="gn-head">
					<span class="gn-icon">
						<i class="hi hi-folder" 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>
	</div>
</div>
<div class="gn-dashboard-section gn-job-listing-wrapper">
	<div class="gn-head">
		<h2 class="gn-title"><?php echo esc_html__( 'My jobs', 'gigneo' ); ?></h2>
		<div class="gn-listing-actions-wrapper">
			<ul id="gn-job-filters" class="gn-filters gn-job-filters">
				<li class="gn-filter <?php echo '' === $p_status || ! in_array( $p_status, $post_statuses, true ) ? 'gn-active' : ''; ?>">
					<a href="<?php echo esc_url( gigneo_dashboard_url( 'job', 'list' ) ); ?>" class="gn-title">
						<?php echo esc_html__( 'All', 'gigneo' ); ?>
						<?php if ( ! empty( $jobs_status_count['all'] ) ) { ?>
								<span class="gn-count"><?php echo esc_html( $jobs_status_count['all'] ); ?></span>
						<?php } ?>
					</a>
				</li>
				<li class="gn-filter <?php echo 'publish' === $p_status ? 'gn-active' : ''; ?>">
					<a href="<?php echo esc_url( gigneo_dashboard_url( 'job', 'list', array( 'status' => 'publish' ) ) ); ?>"
						class="gn-title">
						<?php echo esc_html__( 'Active', 'gigneo' ); ?>
						<?php if ( ! empty( $jobs_status_count['publish'] ) ) { ?>
								<span class="gn-count"><?php echo esc_html( $jobs_status_count['publish'] ); ?></span>
						<?php } ?>
					</a>
				</li>
				<li class="gn-filter <?php echo 'in-progress' === $p_status ? 'gn-active' : ''; ?>">
					<a href="<?php echo esc_url( gigneo_dashboard_url( 'job', 'list', array( 'status' => 'in-progress' ) ) ); ?>"
						class="gn-title">
						<?php echo esc_html__( 'In progress', 'gigneo' ); ?>
						<?php if ( ! empty( $jobs_status_count['in-progress'] ) ) { ?>
								<span class="gn-count"><?php echo esc_html( $jobs_status_count['in-progress'] ); ?></span>
						<?php } ?>
					</a>
				</li>
				<li class="gn-filter <?php echo 'pending' === $p_status ? 'gn-active' : ''; ?>">
					<a href="<?php echo esc_url( gigneo_dashboard_url( 'job', 'list', array( 'status' => 'pending' ) ) ); ?>"
						class="gn-title">
						<?php echo esc_html__( 'Pending', 'gigneo' ); ?>
						<?php if ( ! empty( $jobs_status_count['pending'] ) ) { ?>
								<span class="gn-count"><?php echo esc_html( $jobs_status_count['pending'] ); ?></span>
						<?php } ?>
					</a>
				</li>
				<!-- if dispute module active show dispute filter -->
				<?php if ( in_array( 'dispute', gigneo_active_modules(), true ) ) { ?>
				
				<li class="gn-filter <?php echo 'disputed' === $p_status ? 'gn-active' : ''; ?>">
					<a href="<?php echo esc_url( gigneo_dashboard_url( 'job', 'list', array( 'status' => 'disputed' ) ) ); ?>"
						class="gn-title">
						<?php echo esc_html__( 'Disputed', 'gigneo' ); ?>
						<?php if ( ! empty( $jobs_status_count['disputed'] ) ) { ?>
								<span class="gn-count"><?php echo esc_html( $jobs_status_count['disputed'] ); ?></span>
						<?php } ?>
					</a>
				</li>
				<?php } ?>
				<li class="gn-filter <?php echo 'rejected' === $p_status ? 'gn-active' : ''; ?>">
					<a href="<?php echo esc_url( gigneo_dashboard_url( 'job', 'list', array( 'status' => 'rejected' ) ) ); ?>"
						class="gn-title">
						<?php echo esc_html__( 'Rejected', 'gigneo' ); ?>
						<?php if ( ! empty( $jobs_status_count['rejected'] ) ) { ?>
								<span class="gn-count"><?php echo esc_html( $jobs_status_count['rejected'] ); ?></span>
						<?php } ?>
					</a>
				</li>
				<li class="gn-filter <?php echo 'paused' === $p_status ? 'gn-active' : ''; ?>">
					<a href="<?php echo esc_url( gigneo_dashboard_url( 'job', 'list', array( 'status' => 'paused' ) ) ); ?>"
						class="gn-title">
						<?php echo esc_html__( 'Paused', 'gigneo' ); ?>
						<?php if ( ! empty( $jobs_status_count['paused'] ) ) { ?>
								<span class="gn-count"><?php echo esc_html( $jobs_status_count['paused'] ); ?></span>
						<?php } ?>
					</a>
				</li>
				<li class="gn-filter <?php echo 'draft' === $p_status ? 'gn-active' : ''; ?>">
					<a href="<?php echo esc_url( gigneo_dashboard_url( 'job', 'list', array( 'status' => 'draft' ) ) ); ?>"
						class="gn-title">
						<?php echo esc_html__( 'Draft', 'gigneo' ); ?>
						<?php if ( ! empty( $jobs_status_count['draft'] ) ) { ?>
								<span class="gn-count"><?php echo esc_html( $jobs_status_count['draft'] ); ?></span>
						<?php } ?>
					</a>
				</li>
				<li class="gn-filter <?php echo 'completed' === $p_status ? 'gn-active' : ''; ?>">
					<a href="<?php echo esc_url( gigneo_dashboard_url( 'job', 'list', array( 'status' => 'completed' ) ) ); ?>"
						class="gn-title">
						<?php echo esc_html__( 'Completed', 'gigneo' ); ?>
						<?php if ( ! empty( $jobs_status_count['completed'] ) ) { ?>
							<span class="gn-count"><?php echo esc_html( $jobs_status_count['completed'] ); ?></span>
						<?php } ?>
					</a>
				</li>
				<li class="gn-filter <?php echo 'cancelled' === $p_status ? 'gn-active' : ''; ?>">
					<a href="<?php echo esc_url( gigneo_dashboard_url( 'job', 'list', array( 'status' => 'cancelled' ) ) ); ?>"
						class="gn-title">
						<?php echo esc_html__( 'Cancelled', 'gigneo' ); ?>
						<?php if ( ! empty( $jobs_status_count['cancelled'] ) ) { ?>
								<span class="gn-count"><?php echo esc_html( $jobs_status_count['cancelled'] ); ?></span>
						<?php } ?>
					</a>
				</li>
			</ul>
			<div class="gn-actions">
				<div class="gn-search-wrapper">
					<form class="gn-search">
						<input type="hidden" name="source" value="job">
						<input type="hidden" name="ref" value="list">
						<label for="gn-search-job-input">
							<input id="gn-search-job-input" type="search" name="search" class="gn-search-input"
								value="<?php echo esc_attr( $p_search ); ?>"
								placeholder="<?php echo esc_attr__( 'Search...', 'gigneo' ); ?>" />
						</label>
						<button type="submit" class="gn-search-icon">
							<i class="hi hi-magnifying-glass" aria-hidden="true"></i>
						</button>
					</form>
				</div>
			</div>
		</div>
	</div>
	<?php

	$args = array(
		'post_type'      => 'gigneo_job',
		'post_status'    => $post_statuses,
		'author'         => $current_user->ID,
		'posts_per_page' => $show_per_page,
		'paged'          => $p_step,
		'orderby'        => 'date',
		'order'          => 'DESC',
		'fields'         => 'ids',
	);

	//search query
	if ( ! empty( $p_search ) ) {
		$args['s'] = $p_search;
	}

	//status filter
	if ( ! empty( $p_status ) && in_array( $p_status, $post_statuses, true ) ) {
		$args['post_status'] = $p_status;
	}

	$jobs_query = new WP_Query( $args );
	?>
	<div class="gn-body <?php echo esc_attr( ! $jobs_query->have_posts() ? ' gn-no-jobs' : '' ); ?>">
		<?php if ( $jobs_query->have_posts() ) { ?>
				<div class="gn-table-wrapper">
					<table class="gn-job-items gn-table gn-table-borderless">
						<thead class="gn-job-item-header">
							<tr class="gn-job-item-row">
								<th class="gn-job-item-header-cell gn-details">
									<?php echo esc_html__( 'Job details', 'gigneo' ); ?>
								</th>
								<th class="gn-job-item-header-cell gn-applications">
									<?php echo esc_html__( 'Proposals', 'gigneo' ); ?>
								</th>
								<th class="gn-job-item-header-cell gn-progress">
									<?php echo esc_html__( 'Hiring progress', 'gigneo' ); ?>
								</th>
								<th class="gn-job-item-header-cell gn-amount">
									<?php echo esc_html__( 'Amount', 'gigneo' ); ?>
								</th>
								<th class="gn-job-item-header-cell gn-status">
									<?php echo esc_html__( 'Status', 'gigneo' ); ?>
								</th>
								<th class="gn-job-item-header-cell gn-action">
									<?php echo esc_html__( 'Action', 'gigneo' ); ?>
								</th>
							</tr>
						</thead>
						<tbody class="gn-job-items-body">
							<?php
							while ( $jobs_query->have_posts() ) {
								$jobs_query->the_post();
								$job_id = get_the_ID();
								//Job Card
								gigneo_load_template(
									'dashboard/job/parts/list/card',
									false,
									array(
										'job_id' => $job_id,
									)
								);
							}
							wp_reset_postdata();
							?>
						</tbody>
					</table>
				</div>
				<?php
		} else {
			$not_found_args = array(
				'title'       => esc_html__( 'No jobs', 'gigneo' ),
				'description' => esc_html__( 'All your jobs will appear here once created.', 'gigneo' ),
			);
			gigneo_not_found( 'job_list', $not_found_args );
		}
		?>
	</div>
	<?php gigneo_pagination( $jobs_query, $p_step, 'gn-job-pagination' ); ?>
</div>