<?php

use Gigneo\Modules;

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

global $post;

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

$active_modules = gigneo_active_modules();
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only parameters no nonce required.
$current_tab  = ! empty( $_GET['tab'] ) ? sanitize_text_field( wp_unslash( $_GET['tab'] ) ) : '';
$allowed_tabs = array( 'overview', 'activity', 'messages', 'attachments', 'payments' );
if ( in_array( 'intake_form', $active_modules, true ) ) {
	$intake_form_type = gigneo_post_meta( $order_id, '_gigneo_pm_intake_form_type' );
	if ( ! empty( $intake_form_type ) ) {
		$allowed_tabs[] = 'requirements';
	}
}
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';

$service_detail_url   = get_edit_post_link( $order_id );
$db_order_id          = gigneo_format_id( $order_id, 'order' );
$service_id           = gigneo_post_meta( $order_id, '_gigneo_pm_service_id' );
$service_thumbnail_id = get_post_thumbnail_id( $service_id );
$service_title        = get_the_title( $service_id );
$categories           = get_the_terms( $service_id, 'gigneo_facet' );
$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 );

$freelancer_profile_id = gigneo_post_meta( $order_id, '_gigneo_pm_freelancer_profile_id' );
$profile_id            = gigneo_post_meta( $order_id, '_gigneo_pm_profile_id' );
$service_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();

$plan_data        = get_post_meta( $order_id, '_gigneo_pm_price_plan' );
$revisions        = ! empty( $plan_data['revisions'] ) ? $plan_data['revisions'] : esc_html__( 'Unlimited', 'gigneo' );
$service_price    = ! empty( $plan_data['price'] ) ? gigneo_price_format( $plan_data['price'], true ) : '';
$dispute_activity = get_post_meta( $order_id, '_gigneo_pm_dispute_activity', true );
$price_plan       = gigneo_post_meta( $order_id, '_gigneo_pm_price_plan' );
$price_plan       = ! empty( $price_plan['title'] ) ? $price_plan['title'] : '';
?>
<div class="gn-order-detail-wrapper gn-service-order-detail">
	<div class="gn-header">
		<div class="gn-order-info">
			<figure class="gn-thumbnail">
				<?php gigneo_attachment_image_src( $service_thumbnail_id ); ?>
			</figure>
			<div class="gn-content-info">
				<div class="gn-title-wrapper">
					<h2 class="gn-title"><?php echo esc_html( $service_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>
					<?php if ( ! empty( $price_plan ) ) { ?>
						<li>
				<span class="gn-service-plan">
				<i class="hi hi-shopping-bag" aria-hidden="true"></i>
							<?php
							printf(
									// translators: %s is price plan
								esc_html__( 'Package: %s', 'gigneo' ),
								esc_html( $price_plan )
							);
							?>
				</span>
						</li>
					<?php } ?>
					<li>
						<a class="gn-link gn-link-primary"
							href="<?php echo esc_url( get_permalink( $service_id ) ); ?>" target="_blank">
							<?php echo esc_html__( 'View service 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-service-order-complete"
							data-gn-modal-toggle="gn-service-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-service-order-cancel-request-btn"
							data-gn-modal-toggle="gn-service-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( $service_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( $service_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( $service_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>
				<?php if ( $service_intake_form ) { ?>
					<li class="gn-requirements">
						<a href="<?php echo esc_url( $service_detail_url . '&tab=requirements' ); ?>"
							class="gn-tab-list-item <?php echo esc_attr( 'requirements' === $current_tab ? ' gn-active' : '' ); ?>">
							<?php echo esc_html__( 'Requirements', 'gigneo' ); ?>
						</a>
					</li>
				<?php } ?>
				<li class="gn-attachments">
					<a href="<?php echo esc_url( $service_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( $service_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( $service_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 } ?>
			</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,
							'service_id'            => $service_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/service/{$current_tab}", false, $args );
						?>
					</div>
				</div>
			</div>
		</div>
	</div>
</div>
<?php
gigneo_load_template( 'admin/parts/order/service/modal/complete-order.php', false, $args );
gigneo_load_template( 'admin/parts/order/service/modal/cancel-order.php', false, $args );
?>
