<?php

use Gigneo\Modules;

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}
$p_id                      = $args['p_id'] ?? 0;
$current_step              = $args['current_step'] ?? 'basic';
$redirect_url              = $args['redirect_url'] ?? '';
$min_date                  = gigneo_datetime( gmdate( 'Y-m-d' ), 'Y-m-d' );
$timezone_module           = in_array( 'timezone', gigneo_active_modules(), true );
$country_list              = gigneo_allowed_countries();
$language_proficiency_list = gigneo_language_levels( false );
$field_types               = gigneo_intake_form_field_types();
$p_status                  = get_post_status( $p_id );
$screening_questions       = ! empty( $p_id ) ? gigneo_post_meta( $p_id, '_gigneo_pm_intake_form' ) : '';
$form_type                 = ! empty( $p_id ) ? gigneo_post_meta( $p_id, '_gigneo_pm_intake_form_type' ) : 'pre';
$pref_timezone             = ! empty( $p_id ) ? gigneo_post_meta( $p_id, '_gigneo_pm_preferred_timezone' ) : '';
$pref_location             = ! empty( $p_id ) ? gigneo_post_meta( $p_id, '_gigneo_pm_preferred_location' ) : '';
$experience_level          = ! empty( $p_id ) ? gigneo_post_meta( $p_id, '_gigneo_pm_experience_level' ) : '';
$language_proficiency      = ! empty( $p_id ) ? gigneo_post_meta( $p_id, '_gigneo_pm_language_proficiency' ) : '';
$preferred_language        = ! empty( $p_id ) ? gigneo_post_meta( $p_id, '_gigneo_pm_preferred_language' ) : '';
$deadline                  = ! empty( $p_id ) ? gigneo_post_meta( $p_id, '_gigneo_pm_application_deadline' ) : '';
$job_licenses              = ! empty( $p_id ) ? gigneo_post_meta( $p_id, '_gigneo_pm_job_licenses' ) : '';
$pm_status                 = ! empty( $p_id ) ? gigneo_post_meta( $p_id, '_gigneo_pm_status' ) : '';
$jobs_url                  = gigneo_dashboard_url( 'job', 'edit', array( 'p_id' => $p_id ) );
$languages_list            = array();
$languages                 = new WP_Term_Query(
	array(
		'taxonomy'   => 'gigneo_language',
		'hide_empty' => false,
	)
);
if ( ! empty( $languages->terms ) ) {
	foreach ( $languages->terms as $language ) {
		$languages_list[ $language->term_id ] = $language->name;
	}
}

$license_list = array();
$licenses     = new WP_Term_Query(
	array(
		'taxonomy'   => 'gigneo_license',
		'hide_empty' => false,
	)
);
if ( ! empty( $licenses->terms ) ) {
	foreach ( $licenses->terms as $license ) {
		$license_list[ $license->term_id ] = $license->name;
	}
}

$template_args = array(
	'p_id'         => $p_id,
	'current_step' => $current_step,
	'redirect_url' => $jobs_url,
);
?>

<div id="gn-job-edit-screening" class="gn-job-edit-step gn-job-edit-screening <?php echo esc_attr( 'screening' !== $current_step ? ' d-none' : '' ); ?>">
	<div class="row gy-4">
		<div class="col-lg-8 col-xl-9 order-2 order-lg-1">
			<form id="gn-job-form-screening" class="gn-main gn-job-edit-form">
				<div class="gn-edit-job-section gn-experience-level-section">
					<div class="gn-head">
						<div class="gn-heading-area">
							<h3 class="gn-title gn-required-field-label"><?php echo esc_html__( 'Experience level', 'gigneo' ); ?></h3>
							<p class="gn-description"><?php echo esc_html__( 'Choose experience level based on job complexity.', 'gigneo' ); ?></p>
						</div>
					</div>
					<div class="gn-body">
						<div class="gn-form-fields">
							<div class="gn-form-field gn-job-type">
								<div class="gn-radio">
									<input type="radio" name="experience_level" class="gn-required-field" id="entry"<?php echo esc_attr( empty( $experience_level ) || 'entry' === $experience_level ? ' checked="checked"' : '' ); ?> value="entry">
									<label for="entry">
										<i class="hi hi-briefcase"></i>
										<span><?php echo esc_html__( 'Entry level', 'gigneo' ); ?></span>
										<em><?php echo esc_html__( 'New to the field and eager to learn', 'gigneo' ); ?></em>
									</label>
								</div>
								<div class="gn-radio">
									<input type="radio" name="experience_level" class="gn-required-field" id="intermediate"<?php echo esc_attr( 'intermediate' === $experience_level ? ' checked="checked"' : '' ); ?>value="intermediate">
									<label for="intermediate">
										<i class="hi hi-star"></i>
										<span><?php echo esc_html__( 'Intermediate', 'gigneo' ); ?></span>
										<em><?php echo esc_html__( 'Has experience and proven skills', 'gigneo' ); ?></em>
									</label>
								</div>
								<div class="gn-radio">
									<input type="radio" name="experience_level" class="gn-required-field" id="expert"<?php echo esc_attr( 'expert' === $experience_level ? ' checked="checked"' : '' ); ?>value="expert">
									<label for="expert">
										<i class="hi hi-trophy"></i>
										<span><?php echo esc_html__( 'Expert', 'gigneo' ); ?></span>
										<em><?php echo esc_html__( 'Highly skilled with extensive experience', 'gigneo' ); ?></em>
									</label>
								</div>
							</div>
						</div>
					</div>
				</div>
				<?php if ( ! empty( $license_list ) ) { ?>
					<div class="gn-edit-job-section gn-certifications-section">
						<div class="gn-head">
							<div class="gn-heading-area">
								<h3 class="gn-title"><?php echo esc_html__( 'Required license', 'gigneo' ); ?></h3>
								<p class="gn-description"><?php echo esc_html__( 'Specify any required license to ensure qualified providers apply.', 'gigneo' ); ?></p>
							</div>
						</div>
						<div class="gn-body">
							<div class="gn-form-fields">
								<div class="row gy-4">
									<div class="col-md-12">
										<div class="gn-form-field gn-select2">
											<label for="gn-job-licenses"
													class="gn-field-label"><?php echo esc_html__( 'Select license', 'gigneo' ); ?></label>
											<select id="gn-job-licenses" name="job_licenses[]" multiple class="gn-field" placeholder="<?php echo esc_html__( 'Select license', 'gigneo' ); ?>">
												<?php
												foreach ( $license_list as $key => $license ) {
													?>
													<option value="<?php echo esc_attr( $key ); ?>"
															<?php echo in_array( (int) $key, array_map( 'intval', (array) $job_licenses ), true ) ? 'selected' : ''; ?>>
														<?php echo esc_html( $license ); ?>
													</option>
													<?php
												}
												?>
											</select>
										</div>
									</div>
								</div>
							</div>
						</div>
					</div>
				<?php } ?>
				<div class="gn-edit-job-section gn-location-section">
					<div class="gn-head">
						<div class="gn-heading-area">
							<h3 class="gn-title"><?php echo esc_html__( 'Preferences', 'gigneo' ); ?></h3>
							<p class="gn-description"><?php echo esc_html__( 'Set preferences to match with the most suitable providers for your job.', 'gigneo' ); ?></p>
						</div>
					</div>
					<div class="gn-body">
						<div class="gn-form-fields">
							<div class="row gy-4">
								<div class="<?php echo $timezone_module ? 'col-md-12' : 'col-md-6'; ?>">
									<div class="gn-form-field gn-select2">
										<label for="gn-preferred-location"
												class="gn-field-label"><?php echo esc_html__( 'Preferred countries', 'gigneo' ); ?></label>
										<select id="gn-preferred-location" name="preferred_location" class="gn-field" multiple placeholder="<?php echo esc_html__( 'Select country', 'gigneo' ); ?>">
											<?php foreach ( $country_list as $key => $country ) { ?>
												<option <?php echo in_array( $key, (array) $pref_location, true ) ? 'selected' : ''; ?> value="<?php echo esc_attr( $key ); ?>"><?php echo esc_html( $country ); ?></option>
											<?php } ?>
										</select>
									</div>
								</div>
								<div class="col-md-6">
									<div class="gn-form-field gn-datepicker">
										<label class="gn-field-label" for="gn-application-deadline"><?php echo esc_html__( 'Application deadline', 'gigneo' ); ?></label>
										<input id="gn-application-deadline" value="<?php echo esc_attr( $deadline ); ?>" class="flatpickr-input gn-field" data-single="true" data-min-date="<?php echo esc_attr( $min_date ); ?>" type="hidden" name="application_deadline" autocomplete="off"  placeholder="<?php echo esc_attr__( 'Select date (optional)', 'gigneo' ); ?>">
									</div>
								</div>
								<div class="col-md-6">
									<div class="gn-form-field gn-select2">
										<label for="gn-preferred-language"
												class="gn-field-label"><?php echo esc_html__( 'Preferred language', 'gigneo' ); ?></label>
										<select id="gn-preferred-language" name="preferred_language" class="gn-field" placeholder="<?php echo esc_html__( 'Select language', 'gigneo' ); ?>">
											<option value=""><?php echo esc_html__( 'Any language', 'gigneo' ); ?></option>
											<?php foreach ( $languages_list as $key => $language ) { ?>
												<option <?php selected( $preferred_language, $key ) ?? ''; ?> value="<?php echo esc_attr( $key ); ?>"><?php echo esc_html( $language ); ?></option>
											<?php } ?>
										</select>
									</div>
								</div>
								<div class="col-md-6">
									<div class="gn-form-field gn-select2">
										<label for="gn-language-proficiency"
												class="gn-field-label"><?php echo esc_html__( 'Language proficiency', 'gigneo' ); ?></label>
										<select id="gn-language-proficiency" name="language_proficiency" class="gn-field" placeholder="<?php echo esc_html__( 'Select level', 'gigneo' ); ?>">
											<option value=""><?php echo esc_html__( 'Any proficiency level', 'gigneo' ); ?></option>
											<?php foreach ( $language_proficiency_list as $key => $lang_proficiency ) { ?>
												<option <?php selected( $language_proficiency, $key ) ?? ''; ?> value="<?php echo esc_attr( $key ); ?>"><?php echo esc_html( $lang_proficiency ); ?></option>
											<?php } ?>
										</select>
									</div>
								</div>
								<?php if ( $timezone_module ) { ?>
								<div class="col-md-6">
									<div class="gn-form-field gn-select2">
										<label for="gn-preferred-timezone"
												class="gn-field-label"><?php echo esc_html__( 'Preferred timezone', 'gigneo' ); ?></label>
										<select id="gn-preferred-timezone" name="preferred_timezone" class="gn-field" placeholder="<?php echo esc_html__( 'Select timezone', 'gigneo' ); ?>">
											<option value=""><?php echo esc_html__( 'Any timezone', 'gigneo' ); ?></option>
											<?php echo wp_timezone_choice( $pref_timezone ); ?>
										</select>
									</div>
								</div>
								<?php } ?>
							</div>
						</div>
					</div>
				</div>
				<?php
				if ( in_array( 'intake_form', gigneo_active_modules(), true ) ) {
					?>
					<div id="gn-screening-questions" class="gn-edit-job-section gn-screening-questions-section">
						<div class="gn-head">
							<div class="gn-heading-area">
								<h3 class="gn-title"><?php echo esc_html__( 'Screening questions', 'gigneo' ); ?></h3>
								<p class="gn-description"><?php echo esc_html__( 'Ask questions to screen candidates and find the best fit for your job.', 'gigneo' ); ?></p>
							</div>
							<div class="gn-actions">
								<button type="button" class="gn-link gn-link-primary gn-add-screening-question">
									<i class="hi hi-plus" aria-hidden="true"></i>
									<?php echo esc_html__( 'Add question', 'gigneo' ); ?>
								</button>
							</div>
						</div>
						<div class="gn-body">
							<div class="gn-screening-questions">
								<?php
								if ( ! empty( $screening_questions ) ) {
									foreach ( $screening_questions as $index => $screening_question ) {
										$sq_type     = $screening_question['type'] ?? '';
										$sq_required = $screening_question['required'] ?? false;
										$sq_required = ( ! empty( $screening_question['required'] ) && 'yes' === $screening_question['required'] ) ? 'checked' : '';
										$sq_options  = $screening_question['options'] ?? array();
										$is_multi    = ( ! empty( $screening_question['multi'] ) && 'yes' === $screening_question['multi'] ) ? 'checked' : '';
										?>
										<div id="gn-screening-question-<?php echo esc_attr( $index ); ?>"
											class="gn-screening-question">
											<div class="gn-form-fields">
												<div class="gn-form-field">
													<label class="gn-required-field-label"
															for="gn-screening-question-title-<?php echo esc_attr( $index ); ?>"><?php echo esc_html__( 'Question', 'gigneo' ); ?> </label>
													<button type="button" class="gn-btn gn-remove-screening-question"
															data-gn-screening-question-id="<?php echo esc_attr( $index ); ?>">
														<i class="hi hi-trash" aria-hidden="true"></i>
														<?php echo esc_html__( 'Remove', 'gigneo' ); ?>
													</button>
													<textarea id="gn-screening-question-title-<?php echo esc_attr( $index ); ?>" class="gn-required-field"
															name="screening_questions[<?php echo esc_attr( $index ); ?>][title]"><?php echo esc_html( $screening_question['title'] ?? '' ); ?></textarea>
												</div>
												<div class="gn-form-field gn-select2">
													<label class="gn-required-field-label" for="gn-screening-question-answer-type-<?php echo esc_attr( $index ); ?>"><?php echo esc_html__( 'Answer type', 'gigneo' ); ?> </label>
													<div class="gn-field-stack">
													<span class="gn-select">
														<select id="gn-screening-question-answer-type-<?php echo esc_attr( $index ); ?>" class="gn-screening-question-answer-type"
																name="screening_questions[<?php echo esc_attr( $index ); ?>][type]">
															<?php foreach ( $field_types as $fi => $field ) { ?>
																<option value="<?php echo esc_attr( $fi ); ?>"<?php echo esc_attr( $fi === $sq_type ? ' selected' : '' ); ?>><?php echo esc_html( $field ); ?></option>
															<?php } ?>
														</select>
													</span>
														<div class="gn-checkbox">
															<input type="checkbox" id="gn-screening-question-required-<?php echo esc_attr( $index ); ?>"
																	name="screening_questions[<?php echo esc_attr( $index ); ?>][required]"
																	value="yes"<?php echo esc_attr( $sq_required ? ' checked' : '' ); ?>>
															<label for="gn-screening-question-required-<?php echo esc_attr( $index ); ?>"><span><?php echo esc_html__( 'Required', 'gigneo' ); ?></span></label>
														</div>
													</div>
													<div class="gn-screening-question-choices <?php echo 'choice' !== $sq_type ? esc_attr( 'd-none' ) : ''; ?>">
														<?php
														if ( 'choice' === $sq_type && ! empty( $sq_options ) ) {
															foreach ( $sq_options as $sqo => $option ) {
																?>
																<div id="gn-screening-question-choice-<?php echo esc_attr( $index ); ?>-<?php echo esc_attr( $sqo ); ?>"
																	class="gn-screening-question-choice">
																	<label>
																		<input class="gn-required-field" type="text" name="screening_questions[<?php echo esc_attr( $index ); ?>][options][<?php echo esc_attr( $sqo ); ?>][label]"
																				value="<?php echo esc_attr( $option['label'] ); ?>">
																	</label>
																	<button type="button" class="gn-remove-screening-question-choice"
																			data-gn-screening-question-choice-id="gn-screening-question-choice-<?php echo esc_attr( $index ); ?>-<?php echo esc_attr( $sqo ); ?>">
																		<i class="hi hi-trash" aria-hidden="true"></i>
																	</button>
																</div>
																<?php
															}
														} else {
															?>
															<div id="gn-screening-question-choice-<?php echo esc_attr( $index ); ?>-0"
																class="gn-screening-question-choice">
																<label>
																	<input class="gn-not-required-field" type="text" name="screening_questions[<?php echo esc_attr( $index ); ?>][options][0][label]">
																</label>
																<button type="button"
																		class="gn-remove-screening-question-choice"
																		data-gn-screening-question-choice-id="gn-screening-question-choice-<?php echo esc_attr( $index ); ?>-0">
																	<i class="hi hi-trash" aria-hidden="true"></i>
																</button>
															</div>
															<?php } ?>
														<div class="gn-screening-question-choice-actions">
															<div class="gn-checkbox">
																<input type="checkbox" <?php echo esc_attr( $is_multi ); ?>
																		id="gn-screening-question-choice-multi-<?php echo esc_attr( $index ); ?>"
																		name="screening_questions[<?php echo esc_attr( $index ); ?>][multi]"
																		value="yes">
																<label for="gn-screening-question-choice-multi-<?php echo esc_attr( $index ); ?>"><span><?php echo esc_html__( 'Allow more than 1 answer', 'gigneo' ); ?></span></label>
															</div>
															<button type="button" class="gn-link gn-add-screening-question-choice"
																	data-gn-field-id="<?php echo esc_attr( $index ); ?>"><?php echo esc_html__( 'Add another choice', 'gigneo' ); ?></button>
														</div>
													</div>
												</div>
											</div>
										</div>
										<?php
									}
								}
								?>
							</div>
						</div>
					</div>
					<?php
				}
				?>
				<div class="gn-edit-job-footer gn-actions">
					<button type="button" data-gn-step="screening" data-gn-type="draft" data-gn-redirect-url="<?php echo esc_url( $redirect_url ); ?>"
							class="gn-btn gn-btn-outline-grey gn-job-save"><?php echo esc_html__( 'Save as draft', 'gigneo' ); ?></button>
					<button type="button" data-gn-step="screening" data-gn-prev-step="details" data-gn-next-step="screening"
							class="gn-btn gn-btn-outline-grey gn-job-back">
						<?php echo esc_html__( 'Back', 'gigneo' ); ?>
					</button>
					<button type="button" data-gn-step="screening" data-gn-prev-step="details" data-gn-next-step="screening" data-gn-type="<?php echo esc_attr( ( ( 'publish' === $pm_status && 'publish' !== $p_status ) || ( empty( $pm_status ) || 'rejected' === $pm_status ) ) ? 'publish' : 'redirect' ); ?>" data-gn-redirect-url="<?php echo esc_url( $redirect_url ); ?>" class="gn-btn gn-btn-primary gn-job-save">
						<?php echo esc_html__( 'Save', 'gigneo' ); ?>
						</button>
				</div>
			</form>
		</div>
		<div class="col-lg-4 col-xl-3 order-1 order-lg-2">
			<div class="gn-aside">
				<div class="gn-aside-content">
					<h4 class="gn-title"><?php echo esc_html__( 'Screening details:', 'gigneo' ); ?></h4>
					<div class="gn-aside-body">
						<ul class="gn-list">
							<li><p class="gn-description"><?php echo esc_html__( 'Select the experience level that fits your job’s complexity to ensure the right expertise.', 'gigneo' ); ?></p></li>
							<?php if ( ! empty( $license_list ) ) { ?>
							<li><p class="gn-description"><?php echo esc_html__( 'Specify any required licenses or certifications to attract qualified and compliant providers.', 'gigneo' ); ?></p></li>
							<?php } ?>
							<li><p class="gn-description"><?php echo esc_html__( 'Set your job preferences to match with providers who meet requirements.', 'gigneo' ); ?></p></li>
						</ul>
					</div>
				</div>
			</div>
		</div>
	</div>
</div>
<?php
gigneo_load_template( 'dashboard/job/parts/edit/publish', true, $args );
if ( in_array( 'intake_form', gigneo_active_modules(), true ) ) {
	?>
<script type="text/html" id="tmpl-gn-screening-question">
	<div id="gn-screening-question-{{data.index}}" class="gn-screening-question">
		<div class="gn-form-fields">
			<div class="gn-form-field">
				<label class="gn-required-field-label" for="gn-screening-question-title-{{data.index}}"><?php echo esc_html__( 'Question', 'gigneo' ); ?> </label>
				<button type="button" class="gn-btn gn-remove-screening-question" data-gn-screening-question-id="{{data.index}}"><i class="hi hi-trash" aria-hidden="true"></i>
					<?php echo esc_html__( 'Remove', 'gigneo' ); ?>
				</button>
				<textarea id="gn-screening-question-title-{{data.index}}" class="gn-required-field" name="screening_questions[{{data.index}}][title]"></textarea>
			</div>
			<div class="gn-form-field gn-select2">
				<label class="gn-required-field-label" for="gn-screening-question-answer-type-{{data.index}}"><?php echo esc_html__( 'Answer Type', 'gigneo' ); ?> </label>
				<div class="gn-field-stack">
					<span class="gn-select">
						<select id="gn-screening-question-answer-type-{{data.index}}" class="gn-screening-question-answer-type" name="screening_questions[{{data.index}}][type]">
							<?php foreach ( $field_types as $fi => $field ) { ?>
								<option value="<?php echo esc_attr( $fi ); ?>"><?php echo esc_html( $field ); ?></option>
							<?php } ?>
						</select>
					</span>
					<div class="gn-checkbox">
						<input type="checkbox" id="gn-screening-question-required-{{data.index}}" name="screening_questions[{{data.index}}][required]" value="yes">
						<label for="gn-screening-question-required-{{data.index}}"><span><?php echo esc_html__( 'Required', 'gigneo' ); ?></span></label>
					</div>
				</div>
				<div class="gn-screening-question-choices d-none">
					<div id="gn-screening-question-choice-{{data.index}}-0" class="gn-screening-question-choice">
						<label>
							<input class="gn-not-required-field" type="text" name="screening_questions[{{data.index}}][options][0][label]">
						</label>
						<button type="button" class="gn-remove-screening-question-choice" data-gn-screening-question-choice-id="gn-screening-question-choice-{{data.index}}-0">
							<i class="hi hi-trash" aria-hidden="true"></i>
						</button>
					</div>
					<div class="gn-screening-question-choice-actions">
						<div class="gn-checkbox">
							<input type="checkbox" id="gn-screening-question-choice-multi-{{data.index}}" name="screening_questions[{{data.index}}][multi]" value="yes">
							<label for="gn-screening-question-choice-multi-{{data.index}}"><span><?php echo esc_html__( 'Allow more than 1 answer', 'gigneo' ); ?></span></label>
						</div>
						<button type="button" class="gn-link gn-add-screening-question-choice" data-gn-field-id="{{data.index}}"><?php echo esc_html__( 'Add another choice', 'gigneo' ); ?></button>
					</div>
				</div>
			</div>
		</div>
	</div>
</script>
<script type="text/html" id="tmpl-gn-screening-question-choice">
	<div id="gn-screening-question-choice-{{data.field_id}}-{{data.choice_id}}" class="gn-screening-question-choice">
		<label>
			<input class="gn-not-required-field" type="text" name="screening_questions[{{data.field_id}}][options][{{data.choice_id}}][label]">
		</label>
		<button type="button" class="gn-remove-screening-question-choice" data-gn-screening-question-choice-id="gn-screening-question-choice-{{data.field_id}}-{{data.choice_id}}">
			<i class="hi hi-trash" aria-hidden="true"></i>
		</button>
	</div>
</script>
<?php } ?>