<?php
/**
 * Show job application when viewing a single job listing.
 *
 * Copy to: yourtheme/job_manager/job-application.php
 *
 * @see         https://wpjobmanager.com/document/template-overrides/
 * @author      Automattic / customized by you
 * @package     wp-job-manager
 * @category    Template
 * @version     1.31.1-custom
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

if ( $apply = get_the_job_application_method() ) :

	// Build the destination for the button.
	$href = '';
	if ( $apply->type === 'url' ) {
		$href = $apply->url;
	} elseif ( $apply->type === 'email' ) {
		$subject = isset( $apply->subject ) ? $apply->subject : '';
		$href    = 'mailto:' . $apply->email . ( $subject ? '?subject=' . rawurlencode( $subject ) : '' );
	}

	// Detect Loxo imports via meta (adjust if you use a different flag).
	$is_loxo = (bool) get_post_meta( get_the_ID(), '_loxo_job_id', true );

	// If this is a Loxo URL application, we'll open their hosted form in a modal.
	$iframe_src = '';
	if ( $apply->type === 'url' && $is_loxo ) {
		// Add a source hint; tweak if your vendor expects different params.
		$iframe_src = esc_url( rtrim( $href, '/' ) . '/form?source_type=app' );
	}
	?>
	<div class="application">
		<?php if ( $apply->type === 'url' && $is_loxo ) : ?>

			<a href="#" id="apply-job-button" class="application_button button">
				<?php esc_html_e( 'Apply for this job', 'wp-job-manager' ); ?>
			</a>

			<div id="apply-job-modal" class="rjr-modal" aria-hidden="true">
				<div class="rjr-modal__dialog" role="dialog" aria-modal="true" aria-label="<?php esc_attr_e( 'Job application', 'wp-job-manager' ); ?>">
					<iframe id="apply-job-iframe" src="" loading="lazy" title="<?php esc_attr_e( 'Application form', 'wp-job-manager' ); ?>"></iframe>
					<button type="button" class="rjr-modal__close" id="apply-job-close" aria-label="<?php esc_attr_e( 'Close application', 'wp-job-manager' ); ?>">
						<?php esc_html_e( 'Close', 'wp-job-manager' ); ?>
					</button>
				</div>
			</div>

			<style>
				/* Modal shell */
				.rjr-modal{ position:fixed; inset:0; background:rgba(0,0,0,.8); z-index:100000; display:none; }
				.rjr-modal.is-open{ display:block; }
				/* Dialog */
				.rjr-modal__dialog{
					position:absolute; top:50%; left:50%; transform:translate(-50%,-50%);
					width:min(1000px, 90vw); height:min(720px, 80vh);
					background:#fff; border-radius:10px; overflow:hidden; box-shadow:0 20px 60px rgba(0,0,0,.35);
				}
				/* Iframe */
				#apply-job-iframe{ width:100%; height:100%; border:none; display:block; }
				/* Close button */
				.rjr-modal__close{
					position:absolute; top:10px; right:10px;
					background:#c00; color:#fff; border:none; border-radius:6px;
					padding:6px 12px; cursor:pointer; font-weight:600;
				}
			</style>

			<script>
				document.addEventListener('DOMContentLoaded', function(){
					var btn    = document.getElementById('apply-job-button');
					var modal  = document.getElementById('apply-job-modal');
					var closeB = document.getElementById('apply-job-close');
					var iframe = document.getElementById('apply-job-iframe');
					var src    = <?php echo wp_json_encode( $iframe_src ); ?>;

					if (!btn || !modal || !closeB || !iframe) return;

					function openModal(e){
						if (e) e.preventDefault();
						if (!iframe.getAttribute('src')) iframe.setAttribute('src', src); // lazy init
						modal.classList.add('is-open');
						modal.setAttribute('aria-hidden', 'false');
						document.documentElement.style.overflow = 'hidden'; // lock scroll
					}
					function closeModal(){
						modal.classList.remove('is-open');
						modal.setAttribute('aria-hidden', 'true');
						document.documentElement.style.overflow = '';
					}

					btn.addEventListener('click', openModal);
					closeB.addEventListener('click', closeModal);

					// Click backdrop to close
					modal.addEventListener('click', function(e){
						if (e.target === modal) closeModal();
					});

					// Escape to close
					document.addEventListener('keydown', function(e){
						if (e.key === 'Escape') closeModal();
					});
				});
			</script>

		<?php else : ?>

			<a class="application_button button"
			   href="<?php echo esc_url( $href ); ?>"
			   <?php if ( $apply->type === 'url' ) : ?>
				   rel="nofollow noopener"
				   target="_blank"
			   <?php endif; ?>
			>
				<?php esc_html_e( 'Apply for this job', 'wp-job-manager' ); ?>
			</a>

		<?php endif; ?>
	</div>

<?php endif; ?>
