<?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'] ?? '';
$description        = ! empty( $p_id ) ? gigneo_post_meta( $p_id, '_gigneo_pm_description' ) : '';
$disable_cities     = gigneo_option( '_gigneo_ts_disable_cities', false );
$country_state_list = gigneo_countries_with_states( 'active' );

//profile pre-filled address
$profile_id       = gigneo_active_profile_id();
$profile_location = gigneo_post_meta( $profile_id, '_gigneo_pm_location' );

$profile_data = array(
	'country_state'    => gigneo_post_meta( $profile_id, '_gigneo_pm_country_state' ) ?? '',
	'city'             => gigneo_post_meta( $profile_id, '_gigneo_pm_city' ) ?? '',
	'area'             => gigneo_post_meta( $profile_id, '_gigneo_pm_area' ) ?? '',
	'zip_code'         => gigneo_post_meta( $profile_id, '_gigneo_pm_zip_code' ) ?? '',
	'address'          => $profile_location['address'] ?? '',
	'location'         => $profile_location ?? array(),
	'friendly_address' => gigneo_post_meta( $profile_id, '_gigneo_pm_address' ) ?? '',
);

$attachments           = ! empty( $p_id ) ? gigneo_post_meta( $p_id, '_gigneo_pm_attachments' ) : array();
$location              = ! empty( $p_id ) ? gigneo_post_meta( $p_id, '_gigneo_pm_location' ) : array();
$location              = ! empty( $location ) ? $location : $profile_data['location'];
$country_state_raw     = ! empty( $p_id ) ? gigneo_post_meta( $p_id, '_gigneo_pm_country_state' ) : '';
$country_state_raw     = ! empty( $country_state_raw ) ? $country_state_raw : $profile_data['country_state'];
$address               = ! empty( $p_id ) ? gigneo_post_meta( $p_id, '_gigneo_pm_friendly_address' ) : '';
$address               = ! empty( $address ) ? $address : $profile_data['friendly_address'];
$zip_code              = ! empty( $p_id ) ? gigneo_post_meta( $p_id, '_gigneo_pm_zip_code' ) : '';
$zip_code              = ! empty( $zip_code ) ? $zip_code : $profile_data['zip_code'];
$area                  = ! empty( $p_id ) ? gigneo_post_meta( $p_id, '_gigneo_pm_area' ) : '';
$area                  = ! empty( $area ) ? $area : $profile_data['area'];
$city_val              = ! empty( $p_id ) ? gigneo_post_meta( $p_id, '_gigneo_pm_city' ) : '';
$city_val              = ! empty( $city_val ) ? $city_val : $profile_data['city'];
$attachment_extensions = gigneo_option( '_gigneo_ts_job_attachment_extensions', array( 'jpg', 'jpeg', 'png', 'pdf', 'doc', 'docx', 'mp4' ) );
$attachment_max_size   = gigneo_option( '_gigneo_ts_job_attachment_max_size', '5' );
$attachment_limit      = gigneo_option( '_gigneo_ts_job_attachment_limit', 1 );
?>
<div id="gn-job-edit-details"
	class="gn-job-edit-step gn-job-edit-details <?php echo esc_attr( 'details' !== $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-details" 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 gn-details-fields gn-wysiwyg-editor-two">
								<label for="gn-description" class="gn-required-field-label">
									<?php echo esc_html__( 'Description', 'gigneo' ); ?>
								</label>
								<?php
								wp_editor(
									wp_kses_post( $description ),
									'gn-description',
									array(
										'textarea_name' => 'description',
										'media_buttons' => false,
										'quicktags'     => false,
										'textarea_rows' => 10,
										'editor_class'  => 'gn-wp-editor gn-required-field',
										'tinymce'       => array(
											'toolbar1' => 'bold italic bullist numlist link unlink',
											'toolbar2' => '',
											'plugins'  => 'lists link',
											'directionality' => is_rtl() ? 'rtl' : 'ltr',
											'forced_root_block' => false,
											'force_br_newlines' => true,
											'force_p_newlines' => false,
											'convert_newlines_to_brs' => true,
											'valid_elements' => 'strong/b,em/i,ul,ol,li,a[href|target],br,hr',
											'invalid_elements' => 'p,pre,h1,h2,h3,h4,h5,h6,img',
											'content_style' => 'body { font-size: 14px; background-color: transparent; font-family: var(--gigneo-primary-font,-apple-system,BlinkMacSystemFont,Segoe UI,Roboto,Helvetica Neue,Arial,Noto Sans,sans-serif,Apple Color Emoji,Segoe UI Emoji,Segoe UI Symbol,Noto Color Emoji) }',
										),
									)
								);
								?>
							</div>
						</div>
					</div>
				</div>
				<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__( 'Location', 'gigneo' ); ?></h3>
							<p class="gn-description">
								<?php echo esc_html__( 'Specify your location to help provider find region-relevant jobs.', 'gigneo' ); ?>
							</p>
						</div>
					</div>
					<div class="gn-body">
						<fieldset class="gn-form-fields">
							<div class="row gy-4">
								<div class="col-md-6">
									<div class="gn-form-field gn-select2">
										<label for="gn-country-state"
											class="gn-required-field-label"><?php echo esc_html__( 'Country / State', 'gigneo' ); ?></label>
										<select id="gn-country-state" name="country_state" class="gn-required-field"
											placeholder="<?php echo esc_html__( 'Select country / state', 'gigneo' ); ?>">
											<?php foreach ( $country_state_list as $key => $country_state ) { ?>
												<option <?php selected( $country_state_raw, $key ); ?>
													value="<?php echo esc_attr( $key ); ?>">
													<?php echo esc_html( $country_state ); ?>
												</option>
											<?php } ?>
										</select>
									</div>
								</div>
								<?php
								if ( ! $disable_cities ) {
									$city_list = ! empty( $country_state_raw ) ? gigneo_cities( $country_state_raw ) : array();
									?>
									<div class="col-md-6">
										<div class="gn-form-field gn-select2">
											<label for="gn-city"><?php echo esc_html__( 'City', 'gigneo' ); ?></label>
											<select id="gn-city" name="city"
												placeholder="<?php echo esc_html__( 'Select city', 'gigneo' ); ?>">
												<?php foreach ( $city_list as $key => $city ) { ?>
													<option <?php selected( $city_val ?? '', $key ); ?>
														value="<?php echo esc_attr( $key ); ?>"><?php echo esc_html( $city ); ?>
													</option>
												<?php } ?>
											</select>
										</div>
									</div>
								<?php } ?>
								<div class="col-md-6">
									<div class="gn-form-field">
										<label for="gn-area"><?php echo esc_html__( 'Area', 'gigneo' ); ?></label>
										<input id="gn-area" type="text" name="area" autocomplete="off"
											value="<?php echo esc_attr( $area ?? '' ); ?>"
											placeholder="<?php echo esc_attr__( 'Block / Sector / Town', 'gigneo' ); ?>">
									</div>
								</div>
								<div class="col-md-<?php echo esc_attr( ! $disable_cities ? '6' : '12' ); ?>">
									<div class="gn-form-field">
										<label for="gn-zip-code"
											class="gn-required-field-label"><?php echo esc_html__( 'Postcode / ZIP', 'gigneo' ); ?></label>
										<input id="gn-zip-code" type="text" class="gn-required-field" name="zip_code"
											autocomplete="off" value="<?php echo esc_attr( $zip_code ?? '' ); ?>"
											placeholder="<?php echo esc_attr__( 'Enter postcode or ZIP', 'gigneo' ); ?>">
									</div>
								</div>
								<div class="col-md-12">
									<div class="gn-form-field">
										<label for="gn-address"
											class="gn-required-field-label"><?php echo esc_html__( 'Address', 'gigneo' ); ?></label>
										<textarea id="gn-address" class="gn-required-field" name="friendly_address"
											autocomplete="off" rows="5"
											placeholder="<?php echo esc_attr__( 'Friendly address', 'gigneo' ); ?>"><?php echo esc_attr( $address ?? '' ); ?></textarea>
									</div>
								</div>
						</fieldset>
					</div>
				</div>
				<div class="gn-edit-job-section gn-documents-section">
					<div class="gn-head">
						<div class="gn-heading-area">
							<h3 class="gn-title">
								<?php
								echo esc_html__( 'Attachments', 'gigneo' );
								?>
							</h3>
							<p class="gn-description">
								<?php echo esc_html__( 'Share files that help explain the work.', 'gigneo' ); ?>
							</p>
						</div>
					</div>
					<div class="gn-body">
						<div class="gn-form-fields">
							<div class="gn-form-field">
								<div class="gn-edit-media-content">
									<ul class="gn-job-media-list gn-job-media-documents">
										<?php
										$attachments                = ! empty( $attachments ) && is_array( $attachments ) ? $attachments : array();
										$attachment_placeholder_url = GIGNEO_DIR_URL . 'assets/img/icon/document-svg.png';
										for ( $attachment_index = 0; $attachment_index < $attachment_limit; $attachment_index++ ) {
											$attachment_name = '';
											$attachment_id   = $attachments[ $attachment_index ] ?? '';
											if ( ! empty( $attachment_id ) ) {
												$file_path       = get_attached_file( $attachment_id );
												$attachment_name = basename( $file_path );
											}
											?>
											<li
												class="gn-job-media gn-job-media-<?php echo esc_attr( $attachment_index ); ?><?php echo esc_attr( empty( $attachment_id ) ? ' gn-job-media-placeholder' : '' ); ?>">
												<div class="gn-job-media-info">
													<figure class="gn-job-image">
														<img src="<?php echo esc_url( $attachment_placeholder_url ); ?>"
															alt="<?php echo esc_attr__( 'Attachment', 'gigneo' ); ?>">
														<figcaption class="gn-name">
															<?php echo esc_html( $attachment_name ); ?>
														</figcaption>
													</figure>
													<input type="hidden" name="attachments[]"
														value="<?php echo esc_attr( $attachment_id ); ?>"
														id="gn-job-media-input-<?php echo esc_attr( $attachment_index ); ?>" />
													<div class="gn-job-media-action">
														<button type="button" class="gn-job-media-upload-toggle"><i
																class="hi hi-pencil-edit" aria-hidden="true"></i>
														</button>
														<button type="button" class="gn-job-media-remove"><i
																class="hi hi-trash" aria-hidden="true"></i></button>
													</div>
												</div>
												<div class="gn-job-media-upload"
													id="gn-upload-job-gallery-<?php echo esc_attr( $attachment_index ); ?>">
													<i class="hi hi-arrow-up-tray" aria-hidden="true"></i>
													<div class="gn-job-media-upload-info">
														<p class="gn-message"><?php echo esc_html__( 'Click here to upload.', 'gigneo' ); ?></p>
														<p class="gn-description">
															<?php
															printf(
																// translators: %1$s: Valid extensions %2$s Maximum file size %3$s Number of files.
																esc_html__( '%1$s - Up to %3$s files (Max. %2$s MB)', 'gigneo' ),
																esc_html( strtoupper( implode( ',', $attachment_extensions ) ) ),
																esc_html( $attachment_max_size ),
																esc_html( $attachment_limit )
															);
															?>
														</p>
														<button class="gn-job-media-upload-toggle"
															type="button"><?php echo esc_html__( 'Browse file', 'gigneo' ); ?></button>
													</div>
												</div>
											</li>
											<?php
										}
										?>
									</ul>
								</div>
							</div>
						</div>
					</div>
				</div>

				<div class="gn-edit-job-footer gn-actions">
					<button type="button" data-gn-step="details" 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="details" data-gn-prev-step="pricing" class="gn-btn gn-btn-outline-grey gn-job-back"><?php echo esc_html__( 'Back', 'gigneo' ); ?></button>
					<button type="button" data-gn-step="details" data-gn-type="continue" data-gn-next-step="screening" 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__( 'Job details:', 'gigneo' ); ?></h4>
					<div class="gn-aside-body">
						<ul class="gn-list">
							<li>
								<p class="gn-description">
									<?php echo esc_html__( 'Describe your job clearly to outline the process, deliverables, and expectations.', 'gigneo' ); ?>
								</p>
							</li>
							<li>
								<p class="gn-description">
									<?php echo esc_html__( 'Add your location to help provider find region-specific services.', 'gigneo' ); ?>
								</p>
							</li>
							<li>
								<p class="gn-description">
									<?php echo esc_html__( 'Add attachments that provide additional details about the work.', 'gigneo' ); ?>
								</p>
							</li>
						</ul>
					</div>
				</div>
			</div>
		</div>
	</div>
</div>