<?php
declare(strict_types=1);

require_once __DIR__ . '/../includes/bootstrap.php';
require_once __DIR__ . '/../includes/admin_layout.php';

require_admin();

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $action = $_POST['action'] ?? '';
    $id = (string) ($_POST['id'] ?? '');

    if ($action === 'toggle_product') {
        $p = store_find('products', $id);
        if ($p) {
            $p['status'] = (($p['status'] ?? 'active') === 'active') ? 'hidden' : 'active';
            store_upsert('products', $p);
            flash('success', 'Ürün durumu güncellendi.');
        }
    }

    if ($action === 'delete_product') {
        store_delete('products', $id);
        flash('success', 'Ürün silindi.');
    }

    redirect(url('admin/urunler.php'));
}

$products = store_read('products');
usort($products, static fn(array $a, array $b): int => strcmp($b['created_at'] ?? '', $a['created_at'] ?? ''));
$cats = categories();

$okId = (string) ($_GET['ok'] ?? '');
$okLink = (string) ($_GET['link'] ?? '');

admin_layout_start('Ürünler', 'products');
?>

<?php if ($okId && $okLink): ?>
<section class="admin-panel">
    <h2>Ürün yayınlandı</h2>
    <div class="share-line" data-share>
        <input type="text" readonly value="<?= e($okLink) ?>" class="share-input">
        <button type="button" class="admin-btn sm" data-copy-link data-url="<?= e($okLink) ?>">Linki Kopyala</button>
        <a class="admin-btn ghost sm" href="<?= e($okLink) ?>" target="_blank" rel="noopener">Aç</a>
        <a class="admin-btn ghost sm" target="_blank" rel="noopener"
           href="https://wa.me/?text=<?= e(rawurlencode($okLink)) ?>">WhatsApp</a>
    </div>
</section>
<?php endif; ?>

<section class="admin-panel">
    <div class="admin-panel-head">
        <h2>Tüm ürünler (<?= count($products) ?>)</h2>
        <a class="admin-btn sm" href="<?= e(url('admin/urun-ekle.php')) ?>">+ Yeni</a>
    </div>

    <div class="admin-table">
        <?php if (!$products): ?>
            <p class="hint">Henüz ürün yok.</p>
        <?php endif; ?>

        <?php foreach ($products as $p):
            $share = product_share_url((string) $p['id']);
            $cat = !empty($p['category_tree'])
                ? (string) $p['category_tree']
                : ($cats[product_category_root_slug($p)] ?? category_label((string) ($p['category'] ?? '')));
            $imgCount = count(product_images($p));
            $iban = iban_for_product($p);
        ?>
            <div class="admin-row">
                <img src="<?= e(product_image_url($p)) ?>" alt="">
                <div>
                    <strong><?= e($p['title'] ?? '') ?></strong>
                    <div class="hint">
                        <?= e(money($p['price'] ?? 0)) ?> · <?= e($cat) ?> · Stok <?= (int) ($p['stock'] ?? 0) ?>
                        · ♡ <?= product_likes($p) ?> · <?= $imgCount ?> foto
                        <?php if ($iban): ?> · IBAN: <?= e($iban['bank'] ?? $iban['account_name'] ?? '') ?><?php endif; ?>
                        <?= admin_status_badge((string) ($p['status'] ?? 'active')) ?>
                    </div>
                    <div class="share-line" data-share>
                        <input type="text" readonly value="<?= e($share) ?>" class="share-input">
                        <button type="button" class="admin-btn ghost sm" data-copy-link data-url="<?= e($share) ?>">Kopyala</button>
                        <a class="admin-btn ghost sm" href="<?= e($share) ?>" target="_blank" rel="noopener">Aç</a>
                        <a class="admin-btn ghost sm" target="_blank" rel="noopener"
                           href="https://wa.me/?text=<?= e(rawurlencode($share)) ?>">WhatsApp</a>
                    </div>
                </div>
                <div class="admin-actions">
                    <a class="admin-btn ghost sm" href="<?= e(url('admin/urun-duzenle.php?id=' . urlencode((string) $p['id']))) ?>">Düzenle</a>
                    <form method="post">
                        <input type="hidden" name="action" value="toggle_product">
                        <input type="hidden" name="id" value="<?= e((string) $p['id']) ?>">
                        <button class="admin-btn ghost sm" type="submit">Gizle/Aç</button>
                    </form>
                    <form method="post" onsubmit="return confirm('Bu ürün silinsin mi?')">
                        <input type="hidden" name="action" value="delete_product">
                        <input type="hidden" name="id" value="<?= e((string) $p['id']) ?>">
                        <button class="admin-btn danger sm" type="submit">Sil</button>
                    </form>
                </div>
            </div>
        <?php endforeach; ?>
    </div>
</section>

<?php admin_layout_end(); ?>
