<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

$proposal_id    = $args['p_id'] ?? '';
$job_id         = $args['job_id'] ?? gigneo_post_meta( $proposal_id, '_gigneo_pm_job_id' );
$form_questions = gigneo_post_meta( $job_id, '_gigneo_pm_intake_form' );
$form_answers   = gigneo_post_meta( $proposal_id, '_gigneo_pm_intake_form' );

if ( empty( $form_questions ) ) {
	return;
}

?>
<div class="gn-section gn-questions-answers-section">
	<div class="gn-head">
		<h3 class="gn-title"><?php esc_html_e( 'Screening questions', 'gigneo' ); ?></h3>
	</div>
	<div class="gn-body">
		<?php
		global $wpdb;
		foreach ( $form_questions as $index => $form_question ) {
			$requirement_title       = ! empty( $form_question['title'] ) ? $form_question['title'] : '';
			$requirement_type        = ! empty( $form_question['type'] ) ? $form_question['type'] : '';
			$requirement_value       = ! empty( $form_answers[ $index ]['value'] ) ? $form_answers[ $index ]['value'] : null;
			$requirement_label_class = ! empty( $form_question['required'] ) && 'yes' === $form_question['required'] ? ' gn-required-field-label' : '';

			if ( 'text' !== $requirement_type && 'choice' !== $requirement_type && 'multichoice' !== $requirement_type && 'attachment' !== $requirement_type && 'date' !== $requirement_type && 'input' !== $requirement_type && 'time' !== $requirement_type ) {
				continue;
			}
			?>
			<?php if ( ! empty( $requirement_value ) ) { ?>
				<div class="gn-questions-answers">
					<span class="gn-icon">
						<i class="hi hi-check-circle" aria-hidden="true"></i>
					</span>
					<div class="gn-questions-answers-content">
						<h4 class="gn-title<?php echo esc_attr( $requirement_label_class ); ?>">
							<?php echo esc_html( $requirement_title ); ?></h4>
						<?php if ( 'text' === $requirement_type ) { ?>
							<div class="gn-editor-content">
								<p class="gn-description"><?php echo wp_kses_post( $requirement_value ); ?></p>
							</div>
						<?php } elseif ( 'input' === $requirement_type ) { ?>
							<p class="gn-description"><?php echo esc_html( $requirement_value ); ?></p>
						<?php } elseif ( 'textarea' === $requirement_type ) { ?>
							<p class="gn-description"><?php echo esc_html( $requirement_value ); ?></p>
						<?php } elseif ( 'date' === $requirement_type ) { ?>
							<p class="gn-description"><?php echo esc_html( gigneo_datetime( $requirement_value, get_option( 'date_format' ) ) ); ?></p>
						<?php } elseif ( 'choice' === $requirement_type || 'multichoice' === $requirement_type ) {
							$requirement_value = is_array( $requirement_value ) ? $requirement_value : array( $requirement_value );
							?>
							<div class="gn-skills">
								<?php foreach ( $requirement_value as $value ) : ?>
									<span class="gn-status-tag"><?php echo esc_html( $value ); ?></span>
								<?php endforeach; ?>
							</div>
						<?php } elseif ( 'time' === $requirement_type ) { ?>
							<p class="gn-description"><?php echo esc_html( gigneo_datetime( $requirement_value, get_option( 'time_format' ) ) ); ?></p>
						<?php } elseif ( 'attachment' === $requirement_type && is_array( $requirement_value ) ) { ?>
							<ul class="gn-attachment-value">
								<?php
								foreach ( $requirement_value as $opt_index => $option ) {
									// phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
									$attachment = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}gigneo_attachments WHERE id = %d LIMIT 1", $option ) );
									// phpcs:enable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
									if ( $attachment ) {
										$attachment_path = gigneo_upload_dir_path( $attachment->path );
										$attachment_url  = gigneo_upload_dir_url( $attachment->path );
										if ( file_exists( $attachment_path ) ) {
											?>
											<li>
												<div class="gn-attachment-info">
													<i class="hi hi-document" aria-hidden="true"></i>
													<span class="gn-name"><?php echo esc_html( $attachment->name ); ?></span>
													<a href="<?php echo esc_url( $attachment_url ); ?>" download><i class="hi hi-arrow-down-tray" aria-hidden="true"></i></a>
												</div>
											</li>
											<?php
										}
									}
								}
								?>
							</ul>
						<?php } ?>
					</div>
				</div>
			<?php } ?>
			<?php
		}
		?>
	</div>
</div>