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

$p_id         = $args['p_id'] ?? 0;
$current_step = $args['current_step'] ?? '';
$redirect_url = $args['redirect_url'] ?? '';

$max_skills         = gigneo_option( '_gigneo_ts_service_max_skills', '5' );
$max_specific_types = gigneo_option( '_gigneo_ts_service_max_specific_types', '5' );
$title_prefix       = gigneo_option( '_gigneo_ts_service_title_prefix' );
$category_list      = gigneo_term_list(
	array(
		'taxonomy' => 'gigneo_facet',
		'parent'   => 0,
	),
	false
);
$skill_list         = gigneo_term_list( array( 'taxonomy' => 'gigneo_skill' ), false );
$service_types      = gigneo_option( '_gigneo_ts_service_type', 'both' );

$service_title        = '';
$selected_category    = '';
$selected_subcategory = '';
$list_subcategories   = array();
$saved_specific_types = array();
$list_specific_types  = array();
$skills               = array();

if ( ! empty( $p_id ) ) {

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

	$service_type = get_post_meta( $p_id, '_gigneo_pm_type', true );
	$facets       = get_the_terms( $p_id, 'gigneo_facet' );
	$skills       = get_the_terms( $p_id, 'gigneo_skill' );
	$parent_id    = 0;
	$subcat_term  = null;

	// Single category and subcategory from gigneo_facet.
	if ( ! empty( $facets ) && ! is_wp_error( $facets ) ) {
		foreach ( $facets as $facet_term ) {
			if ( 0 === (int) $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;
				$subcat_term          = $facet_term;
				break;
			}
		}
		// Specific types: grandchildren of category.
		foreach ( $facets as $cat_term ) {
			if ( $subcat_term && $cat_term->parent === $subcat_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 ) && $subcat_term ) {
		$list_specific_types = gigneo_term_list(
			array(
				'taxonomy' => 'gigneo_facet',
				'parent'   => $subcat_term->term_id,
			),
			false
		);
	}

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

$service_type = ! empty( $service_type ) ? $service_type : 'remote';
?>
<div id="gn-service-edit-basic" class="gn-service-edit-step gn-service-edit-basic <?php echo esc_attr( empty( $current_step ) || 'basic' === $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-service-form-basic" class="gn-main gn-service-edit-form">
				<div class="gn-edit-service-section">
					<div class="gn-body">
						<div class="gn-form-fields">
							<div class="gn-form-field">
								<label class="gn-required-field-label"
										for="gn-service-title"><?php echo esc_html__( 'Service title', 'gigneo' ); ?></label>
								<div class="gn-edit-service-textarea">
									<?php if ( ! empty( $title_prefix ) ) { ?>
										<span class="gn-title-prefix"><?php echo esc_html( $title_prefix ); ?></span>
									<?php } ?>
									<textarea id="gn-service-title" name="service_title" class="gn-required-field gn-textarea-counter" data-max-length="100" rows="3" placeholder=""><?php echo esc_attr( $service_title ); ?></textarea>
								</div>
							</div>
						</div>
					</div>
				</div>
				<div class="gn-edit-service-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-service-category"
										class="gn-required-field-label"><?php echo esc_html__( 'Category', 'gigneo' ); ?></label>
								<select id="gn-service-category" name="service_category" class="gn-required-field"
										placeholder="<?php echo esc_html__( 'Select 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-service-subcategory"
										class="gn-required-field-label"><?php echo esc_html__( 'Subcategory', 'gigneo' ); ?></label>
								<select id="gn-service-subcategory" name="service_subcategory" class="gn-required-field"
										placeholder="<?php echo esc_html__( 'Select sub 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-service-specific-types"><?php echo esc_html__( 'Specific types', 'gigneo' ); ?></label>
								<select id="gn-service-specific-types" name="service_specific_types[]" multiple
										data-maximum-selection-length="<?php echo esc_attr( $max_specific_types ); ?>"
										placeholder="<?php echo esc_html__( 'Select 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
										esc_html__( 'Maximum %s allowed', 'gigneo' ),
										esc_html( $max_specific_types )
									);
									?>
								</div>
							</div>
						</div>
					</div>
				</div>
				<div class="gn-edit-service-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-service-skills"
											class="gn-required-field-label"><?php echo esc_html__( 'Skills', 'gigneo' ); ?></label>
									<select id="gn-service-skills" name="service_skills[]" multiple
											data-maximum-selection-length="<?php echo esc_attr( $max_skills ); ?>"
											class="gn-required-field"
											placeholder="<?php echo esc_html__( 'Select 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, $skills, true ) ); ?>><?php echo esc_html( $skill_item ); ?></option>
												<?php
											}
										}
										?>
									</select>
									<div class="gn-field-notice">
										<?php
										printf(
											// translators: %s: Maximum number
											esc_html__( 'Maximum %s allowed', 'gigneo' ),
											esc_html( $max_skills )
										);
										?>
									</div>
								</div>
							</div>
						</div>
					</div>
				</div>
				<?php if ( 'both' === $service_types ) { ?>
					<div class="gn-edit-service-section">
						<div class="gn-head">
							<div class="gn-heading-area">
								<h3 class="gn-title"><?php echo esc_html__( 'Service type', 'gigneo' ); ?></h3>
								<p class="gn-description"><?php echo esc_html__( 'Specifies the service is delivered remotely or onsite.', 'gigneo' ); ?></p>
							</div>
						</div>
						<div class="gn-body">
							<div class="gn-form-fields">
								<div class="gn-form-field gn-service-type">
									<div class="gn-radio">
										<input type="radio" name="service_type" class="gn-required-field"
												id="remote"<?php echo esc_attr( 'remote' === $service_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="service_type" class="gn-required-field"
												id="onsite"<?php echo esc_attr( 'onsite' === $service_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 a specific location', 'gigneo' ); ?></em>
										</label>
									</div>
								</div>
							</div>
						</div>
					</div>
				<?php } ?>
				<div class="gn-edit-service-footer gn-actions">
					<button type="button" data-gn-step="basic" data-gn-type="draft"
							data-gn-redirect-url="<?php echo esc_url( $redirect_url ); ?>"
							class="gn-btn gn-btn-outline-grey gn-service-save"><?php echo esc_html__( 'Save as draft', 'gigneo' ); ?></button>
					<a href="<?php echo esc_url( gigneo_dashboard_url( 'service', 'list' ) ); ?>"
						class="gn-btn gn-btn-outline-grey gn-service-back-link">
						<?php echo esc_html__( 'Back', 'gigneo' ); ?>
					</a>
					<button type="button" data-gn-step="basic" data-gn-type="continue" data-gn-next-step="pricing"
							class="gn-btn gn-btn-primary gn-service-save">
						<?php echo esc_html__( 'Save & 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 skills to improve discoverability and attract the right clients.', 'gigneo' ); ?></p></li>
							<?php if ( 'both' === $service_types ) { ?>
								<li><p class="gn-description"><?php echo esc_html__( 'Select whether your service is offered remotely or onsite.', 'gigneo' ); ?></p></li>
							<?php } ?>
						</ul>
					</div>
				</div>
			</div>
		</div>
	</div>
</div>