<?php

use Gigneo\Modules;

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

global $post;

if ( ! isset( $post->ID ) ) {
	return;
}

// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only parameters no nonce required.
$current_tab = ! empty( $_GET['tab'] ) ? sanitize_text_field( wp_unslash( $_GET['tab'] ) ) : '';

$order_id           = $post->ID;
$job_detail_url     = get_edit_post_link( $order_id );
$db_order_id        = gigneo_format_id( $order_id, 'order' );
$job_id             = gigneo_post_meta( $order_id, '_gigneo_pm_job_id' );
$job_title          = get_the_title( $job_id );
$order_status       = get_post_status( $order_id );
$date_format        = get_option( 'date_format' );
$time_format        = get_option( 'time_format' );
$order_status       = 'publish' === $order_status ? 'active' : $order_status;
$order_status_class = gigneo_status_class( $order_status );
$order_status_label = gigneo_status_label( $order_status );
$active_modules     = gigneo_active_modules();
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only parameters no nonce required.
$allowed_tabs = array( 'overview', 'activity', 'messages', 'attachments', 'payments' );

if ( in_array( 'dispute', $active_modules, true ) ) {
	$disputed = gigneo_post_meta( $order_id, '_gigneo_pm_dispute_activity' );
	if ( ! empty( $disputed ) ) {
		$allowed_tabs[] = 'dispute';
	}
}

$current_tab = ! empty( $current_tab ) && in_array( $current_tab, $allowed_tabs, true ) ? $current_tab : 'overview';

$freelancer_profile_id = gigneo_post_meta( $order_id, '_gigneo_pm_freelancer_profile_id' );
$profile_id            = gigneo_post_meta( $order_id, '_gigneo_pm_profile_id' );
$proposal_id           = gigneo_post_meta( $order_id, '_gigneo_pm_proposal_id' );
$job_intake_form       = gigneo_post_meta( $order_id, '_gigneo_pm_intake_form' );
$order_location        = gigneo_post_meta( $order_id, '_gigneo_pm_location' );
$order_location        = ! empty( $order_location ) ? $order_location : array();
$job_price             = gigneo_post_meta( $order_id, '_gigneo_pm_subtotal' );
$job_price             = ! empty( $job_price ) ? gigneo_price_format( $job_price, true ) : '';
$dispute_activity      = get_post_meta( $order_id, '_gigneo_pm_dispute_activity', true );
?>
<div class="gn-order-detail-wrapper gn-job-order-detail gn-service-order-detail">
	<div class="gn-header">
		<div class="gn-order-info">
			<div class="gn-content-info">
				<div class="gn-title-wrapper">
					<h2 class="gn-title"><?php echo esc_html( $job_title ); ?></h2>
					<span class="gn-status-tag <?php echo esc_attr( $order_status_class ); ?>"><?php echo esc_html( $order_status_label ); ?></span>
				</div>
				<ul class="gn-order-meta">
					<li>
						<span class="gn-order-id">
							<i class="hi hi-briefcase" aria-hidden="true"></i>
							<?php
							printf(
							// translators: %s is order ID.
								esc_html__( 'ID %s', 'gigneo' ),
								esc_html( gigneo_format_id( $order_id, 'order' ) )
							);
							?>
						</span>
					</li>
					<li>
						<a class="gn-link gn-link-primary"
							href="<?php echo esc_url( get_permalink( $job_id ) ); ?>" target="_blank">
							<?php echo esc_html__( 'View job detail', 'gigneo' ); ?>
							<i class="hi hi-arrow-up-right" aria-hidden="true"></i>
						</a>
					</li>
				</ul>
			</div>
		</div>
		<?php if ( 'active' === $order_status || 'delivered' === $order_status ) { ?>
			<div class="gn-actions">
				<div class="gn-dropdown-container">
					<button type="button"
							class="gn-order-dropdown-toggle gn-dropdown-toggle gn-btn gn-btn-outline-grey gn-btn-sm"
							data-gn-target="#gn-order-action-dropdown-<?php echo esc_attr( $order_id ); ?>">
						<i class="hi hi-ellipsis-horizontal" aria-hidden="true"></i>
					</button>
					<div class="gn-dropdown-content gn-enter-bottom"
						id="gn-order-action-dropdown-<?php echo esc_attr( $order_id ); ?>">
						<ul class="gn-dropdown-menu">
							<li class="gn-dropdown-item gn-complete">
					<span id="gn-job-order-complete"
							data-gn-modal-toggle="gn-job-order-complete-request"
							data-gn-order-id="<?php echo esc_attr( $order_id ); ?>"><?php echo esc_html__( 'Complete order', 'gigneo' ); ?></span>
							</li>
							<li class="gn-dropdown-item gn-cancel">
					<span id="gn-job-order-cancel-request-btn"
							data-gn-modal-toggle="gn-job-order-cancel-request"
							data-gn-order-id="<?php echo esc_attr( $order_id ); ?>"><?php echo esc_html__( 'Cancel order', 'gigneo' ); ?></span>
							</li>
						</ul>
					</div>
				</div>
			</div>
		<?php } ?>
	</div>
	<div class="gn-body">
		<nav class="gn-nav-tabs">
			<ul class="gn-nav-tab">
				<li class="gn-overview">
					<a href="<?php echo esc_url( $job_detail_url . '&tab=overview' ); ?>"
						class="gn-tab-list-item <?php echo esc_attr( 'overview' === $current_tab ? ' gn-active' : '' ); ?>">
						<?php echo esc_html__( 'Overview', 'gigneo' ); ?>
					</a>
				</li>
				<li class="gn-activity">
					<a href="<?php echo esc_url( $job_detail_url . '&tab=activity' ); ?>"
						class="gn-tab-list-item <?php echo esc_attr( 'activity' === $current_tab ? ' gn-active' : '' ); ?>">
						<?php echo esc_html__( 'Activity', 'gigneo' ); ?>
					</a>
				</li>
				<li class="gn-message">
					<a href="<?php echo esc_url( $job_detail_url . '&tab=messages' ); ?>"
						class="gn-tab-list-item <?php echo esc_attr( 'messages' === $current_tab ? ' gn-active' : '' ); ?>">
						<?php echo esc_html__( 'Messages', 'gigneo' ); ?>
					</a>
				</li>
				<li class="gn-attachments">
					<a href="<?php echo esc_url( $job_detail_url . '&tab=attachments' ); ?>"
						class="gn-tab-list-item <?php echo esc_attr( 'attachments' === $current_tab ? ' gn-active' : '' ); ?>">
						<?php echo esc_html__( 'Attachments', 'gigneo' ); ?>
					</a>
				</li>
				<li class="gn-payments">
					<a href="<?php echo esc_url( $job_detail_url . '&tab=payments' ); ?>"
						class="gn-tab-list-item <?php echo esc_attr( 'payments' === $current_tab ? ' gn-active' : '' ); ?>">
						<?php echo esc_html__( 'Payments', 'gigneo' ); ?>
					</a>
				</li>
				<?php if ( in_array( 'dispute', $active_modules, true ) && $dispute_activity ) { ?>
					<li class="gn-dispute">
						<a href="<?php echo esc_url( $job_detail_url . '&tab=dispute' ); ?>"
							class="gn-tab-list-item <?php echo esc_attr( 'dispute' === $current_tab ? ' gn-active' : '' ); ?>">
							<?php echo esc_html__( 'Dispute', 'gigneo' ); ?>
						</a>
					</li>
				<?php } ?>
				<?php if ( in_array( 'intake-form', $active_modules, true ) && ! empty( $job_intake_form ) ) { ?>
					<li class="gn-requirements">
						<a href="<?php echo esc_url( $job_detail_url . '&tab=requirements' ); ?>"
							class="gn-tab-list-item <?php echo esc_attr( 'requirements' === $current_tab ? ' gn-active' : '' ); ?>">
							<?php echo esc_html__( 'Screening', 'gigneo' ); ?>
						</a>
					</li>
				<?php } ?>
			</ul>
		</nav>
		<div class="gn-order-details">
			<div class="row g-4">
				<div class="col-md-12">
					<div class="gn-order-detail-main">
						<?php
						$args = array(
							'p_id'                  => $order_id,
							'job_id'                => $job_id,
							'freelancer_profile_id' => $freelancer_profile_id,
							'profile_id'            => $profile_id,
							'date_format'           => $date_format,
							'time_format'           => $time_format,
						);
						gigneo_load_template( "admin/parts/order/job/{$current_tab}", false, $args );
						?>
					</div>
				</div>
			</div>
		</div>
	</div>
</div>
<?php
gigneo_load_template( 'admin/parts/order/job/modal/complete-order.php', false, $args );
gigneo_load_template( 'admin/parts/order/job/modal/cancel-order.php', false, $args );
?>
