<?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'] ?? gigneo_datetime( gmdate( 'Y-m-d' ), 'Y-m-d' );
$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' );

$license               = gigneo_post_meta( $profile_id, '_gigneo_pm_licenses' );
$license_list          = gigneo_term_list( array( 'taxonomy' => 'gigneo_license' ) );
$attachment_extensions = gigneo_option( '_gigneo_ts_license_extensions', array( 'jpg', 'jpeg', 'png', 'doc' ) );
$attachment_max_files  = gigneo_option( '_gigneo_ts_license_max_files', '3' );
$attachment_max_size   = gigneo_option( '_gigneo_ts_license_max_size', '1' );

$active_modules = gigneo_active_modules();
?>
<div id="gn-profile-licenses" class="gn-setting-section gn-profile-licenses-list">
	<div class="gn-head">
		<h3 class="gn-title"><?php echo esc_html__( 'Licenses', 'gigneo' ); ?></h3>
		<button class="gn-link gn-link-primary" data-gn-fields="" data-gn-previews="" data-gn-modal-toggle="gn-edit-profile-license">
			<i class="hi hi-plus" aria-hidden="true"></i>
			<?php echo esc_html__( 'Add', 'gigneo' ); ?>
		</button>
	</div>
	<div class="gn-body">
		<?php if ( ! empty( $license ) ) { ?>
			<ul class="gn-info-list">
				<?php
				foreach ( $license as $key => $license_item ) {
					$license          = ! empty( $license_item['license'] ) ? $license_item['license'] : '';
					$license_obj      = get_term_by( 'slug', $license, 'gigneo_license' );
					$license_name     = ! empty( $license_item['license'] ) ? ( $license_obj ? $license_obj->name : $license_item['license'] ) : '';
					$provider         = ! empty( $license_item['provider'] ) ? $license_item['provider'] : '';
					$license_number   = ! empty( $license_item['license_number'] ) ? $license_item['license_number'] : '';
					$verification_url = ! empty( $license_item['verification_url'] ) ? $license_item['verification_url'] : '';
					$issue_date       = ! empty( $license_item['issue_date'] ) ? date_i18n( $date_format, strtotime( $license_item['issue_date'] ), true ) : '';
					$expiry_date      = ! empty( $license_item['expiry_date'] ) ? date_i18n( $date_format, strtotime( $license_item['expiry_date'] ), true ) : esc_html__( 'None', 'gigneo' );
					$additional_note  = ! empty( $license_item['note'] ) ? $license_item['note'] : '';

					if ( empty( $license_name ) || empty( $provider ) || empty( $license_number ) ) {
						continue;
					}

					$fields_data = array(
						'license_id'               => esc_attr( $key ),
						'license'                  => esc_attr( $license ),
						'license_provider'         => esc_attr( $provider ),
						'license_number'           => esc_attr( $license_number ),
						'license_verification_url' => esc_attr( $verification_url ),
						'license_issue_date'       => esc_attr( $license_item['issue_date'] ),
						'license_expiry_date'      => esc_attr( $license_item['expiry_date'] ),
						'license_note'             => esc_attr( $additional_note ),
					);
					?>
					<li class="gn-info-list-item">
						<span class="gn-icon">
							<i class="hi hi-identification" aria-hidden="true"></i>
						</span>
						<div class="gn-info">
							<h4 class="gn-title"><?php echo esc_html( $license_name ); ?></h4>
							<ul class="gn-meta">
								<li class="gn-provider">
									<i class="hi hi-building-office" aria-hidden="true"></i>
									<?php echo esc_html( $provider ); ?>
								</li>
								<?php
								if ( ! empty( $license_item['issue_date'] ) ) {
									?>
								<li class="gn-date">
									<i class="hi hi-calendar" aria-hidden="true"></i>
									<?php
									printf(
									// translators: %1$s: Issue date. %2$s: Expiry date.
										esc_html__( '%1$s - %2$s', 'gigneo' ),
										esc_html( $issue_date ),
										esc_html( $expiry_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-license">
								<i class="hi hi-pencil-edit" aria-hidden="true"></i>
							</button>
							<button class="gn-btn gn-delete-profile-detail" data-gn-type="license" 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 licenses', 'gigneo' ),
				'description' => esc_html__( 'Add your license to enhance your profile.', 'gigneo' ),
			);
			gigneo_not_found( 'gn-profile-license', $not_found_args );
		}
		?>
	</div>
	<!--Add/Edit Modal-->
	<div id="gn-edit-profile-license" 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__( 'License', 'gigneo' ); ?></h4>
						<p class="gn-description"><?php echo esc_html__( 'Manage or update your licenses.', 'gigneo' ); ?></p>
						<button type="button" data-gn-modal-close="gn-edit-profile-license" 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 gn-select2">
											<label class="gn-required-field-label"
													for="gn-license"><?php echo esc_html__( 'License', 'gigneo' ); ?></label>
											<select id="gn-license" name="license"
													data-minimum-results-for-search="Infinity"
													placeholder="<?php echo esc_html__( 'Select License', 'gigneo' ); ?>">
												<?php foreach ( $license_list as $license_key => $license_name ) { ?>
													<option value="<?php echo esc_attr( $license_key ); ?>"><?php echo esc_html( $license_name ); ?></option>
												<?php } ?>
											</select>
										</div>
									</div>
									<div class="col-md-6">
										<div class="gn-form-field">
											<label class="gn-required-field-label"
													for="gn-license-number"><?php echo esc_html__( 'Number', 'gigneo' ); ?></label>
											<input id="gn-license-number" class="gn-required-field" type="text"
													name="license_number" autocomplete="off" value=""
													placeholder="<?php echo esc_attr__( 'Enter license number', 'gigneo' ); ?>">
										</div>
									</div>
									<div class="col-md-6">
										<div class="gn-form-field">
											<label class="gn-required-field-label"
													for="gn-license-provider"><?php echo esc_html__( 'Provider', 'gigneo' ); ?></label>
											<input id="gn-license-provider" class="gn-required-field" type="text"
													name="license_provider" autocomplete="off" value=""
													placeholder="<?php echo esc_attr__( 'Enter provider name', 'gigneo' ); ?>">
										</div>
									</div>
									<div class="col-md-12">
										<div class="gn-form-field">
											<label for="gn-license-verification"><?php echo esc_html__( 'Verification URL', 'gigneo' ); ?></label>
											<input id="gn-license-verification" type="text"
													name="license_verification_url" autocomplete="off" value=""
													placeholder="<?php echo esc_attr__( 'Enter verification URL', 'gigneo' ); ?>">
										</div>
									</div>
									<div class="col-md-6">
										<div class="gn-form-field gn-datepicker">
											<label class="gn-required-field-label"
													for="gn-license-date"><?php echo esc_html__( 'Issue date', 'gigneo' ); ?></label>
											<input id="gn-license-date" class="flatpickr-input gn-required-field"
													data-single="true" data-max-date="<?php echo esc_attr( $max_date ); ?>"
													type="hidden" name="license_issue_date" autocomplete="off" value=""
													placeholder="<?php echo esc_attr__( 'Enter issue date', 'gigneo' ); ?>">
										</div>
									</div>
									<div class="col-md-6">
										<div class="gn-form-field gn-datepicker">
											<label for="gn-license-date"><?php echo esc_html__( 'Expiry date', 'gigneo' ); ?></label>
											<input id="gn-license-date" class="flatpickr-input" data-single="true"
													type="hidden" name="license_expiry_date" autocomplete="off" value=""
													placeholder="<?php echo esc_attr__( 'Enter expiry date', 'gigneo' ); ?>">
										</div>
									</div>
									<?php if ( in_array( 'license_verification', $active_modules, true ) ) { ?>
										<div class="col-md-12">
											<div class="gn-form-field gn-plupload">
												<label for="gn-license-attachments"><?php echo esc_html__( 'Attachments', 'gigneo' ); ?></label>
												<div id="gn-license-attachment-container" class="gn-plupload-container">
													<div id="gn-license-attachment-drop-area"
														class="gn-plupload-drop-area">
														<i class="hi hi-cloud-arrow-up" aria-hidden="true"></i>
														<div class="gn-action-info">
															<button id="gn-license-attachment-uploader" type="button"
																	class="gn-upload"><?php echo esc_html__( 'Click here to upload', 'gigneo' ); ?></button>
															<p class="gn-note">
																<?php
																printf(
																// translators: %1$s: Valid extensions %2$s Maximum files %3$s Maximum file size.
																	esc_html__( '%1$s — Up to %2$s files (Max. %3$s MB)', 'gigneo' ),
																	esc_html( strtoupper( implode( ',', $attachment_extensions ) ) ),
																	esc_html( $attachment_max_files ),
																	esc_html( $attachment_max_size )
																);
																?>
															</p>
														</div>
													</div>
													<div id="gn-license-attachment-list"
														class="gn-plupload-attachments"></div>
												</div>
											</div>
										</div>
									<?php } ?>
									<div class="col-md-12">
										<div class="gn-form-field">
											<label for="gn-license-note"><?php echo esc_html__( 'Addition Note', 'gigneo' ); ?></label>
											<textarea id="gn-license-note" class="gn-textarea-counter"
														name="license_note" autocomplete="off" rows="5"
														placeholder="<?php echo esc_attr__( 'Enter additional note.', 'gigneo' ); ?>"></textarea>
										</div>
									</div>
								</div>
								<input type="hidden" name="license_id" value="">
							</fieldset>
						</div>
						<div class="gn-modal-footer">
							<input type="hidden" name="type" value="licenses">
							<button type="button" class="gn-btn gn-btn-outline-grey"
									data-gn-modal-close="gn-edit-profile-license"><?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>
