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

// 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.

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

if ( ! empty( $p_id ) && ( ! gigneo_validate_post_author( $p_id ) || 'trash' === get_post_status( $p_id ) ) ) {
	gigneo_not_found( 'job_edit' );
	return;
}

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

$allowed_steps = array( 'basic', 'pricing', 'details', 'screening' );
$current_step  = in_array( $current_step, $allowed_steps, true ) ? $current_step : 'basic';

$jobs_url               = gigneo_dashboard_url( 'job', 'list' );
$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' => $jobs_url,
);
?>
<div class="gn-job-edit-wrapper">
	<?php if ( 'rejected' === $manual_approval_status ) { ?>
		<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
	}
	//Steps
	gigneo_load_template( 'dashboard/job/parts/edit/steps', true, $template_args );
	?>
	<div class="gn-job-edit-inner">
		<?php
		//Basic
		gigneo_load_template( 'dashboard/job/parts/edit/basic', true, $template_args );
		//pricing
		gigneo_load_template( 'dashboard/job/parts/edit/pricing', true, $template_args );
		//Details
		gigneo_load_template( 'dashboard/job/parts/edit/details', true, $template_args );
		//Screening
		gigneo_load_template( 'dashboard/job/parts/edit/screening', true, $template_args );
		?>
	</div>
</div>