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

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

if ( ! empty( $p_id ) && ! gigneo_validate_post_author( $p_id ) ) {
	gigneo_not_found( 'service_edit' );
	return;
}

$profile_id   = gigneo_active_profile_id();
$profile_type = gigneo_profile_type( $profile_id );

if ( 'freelancer' !== $profile_type || 'trash' === get_post_status( $p_id ) ) {
	gigneo_not_found( 'service_edit' );
	return;
}

$services_url           = gigneo_dashboard_url( 'service', 'list' );
$intake_form_for        = gigneo_option( '_gigneo_ts_intake_form_for', 'both' );
$manual_approval_info   = gigneo_post_meta( $p_id, '_gigneo_pm_manual_approval_info' );
$manual_approval_status = $manual_approval_info['status'] ?? '';
$rejection_type         = $manual_approval_info['rejection']['type'] ?? '';
$rejection_type         = 'soft' === $rejection_type ? esc_html__( 'Soft rejection', 'gigneo' ) : '';
$rejection_detail       = $manual_approval_info['rejection']['detail'] ?? '';

$template_args = array(
	'p_id'         => $p_id,
	'current_step' => $current_step,
	'redirect_url' => $services_url,
);
if ( isset( $manual_approval_status ) && 'rejected' === $manual_approval_status ) {
	?>
<div class="gn-job-edit-wrapper">
	<div class="gn-notices-wrapper">
		<div class="gn-notice gn-notice-danger">
			<div class="gn-notice-content">
				<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 echo esc_html( $rejection_type ); ?></h3>
					<p class="gn-description"><?php echo esc_html( $rejection_detail ); ?></p>
				</div>
			</div>
		</div>
	</div>
	<?php
}
?>
<div class="gn-service-edit-wrapper">
	<?php
	//Steps
	gigneo_load_template( 'dashboard/service/parts/edit/steps', true, $template_args );
	?>
	<div class="gn-service-edit-inner">
		<?php
		//Overview
		gigneo_load_template( 'dashboard/service/parts/edit/basic', true, $template_args );
		//Pricing
		gigneo_load_template( 'dashboard/service/parts/edit/pricing', true, $template_args );
		//Details
		gigneo_load_template( 'dashboard/service/parts/edit/details', true, $template_args );
		//Media
		gigneo_load_template( 'dashboard/service/parts/edit/media', true, $template_args );
		//Requirements
		if ( in_array( 'intake_form', gigneo_active_modules(), true ) && 'job' !== $intake_form_for ) {
			gigneo_load_template( 'dashboard/service/parts/edit/requirements', true, $template_args );
		}
		?>
	</div>
</div>