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

$p_id                   = $args['p_id'] ?? 0;
$author_id              = get_post_field( 'post_author', $p_id );
$author_profile_id      = gigneo_profile_id( $author_id, 'freelancer' );
$author_profile_name    = gigneo_profile_name( $author_profile_id );
$manual_approval_info   = gigneo_post_meta( $p_id, '_gigneo_pm_manual_approval_info' );
$manual_approval_status = $manual_approval_info['status'] ?? '';

$date_format = get_option( 'date_format' );
$time_format = get_option( 'time_format' );

$parent_category = null;
$sub_parent      = null;

$facets = wp_get_post_terms( $p_id, 'gigneo_facet' );

if ( ! empty( $facets ) && ! is_wp_error( $facets ) ) {
	foreach ( $facets as $facet ) {
		if ( 0 === (int) $facet->parent ) {
			$parent_category = $facet->name;
		} elseif ( $facet->parent > 0 ) {
			$parent = get_term( $facet->parent, 'gigneo_facet' );
			if ( $parent && 0 === $parent->parent ) {
				$sub_parent = $facet->name;
			}
		}
	}
}

$time_types = gigneo_delivery_time_types();

$plan_one_price   = gigneo_post_meta( $p_id, '_gigneo_pm_price_plan_one_price' );
$plan_three_price = gigneo_post_meta( $p_id, '_gigneo_pm_price_plan_three_price' );

$price = gigneo_price_format( $plan_one_price, true );
$price = ! empty( $plan_three_price ) ? $price . ' - ' . gigneo_price_format( $plan_three_price, true ) : $price;

$plan_one_delivery    = gigneo_post_meta( $p_id, '_gigneo_pm_price_plan_one_delivery_time' );
$plan_one_time_type   = gigneo_post_meta( $p_id, '_gigneo_pm_price_plan_one_delivery_time_type' );
$plan_three_delivery  = gigneo_post_meta( $p_id, '_gigneo_pm_price_plan_three_delivery_time' );
$plan_three_time_type = gigneo_post_meta( $p_id, '_gigneo_pm_price_plan_three_delivery_time_type' );
$plan_one_time_type   = $time_types[ $plan_one_time_type ] ?? '';
$plan_three_time_type = $time_types[ $plan_three_time_type ] ?? '';

$service_types       = array(
	'remote' => esc_html__( 'Remote', 'gigneo' ),
	'onsite' => esc_html__( 'On Site', 'gigneo' ),
);
$delivery_time       = "$plan_one_delivery $plan_one_time_type";
$service_type_option = gigneo_option( '_gigneo_ts_service_type', 'both' );
$service_type        = gigneo_post_meta( $p_id, '_gigneo_pm_type' );
$service_type        = 'both' !== $service_type_option ? $service_type_option : $service_type;
$service_type        = $service_types[ $service_type ] ?? '';

if ( ! empty( $plan_three_delivery ) && ! empty( $plan_three_time_type ) ) {
	if ( $plan_one_time_type === $plan_three_time_type ) {
		$delivery_time = "$plan_one_delivery - $plan_three_delivery $plan_one_time_type";
	} else {
		$delivery_time = "$plan_one_delivery $plan_one_time_type - $plan_three_delivery $plan_three_time_type";
	}
}

$description = get_post_meta( $p_id, '_gigneo_pm_description', true );
$description = wp_strip_all_tags( $description );
$description = wp_trim_words( $description, 20, '...' );

$pm_status = gigneo_post_meta( $p_id, '_gigneo_pm_status' );

$manual_approval_info = gigneo_post_meta( $p_id, '_gigneo_pm_manual_approval_info' );
$rejection_type       = $manual_approval_info['rejection']['type'] ?? '';
$rejection_detail     = $manual_approval_info['rejection']['detail'] ?? '';
?>
<div id="gn-service-review-modal-<?php echo esc_attr( $p_id ); ?>" class="gn-modal-wrapper gn-review-service-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__( 'Review service submission', 'gigneo' ); ?>
						<?php if ( ! empty( $manual_approval_info['status'] ) ) { ?>
							<span class="gn-status-tag <?php echo esc_attr( gigneo_status_class( $manual_approval_info['status'] ) ); ?>">
							<?php echo esc_html( gigneo_status_label( $manual_approval_info['status'] ) ); ?>
						</span>
						<?php } ?>
					</h4>
					<p class="gn-description"><?php echo esc_html__( 'You can approve or reject service here.', 'gigneo' ); ?></p>
					<button type="button" data-gn-modal-close="gn-service-review-modal-<?php echo esc_attr( $p_id ); ?>" class="gn-modal-close">
						<i class="hi hi-x-mark" aria-hidden="true"></i>
					</button>
				</div>
				<div class="gn-modal-body gn-custom-scrollbar">
					<?php if ( 'rejected' === $pm_status && ! empty( $rejection_type ) && ! empty( $rejection_detail ) ) { ?>
						<div class="gn-notices-wrapper">
							<div class="gn-notice gn-notice-danger">
								<div class="gn-notice-icon">
									<i class="hi hi-exclamation-circle" aria-hidden="true"></i>
								</div>
								<div class="gn-notice-info">
									<h3 class="gn-title">
									<?php
									printf(
											// translators: %s is rejection type, either soft or hard.
										esc_html__( '%s Rejection', 'gigneo' ),
										'soft' === $rejection_type ? esc_html__( 'Soft', 'gigneo' ) : esc_html__( 'Hard', 'gigneo' )
									);
									?>
										</h3>
									<p class="gn-description"><?php echo esc_html( $rejection_detail ); ?></p>
								</div>
							</div>
						</div>
					<?php } ?>
					<div class="gn-servic-review-section gn-identity-account-info">
						<div class="gn-heading-area">
							<h5 class="gn-title"><?php echo esc_html__( 'Service Details', 'gigneo' ); ?></h5>
							<?php if ( ! empty( $manual_approval_info['submit_at'] ) ) { ?>
								<span class="gn-date"><?php echo esc_html__( 'Submitted at', 'gigneo' ); ?> <?php echo esc_html( wp_date( "$date_format $time_format", strtotime( $manual_approval_info['submit_at'] ) ) ); ?></span>
							<?php } ?>
						</div>
						<div class="gn-service-info">
							<div class="gn-service-title">
								<h5 class="gn-title"><?php echo esc_html( get_the_title( $p_id ) ); ?></h5>
								<?php if ( ! empty( $parent_category ) && ! empty( $sub_parent ) ) { ?>
									<div class="gn-category">
										<?php echo esc_html( $parent_category ); ?> / <?php echo esc_html( $sub_parent ); ?>
									</div>
								<?php } ?>
							</div>
							<div class="gn-info-grid-wrap">
								<div class="gn-info-grid">
									<h5 class="gn-label"><?php echo esc_html( gigneo_freelancer_singular() ); ?></h5>
									<p class="gn-value"><?php echo esc_html( $author_profile_name ); ?></p>
								</div>
								<div class="gn-info-grid">
									<h5 class="gn-label"><?php echo esc_html__( 'Price', 'gigneo' ); ?></h5>
									<p class="gn-value"><?php echo esc_html( $price ); ?></p>
								</div>
								<div class="gn-info-grid">
									<h5 class="gn-label"><?php echo esc_html__( 'Delivery', 'gigneo' ); ?></h5>
									<p class="gn-value"><?php echo esc_html( $delivery_time ); ?></p>
								</div>
								<div class="gn-info-grid">
									<h5 class="gn-label"><?php echo esc_html__( 'Type', 'gigneo' ); ?></h5>
									<p class="gn-value"><?php echo esc_html( $service_type ); ?></p>
								</div>
							</div>
							<p class="gn-description"><?php echo esc_html( $description ); ?></p>
							<a href="<?php echo esc_url( get_permalink( $p_id ) ); ?>" target="_blank" class="gn-link gn-link-primary">
								<?php echo esc_html__( 'View full details', 'gigneo' ); ?>
								<i class="hi hi hi-arrow-up-right" aria-hidden="true"></i>
							</a>
						</div>
					</div>
				</div>
				<div class="gn-modal-footer">
					<button type="button" class="gn-btn gn-btn-outline-grey" data-gn-modal-close="gn-service-review-modal-<?php echo esc_attr( $p_id ); ?>"><?php echo esc_html__( 'Close', 'gigneo' ); ?></button>
					<?php if ( 'pending' === $manual_approval_status ) { ?>
						<button type="button" class="gn-btn gn-btn-outline-danger"
								data-gn-id="<?php echo esc_attr( $p_id ); ?>"
								data-gn-modal-close="gn-service-review-modal-<?php echo esc_attr( $p_id ); ?>"
								data-gn-modal-toggle="gn-service-reject-modal"><?php echo esc_html__( 'Reject', 'gigneo' ); ?></button>
					<?php } ?>
					<?php if ( 'approved' !== $manual_approval_status ) { ?>
						<button type="button" data-gn-id="<?php echo esc_attr( $p_id ); ?>"
								class="gn-btn gn-btn-primary gn-service-approve-btn"><?php echo esc_html__( 'Approve', 'gigneo' ); ?></button>
					<?php } ?>
				</div>
			</div>
		</div>
	</div>
</div>
