<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}
$varsayilan_sira = tilfy_galeri_varsayilan_sira();
$filtre_arama    = tilfy_galeri_filtre_aktif_mi( 'arama' );
$filtre_tarih    = tilfy_galeri_filtre_aktif_mi( 'tarih' );
$filtre_medya    = tilfy_galeri_filtre_aktif_mi( 'medya' );
$filtre_sira     = tilfy_galeri_filtre_aktif_mi( 'sira' );
$lazy_modal      = tilfy_galeri_modal_lazy_mi();
$sayfa           = isset( $sayfa ) ? max( 1, (int) $sayfa ) : 1;
$sayfa_sayisi    = isset( $sayfa_sayisi ) ? max( 1, (int) $sayfa_sayisi ) : 1;
$toplam          = isset( $toplam ) ? max( 0, (int) $toplam ) : count( (array) $icerikler );
?>
<div
	class="tilfy-galeri"
	id="<?php echo esc_attr( $instance ); ?>"
	data-varsayilan-sira="<?php echo esc_attr( $varsayilan_sira ); ?>"
	data-lazy="<?php echo $lazy_modal ? '1' : '0'; ?>"
>
	<?php if ( $filtre_arama || $filtre_tarih || $filtre_medya || $filtre_sira ) : ?>
		<div class="tilfy-galeri__filtreler" role="search">
			<?php if ( $filtre_arama ) : ?>
				<label class="tilfy-galeri__filtre tilfy-galeri__filtre--arama">
					<span class="tilfy-galeri__filtre-etiket"><?php echo esc_html( tilfy__( 'Ara' ) ); ?></span>
					<input type="search" class="tilfy-galeri__kontrol tilfy-galeri__arama" placeholder="<?php echo esc_attr( tilfy__( 'İçerik ara…' ) ); ?>" autocomplete="off" />
				</label>
			<?php endif; ?>
			<?php if ( $filtre_tarih ) : ?>
				<label class="tilfy-galeri__filtre">
					<span class="tilfy-galeri__filtre-etiket"><?php echo esc_html( tilfy__( 'Tarih' ) ); ?></span>
					<input type="date" class="tilfy-galeri__kontrol tilfy-galeri__tarih" />
				</label>
			<?php endif; ?>
			<?php if ( $filtre_medya ) : ?>
				<label class="tilfy-galeri__filtre">
					<span class="tilfy-galeri__filtre-etiket"><?php echo esc_html( tilfy__( 'Medya' ) ); ?></span>
					<select class="tilfy-galeri__kontrol tilfy-galeri__medya">
						<option value=""><?php echo esc_html( tilfy__( 'Tümü' ) ); ?></option>
						<option value="gorsel"><?php echo esc_html( tilfy__( 'Görsel' ) ); ?></option>
						<option value="video"><?php echo esc_html( tilfy__( 'Video' ) ); ?></option>
					</select>
				</label>
			<?php endif; ?>
			<?php if ( $filtre_sira ) : ?>
				<label class="tilfy-galeri__filtre">
					<span class="tilfy-galeri__filtre-etiket"><?php echo esc_html( tilfy__( 'Sıralama' ) ); ?></span>
					<select class="tilfy-galeri__kontrol tilfy-galeri__sira">
						<option value="yeni"<?php echo $varsayilan_sira === 'yeni' ? ' selected' : ''; ?>><?php echo esc_html( tilfy__( 'Yeni → eski' ) ); ?></option>
						<option value="eski"<?php echo $varsayilan_sira === 'eski' ? ' selected' : ''; ?>><?php echo esc_html( tilfy__( 'Eski → yeni' ) ); ?></option>
					</select>
				</label>
			<?php endif; ?>
		</div>
	<?php endif; ?>

	<ul class="tilfy-galeri__liste" role="list">
		<?php if ( empty( $icerikler ) ) : ?>
			<li class="tilfy-galeri__bos-liste"><?php echo esc_html( tilfy__( 'Henüz listelenecek içerik yok.' ) ); ?></li>
		<?php else : ?>
		<?php foreach ( $icerikler as $satir ) :
			$veri = tilfy_galeri_icerik_satir_verisi( $satir );
			if ( ! $veri ) {
				continue;
			}
			$modal_json = '';
			if ( ! $lazy_modal ) {
				$modal_ham = tilfy_galeri_icerik_modal_verisi( $satir );
				if ( $modal_ham ) {
					$modal_json = wp_json_encode( $modal_ham );
				}
			}
			?>
			<li
				class="tilfy-galeri__oge"
				data-id="<?php echo (int) $veri['id']; ?>"
				data-token="<?php echo esc_attr( $veri['token'] ); ?>"
				data-baslik="<?php echo esc_attr( $veri['baslik'] ); ?>"
				data-platform="<?php echo esc_attr( $veri['platform'] ); ?>"
				data-medya="<?php echo esc_attr( $veri['medya_grup'] ); ?>"
				data-tarih="<?php echo esc_attr( (string) $veri['tarih_ts'] ); ?>"
				data-tarih-gun="<?php echo esc_attr( $veri['tarih_gun'] ); ?>"
				data-arama="<?php echo esc_attr( $veri['arama'] ); ?>"
				<?php if ( $modal_json !== '' ) : ?>
					data-modal="<?php echo esc_attr( $modal_json ); ?>"
				<?php endif; ?>
			>
				<button type="button" class="tilfy-galeri__oge-tetik">
					<span class="tilfy-galeri__oge-baslik"><?php echo esc_html( $veri['baslik'] ); ?></span>
					<span class="tilfy-galeri__oge-meta">
						<?php echo esc_html( $veri['platform_etiket'] ); ?>
						<?php if ( $veri['tarih'] !== '' ) : ?>
							· <?php echo esc_html( $veri['tarih'] ); ?>
						<?php endif; ?>
						· <?php echo esc_html( $veri['medya_etiket'] ); ?>
					</span>
				</button>
			</li>
		<?php endforeach; ?>
		<?php endif; ?>
	</ul>
	<p class="tilfy-galeri__bos" hidden><?php echo esc_html( tilfy__( 'Sonuç bulunamadı.' ) ); ?></p>

	<?php if ( $sayfa_sayisi > 1 ) : ?>
		<nav class="tilfy-galeri__sayfalama" aria-label="<?php echo esc_attr( tilfy__( 'Sayfalama' ) ); ?>">
			<?php if ( $sayfa > 1 ) : ?>
				<a class="tilfy-galeri__sayfa-link" href="<?php echo esc_url( tilfy_galeri_sayfa_url( $sayfa - 1 ) ); ?>"><?php echo esc_html( tilfy__( 'Önceki' ) ); ?></a>
			<?php endif; ?>
			<span class="tilfy-galeri__sayfa-durum">
				<?php
				echo esc_html(
					sprintf(
						tilfy__( 'Sayfa %1$d / %2$d (%3$d içerik)' ),
						(int) $sayfa,
						(int) $sayfa_sayisi,
						(int) $toplam
					)
				);
				?>
			</span>
			<?php if ( $sayfa < $sayfa_sayisi ) : ?>
				<a class="tilfy-galeri__sayfa-link" href="<?php echo esc_url( tilfy_galeri_sayfa_url( $sayfa + 1 ) ); ?>"><?php echo esc_html( tilfy__( 'Sonraki' ) ); ?></a>
			<?php endif; ?>
		</nav>
	<?php endif; ?>
</div>
