<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}
$durum_sinif  = sanitize_html_class( $p->durum );
$durum_yazi   = function_exists( 'tilfy_paylasim_durum_etiket' )
	? tilfy_paylasim_durum_etiket( $p->durum, $p->zamanlama ?? null )
	: $p->durum;
$otomasyon_rozet = function_exists( 'tilfy_paylasim_kaynak_etiket' )
	? tilfy_paylasim_kaynak_etiket( $p->kaynak ?? 'manuel' )
	: '';
$baslik       = $p->baslik ?: wp_trim_words( wp_strip_all_tags( $p->aciklama ?? '' ), 10, '…' );
if ( $baslik === '' ) {
	$baslik = '#' . (int) $p->icerik_id;
}
$p_kapak      = function_exists( 'tilfy_icerik_kapak_url_al' ) ? tilfy_icerik_kapak_url_al( $p ) : ( $p->kapak_url ?: $p->medya_url );
$kapak        = $p_kapak;
$kaynak_url   = function_exists( 'tilfy_icerik_kaynak_url' ) ? tilfy_icerik_kaynak_url( $p ) : '';
$studyosu_url = add_query_arg( array( 'page' => 'tilfy-studyosu', 'icerik_id' => (int) $p->icerik_id ), admin_url( 'admin.php' ) );
$ikon_kaynak  = function_exists( 'tilfy_platform_ikon_ad' ) ? tilfy_platform_ikon_ad( $p->kaynak_platform ?? '' ) : '';
$ikon_hedef   = function_exists( 'tilfy_platform_ikon_ad' ) ? tilfy_platform_ikon_ad( $p->hedef_platform ) : '';
$durum_sinif_kart = 'tilfy-paylasim-kart--' . $durum_sinif;
if ( in_array( $p->durum, array( 'yuklendi', 'manuel_bekliyor' ), true ) ) {
	$durum_sinif_kart = 'tilfy-paylasim-kart--basari';
} elseif ( $p->durum === 'hata' ) {
	$durum_sinif_kart = 'tilfy-paylasim-kart--hata';
}
$ipucu = $durum_yazi;
if ( $p->hata_mesaji ) {
	$ipucu = $p->hata_mesaji;
} elseif ( in_array( $p->durum, array( 'yuklendi', 'manuel_bekliyor' ), true ) ) {
	$ipucu = sprintf( tilfy__( 'Yükleme tamamlandı · %s' ), tilfy_platform_etiket( $p->hedef_platform ) );
}
$yeniden_goster = in_array( $p->durum, array( 'hata', 'beklemede', 'yukleniyor', 'zamanlandi', 'sirada' ), true );
$tekrar_goster  = in_array( $p->durum, array( 'hata', 'beklemede', 'yukleniyor', 'zamanlandi' ), true );
$manuel_tamam   = (string) $p->durum === 'manuel_bekliyor';
?>
<article
	class="tilfy-grid-kart tilfy-paylasim-kart <?php echo esc_attr( $durum_sinif_kart ); ?>"
	data-paylasim-id="<?php echo (int) $p->id; ?>"
	data-durum="<?php echo esc_attr( $p->durum ); ?>"
>
	<div class="tilfy-grid-kart__cerceve">
		<label class="tilfy-paylasim-kart__sec">
			<input type="checkbox" class="tilfy-paylasim-sec" name="paylasim_idler[]" value="<?php echo (int) $p->id; ?>" />
			<span class="tilfy-paylasim-kart__sec-kutu" aria-hidden="true"></span>
		</label>
		<?php if ( $kapak ) : ?>
			<img class="tilfy-grid-kart__gorsel" src="<?php echo esc_url( $kapak ); ?>" alt="" loading="lazy" decoding="async" />
		<?php else : ?>
			<div class="tilfy-grid-kart__bos-gorsel"></div>
		<?php endif; ?>
		<div class="tilfy-grid-kart__overlay" aria-hidden="true"></div>
		<div class="tilfy-paylasim-kart__durum-overlay" aria-hidden="true"></div>
		<div class="tilfy-grid-kart__ust">
			<?php if ( $ikon_kaynak !== '' ) : ?>
				<?php tilfy_ikon_img_e( $ikon_kaynak, array( 'class' => 'tilfy-grid-kart__platform-ikon', 'alt' => tilfy_platform_etiket( $p->kaynak_platform ?? '' ) ) ); ?>
			<?php endif; ?>
			<span class="tilfy-paylasim-kart__ok" aria-hidden="true">→</span>
			<?php if ( $ikon_hedef !== '' ) : ?>
				<?php tilfy_ikon_img_e( $ikon_hedef, array( 'class' => 'tilfy-grid-kart__platform-ikon', 'alt' => tilfy_platform_etiket( $p->hedef_platform ) ) ); ?>
			<?php else : ?>
				<span class="tilfy-rozet tilfy-rozet--<?php echo esc_attr( $p->hedef_platform ); ?>"><?php echo esc_html( tilfy_platform_etiket( $p->hedef_platform ) ); ?></span>
			<?php endif; ?>
		</div>
		<div class="tilfy-grid-kart__alt-sol">
			<span class="tilfy-paylasim-kart__durum-yazi"><?php echo esc_html( $durum_yazi ); ?></span>
			<?php if ( $otomasyon_rozet !== '' ) : ?>
				<span class="tilfy-rozet tilfy-rozet--otomasyon"><?php echo esc_html( $otomasyon_rozet ); ?></span>
			<?php endif; ?>
		</div>
		<div class="tilfy-grid-kart__alt-sag tilfy-paylasim-kart__linkler">
			<button type="button" class="tilfy-grid-kart__aksiyon tilfy-paylasim-detay-ac" data-paylasim-id="<?php echo (int) $p->id; ?>" title="<?php echo esc_attr( tilfy__( 'Detay' ) ); ?>" aria-label="<?php echo esc_attr( tilfy__( 'Detay' ) ); ?>"><?php echo esc_html( tilfy__( 'i' ) ); ?></button>
			<?php if ( $kaynak_url ) : ?>
				<a class="tilfy-grid-kart__aksiyon" href="<?php echo esc_url( $kaynak_url ); ?>" target="_blank" rel="noopener noreferrer" title="<?php echo esc_attr( tilfy__( 'Kaynağı aç' ) ); ?>" aria-label="<?php echo esc_attr( tilfy__( 'Kaynağı aç' ) ); ?>">↗</a>
			<?php endif; ?>
			<?php if ( $p->hedef_url ) : ?>
				<a class="tilfy-grid-kart__aksiyon" href="<?php echo esc_url( $p->hedef_url ); ?>" target="_blank" rel="noopener noreferrer" title="<?php echo esc_attr( tilfy__( 'Hedefi aç' ) ); ?>" aria-label="<?php echo esc_attr( tilfy__( 'Hedefi aç' ) ); ?>"><?php echo esc_html( tilfy__( '✓' ) ); ?></a>
			<?php endif; ?>
			<a class="tilfy-grid-kart__aksiyon" href="<?php echo esc_url( $studyosu_url ); ?>" title="<?php echo esc_attr( tilfy__( 'Studyoda aç' ) ); ?>" aria-label="<?php echo esc_attr( tilfy__( 'Studyoda aç' ) ); ?>">
				<?php tilfy_ikon_img_e( 'studyoda-ac', array( 'class' => 'tilfy-grid-kart__aksiyon-ikon', 'alt' => '' ) ); ?>
			</a>
			<?php if ( in_array( $p->durum, array( 'hata', 'beklemede', 'yukleniyor', 'zamanlandi', 'sirada' ), true ) ) : ?>
				<button type="button" class="tilfy-grid-kart__aksiyon tilfy-paylasim-yoksay" data-paylasim-id="<?php echo (int) $p->id; ?>" title="<?php echo esc_attr( tilfy__( 'Yoksay' ) ); ?>" aria-label="<?php echo esc_attr( tilfy__( 'Yoksay' ) ); ?>">⊘</button>
			<?php endif; ?>
		</div>
		<div class="tilfy-grid-kart__ipucu tilfy-paylasim-kart__ipucu">
			<strong><?php echo esc_html( $baslik ); ?></strong>
			<p><?php echo esc_html( $ipucu ); ?></p>
			<?php if ( $kaynak_url || $p->hedef_url ) : ?>
				<p class="tilfy-paylasim-kart__ipucu-linkler">
					<?php if ( $kaynak_url ) : ?><a href="<?php echo esc_url( $kaynak_url ); ?>" target="_blank" rel="noopener noreferrer"><?php echo esc_html( tilfy__( 'Kaynak' ) ); ?></a><?php endif; ?>
					<?php if ( $kaynak_url && $p->hedef_url ) : ?> · <?php endif; ?>
					<?php if ( $p->hedef_url ) : ?><a href="<?php echo esc_url( $p->hedef_url ); ?>" target="_blank" rel="noopener noreferrer"><?php echo esc_html( tilfy__( 'Hedef' ) ); ?></a><?php endif; ?>
				</p>
			<?php endif; ?>
			<?php if ( $manuel_tamam ) : ?>
				<p class="tilfy-paylasim-kart__ipucu-aksiyon tilfy-medya-aksiyonlar">
					<button type="button" class="tilfy-btn tilfy-btn--kucuk tilfy-btn--birincil tilfy-paylasim-manuel-tamam" data-paylasim-id="<?php echo (int) $p->id; ?>"><?php echo esc_html( tilfy__( 'Yüklendi işaretle' ) ); ?></button>
				</p>
			<?php elseif ( $yeniden_goster ) : ?>
				<p class="tilfy-paylasim-kart__ipucu-aksiyon tilfy-medya-aksiyonlar">
					<?php $wp_kaynak = sanitize_key( (string) ( $p->kaynak_platform ?? '' ) ) === 'wordpress'; ?>
					<button type="button" class="tilfy-btn tilfy-btn--kucuk tilfy-btn--ikincil tilfy-paylasim-medya-ata" data-paylasim-id="<?php echo (int) $p->id; ?>"><?php echo esc_html( tilfy__( 'Yeni ekle' ) ); ?></button>
					<?php if ( $kapak && $wp_kaynak ) : ?>
						<button type="button" class="tilfy-btn tilfy-btn--kucuk tilfy-btn--ikincil tilfy-paylasim-one-cikan" data-paylasim-id="<?php echo (int) $p->id; ?>"><?php echo esc_html( tilfy__( 'Öne çıkanı kullan' ) ); ?></button>
					<?php endif; ?>
					<?php if ( $tekrar_goster ) : ?>
						<button type="button" class="tilfy-btn tilfy-btn--kucuk tilfy-btn--ikincil tilfy-paylasim-tekrar" data-paylasim-id="<?php echo (int) $p->id; ?>"><?php echo esc_html( tilfy__( 'Yeniden dene' ) ); ?></button>
					<?php endif; ?>
					<button type="button" class="tilfy-btn tilfy-btn--kucuk tilfy-btn--ghost tilfy-paylasim-yoksay" data-paylasim-id="<?php echo (int) $p->id; ?>"><?php echo esc_html( tilfy__( 'Yoksay' ) ); ?></button>
				</p>
			<?php endif; ?>
		</div>
	</div>
</article>
