<?php
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'] ?? '';
$max_skills           = gigneo_option( '_gigneo_ts_job_max_skills', '5' );
$max_specific_types   = gigneo_option( '_gigneo_ts_job_max_specific_types', '5' );
$title_prefix         = gigneo_option( '_gigneo_ts_job_title_prefix' );
$category_list        = gigneo_term_list(
	array(
		'taxonomy' => 'gigneo_facet',
		'parent'   => 0,
	),
	false
);
$skill_list           = gigneo_term_list( array( 'taxonomy' => 'gigneo_skill' ), false );
$job_types            = gigneo_option( '_gigneo_ts_job_type', 'both' );
$job_title            = '';
$selected_category    = '';
$selected_subcategory = '';
$list_subcategories   = array();
$saved_specific_types = array();
$list_specific_types  = array();
$selected_tags        = array();

if ( ! empty( $p_id ) ) {

	//Title
	$job_title = get_the_title( $p_id );
	if ( ! empty( $title_prefix ) && str_starts_with( $job_title, $title_prefix ) ) {
		$job_title = trim( substr( $job_title, strlen( $title_prefix ) ) );
	}

	$job_type     = gigneo_post_meta( $p_id, '_gigneo_pm_type' );
	$facets       = get_the_terms( $p_id, 'gigneo_facet' );
	$skills       = get_the_terms( $p_id, 'gigneo_skill' );
	$parent_id    = 0;
	$sub_cat_term = null;

	// Single category and subcategory from gigneo_facet.
	if ( ! empty( $facets ) && ! is_wp_error( $facets ) ) {
		foreach ( $facets as $facet_term ) {
			if ( 0 === $facet_term->parent ) {
				$selected_category = $facet_term->slug;
				$parent_id         = $facet_term->term_id;
				break;
			}
		}
		foreach ( $facets as $facet_term ) {
			if ( $facet_term->parent === $parent_id ) {
				$selected_subcategory = $facet_term->slug;
				$sub_cat_term         = $facet_term;
				break;
			}
		}
		// Specific types: grandchildren of category.
		foreach ( $facets as $cat_term ) {
			if ( $sub_cat_term && $cat_term->parent === $sub_cat_term->term_id ) {
				$saved_specific_types[ $cat_term->slug ] = $cat_term->slug;
			}
		}
	}

	if ( ! empty( $selected_category ) && $parent_id > 0 ) {
		$list_subcategories = gigneo_term_list(
			array(
				'taxonomy' => 'gigneo_facet',
				'parent'   => $parent_id,
			),
			false
		);
	}
	if ( ! empty( $selected_subcategory ) && $sub_cat_term ) {
		$list_specific_types = gigneo_term_list(
			array(
				'taxonomy' => 'gigneo_facet',
				'parent'   => $sub_cat_term->term_id,
			),
			false
		);
	}

	$selected_tags = ! empty( $skills ) && ! is_wp_error( $skills ) ? array_map(
		function ( $skill ) {
			return $skill->slug;
		},
		$skills
	) : array();
}

$job_specific_types_disabled = 'disabled';
$job_subcategory_disabled    = 'disabled';

if ( ! empty( $list_specific_types ) ) {
	$job_specific_types_disabled = '';
}
if ( ! empty( $list_subcategories ) ) {
	$job_subcategory_disabled = '';
}

$jobs_url     = gigneo_dashboard_url( 'job', 'list' );
$job_type     = ! empty( $job_type ) ? $job_type : gigneo_option( '_gigneo_ts_default_job_type', 'remote' );
$active_class = empty( $current_step ) || 'basic' === $current_step ? '' : 'd-none';
?>
<div id="gn-job-edit-basic" class="gn-job-edit-step gn-job-edit-basic <?php echo esc_attr( $active_class ); ?>">
	<div class="row gy-4">
		<div class="col-lg-8 col-xl-9 order-2 order-lg-1">
			<form id="gn-job-form-basic" class="gn-main gn-job-edit-form">
				<div class="gn-edit-job-section">
					<div class="gn-body">
						<div class="gn-form-fields">
							<div class="gn-form-field">
								<label class="gn-required-field-label"
										for="gn-job-title"><?php echo esc_html__( 'Job title', 'gigneo' ); ?></label>
								<div class="gn-edit-job-textarea">
									<?php if ( ! empty( $title_prefix ) ) { ?>
										<span class="gn-title-prefix"><?php echo esc_html( $title_prefix ); ?></span>
									<?php } ?>
									<textarea id="gn-job-title" name="job_title" class="gn-required-field gn-textarea-counter" data-max-length="85" rows="3" ><?php echo esc_attr( $job_title ); ?></textarea>
								</div>
							</div>
						</div>
					</div>
				</div>
				<div class="gn-edit-job-section">
					<div class="gn-body">
						<div class="gn-form-fields gn-category-fields">
							<div class="gn-form-field gn-category gn-select2">
								<label for="gn-job-category" class="gn-required-field-label"><?php echo esc_html__( 'Category', 'gigneo' ); ?></label>
								<select id="gn-job-category" name="job_category" class="gn-required-field"
										placeholder="<?php echo esc_html__( 'Select a category', 'gigneo' ); ?>">
									<option value=""><?php echo esc_html__( 'Select category', 'gigneo' ); ?></option>
									<?php
									if ( ! empty( $category_list ) && is_array( $category_list ) ) {
										foreach ( $category_list as $cat_key => $category ) {
											?>
											<option value="<?php echo esc_attr( $cat_key ); ?>" <?php echo esc_attr( $cat_key === $selected_category ? ' selected' : '' ); ?>><?php echo esc_html( $category ); ?></option>
											<?php
										}
									}
									?>
								</select>
							</div>
							<div class="gn-form-field gn-subcategory gn-select2">
								<label for="gn-job-subcategory"
										class="gn-required-field-label"><?php echo esc_html__( 'Subcategory', 'gigneo' ); ?></label>
								<select id="gn-job-subcategory" <?php echo esc_attr( $job_subcategory_disabled ); ?>
										name="job_subcategory" class="gn-required-field"
										placeholder="<?php echo esc_html__( 'Narrow down category', 'gigneo' ); ?>">
									<?php
									if ( $list_subcategories && is_array( $list_subcategories ) ) {
										?>
										<option value=""><?php echo esc_html__( 'Select sub category', 'gigneo' ); ?></option>
										<?php
										foreach ( $list_subcategories as $sub_cat => $cat_item ) {
											?>
											<option value="<?php echo esc_attr( $sub_cat ); ?>" <?php selected( $sub_cat, $selected_subcategory, true ); ?>><?php echo esc_html( $cat_item ); ?></option>
											<?php
										}
									}
									?>
								</select>
							</div>
							<div class="gn-form-field gn-specific-types gn-select2">
								<label for="gn-job-specific-types"><?php echo esc_html__( 'Specific types', 'gigneo' ); ?></label>
								<select id="gn-job-specific-types" <?php echo esc_attr( $job_specific_types_disabled ); ?>
										name="job_specific_types[]" multiple
										data-maximum-selection-length="<?php echo esc_attr( $max_specific_types ); ?>"
										placeholder="<?php echo esc_html__( 'Choose specific types', 'gigneo' ); ?>">
									<?php
									if ( ! empty( $list_specific_types ) && is_array( $list_specific_types ) ) {
										foreach ( $list_specific_types as $specific_type_key => $specific_type ) {
											?>
											<option value="<?php echo esc_attr( $specific_type_key ); ?>" <?php selected( in_array( $specific_type_key, $saved_specific_types, true ) ); ?>><?php echo esc_html( $specific_type ); ?></option>
											<?php
										}
									}
									?>
								</select>
								<div class="gn-field-notice">
									<?php
									printf(
										// translators: %s: Maximum number of specific types
										esc_html__( 'Maximum %s Allowed', 'gigneo' ),
										esc_html( $max_specific_types )
									);
									?>
								</div>
							</div>
						</div>
					</div>
				</div>
				<div class="gn-edit-job-section">
					<div class="gn-body">
						<div class="gn-form-fields">
							<div class="gn-form-field">
								<div class="gn-form-field gn-select2">
									<label for="gn-job-skills"
											class="gn-required-field-label"><?php echo esc_html__( 'Skills', 'gigneo' ); ?></label>
									<select id="gn-job-skills" name="job_skills[]" multiple
											data-maximum-selection-length="<?php echo esc_attr( $max_skills ); ?>"
											class="gn-required-field"
											placeholder="<?php echo esc_html__( 'Select search skills', 'gigneo' ); ?>">
										<option value=""><?php echo esc_html__( 'Select Skills', 'gigneo' ); ?></option>
										<?php
										if ( ! empty( $skill_list ) && is_array( $skill_list ) ) {
											foreach ( $skill_list as $skill_key => $skill_item ) {
												?>
												<option value="<?php echo esc_attr( $skill_key ); ?>" <?php selected( in_array( $skill_key, $selected_tags, true ) ); ?>><?php echo esc_html( $skill_item ); ?></option>
												<?php
											}
										}
										?>
									</select>
									<div class="gn-field-notice">
										<?php
										printf(
											// translators: %s: Maximum number of skills
											esc_html__( 'Maximum %s Allowed', 'gigneo' ),
											esc_html( $max_skills )
										);
										?>
									</div>
								</div>
							</div>
						</div>
					</div>
				</div>
				<?php if ( 'both' === $job_types ) { ?>
					<div class="gn-edit-job-section">
						<div class="gn-head">
							<div class="gn-heading-area">
								<h3 class="gn-title"><?php echo esc_html__( 'Job type', 'gigneo' ); ?></h3>
								<p class="gn-description"><?php echo esc_html__( 'Specifies the job is delivered remotely or onsite.', '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="job_type" class="gn-required-field" id="remote"<?php echo esc_attr( 'remote' === $job_type ? ' checked="checked"' : '' ); ?> value="remote">
										<label for="remote">
											<i class="hi hi-globe-alt"></i>
											<span><?php echo esc_html__( 'Remote', 'gigneo' ); ?></span>
											<em><?php echo esc_html__( 'Work from anywhere.', 'gigneo' ); ?></em>
										</label>
									</div>
									<div class="gn-radio">
										<input type="radio" name="job_type" class="gn-required-field" id="onsite"<?php echo esc_attr( 'onsite' === $job_type ? ' checked="checked"' : '' ); ?> value="onsite">
										<label for="onsite">
											<i class="hi hi-map-pin"></i>
											<span><?php echo esc_html__( 'Onsite', 'gigneo' ); ?></span>
											<em><?php echo esc_html__( 'Work at specific location.', 'gigneo' ); ?></em>
										</label>
									</div>
								</div>
							</div>
						</div>
					</div>
				<?php } ?>
				<div class="gn-edit-job-footer gn-actions">
					<button type="button" data-gn-step="basic" data-gn-type="draft"
							data-gn-redirect-url="<?php echo esc_url( $jobs_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="basic" data-gn-type="continue" data-gn-next-step="pricing"
							class="gn-btn gn-btn-primary gn-job-save">
						<?php echo esc_html__( 'Continue', '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__( 'Overview setup:', 'gigneo' ); ?></h4>
					<div class="gn-aside-body">
						<ul class="gn-list">
							<li><p class="gn-description"><?php echo esc_html__( 'Create a clear and engaging title that captures attention.', 'gigneo' ); ?></p>
							</li><li><p class="gn-description"><?php echo esc_html__( 'Select a relevant category and add tags to improve discoverability and attract the right clients.', 'gigneo' ); ?></p></li>
							<?php if ( 'both' === $job_types ) { ?>
								<li><p class="gn-description"><?php echo esc_html__( 'Select whether your job is offered remotely or onsite.', 'gigneo' ); ?></p></li>
							<?php } ?>
						</ul>
					</div>
				</div>
			</div>
		</div>
	</div>
</div>
