<?php

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

$job_id      = $args['p_id'] ?? '';
$date_format = $args['date_format'] ?? '';
$time_format = $args['time_format'] ?? '';

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

$post_per_page     = apply_filters( 'gigneo_proposal_per_page', 10 );
$post_statuses     = array( 'publish', 'rejected', 'paused', 'draft', 'viewed', 'in-progress', 'shortlisted', 'hired' );
$jobs_status_count = gigneo_get_job_proposals_status_count( $job_id );
?>
<div class="gn-dashboard-section gn-proposal-proposals-wrapper">
	<div class="gn-head">
		<h2 class="gn-title"><?php echo esc_html__( 'Proposals', 'gigneo' ); ?></h2>
		<div class="gn-listing-actions-wrapper">
			<ul id="gn-proposal-filters" class="gn-filters gn-proposals-filters">
				<li class="gn-filter <?php echo '' === $status_val && ! in_array( $status_val, $post_statuses, true ) ? 'gn-active' : ''; ?>" data-gn-filter="gn-all">
					<a href="<?php echo esc_url( gigneo_dashboard_url( 'job', 'proposal', array( 'p_id' => $job_id ) ) ); ?>"
						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 'viewed' === $status_val ? 'gn-active' : ''; ?>"
					data-gn-filter="gn-reviewed">
					<a href="
					<?php
					echo esc_url(
						gigneo_dashboard_url(
							'job',
							'proposal',
							array(
								'p_id'   => $job_id,
								'status' => 'viewed',
							)
						)
					);
					?>
					" class="gn-title"><?php echo esc_html__( 'Viewed', 'gigneo' ); ?>
						<?php if ( ! empty( $jobs_status_count['viewed'] ) ) { ?>
							<span class="gn-count"><?php echo esc_html( $jobs_status_count['viewed'] ); ?></span>
						<?php } ?>
					</a>
				</li>
				<li class="gn-filter <?php echo 'shortlisted' === $status_val ? 'gn-active' : ''; ?>"
					data-gn-filter="gn-shortlisted">
					<a href="
					<?php
					echo esc_url(
						gigneo_dashboard_url(
							'job',
							'proposal',
							array(
								'p_id'   => $job_id,
								'status' => 'shortlisted',
							)
						)
					);
					?>
					" class="gn-title"><?php echo esc_html__( 'Shortlisted', 'gigneo' ); ?>
						<?php if ( ! empty( $jobs_status_count['shortlisted'] ) ) { ?>
							<span class="gn-count"><?php echo esc_html( $jobs_status_count['shortlisted'] ); ?></span>
						<?php } ?>
					</a>
				</li>
				<li class="gn-filter <?php echo 'hired' === $status_val ? 'gn-active' : ''; ?>"
					data-gn-filter="gn-hired">
					<a href="
					<?php
					echo esc_url(
						gigneo_dashboard_url(
							'job',
							'proposal',
							array(
								'p_id'   => $job_id,
								'status' => 'hired',
							)
						)
					);
					?>
					" class="gn-title"><?php echo esc_html__( 'Hired', 'gigneo' ); ?>
						<?php if ( ! empty( $jobs_status_count['hired'] ) ) { ?>
							<span class="gn-count"><?php echo esc_html( $jobs_status_count['hired'] ); ?></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="proposal">
						<input type="hidden" name="p_id" value="<?php echo esc_attr( $job_id ); ?>">
						<label for="gn-search-proposal-input">
							<input id="gn-search-proposal-input" type="search" name="search" class="gn-search-input"
									value="<?php echo esc_attr( $search_val ); ?>"
									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_proposal',
		'post_status'    => $post_statuses,
		'posts_per_page' => $post_per_page,
		'paged'          => $step,
		'orderby'        => 'date',
		'order'          => 'DESC',
		'fields'         => 'ids',
		// phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query
			'meta_query' => array(
				array(
					'key'     => '_gigneo_pm_job_id',
					'value'   => $job_id,
					'compare' => '=',
				),
			),
	);

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

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

	$proposals_query = new WP_Query( $args );
	?>
	<div
			class="gn-proposals-wrapper <?php echo esc_attr( ! $proposals_query->have_posts() ? ' gn-no-proposals' : '' ); ?>">
		<?php if ( $proposals_query->have_posts() ) { ?>
			<div class="gn-proposals-list-wrapper">
				<?php
				while ( $proposals_query->have_posts() ) {
					$proposals_query->the_post();
					$proposal_id = get_the_ID();
					//proposal Card
					gigneo_load_template(
						'dashboard/job/parts/proposal/card',
						false,
						array(
							'p_id'        => $proposal_id,
							'date_format' => $date_format,
							'time_format' => $time_format,

						)
					);
				}
				wp_reset_postdata();
				?>
			</div>
			<?php
		} else {
			gigneo_not_found( 'proposal_list' );
		}
		?>
	</div>
	<?php gigneo_pagination( $proposals_query, $step, 'gn-proposal-pagination' ); ?>
</div>