<?php
/**
 * The front page template.
 *
 * If the static front page (Settings → Reading) is built with Elementor,
 * render its Elementor content. Otherwise, render the theme's default
 * conference homepage sections.
 *
 * @package BASCO_UAE
 */

// --- Elementor-built homepage takeover ----------------------------------
$basco_front_id = (int) get_option( 'page_on_front' );
if ( $basco_front_id && function_exists( 'basco_uae_is_built_with_elementor' ) && basco_uae_is_built_with_elementor( $basco_front_id ) ) {
	get_header();
	?>
	<main id="content" class="site-main basco-elementor-front">
		<?php
		// Run the page query and render Elementor content via the_content().
		$basco_q = new WP_Query( array( 'page_id' => $basco_front_id ) );
		if ( $basco_q->have_posts() ) {
			while ( $basco_q->have_posts() ) {
				$basco_q->the_post();
				the_content();
			}
			wp_reset_postdata();
		}
		?>
	</main>
	<?php
	get_footer();
	return;
}
// --- Otherwise: default hardcoded conference homepage -------------------

get_header();
?>

<main id="content" class="site-main">

	<!-- HERO -->
	<section class="hero">
		<div class="container">
			<div class="hero-inner">
				<span class="eyebrow"><?php echo esc_html( get_theme_mod( 'basco_hero_eyebrow', 'Official ASCO® Annual Meeting Review' ) ); ?></span>
				<h1><?php echo esc_html( get_theme_mod( 'basco_hero_title', 'Best of ASCO® UAE' ) ); ?></h1>
				<p class="hero-lede"><?php echo esc_html( get_theme_mod( 'basco_hero_subtitle', 'Practice-changing data from the ASCO® Annual Meeting, presented by leading regional and international faculty.' ) ); ?></p>

				<div class="hero-meta">
					<div class="hero-meta-item">
						<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><rect x="3" y="4" width="18" height="18" rx="2" ry="2"/><line x1="16" y1="2" x2="16" y2="6"/><line x1="8" y1="2" x2="8" y2="6"/><line x1="3" y1="10" x2="21" y2="10"/></svg>
						<div>
							<div class="label"><?php esc_html_e( 'Date', 'basco-uae' ); ?></div>
							<div class="value"><?php echo esc_html( get_theme_mod( 'basco_hero_date', '21 – 22 June 2025' ) ); ?></div>
						</div>
					</div>
					<div class="hero-meta-item">
						<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M21 10c0 7-9 13-9 13s-9-6-9-13a9 9 0 0 1 18 0z"/><circle cx="12" cy="10" r="3"/></svg>
						<div>
							<div class="label"><?php esc_html_e( 'Location', 'basco-uae' ); ?></div>
							<div class="value"><?php echo esc_html( get_theme_mod( 'basco_hero_location', 'Abu Dhabi, United Arab Emirates' ) ); ?></div>
						</div>
					</div>
					<div class="hero-meta-item">
						<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M19 21V5a2 2 0 0 0-2-2H7a2 2 0 0 0-2 2v16l7-3 7 3z"/></svg>
						<div>
							<div class="label"><?php esc_html_e( 'Hosted by', 'basco-uae' ); ?></div>
							<div class="value"><?php echo esc_html( get_theme_mod( 'basco_hero_host', 'Tawam Hospital' ) ); ?></div>
						</div>
					</div>
				</div>

				<div class="hero-actions">
					<a href="<?php echo esc_url( get_theme_mod( 'basco_hero_btn_primary_url', '#register' ) ); ?>" class="btn btn-primary">
						<?php echo esc_html( get_theme_mod( 'basco_hero_btn_primary_text', 'Register Now' ) ); ?>
					</a>
					<a href="<?php echo esc_url( get_theme_mod( 'basco_hero_btn_secondary_url', '#program' ) ); ?>" class="btn btn-light">
						<?php echo esc_html( get_theme_mod( 'basco_hero_btn_secondary_text', 'View Program' ) ); ?>
					</a>
				</div>
			</div>
		</div>
	</section>

	<!-- ABOUT -->
	<section id="about" class="section">
		<div class="container">
			<div class="about-grid">
				<div>
					<span class="eyebrow" style="color: var(--color-accent); font-weight:600; text-transform:uppercase; letter-spacing:.15em; font-size:.8rem;"><?php esc_html_e( 'About the Conference', 'basco-uae' ); ?></span>
					<h2><?php echo esc_html( get_theme_mod( 'basco_about_title', 'About the Conference' ) ); ?></h2>
					<p><?php echo wp_kses_post( nl2br( get_theme_mod( 'basco_about_text', 'Best of ASCO UAE is an Official Annual Meeting Review of the ASCO® Annual Meeting — a two-day, in-person program designed to deliver the latest practice-changing data in cancer treatment. Expert faculty review the most clinically relevant findings, discuss potential impacts on the current standard of care, and translate results into actionable insights for daily oncology practice.' ) ) ); ?></p>
					<a href="<?php echo esc_url( home_url( '/about/' ) ); ?>" class="btn btn-outline"><?php esc_html_e( 'Learn More', 'basco-uae' ); ?></a>
				</div>
				<div class="image-wrap">
					<?php
					$about_img = get_theme_mod( 'basco_about_image' );
					if ( $about_img ) {
						echo '<img src="' . esc_url( $about_img ) . '" alt="" loading="lazy" />';
					} else {
						echo '<div style="width:100%;height:100%;display:flex;align-items:center;justify-content:center;color:#fff;font-size:1rem;letter-spacing:.05em;text-transform:uppercase;font-weight:600;">' . esc_html__( 'Add Image in Customizer', 'basco-uae' ) . '</div>';
					}
					?>
				</div>
			</div>
		</div>
	</section>

	<!-- SPEAKERS -->
	<section id="faculty" class="section section--alt">
		<div class="container">
			<div class="section-head">
				<span class="eyebrow"><?php esc_html_e( 'Faculty', 'basco-uae' ); ?></span>
				<h2><?php esc_html_e( 'Distinguished Faculty', 'basco-uae' ); ?></h2>
				<p><?php esc_html_e( 'Internationally recognised oncology experts who will review the latest findings and discuss their clinical impact.', 'basco-uae' ); ?></p>
			</div>

			<div class="speakers-grid">
				<?php
				$speakers = new WP_Query( array(
					'post_type'      => 'speaker',
					'posts_per_page' => 8,
					'orderby'        => 'menu_order title',
					'order'          => 'ASC',
				) );

				if ( $speakers->have_posts() ) :
					while ( $speakers->have_posts() ) : $speakers->the_post();
						get_template_part( 'template-parts/speaker', 'card' );
					endwhile;
					wp_reset_postdata();
				else :
					// Placeholder cards so the section isn't empty on a fresh install.
					$placeholders = array(
						array( 'name' => 'Dr. Faculty Member', 'role' => 'Consultant Medical Oncologist', 'aff' => 'Tawam Hospital, UAE' ),
						array( 'name' => 'Prof. Faculty Member', 'role' => 'Senior Consultant', 'aff' => 'SQCCCRC, Oman' ),
						array( 'name' => 'Dr. Faculty Member', 'role' => 'Chief of Hematology/Oncology', 'aff' => 'GICC, Abu Dhabi' ),
						array( 'name' => 'Dr. Faculty Member', 'role' => 'Clinical Professor', 'aff' => 'Gulf Medical University, UAE' ),
					);
					foreach ( $placeholders as $p ) {
						echo '<article class="speaker-card">';
						echo '<div class="photo"><div class="placeholder">' . esc_html( basco_uae_initials( $p['name'] ) ) . '</div></div>';
						echo '<div class="info">';
						echo '<h3>' . esc_html( $p['name'] ) . '</h3>';
						echo '<div class="role">' . esc_html( $p['role'] ) . '</div>';
						echo '<p class="affiliation">' . esc_html( $p['aff'] ) . '</p>';
						echo '</div></article>';
					}
				endif;
				?>
			</div>

			<div class="text-center" style="margin-top:2.5rem;">
				<a href="<?php echo esc_url( get_post_type_archive_link( 'speaker' ) ?: home_url( '/faculty/' ) ); ?>" class="btn btn-outline"><?php esc_html_e( 'View All Faculty', 'basco-uae' ); ?></a>
			</div>
		</div>
	</section>

	<!-- AGENDA -->
	<section id="program" class="section">
		<div class="container">
			<div class="section-head">
				<span class="eyebrow"><?php esc_html_e( 'Program', 'basco-uae' ); ?></span>
				<h2><?php esc_html_e( 'Conference Program', 'basco-uae' ); ?></h2>
				<p><?php esc_html_e( 'Two days of in-depth sessions covering the most clinically relevant data from ASCO®.', 'basco-uae' ); ?></p>
			</div>

			<div class="agenda-tabs" role="tablist">
				<button class="agenda-tab is-active" data-target="day-1" role="tab" aria-selected="true"><?php esc_html_e( 'Day 1', 'basco-uae' ); ?></button>
				<button class="agenda-tab" data-target="day-2" role="tab" aria-selected="false"><?php esc_html_e( 'Day 2', 'basco-uae' ); ?></button>
			</div>

			<?php foreach ( array( 'day-1', 'day-2' ) as $i => $day ) :
				$active = ( 0 === $i ) ? ' is-active' : ''; ?>
				<div class="agenda-panel<?php echo esc_attr( $active ); ?>" id="<?php echo esc_attr( $day ); ?>" role="tabpanel">
					<ul class="agenda-list">
						<?php
						$sessions = new WP_Query( array(
							'post_type'      => 'agenda',
							'posts_per_page' => -1,
							'orderby'        => 'menu_order',
							'order'          => 'ASC',
							'meta_query'     => array(
								array(
									'key'   => '_basco_day',
									'value' => $day,
								),
							),
						) );

						if ( $sessions->have_posts() ) :
							while ( $sessions->have_posts() ) : $sessions->the_post();
								$time    = basco_uae_speaker_meta( 'time' );
								$speaker = basco_uae_speaker_meta( 'speaker' );
								$topic   = basco_uae_speaker_meta( 'topic' );
								?>
								<li class="agenda-item">
									<div class="time"><?php echo esc_html( $time ); ?></div>
									<div>
										<?php if ( $topic ) : ?><div class="topic"><?php echo esc_html( $topic ); ?></div><?php endif; ?>
										<h4><?php the_title(); ?></h4>
										<?php if ( $speaker ) : ?><p class="speaker"><?php echo esc_html( $speaker ); ?></p><?php endif; ?>
									</div>
								</li>
								<?php
							endwhile;
							wp_reset_postdata();
						else :
							// Placeholder rows
							$samples = ( 0 === $i ) ? array(
								array( '08:30 – 09:00', 'Registration', 'Welcome & Opening Remarks', '' ),
								array( '09:00 – 10:30', 'GI Cancers',   'Best of ASCO® — GI Tumours', 'Faculty Panel' ),
								array( '11:00 – 12:30', 'Breast Cancer','Best of ASCO® — Breast', 'Faculty Panel' ),
								array( '14:00 – 15:30', 'Lung Cancer',  'Best of ASCO® — Thoracic', 'Faculty Panel' ),
							) : array(
								array( '09:00 – 10:30', 'GU Cancers',   'Best of ASCO® — Genitourinary', 'Faculty Panel' ),
								array( '11:00 – 12:30', 'Hematology',   'Best of ASCO® — Hematologic Malignancies', 'Faculty Panel' ),
								array( '14:00 – 15:30', 'H&N / Skin',   'Best of ASCO® — Head & Neck, Skin', 'Faculty Panel' ),
								array( '15:45 – 16:30', 'Closing',      'Key Takeaways & Closing', '' ),
							);
							foreach ( $samples as $s ) {
								echo '<li class="agenda-item">';
								echo '<div class="time">' . esc_html( $s[0] ) . '</div>';
								echo '<div>';
								if ( $s[1] ) { echo '<div class="topic">' . esc_html( $s[1] ) . '</div>'; }
								echo '<h4>' . esc_html( $s[2] ) . '</h4>';
								if ( $s[3] ) { echo '<p class="speaker">' . esc_html( $s[3] ) . '</p>'; }
								echo '</div></li>';
							}
						endif;
						?>
					</ul>
				</div>
			<?php endforeach; ?>
		</div>
	</section>

	<!-- SPONSORS -->
	<section id="sponsors" class="section section--alt">
		<div class="container">
			<div class="section-head">
				<span class="eyebrow"><?php esc_html_e( 'Partners', 'basco-uae' ); ?></span>
				<h2><?php esc_html_e( 'Sponsors & Supporters', 'basco-uae' ); ?></h2>
			</div>

			<div class="sponsors-grid">
				<?php
				$sponsors = new WP_Query( array(
					'post_type'      => 'sponsor',
					'posts_per_page' => 12,
					'orderby'        => 'menu_order',
					'order'          => 'ASC',
				) );

				if ( $sponsors->have_posts() ) {
					while ( $sponsors->have_posts() ) : $sponsors->the_post();
						$url = basco_uae_speaker_meta( 'url' );
						$tag_open  = $url ? '<a href="' . esc_url( $url ) . '" target="_blank" rel="noopener" class="sponsor-logo">' : '<div class="sponsor-logo">';
						$tag_close = $url ? '</a>' : '</div>';
						echo $tag_open; // phpcs:ignore
						if ( has_post_thumbnail() ) {
							the_post_thumbnail( 'medium', array( 'alt' => esc_attr( get_the_title() ) ) );
						} else {
							echo '<span style="font-weight:600;color:var(--color-text-light);">' . esc_html( get_the_title() ) . '</span>';
						}
						echo $tag_close; // phpcs:ignore
					endwhile;
					wp_reset_postdata();
				} else {
					for ( $i = 1; $i <= 6; $i++ ) {
						echo '<div class="sponsor-logo"><span style="color:var(--color-text-light);font-weight:600;">' . esc_html__( 'Sponsor Logo', 'basco-uae' ) . '</span></div>';
					}
				}
				?>
			</div>
		</div>
	</section>

	<!-- CTA -->
	<section id="register" class="cta-strip">
		<div class="container">
			<h2><?php esc_html_e( 'Reserve Your Place', 'basco-uae' ); ?></h2>
			<p><?php esc_html_e( 'Join leading oncologists and clinicians for two days of practice-changing insights and discussion.', 'basco-uae' ); ?></p>
			<a href="<?php echo esc_url( home_url( '/registration/' ) ); ?>" class="btn btn-light"><?php esc_html_e( 'Register for the Conference', 'basco-uae' ); ?></a>
		</div>
	</section>

</main>

<?php get_footer(); ?>
