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

global $current_user;

// 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'] ) ) : '';
$current_page = ! empty( $_GET['step'] ) ? intval( $_GET['step'] ) : 1;
// phpcs:enable WordPress.Security.NonceVerification.Recommended -- Read-only parameters no nonce required.

$profile_id            = gigneo_active_profile_id( $current_user->ID );
$proposal_status_count = gigneo_posts_status_count( 'gigneo_proposal', $profile_id );
$template_jobs         = gigneo_option( '_gigneo_ts_template_jobs' );
$search_button_link    = ! empty( $template_jobs ) ? get_the_permalink( $template_jobs ) : '';
$show_per_page         = apply_filters( 'gigneo_proposal_per_page', 10 );
$post_statuses         = array( 'publish', 'hired', 'viewed', 'shortlisted' );
?>

<div class="gn-dashboard-section gn-section-intro gn-proposal-overview">
	<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__( 'A summary of your proposal activity.', 'gigneo' ); ?></p>
		</div>
		<div class="gn-actions mt-3">
			<a href="<?php echo esc_url( $search_button_link ); ?>" class="gn-btn gn-btn-md gn-btn-outline-grey">
				<?php echo esc_html__( 'Find more jobs', '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-document-text" aria-hidden="true"></i>
					</span>
				</div>
				<h3 class="gn-value"><?php echo esc_html( $proposal_status_count['all'] ?? 0 ); ?></h3>
				<h6 class="gn-label"><?php echo esc_html__( 'Total proposals', 'gigneo' ); ?></h6>
			</div>
			<div class="gn-stat-card">
				<div class="gn-head">
					<span class="gn-icon">
						<i class="hi hi-eye" aria-hidden="true"></i>
					</span>
				</div>
				<h3 class="gn-value"><?php echo esc_html( ( $proposal_status_count['viewed'] ?? 0 ) + ( $proposal_status_count['publish'] ?? 0 ) ); ?></h3>
				<h6 class="gn-label"><?php echo esc_html__( 'Under review', 'gigneo' ); ?></h6>
			</div>
			<div class="gn-stat-card">
				<div class="gn-head">
					<span class="gn-icon">
						<i class="hi hi-check-circle" aria-hidden="true"></i>
					</span>
				</div>
				<h3 class="gn-value"><?php echo esc_html( $proposal_status_count['shortlisted'] ?? 0 ); ?></h3>
				<h6 class="gn-label"><?php echo esc_html__( 'Shortlisted', '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 proposals', 'gigneo' ); ?></h2>
		<div class="gn-listing-actions-wrapper">
			<ul id="gn-proposal-filters" class="gn-filters gn-job-filters">
				<li class="gn-filter <?php echo '' === $p_status && ! in_array( $p_status, $post_statuses, true ) ? 'gn-active' : ''; ?>" data-gn-filter="gn-all">
					<a href="<?php echo esc_url( gigneo_dashboard_url( 'proposal', 'list' ) ); ?>" class="gn-title">
						<?php echo esc_html__( 'All', 'gigneo' ); ?>
						<?php if ( ! empty( $proposal_status_count['all'] ) ) : ?>
							<span class="gn-count"><?php echo esc_html( $proposal_status_count['all'] ); ?></span>
						<?php endif; ?>
					</a>
				</li>
				<li class="gn-filter <?php echo 'pending' === $p_status ? 'gn-active' : ''; ?>">
					<a href="<?php echo esc_url( gigneo_dashboard_url( 'proposal', 'list', array( 'status' => 'pending' ) ) ); ?>" class="gn-title">
						<?php echo esc_html__( 'Pending', 'gigneo' ); ?>
						<?php if ( ! empty( $proposal_status_count['publish'] ) ) : ?>
							<span class="gn-count"><?php echo esc_html( $proposal_status_count['publish'] ); ?></span>
						<?php endif; ?>
					</a>
				</li>
				<li class="gn-filter <?php echo 'viewed' === $p_status ? 'gn-active' : ''; ?>">
					<a href="<?php echo esc_url( gigneo_dashboard_url( 'proposal', 'list', array( 'status' => 'viewed' ) ) ); ?>" class="gn-title">
						<?php echo esc_html__( 'Viewed', 'gigneo' ); ?>
						<?php if ( ! empty( $proposal_status_count['viewed'] ) ) : ?>
							<span class="gn-count"><?php echo esc_html( $proposal_status_count['viewed'] ); ?></span>
						<?php endif; ?>
					</a>
					
				</li>
				<li class="gn-filter <?php echo 'shortlisted' === $p_status ? 'gn-active' : ''; ?>">
					<a href="<?php echo esc_url( gigneo_dashboard_url( 'proposal', 'list', array( 'status' => 'shortlisted' ) ) ); ?>" class="gn-title">
						<?php echo esc_html__( 'Shortlisted', 'gigneo' ); ?>
						<?php if ( ! empty( $proposal_status_count['shortlisted'] ) ) : ?>
							<span class="gn-count"><?php echo esc_html( $proposal_status_count['shortlisted'] ); ?></span>
						<?php endif; ?>
					</a>
				</li>
				<li class="gn-filter <?php echo 'hired' === $p_status ? 'gn-active' : ''; ?>">
					<a href="<?php echo esc_url( gigneo_dashboard_url( 'proposal', 'list', array( 'status' => 'hired' ) ) ); ?>" class="gn-title">
						<?php echo esc_html__( 'Hired', 'gigneo' ); ?>
						<?php if ( ! empty( $proposal_status_count['hired'] ) ) : ?>
							<span class="gn-count"><?php echo esc_html( $proposal_status_count['hired'] ); ?></span>
						<?php endif; ?>
					</a>
				</li>
			</ul>
			<div class="gn-actions">
				<div class="gn-search-wrapper">
					<form class="gn-search">
						<input type="hidden" name="source" value="proposal">
						<input type="hidden" name="ref" value="list">
						<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( $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_proposal',
		'post_status'    => $post_statuses,
		'author'         => $current_user->ID,
		'posts_per_page' => $show_per_page,
		'paged'          => $current_page,
		'orderby'        => 'date',
		'order'          => 'DESC',
		'fields'         => 'ids',
	);

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

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

	$proposals_query = new WP_Query( $args );
	?>

	<div class="gn-body <?php echo esc_attr( ! $proposals_query->have_posts() ? ' gn-no-results' : '' ); ?>">
		<?php if ( $proposals_query->have_posts() ) { ?>
			<div class="gn-table-wrapper">
				<table class="gn-proposal-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-amount"><?php echo esc_html__( 'Your bid', 'gigneo' ); ?></th>
							<th class="gn-job-item-header-cell gn-timeline"><?php echo esc_html__( 'Timeline', 'gigneo' ); ?></th>
							<th class="gn-job-item-header-cell gn-submitted"><?php echo esc_html__( 'Submitted at', '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 ( $proposals_query->have_posts() ) {
							$proposals_query->the_post();
							gigneo_load_template(
								'dashboard/proposal/parts/list/card',
								false,
								array(
									'p_id' => get_the_ID(),
								)
							);
						}
						wp_reset_postdata();
						?>
					</tbody>
				</table>
			</div>
			<?php
		} else {
			gigneo_not_found( 'proposal_list' ); }
		?>
	</div>
	
	<?php gigneo_pagination( $proposals_query, $current_page, 'gn-proposal-pagination' ); ?>
</div>
