<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}
$durum_etiket = array(
	'beklemede'       => tilfy__( 'Beklemede' ),
	'yukleniyor'      => tilfy__( 'Yükleniyor' ),
	'yuklendi'        => tilfy__( 'Yüklendi' ),
	'hata'            => tilfy__( 'Hata' ),
	'manuel_bekliyor' => tilfy__( 'Manuel' ),
	'zamanlandi'      => tilfy__( 'Zamanlandı' ),
	'sirada'          => tilfy__( 'Sırada' ),
);
$grup_filtre = isset( $grup_filtre ) ? $grup_filtre : ( isset( $_GET['grup'] ) ? sanitize_key( wp_unslash( $_GET['grup'] ) ) : '' );
$gorunum     = isset( $gorunum ) ? $gorunum : 'liste';
$tilfy_paylasimlar_url = function ( $ek = array() ) use ( $durum_filtre, $hedef_filtre, $arama, $gizle_yuklendi, $grup_filtre, $sayfa, $gorunum ) {
	$q = array( 'page' => 'tilfy-paylasimlar' );
	$durum = array_key_exists( 'durum', $ek ) ? (string) $ek['durum'] : $durum_filtre;
	$hedef = array_key_exists( 'hedef', $ek ) ? (string) $ek['hedef'] : $hedef_filtre;
	$ara   = array_key_exists( 'arama', $ek ) ? (string) $ek['arama'] : $arama;
	$grup  = array_key_exists( 'grup', $ek ) ? (string) $ek['grup'] : $grup_filtre;
	if ( $durum !== '' ) {
		$q['durum'] = $durum;
	}
	if ( $hedef !== '' ) {
		$q['hedef'] = $hedef;
	}
	if ( $ara !== '' ) {
		$q['arama'] = $ara;
	}
	if ( array_key_exists( 'gizle_yuklendi', $ek ) ) {
		if ( $ek['gizle_yuklendi'] === '1' ) {
			$q['gizle_yuklendi'] = '1';
		}
	} elseif ( $gizle_yuklendi ) {
		$q['gizle_yuklendi'] = '1';
	}
	if ( $grup !== '' ) {
		$q['grup'] = $grup;
	}
	$gor = (string) ( array_key_exists( 'gorunum', $ek ) ? $ek['gorunum'] : $gorunum );
	$q['gorunum'] = $gor === 'grid' ? 'grid' : 'liste';
	$sayfa_no = (int) ( $ek['sayfa'] ?? $sayfa );
	if ( $sayfa_no > 1 ) {
		$q['sayfa'] = $sayfa_no;
	}
	return add_query_arg( $q, admin_url( 'admin.php' ) );
};
$tilfy_paylasim_filtre_gizli = function () use ( $durum_filtre, $hedef_filtre, $arama, $gizle_yuklendi, $grup_filtre, $sayfa, $gorunum ) {
	?>
	<input type="hidden" name="page" value="tilfy-paylasimlar" />
	<input type="hidden" name="gorunum" value="<?php echo esc_attr( $gorunum ); ?>" />
	<?php if ( $durum_filtre !== '' ) : ?><input type="hidden" name="durum" value="<?php echo esc_attr( $durum_filtre ); ?>" /><?php endif; ?>
	<?php if ( $hedef_filtre !== '' ) : ?><input type="hidden" name="hedef" value="<?php echo esc_attr( $hedef_filtre ); ?>" /><?php endif; ?>
	<?php if ( $arama !== '' ) : ?><input type="hidden" name="arama" value="<?php echo esc_attr( $arama ); ?>" /><?php endif; ?>
	<?php if ( $gizle_yuklendi ) : ?><input type="hidden" name="gizle_yuklendi" value="1" /><?php endif; ?>
	<?php if ( $grup_filtre !== '' ) : ?><input type="hidden" name="grup" value="<?php echo esc_attr( $grup_filtre ); ?>" /><?php endif; ?>
	<?php
};
$filtre_aktif = ( $durum_filtre !== '' || $hedef_filtre !== '' || $arama !== '' || $gizle_yuklendi || $grup_filtre !== '' );
$yuklenen_gizle_url = $tilfy_paylasimlar_url(
	array(
		'gizle_yuklendi' => $gizle_yuklendi ? '' : '1',
		'sayfa'          => 1,
	)
);
?>
<div class="wrap tilfy-wrap" id="tilfy-paylasimlar-sayfa" data-canli="<?php echo isset( $_GET['canli'] ) ? '1' : '0'; ?>" data-ids="<?php echo isset( $_GET['ids'] ) ? esc_attr( sanitize_text_field( wp_unslash( $_GET['ids'] ) ) ) : ''; ?>">
	<header class="tilfy-header tilfy-header--row">
		<div>
			<h1><?php echo esc_html( tilfy__( 'Paylaşımlar' ) ); ?></h1>
		</div>
		<div class="tilfy-paylasim-ust">
			<form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>" class="tilfy-inline-form">
				<?php wp_nonce_field( 'tilfy_kuyruk_calistir' ); ?>
				<input type="hidden" name="action" value="tilfy_kuyruk_calistir" />
				<button type="submit" class="tilfy-btn tilfy-btn--ikincil tilfy-btn--kucuk" title="<?php echo esc_attr( tilfy__( 'Sadece takılan işler için' ) ); ?>"><?php echo esc_html( tilfy__( 'Takılı işleri sürdür' ) ); ?></button>
			</form>
			<form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>" class="tilfy-inline-form">
				<?php wp_nonce_field( 'tilfy_log_indir' ); ?>
				<input type="hidden" name="action" value="tilfy_log_indir" />
				<button type="submit" class="tilfy-btn tilfy-btn--yesil tilfy-btn--kucuk"><?php echo esc_html( tilfy__( 'İşlem Logu ↓' ) ); ?></button>
			</form>
		</div>
	</header>

	<?php if ( $mesaj === 'paylasim_kuyruga' || $mesaj === 'capraz_basladi' ) : ?>
		<div class="tilfy-notice tilfy-notice--ok"><?php echo esc_html( tilfy__( 'Paylaşımlar kuyruğa eklendi.' ) ); ?></div>
	<?php elseif ( $mesaj === 'manuel_tamam' ) : ?>
		<div class="tilfy-notice tilfy-notice--ok"><?php echo esc_html( tilfy__( 'Manuel paylaşım tamamlandı.' ) ); ?></div>
	<?php elseif ( $mesaj === 'kuyruk_calisti' ) : ?>
		<div class="tilfy-notice tilfy-notice--ok"><?php echo esc_html( tilfy__( 'Takılı kuyruk işlendi.' ) ); ?></div>
	<?php elseif ( $mesaj === 'toplu_silindi' ) : ?>
		<div class="tilfy-notice tilfy-notice--ok"><?php echo (int) ( $_GET['adet'] ?? 0 ); ?> <?php echo esc_html( tilfy__( 'kayıt silindi.' ) ); ?></div>
	<?php elseif ( $mesaj === 'silindi' ) : ?>
		<div class="tilfy-notice tilfy-notice--ok"><?php echo esc_html( tilfy__( 'Kayıt silindi.' ) ); ?></div>
	<?php elseif ( $mesaj === 'yeniden_kuyruk' ) : ?>
		<div class="tilfy-notice tilfy-notice--ok"><?php echo esc_html( tilfy__( 'Paylaşım yeniden kuyruğa eklendi.' ) ); ?></div>
	<?php endif; ?>

	<form method="get" class="tilfy-icerik-filtre tilfy-icerik-filtre--v2 tilfy-kart" action="<?php echo esc_url( admin_url( 'admin.php' ) ); ?>">
		<input type="hidden" name="page" value="tilfy-paylasimlar" />
		<input type="hidden" name="gorunum" value="<?php echo esc_attr( $gorunum ); ?>" />
		<?php if ( $gizle_yuklendi ) : ?><input type="hidden" name="gizle_yuklendi" value="1" /><?php endif; ?>
		<?php if ( $grup_filtre !== '' ) : ?><input type="hidden" name="grup" value="<?php echo esc_attr( $grup_filtre ); ?>" /><?php endif; ?>
		<div class="tilfy-icerik-filtre__ust">
			<label class="tilfy-filtre-alan">
				<span class="tilfy-filtre-alan__etiket"><?php echo esc_html( tilfy__( 'Durum' ) ); ?></span>
				<select name="durum" class="tilfy-filtre-kontrol">
					<option value="" <?php selected( $durum_filtre, '' ); ?>><?php echo esc_html( tilfy__( 'Tümü' ) ); ?></option>
					<?php foreach ( $durum_etiket as $anahtar => $etiket ) : ?>
						<option value="<?php echo esc_attr( $anahtar ); ?>" <?php selected( $durum_filtre, $anahtar ); ?>><?php echo esc_html( $etiket ); ?></option>
					<?php endforeach; ?>
				</select>
			</label>
			<label class="tilfy-filtre-alan">
				<span class="tilfy-filtre-alan__etiket"><?php echo esc_html( tilfy__( 'Hedef platform' ) ); ?></span>
				<select name="hedef" class="tilfy-filtre-kontrol">
					<option value=""><?php echo esc_html( tilfy__( 'Tümü' ) ); ?></option>
					<?php foreach ( $platformlar as $anahtar => $bilgi ) : ?>
						<option value="<?php echo esc_attr( $anahtar ); ?>" <?php selected( $hedef_filtre, $anahtar ); ?>><?php echo esc_html( $bilgi['etiket'] ); ?></option>
					<?php endforeach; ?>
				</select>
			</label>
			<label class="tilfy-filtre-alan tilfy-filtre-alan--arama">
				<span class="tilfy-filtre-alan__etiket"><?php echo esc_html( tilfy__( 'Ara' ) ); ?></span>
				<input type="search" name="arama" class="tilfy-filtre-kontrol" value="<?php echo esc_attr( $arama ); ?>" placeholder="<?php echo esc_attr( tilfy__( 'Başlık, açıklama veya hata mesajı' ) ); ?>" />
			</label>
			<div class="tilfy-filtre-alan tilfy-filtre-alan--aksiyon">
				<button type="submit" class="tilfy-btn tilfy-btn--birincil tilfy-btn--kucuk"><?php echo esc_html( tilfy__( 'Filtrele' ) ); ?></button>
				<a class="tilfy-btn tilfy-btn--ikincil tilfy-btn--kucuk tilfy-filtre-toggle<?php echo $gizle_yuklendi ? ' is-active' : ''; ?>" href="<?php echo esc_url( $yuklenen_gizle_url ); ?>"><?php echo esc_html( tilfy__( 'Yüklenenleri gizle' ) ); ?></a>
				<a class="tilfy-btn tilfy-btn--ikincil tilfy-btn--kucuk<?php echo $grup_filtre === 'kapak_eksik' ? ' is-active' : ''; ?>" href="<?php echo esc_url( $tilfy_paylasimlar_url( array( 'grup' => 'kapak_eksik', 'durum' => '', 'sayfa' => 1 ) ) ); ?>"><?php echo esc_html( tilfy__( 'Kapak eksik' ) ); ?></a>
				<?php if ( $filtre_aktif ) : ?>
					<a class="tilfy-btn tilfy-btn--ikincil tilfy-btn--kucuk" href="<?php echo esc_url( admin_url( 'admin.php?page=tilfy-paylasimlar' ) ); ?>"><?php echo esc_html( tilfy__( 'Sıfırla' ) ); ?></a>
				<?php endif; ?>
			</div>
		</div>
	</form>

	<p class="tilfy-icerik-ozet tilfy-paylasim-ozet">
		<?php if ( $grup_filtre === 'bekleyen' ) : ?>
			<span class="tilfy-rozet tilfy-rozet--bekliyor"><?php echo esc_html( tilfy__( 'Bekleyen grubu' ) ); ?></span>
		<?php endif; ?>
		<?php echo (int) $toplam; ?> <?php echo esc_html( tilfy__( 'paylaşım · Sayfa' ) ); ?> <?php echo (int) $sayfa; ?> / <?php echo (int) $sayfa_sayisi; ?>
		<span class="tilfy-icerik-gorunum tilfy-icerik-gorunum--bar tilfy-paylasim-gorunum">
			<a class="tilfy-gorunum-btn <?php echo $gorunum === 'liste' ? 'is-active' : ''; ?>" href="<?php echo esc_url( $tilfy_paylasimlar_url( array( 'gorunum' => 'liste' ) ) ); ?>"><?php echo esc_html( tilfy__( 'Liste' ) ); ?></a>
			<a class="tilfy-gorunum-btn <?php echo $gorunum === 'grid' ? 'is-active' : ''; ?>" href="<?php echo esc_url( $tilfy_paylasimlar_url( array( 'gorunum' => 'grid' ) ) ); ?>"><?php echo esc_html( tilfy__( 'Izgara' ) ); ?></a>
		</span>
	</p>

	<form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>" id="tilfy-paylasim-toplu-form">
		<?php wp_nonce_field( 'tilfy_paylasim_toplu' ); ?>
		<input type="hidden" name="action" value="tilfy_paylasim_toplu" />
		<input type="hidden" name="toplu_tip" id="tilfy-toplu-tip" value="" />
		<input type="hidden" name="toplu_platform" id="tilfy-toplu-platform" value="" />

		<div class="tilfy-toplu-cubuk tilfy-toplu-cubuk--paylasim">
			<button type="button" class="tilfy-btn tilfy-btn--kucuk tilfy-btn--ikincil" id="tilfy-tum-paylasim-sec-btn"><?php echo esc_html( tilfy__( 'Tümünü seç' ) ); ?></button>
			<button type="button" class="tilfy-btn tilfy-btn--kucuk tilfy-btn--ikincil" id="tilfy-paylasim-medya-ata-sec-btn"><?php echo esc_html( tilfy__( 'Seçilenlere yeni ekle' ) ); ?></button>
			<button type="button" class="tilfy-btn tilfy-btn--kucuk tilfy-btn--ikincil" id="tilfy-paylasim-one-cikan-sec-btn"><?php echo esc_html( tilfy__( 'Seçilenlere öne çıkanı kullan' ) ); ?></button>
			<button type="button" class="tilfy-btn tilfy-btn--kucuk tilfy-btn--ikincil" id="tilfy-paylasim-yeniden-sec-btn"><?php echo esc_html( tilfy__( 'Seçilenleri yeniden dene' ) ); ?></button>
			<select id="tilfy-toplu-platform-sec" class="tilfy-secim">
				<option value=""><?php echo esc_html( tilfy__( 'Platform' ) ); ?></option>
				<?php foreach ( $platformlar as $anahtar => $bilgi ) : ?>
					<option value="<?php echo esc_attr( $anahtar ); ?>"><?php echo esc_html( $bilgi['etiket'] ); ?></option>
				<?php endforeach; ?>
			</select>
			<select id="tilfy-toplu-islem-sec" class="tilfy-secim">
				<option value=""><?php echo esc_html( tilfy__( 'İşlem seçin' ) ); ?></option>
				<option value="secili"><?php echo esc_html( tilfy__( 'Seçilenleri sil' ) ); ?></option>
				<option value="platform"><?php echo esc_html( tilfy__( 'Platformu sil' ) ); ?></option>
				<option value="hata"><?php echo esc_html( tilfy__( 'Hataları sil' ) ); ?></option>
				<option value="basari"><?php echo esc_html( tilfy__( 'Yüklenenleri sil' ) ); ?></option>
			</select>
			<button type="button" class="tilfy-btn tilfy-btn--kucuk tilfy-btn--birincil" id="tilfy-toplu-uygula"><?php echo esc_html( tilfy__( 'Uygula' ) ); ?></button>
		</div>

		<div class="tilfy-tablo-wrap<?php echo $gorunum === 'grid' ? ' tilfy-is-gizli' : ''; ?>">
			<table class="tilfy-tablo tilfy-tablo--siralanabilir" id="tilfy-paylasim-tablo">
				<thead>
					<tr>
						<th class="tilfy-th-sec"></th>
						<th class="tilfy-th-sort" data-sort="icerik" data-type="text"><?php echo esc_html( tilfy__( 'İçerik' ) ); ?></th>
						<th class="tilfy-th-sort" data-sort="hedef" data-type="text"><?php echo esc_html( tilfy__( 'Hedef' ) ); ?></th>
						<th class="tilfy-th-sort" data-sort="durum" data-type="text"><?php echo esc_html( tilfy__( 'Durum' ) ); ?></th>
						<th><?php echo esc_html( tilfy__( 'Link' ) ); ?></th>
						<th class="tilfy-th-sort tilfy-paylasim-liste-guncelleme" data-sort="guncelleme" data-type="date"><?php echo esc_html( tilfy__( 'Güncelleme' ) ); ?></th>
						<th><?php echo esc_html( tilfy__( 'İşlem' ) ); ?></th>
					</tr>
				</thead>
				<tbody id="tilfy-paylasim-tbody">
					<?php if ( empty( $paylasimlar ) ) : ?>
						<tr><td colspan="7" class="tilfy-bos"><?php echo esc_html( tilfy__( 'Paylaşım yok.' ) ); ?></td></tr>
					<?php else : ?>
						<?php foreach ( $paylasimlar as $p ) :
							require TILFY_YOL . 'includes/sayfalar/partials/paylasim-liste-satir.php';
						endforeach; ?>
					<?php endif; ?>
				</tbody>
			</table>
		</div>

		<div class="tilfy-grid-wrap tilfy-paylasim-grid<?php echo $gorunum !== 'grid' ? ' tilfy-is-gizli' : ''; ?>" id="tilfy-paylasim-grid">
			<?php if ( empty( $paylasimlar ) ) : ?>
				<p class="tilfy-bos"><?php echo esc_html( tilfy__( 'Paylaşım yok.' ) ); ?></p>
			<?php else : ?>
				<?php foreach ( $paylasimlar as $p ) :
					require TILFY_YOL . 'includes/sayfalar/partials/paylasim-grid-kart.php';
				endforeach; ?>
			<?php endif; ?>
		</div>
	</form>

	<?php if ( $sayfa_sayisi > 1 ) : ?>
		<nav class="tilfy-sayfalama tilfy-sayfalama--merkez" aria-label="<?php echo esc_attr( tilfy__( 'Sayfalama' ) ); ?>">
			<?php if ( $sayfa > 1 ) : ?>
				<a class="tilfy-btn tilfy-btn--ikincil tilfy-btn--kucuk" href="<?php echo esc_url( $tilfy_paylasimlar_url( array( 'sayfa' => $sayfa - 1 ) ) ); ?>"><?php echo esc_html( tilfy__( '← Önceki' ) ); ?></a>
			<?php endif; ?>
			<form method="get" class="tilfy-sayfa-atla" action="<?php echo esc_url( admin_url( 'admin.php' ) ); ?>">
				<?php $tilfy_paylasim_filtre_gizli(); ?>
				<label class="tilfy-sayfa-atla__etiket">
					<span><?php echo esc_html( tilfy__( 'Sayfa' ) ); ?></span>
					<input type="number" class="tilfy-filtre-kontrol tilfy-sayfa-atla__input" name="sayfa" min="1" max="<?php echo (int) $sayfa_sayisi; ?>" value="<?php echo (int) $sayfa; ?>" required />
				</label>
				<span class="tilfy-sayfalama__bilgi">/ <?php echo (int) $sayfa_sayisi; ?></span>
				<button type="submit" class="tilfy-btn tilfy-btn--ikincil tilfy-btn--kucuk"><?php echo esc_html( tilfy__( 'Git' ) ); ?></button>
			</form>
			<?php if ( $sayfa < $sayfa_sayisi ) : ?>
				<a class="tilfy-btn tilfy-btn--ikincil tilfy-btn--kucuk" href="<?php echo esc_url( $tilfy_paylasimlar_url( array( 'sayfa' => $sayfa + 1 ) ) ); ?>"><?php echo esc_html( tilfy__( 'Sonraki →' ) ); ?></a>
			<?php endif; ?>
		</nav>
	<?php endif; ?>

	<div class="tilfy-iy-modal" id="tilfy-paylasim-detay-modal" hidden>
		<div class="tilfy-iy-modal__backdrop" data-paylasim-modal-kapat></div>
		<div class="tilfy-iy-modal__panel" role="dialog" aria-modal="true" aria-labelledby="tilfy-paylasim-detay-baslik">
			<header class="tilfy-iy-modal__ust">
				<h2 id="tilfy-paylasim-detay-baslik"><?php echo esc_html( tilfy__( 'Paylaşım detayı' ) ); ?></h2>
				<button type="button" class="tilfy-iy-modal__kapat" id="tilfy-paylasim-detay-kapat" aria-label="<?php echo esc_attr( tilfy__( 'Kapat' ) ); ?>">&times;</button>
			</header>
			<div class="tilfy-iy-modal__govde" id="tilfy-paylasim-detay-icerik"></div>
		</div>
	</div>
</div>
