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

$profile_id        = $args['profile_id'] ?? gigneo_active_profile_id();
$max_date          = $args['max_date'] ?? gigneo_datetime( gmdate( 'Y-m-d' ), 'Y-m-d' );
$date_format       = $args['date_format'] ?? get_option( 'date_format' );
$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' );

$award = gigneo_post_meta( $profile_id, '_gigneo_pm_awards' );
?>
<div id="gn-profile-awards" class="gn-setting-section gn-setting-section-list">
	<div class="gn-head">
		<h3 class="gn-title"><?php echo esc_html__( 'Awards', 'gigneo' ); ?></h3>
		<button class="gn-link gn-link-primary" data-gn-fields="" data-gn-previews="" data-gn-modal-toggle="gn-edit-profile-award">
			<i class="hi hi-plus" aria-hidden="true"></i>
			<?php echo esc_html__( 'Add', 'gigneo' ); ?>
		</button>
	</div>
	<div class="gn-body">
		<?php if ( ! empty( $award ) ) { ?>
			<ul class="gn-info-list">
				<?php
				foreach ( $award as $key => $award_item ) {
					$award_title = ! empty( $award_item['title'] ) ? $award_item['title'] : '';
					$award_by    = ! empty( $award_item['award_by'] ) ? $award_item['award_by'] : '';
					$award_date  = ! empty( $award_item['date'] ) ? date_i18n( $date_format, strtotime( $award_item['date'] ), true ) : '';

					if ( empty( $award_title ) || empty( $award_by ) ) {
						continue;
					}

					$fields_data = array(
						'award_id'    => esc_attr( $key ),
						'award_title' => esc_attr( $award_title ),
						'award_by'    => esc_attr( $award_by ),
						'award_date'  => esc_attr( $award_item['date'] ),
					);
					?>
					<li class="gn-info-list-item">
						<span class="gn-icon">
							<i class="hi hi-trophy" aria-hidden="true"></i>
						</span>
						<div class="gn-info">
							<?php if ( ! empty( $award_title ) ) { ?>
								<h4 class="gn-title"><?php echo esc_html( $award_title ); ?></h4>
							<?php } ?>
							<ul class="gn-meta">
								<li class="gn-award-by">
									<i class="hi hi-building-office" aria-hidden="true"></i>
									<?php echo esc_html( $award_by ); ?>
								</li>
								<?php if ( ! empty( $award_date ) ) { ?>
									<li class="gn-date">
										<i class="hi hi-calendar" aria-hidden="true"></i>
										<?php echo esc_html( $award_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-award">
								<i class="hi hi-pencil-edit" aria-hidden="true"></i>
							</button>
							<button class="gn-btn gn-delete-profile-detail" data-gn-type="award" 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 awards', 'gigneo' ),
				'description' => esc_html__( 'Add your awards to enhance your profile.', 'gigneo' ),
			);
			gigneo_not_found( 'gn-profile-awards', $not_found_args );
		}
		?>
	</div>
	<!--Add/Edit Modal-->
	<div id="gn-edit-profile-award" 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__( 'Award', 'gigneo' ); ?></h4>
						<p class="gn-description"><?php echo esc_html__( 'Manage or update your awards.', 'gigneo' ); ?></p>
						<button type="button" data-gn-modal-close="gn-edit-profile-award" 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-award-title"><?php echo esc_html__( 'Title', 'gigneo' ); ?></label>
											<input id="gn-award-title" class="gn-required-field" type="text" name="award_title" autocomplete="off" value="" placeholder="<?php echo esc_attr__( 'Enter title', 'gigneo' ); ?>">
										</div>
									</div>
									<div class="col-md-6">
										<div class="gn-form-field">
											<label class="gn-required-field-label" for="gn-award-by"><?php echo esc_html__( 'Award by', 'gigneo' ); ?></label>
											<input id="gn-award-by" class="gn-required-field" type="text" name="award_by" autocomplete="off" value="" placeholder="<?php echo esc_attr__( 'Enter award by', 'gigneo' ); ?>">
										</div>
									</div>
									<div class="col-md-6">
										<div class="gn-form-field gn-datepicker">
											<label class="gn-required-field-label" for="gn-award-date"><?php echo esc_html__( 'Enter date', 'gigneo' ); ?></label>
											<input id="gn-award-date" class="flatpickr-input gn-required-field" data-single="true" data-max-date="<?php echo esc_attr( $max_date ); ?>" type="hidden" name="award_date" autocomplete="off" value="" placeholder="<?php echo esc_attr__( 'Date', 'gigneo' ); ?>">
										</div>
									</div>
								</div>
								<input type="hidden" name="award_id" value="">
							</fieldset>
						</div>
						<div class="gn-modal-footer">
							<input type="hidden" name="type" value="awards">
							<button type="button" class="gn-btn gn-btn-outline-grey" data-gn-modal-close="gn-edit-profile-award"><?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>
