<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit; // Prevent direct access to the script.
}

$profile_id        = $args['profile_id'] ?? gigneo_active_profile_id();
$date_format       = $args['date_format'] ?? get_option( 'date_format' );
$max_date          = $args['max_date'] ?? '';
$images_extensions = $args['images_extensions'] ?? gigneo_option( '_gigneo_ts_video_extensions', array( 'mp4', 'avi', 'mov' ) );
$image_max_size    = $args['image_max_size'] ?? gigneo_option( '_gigneo_ts_image_max_size', '1' );

$education = gigneo_post_meta( $profile_id, '_gigneo_pm_education' );
?>
<div id="gn-profile-education" class="gn-setting-section gn-setting-section-list">
	<div class="gn-head">
		<h3 class="gn-title"><?php echo esc_html__( 'Education', 'gigneo' ); ?></h3>
		<button class="gn-link gn-link-primary" data-gn-fields="" data-gn-previews="" data-gn-modal-toggle="gn-edit-profile-education">
			<i class="hi hi-plus" aria-hidden="true"></i>
			<?php echo esc_html__( 'Add', 'gigneo' ); ?>
		</button>
	</div>
	<div class="gn-body">
		<?php if ( ! empty( $education ) ) { ?>
			<ul class="gn-info-list">
				<?php
				foreach ( $education as $key => $education_item ) {
					$edu_school         = ! empty( $education_item['school'] ) ? $education_item['school'] : '';
					$edu_degree         = ! empty( $education_item['degree'] ) ? $education_item['degree'] : '';
					$edu_specialization = ! empty( $education_item['specialization'] ) ? $education_item['specialization'] : '';
					$edu_start_date     = ! empty( $education_item['start_date'] ) ? date_i18n( $date_format, strtotime( $education_item['start_date'] ), true ) : '';
					$edu_end_date       = ! empty( $education_item['end_date'] ) ? date_i18n( $date_format, strtotime( $education_item['end_date'] ), true ) : esc_html__( 'Continue', 'gigneo' );

					if ( empty( $edu_school ) || empty( $edu_degree ) || empty( $edu_specialization ) ) {
						continue;
					}

					$fields_data = array(
						'education_id'             => esc_attr( $key ),
						'education_school'         => esc_attr( $edu_school ),
						'education_degree'         => esc_attr( $edu_degree ),
						'education_specialization' => esc_attr( $edu_specialization ),
						'education_start_date'     => esc_attr( $education_item['start_date'] ),
						'education_end_date'       => esc_attr( $education_item['end_date'] ),
					);
					?>
					<li class="gn-info-list-item">
						<span class="gn-icon">
							<i class="hi hi-academic-cap" aria-hidden="true"></i>
						</span>
						<div class="gn-info">
							<h4 class="gn-title gn-school"><?php echo esc_html( $edu_school ); ?></h4>
							<ul class="gn-meta">
								<li class="gn-degree">
									<i class="hi hi-briefcase" aria-hidden="true"></i>
									<?php
									printf(
											// translators: 1: Degree, 2: Specialization
										esc_html__( '%1$s in %2$s', 'gigneo' ),
										esc_html( $edu_degree ),
										esc_html( $edu_specialization )
									);
									?>
								</li>
								<?php if ( ! empty( $edu_start_date ) ) { ?>
									<li class="gn-date">
										<i class="hi hi-calendar" aria-hidden="true"></i>
										<?php
										printf(
										// translators: 1: Start date, 2: End date
											esc_html__( '%1$s - %2$s', 'gigneo' ),
											esc_html( $edu_start_date ),
											esc_html( $edu_end_date )
										);
										?>
									</li>
								<?php } ?>
							</ul>
						</div>
						<div class="gn-actions">
							<button class="gn-btn gn-edit-profile-detail" data-gn-fields="<?php echo esc_attr( wp_json_encode( $fields_data ) ); ?>" data-gn-modal-toggle="gn-edit-profile-education">
								<i class="hi hi-pencil-edit" aria-hidden="true"></i>
							</button>
							<button class="gn-btn gn-delete-profile-detail" data-gn-type="education" data-gn-id="<?php echo esc_attr( $key ); ?>">
								<i class="hi hi-trash" aria-hidden="true"></i>
							</button>
						</div>
					</li>
				<?php } ?>
			</ul>
			<?php
		} else {
			$not_found_args = array(
				'image'       => false,
				'title'       => esc_html__( 'No education', 'gigneo' ),
				'description' => esc_html__( 'Add your education to enhance your profile.', 'gigneo' ),
			);
			gigneo_not_found( 'gn-profile-educations', $not_found_args );
		}
		?>
	</div>
	<!--Add/Edit Modal-->
	<div id="gn-edit-profile-education" class="gn-modal-wrapper gn-profile-setting-modal">
		<div class="gn-modal-dialog">
			<div class="gn-modal-content">
				<div class="gn-modal-info">
					<div class="gn-modal-header">
						<h4 class="gn-title"><?php echo esc_html__( 'Education', 'gigneo' ); ?></h4>
						<p class="gn-description"><?php echo esc_html__( 'Manage or update your education details.', 'gigneo' ); ?></p>
						<button type="button" data-gn-modal-close="gn-edit-profile-education" class="gn-modal-close">
							<i class="hi hi-x-mark" aria-hidden="true"></i>
						</button>
					</div>
					<form id="gn-save-profile-setting-form" class="gn-theme-form gn-save-profile-setting">
						<div class="gn-modal-body gn-custom-scrollbar">
							<fieldset class="gn-form-fields">
								<div class="row g-4">
									<div class="col-md-12">
										<div class="gn-form-field">
											<label class="gn-required-field-label" for="gn-education-school"><?php echo esc_html__( 'School', 'gigneo' ); ?></label>
											<input id="gn-education-school" class="gn-required-field" type="text" name="education_school" autocomplete="off" value="" placeholder="<?php echo esc_attr__( 'Enter school name', 'gigneo' ); ?>">
										</div>
									</div>
									<div class="col-md-6">
										<div class="gn-form-field">
											<label class="gn-required-field-label" for="gn-education-degree"><?php echo esc_html__( 'Degree', 'gigneo' ); ?></label>
											<input id="gn-education-degree" class="gn-required-field" type="text" name="education_degree" autocomplete="off" value="" placeholder="<?php echo esc_attr__( 'Enter degree title', 'gigneo' ); ?>">
										</div>
									</div>
									<div class="col-md-6">
										<div class="gn-form-field">
											<label class="gn-required-field-label" for="gn-education-specialization"><?php echo esc_html__( 'Specialization', 'gigneo' ); ?></label>
											<input id="gn-education-specialization" class="gn-required-field" type="text" name="education_specialization" autocomplete="off" value="" placeholder="<?php echo esc_attr__( 'Enter specialization', 'gigneo' ); ?>">
										</div>
									</div>
									<div class="col-md-6">
										<div class="gn-form-field gn-datepicker">
											<label class="gn-required-field-label" for="gn-education-start-date"><?php echo esc_html__( 'Start date', 'gigneo' ); ?></label>
											<input id="gn-education-start-date" class="flatpickr-input gn-required-field" data-single="true" data-max-date="<?php echo esc_attr( $max_date ); ?>" type="hidden" name="education_start_date" autocomplete="off" value="" placeholder="<?php echo esc_attr__( 'Enter start date', 'gigneo' ); ?>">
										</div>
									</div>
									<div class="col-md-6">
										<div class="gn-form-field gn-datepicker">
											<label for="gn-education-end-date"><?php echo esc_html__( 'End date', 'gigneo' ); ?></label>
											<input id="gn-education-end-date" class="flatpickr-input" data-single="true" data-max-date="<?php echo esc_attr( $max_date ); ?>" type="hidden" name="education_end_date" autocomplete="off" value="" placeholder="<?php echo esc_attr__( 'Enter end date', 'gigneo' ); ?>">
										</div>
									</div>
								</div>
								<input type="hidden" name="education_id" value="">
							</fieldset>
						</div>
						<div class="gn-modal-footer">
							<input type="hidden" name="type" value="educations">
							<button type="button" class="gn-btn gn-btn-outline-grey" data-gn-modal-close="gn-edit-profile-education"><?php echo esc_html__( 'Close', 'gigneo' ); ?></button>
							<button type="submit" class="gn-btn gn-btn-primary"><?php echo esc_html__( 'Save', 'gigneo' ); ?></button>
						</div>
					</form>
				</div>
			</div>
		</div>
	</div>
</div>
