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

use Gigneo\Modules;
use Gigneo\Notifications;

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

$profile_id            = gigneo_active_profile_id();
$profile_type          = gigneo_profile_type();
$p_status              = get_post_type( $p_id );
$employer_profile_id   = get_post_meta( $p_id, '_gigneo_pm_profile_id', true );
$freelancer_profile_id = get_post_meta( $p_id, '_gigneo_pm_freelancer_profile_id', true );

if ( empty( $p_id ) || 'gigneo_order' !== $p_status || ( intval( $employer_profile_id ) !== $profile_id && intval( $freelancer_profile_id ) !== $profile_id ) ) {
	gigneo_load_template( 'dashboard/error' );
	return;
}

$allowed_tabs = array( 'overview', 'activity', 'messages', 'attachments', 'payments' );
$current_tab  = in_array( $current_tab, $allowed_tabs, true ) ? $current_tab : 'overview';

$template_args            = array();
$job_id                   = gigneo_post_meta( $p_id, '_gigneo_pm_job_id' );
$job_title                = gigneo_post_meta( $p_id, '_gigneo_pm_job_title' );
$categories               = get_the_terms( $job_id, 'gigneo_facet' );
$order_status             = get_post_status( $p_id );
$job_detail_url           = gigneo_dashboard_url( 'order', 'job-detail', array( 'p_id' => $p_id ) );
$order_listing_url        = gigneo_dashboard_url( 'order', 'job-list' );
$db_order_id              = gigneo_format_id( $p_id, 'order' );
$order_location           = gigneo_post_meta( $p_id, '_gigneo_pm_location' );
$order_location           = ! empty( $order_location ) ? $order_location : array();
$participant_profile_id   = 'employer' === $profile_type ? $freelancer_profile_id : $employer_profile_id;
$participant_profile_type = 'employer' === $profile_type ? 'freelancer' : 'employer';
$unread_count             = Notifications::get_unread_order_messages_count( $profile_id, $p_id );
$template_args            = array(
	'order_id'                 => $p_id,
	'profile_id'               => $profile_id,
	'profile_type'             => $profile_type,
	'participant_profile_id'   => $participant_profile_id,
	'participant_profile_type' => $participant_profile_type,
	'freelancer_profile_id'    => $freelancer_profile_id,
	'employer_profile_id'      => $employer_profile_id,
	'order_location'           => $order_location,
);

$order_status       = 'publish' === $order_status ? 'active' : $order_status;
$order_status_label = gigneo_status_label( $order_status );

$current_stage = gigneo_post_meta( $p_id, '_gigneo_pm_activity_stage' );
?>
<div class="gn-order-detail-wrapper gn-job-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( gigneo_status_class( $order_status ) ); ?>"><?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( $p_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>
		<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( $p_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( $p_id ); ?>">
					<ul class="gn-dropdown-menu">
						<?php if ( in_array( $order_status, array( 'active', 'delivered' ), true ) && 'employer' === $profile_type ) { ?>
							<li class="gn-dropdown-item gn-complete">
								<span id="gn-job-order-complete" data-gn-order-id="<?php echo esc_attr( $p_id ); ?>"><?php echo esc_html__( 'Complete order', 'gigneo' ); ?></span>
							</li>
							<?php
						}
						if ( in_array( $order_status, array( 'active', 'delivered' ), true ) ) {
							?>
							<li class="gn-dropdown-item gn-cancel">
								<span id="gn-job-order-cancel<?php echo wp_kses_post( 'employer' === $profile_type ? '-request-btn' : '-btn' ); ?>"<?php echo wp_kses_post( 'employer' === $profile_type ) ? ' data-gn-modal-toggle="gn-job-order-cancel-request"' : ''; ?> data-gn-order-id="<?php echo esc_attr( $p_id ); ?>"><?php echo esc_html__( 'Cancel order', 'gigneo' ); ?></span>
							</li>
							<?php
						}
						//Dispute
						if ( in_array( $order_status, array( 'active', 'delivered' ), true ) && in_array( 'dispute', gigneo_active_modules(), true ) ) {
							?>
							<li class="gn-dropdown-item gn-dispute">
								<span data-gn-modal-toggle="gn-job-order-dispute" data-gn-order-id="<?php echo esc_attr( $p_id ); ?>"><?php echo esc_html__( 'Raise dispute', 'gigneo' ); ?></span>
							</li>
						<?php } ?>
						<li class="gn-dropdown-item gn-support">
							<?php
							$support_url = apply_filters( 'gigneo_dashboard_order_support_url', '' );
							if ( empty( $support_url ) ) {
								$contact_page = gigneo_option( '_gigneo_ts_template_contact', '' );
								$support_url  = ! empty( $contact_page ) ? get_permalink( $contact_page ) : home_url();
							}
							?>
							<a href="<?php echo esc_url( $support_url ); ?>" target="_blank"><?php echo esc_html__( 'Contact support', 'gigneo' ); ?></a>
						</li>
					</ul>
				</div>
			</div>
		</div>
		<?php
		//Cancel Request Modal
		if ( 'employer' === $profile_type && in_array( $order_status, array( 'active', 'delivered', 'publish' ), true ) ) {
			?>
			<div id="gn-job-order-cancel-request" class="gn-modal-wrapper gn-order-cancel-modal">
				<div class="gn-modal-dialog">
					<div class="gn-modal-content">
						<div class="gn-modal-info">
							<div class="gn-modal-header">
								<h4 class="gn-title"><?php echo esc_html__( 'Cancel order request', 'gigneo' ); ?></h4>
								<p class="gn-description"><?php echo esc_html__( 'Submit your reason for cancelling this order.', 'gigneo' ); ?></p>
								<button type="button" data-gn-modal-close="gn-job-order-cancel-request" class="gn-modal-close">
									<i class="hi hi-x-mark" aria-hidden="true"></i>
								</button>
							</div>
							<form id="gn-job-order-cancel-request-form" class="gn-theme-form">
								<div class="gn-modal-body gn-custom-scrollbar">
									<fieldset class="gn-form-fields">
										<div class="row g-4">
											<div class="col-md-12">
												<div class="gn-form-field gn-select2">
													<label class="gn-required-field-label" for="gn-order-cancel-reason"><?php echo esc_html__( 'Reason', 'gigneo' ); ?></label>
													<select id="gn-order-cancel-reason" name="order_cancel_reason" placeholder="<?php echo esc_attr__( 'Select Reason', 'gigneo' ); ?>">
														<option value=""><?php echo esc_html__( 'Select Reason', 'gigneo' ); ?></option>
														<?php
														$cancel_reasons = gigneo_option(
															'_gigneo_ts_job_cancel_reasons',
															array(
																0 => array( 'reason' => esc_html__( 'Scheduling Conflict', 'gigneo' ) ),
																1 => array( 'reason' => esc_html__( 'Skills Mismatch', 'gigneo' ) ),
																2 => array( 'reason' => esc_html__( 'Security Issues', 'gigneo' ) ),
															)
														);

														if ( ! empty( $cancel_reasons ) ) {
															foreach ( $cancel_reasons as $cancel_reason ) {
																$reason = $cancel_reason['reason'] ?? '';
																if ( empty( $reason ) ) {
																	continue;
																}
																?>
																<option value="<?php echo esc_attr( $reason ); ?>"><?php echo esc_html( $reason ); ?></option>
																<?php
															}
														}
														?>
													</select>
												</div>
											</div>
											<div class="col-md-12">
												<div class="gn-form-field gn-wysiwyg-editor-two">
													<label for="gn-order-cancel-description" class="gn-required-field-label"><?php echo esc_html__( 'Description', 'gigneo' ); ?></label>
													<?php
													wp_editor(
														'',
														'gn-order-cancel-description',
														array(
															'textarea_name' => 'order_cancel_description',
															'media_buttons' => false,
															'quicktags' => false,
															'textarea_rows' => 7,
															'editor_class' => 'gn-wp-editor gn-required-field',
															'tinymce'      => array(
																'toolbar1' => 'bold italic bullist numlist link unlink',
																'toolbar2' => '',
																'plugins'  => 'lists link',
																'directionality' => is_rtl() ? 'rtl' : 'ltr',
																'forced_root_block' => false,
																'force_br_newlines' => true,
																'force_p_newlines' => false,
																'convert_newlines_to_brs' => true,
																'valid_elements' => 'strong/b,em/i,ul,ol,li,a[href|target],br,hr',
																'invalid_elements' => 'p,pre,h1,h2,h3,h4,h5,h6,img',
																'content_style' => 'body { font-size: 14px; background-color: transparent; font-family: var(--gigneo-primary-font,-apple-system,BlinkMacSystemFont,Segoe UI,Roboto,Helvetica Neue,Arial,Noto Sans,sans-serif,Apple Color Emoji,Segoe UI Emoji,Segoe UI Symbol,Noto Color Emoji) }',
															),
														)
													);
													?>
												</div>
											</div>
										</div>
									</fieldset>
								</div>
								<div class="gn-modal-footer">
									<input type="hidden" name="order_id" value="<?php echo esc_attr( $p_id ); ?>"/>
									<button type="button" class="gn-btn gn-btn-outline-grey" data-gn-modal-close="gn-job-order-cancel-request"><?php echo esc_html__( 'Close', 'gigneo' ); ?></button>
									<button type="submit" class="gn-btn gn-btn-primary"><?php echo esc_html__( 'Submit', 'gigneo' ); ?></button>
								</div>
							</form>
						</div>
					</div>
				</div>
			</div>
			<?php
		}
		//Raise Dispute Modal
		if ( in_array( $order_status, array( 'active', 'delivered', 'publish' ), true ) ) {
			apply_filters( 'gigneo_job_raise_dispute', $template_args );
		}
		?>
	</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-messages">
					<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' ); ?>
						<span class="gn-count" data-gn-count="<?php echo esc_attr( $unread_count ); ?>"><?php echo esc_html( $unread_count ); ?></span>
					</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>
			</ul>
		</nav>
		<div class="gn-order-details">
			<div class="row g-4">
				<div class="col-md-8">
					<div class="gn-order-detail-main">
						<?php
						//Tabs Content
						gigneo_load_template( "dashboard/order/job/parts/detail/$current_tab", false, $template_args );
						?>
					</div>
				</div>
				<div class="col-md-4">
					<div class="gn-order-detail-side">
						<?php
						//Delivery
						gigneo_load_template( 'dashboard/order/job/parts/detail/delivery', false, $template_args );

						//Tacking
						gigneo_load_template( 'dashboard/order/job/parts/detail/tracking', false, $template_args );

						//Participant Info
						gigneo_load_template( 'dashboard/order/job/parts/detail/participant-info', false, $template_args );

						//Private Note
						gigneo_load_template( 'dashboard/order/job/parts/detail/private-notes', false, $template_args );
						?>
					</div>
				</div>
			</div>
		</div>
	</div>
</div>
