<?php

use Gigneo\Orders;

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

$profile_id           = gigneo_active_profile_id( $current_user->ID );
$order_status_count   = gigneo_posts_status_count( 'gigneo_order', $profile_id );
$profile_type         = gigneo_profile_type();
$user_profile_id      = gigneo_active_profile_id();
$date_format          = get_option( 'date_format' );
$job_page             = gigneo_option( '_gigneo_ts_template_jobs' );
$service_page         = gigneo_option( '_gigneo_ts_template_services' );
$profile_singular     = 'freelancer' === $profile_type ? gigneo_employer_singular() : gigneo_freelancer_singular();
$count_service        = Orders::get_order_type_count( $profile_id );
$count_job            = Orders::get_order_type_count( $profile_id, 'job' );
$date_format          = get_option( 'date_format' );
$show_per_page        = apply_filters( 'gigneo_order_per_page', 10 );
$platform_type        = gigneo_platform_type();
$p_search_button_text = '';
$p_search_button_link = '';
$find_button_text     = '';
$jobs_page_url        = '';

if ( 'freelancer' === $profile_type ) {
	if ( in_array( $platform_type, array( 'services', 'both' ), true ) ) {
		$create_button_text = esc_html__( 'Create service', 'gigneo' );
		$create_button_link = gigneo_dashboard_url( 'service', 'edit' );
	} else {
		$create_button_text = esc_html__( 'View proposals', 'gigneo' );
		$create_button_link = gigneo_dashboard_url( 'proposal', 'list' );
	}
	if ( in_array( $platform_type, array( 'jobs', 'both' ), true ) ) {
		$search_button_text = esc_html__( 'Browse jobs', 'gigneo' );
		$template_jobs      = gigneo_option( '_gigneo_ts_template_jobs' );
		$search_button_link = ! empty( $template_jobs ) ? get_the_permalink( $template_jobs ) : '';
	} else {
		$search_button_text = esc_html__( 'List services', 'gigneo' );
		$search_button_link = gigneo_dashboard_url( 'service', 'list' );
	}
} else {
	if ( in_array( $platform_type, array( 'jobs', 'both' ), true ) ) {
		$create_button_text = esc_html__( 'Create new job', 'gigneo' );
		$create_button_link = gigneo_dashboard_url( 'job', 'edit' );
	} else {
		$create_button_text = esc_html__( 'View spending', 'gigneo' );
		$create_button_link = gigneo_dashboard_url( 'account', 'spending' );
	}

	if ( in_array( $platform_type, array( 'services', 'both' ), true ) ) {
		$search_button_text = esc_html__( 'Browse services', 'gigneo' );
		$search_button_link = get_permalink( $service_page );
	} else {
		$search_button_text = esc_html__( 'Browse jobs', 'gigneo' );
		$search_button_link = get_permalink( $job_page );
	}
}
?>


<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__( 'Orders', 'gigneo' ); ?></h2>
			<p class="gn-description"><?php echo esc_html__( 'Manage and monitor all active orders', 'gigneo' ); ?></p>
		</div>
		<div class="gn-actions">
			<?php if ( ! empty( $create_button_text ) && ! empty( $create_button_link ) ) { ?>
				<a href="<?php echo esc_url( $create_button_link ); ?>" class="gn-btn gn-btn-md gn-btn-primary">
					<i class="hi hi-plus" aria-hidden="true"></i>
					<?php echo esc_html( $create_button_text ); ?>
				</a>
			<?php } ?>
			<?php if ( ! empty( $search_button_text ) && ! empty( $search_button_link ) ) { ?>
				<a href="<?php echo esc_url( $search_button_link ); ?>" class="gn-btn gn-btn-md gn-btn-outline-grey">
					<?php echo esc_html( $search_button_text ); ?>
				</a>
			<?php } ?>
		</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( $order_status_count['all'] ?? 0 ); ?></h3>
				<h6 class="gn-label"><?php echo esc_html__( 'Total orders', '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>
				</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-wallet" aria-hidden="true"></i>
					</span>
				</div>
				<h3 class="gn-value"><?php echo esc_html( $order_status_count['completed'] ?? 0 ); ?></h3>
				<h6 class="gn-label"><?php echo esc_html__( 'Completed 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__( 'Work in progress', 'gigneo' ); ?></h2>
		<div class="gn-listing-actions-wrapper">
			<?php if ( 'both' === $platform_type ) { ?>
				<ul id="gn-order-filters" class="gn-filters">
				<li class="gn-filter" data-gn-filter="gn-service">
					<a href="<?php echo esc_url( gigneo_dashboard_url( 'order', 'service-list' ) ); ?>" class="gn-title">
						<?php echo esc_html__( 'Services', 'gigneo' ); ?>
						<?php if ( $count_service ) { ?>
							<span class="gn-count"><?php echo esc_html( $count_service ); ?></span>
						<?php } ?>
					</a>
				</li>
				<li class="gn-filter gn-active" data-gn-filter="gn-job">
					<a href="<?php echo esc_url( gigneo_dashboard_url( 'order', 'job-list' ) ); ?>" class="gn-title">
						<?php echo esc_html__( 'Jobs', 'gigneo' ); ?>
						<?php if ( $count_job ) { ?>
							<span class="gn-count"><?php echo esc_html( $count_job ); ?></span>
						<?php } ?>
					</a>
				</li>
			</ul>
			<?php } ?>
			<div class="gn-actions">
				<div class="gn-search-wrapper">
					<form class="gn-search">
						<input type="hidden" name="source" value="order">
						<input type="hidden" name="ref" value="job-list">
						<label for="gn-job-input">
							<input id="gn-job-input" type="search" name="search" class="gn-search-input" value="<?php echo esc_html( $p_search ); ?>" placeholder="<?php echo esc_html__( '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_order',
		'post_status'    => array( 'publish', 'delayed', 'delivered', 'completed', 'cancelled', 'disputed' ),
		'posts_per_page' => $show_per_page,
		'paged'          => $current_page,
		'orderby'        => 'date',
		'order'          => 'DESC',
		'fields'         => 'ids',
	);

	$meta_query[] = array(
		'key'     => '_gigneo_pm_order_type',
		'value'   => 'job',
		'compare' => '=',
	);

	if ( ! empty( $p_search ) ) {
		$meta_query[] = array(
			'key'     => '_gigneo_pm_job_title',
			'value'   => $p_search,
			'compare' => 'LIKE',
		);
	}

	if ( 'freelancer' === $profile_type ) {
		$meta_query[] = array(
			'key'     => '_gigneo_pm_freelancer_profile_id',
			'value'   => $profile_id,
			'compare' => '=',
		);
	} elseif ( 'employer' === $profile_type ) {
		$meta_query[] = array(
			'key'     => '_gigneo_pm_profile_id',
			'value'   => $profile_id,
			'compare' => '=',
		);
	}

	// phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query
	$args['meta_query'] = $meta_query;

	$job_order_query = new WP_Query( $args );
	?>
	<div class="gn-body <?php echo esc_attr( ! $job_order_query->have_posts() ? ' gn-no-orders' : '' ); ?>">
		<?php if ( $job_order_query->have_posts() ) { ?>
			<div class="gn-table-wrapper">
				<table class="gn-order-items gn-table gn-table-borderless">
					<thead class="gn-order-item-header">
					<tr class="gn-order-item-row">
						<th class="gn-order-item-header-cell gn-details">
							<?php echo esc_html__( 'Order Details', 'gigneo' ); ?>
						</th>
						<th class="gn-order-item-header-cell gn-user">
							<?php echo esc_html( $profile_singular ); ?>
						</th>
						<th class="gn-order-item-header-cell gn-amount">
							<?php echo esc_html__( 'Amount', 'gigneo' ); ?>
						</th>
						<th class="gn-order-item-header-cell gn-status">
							<?php echo esc_html__( 'Status', 'gigneo' ); ?>
						</th>
						<th class="gn-order-item-header-cell gn-action">
							<?php echo esc_html__( 'Action', 'gigneo' ); ?>
						</th>
					</tr>
					</thead>
					<tbody class="gn-order-items-body">
					<?php
					while ( $job_order_query->have_posts() ) {
						$job_order_query->the_post();
						$order_id     = get_the_ID();
						$order_status = get_post_status( $order_id );
						//Job Card
						gigneo_load_template(
							'dashboard/order/job/parts/list/card',
							false,
							array(
								'order_id' => $order_id,
							)
						);
					}
					wp_reset_postdata();
					?>
					</tbody>
				</table>
			</div>
			<?php
		} else {
			gigneo_not_found( 'order_list' );
		}
		?>
	</div>
	<?php gigneo_pagination( $job_order_query, $current_page, 'gn-order-pagination' ); ?>
</div>	
