<?php
if (count(get_included_files()) === 1) { http_response_code(403); exit('Forbidden'); }
$page_title = 'Vale Admin — Directorio';
ob_start();
?>
<div class="dashboard-container">
    <?php $adminNav = 'directorio'; require __DIR__ . '/sidebar.php'; ?>

    <main class="main-content">
        <header class="header-bar">
            <h2>Directorio</h2>
            <a href="/directorio" target="_blank" class="btn btn-primary" style="font-size:.82rem;padding:.45rem .9rem;">Ver directorio público →</a>
        </header>

        <div style="flex:1;overflow-y:auto;padding:1.5rem 2rem;">

            <?php if (!empty($dirNotice)): ?>
            <div class="alert" style="background:rgba(30,122,77,.1);color:var(--color-verde);border:1px solid rgba(30,122,77,.2);margin-bottom:1rem;"><?= htmlspecialchars($dirNotice) ?></div>
            <?php endif; ?>

            <!-- Stats row -->
            <div style="display:grid;grid-template-columns:repeat(auto-fill,minmax(150px,1fr));gap:1rem;margin-bottom:2rem;">
                <?php
                $dStats = [
                    ['label'=>'En directorio', 'value'=>$dirStats['total']],
                    ['label'=>'Con descripción ES', 'value'=>$dirStats['con_desc_es']],
                    ['label'=>'Con descripción EN', 'value'=>$dirStats['con_desc_en']],
                    ['label'=>'Sin descripción', 'value'=>$dirStats['sin_desc']],
                    ['label'=>'Ciudades únicas', 'value'=>$dirStats['ciudades']],
                ];
                foreach ($dStats as $s): ?>
                <div style="background:#fff;border:1px solid var(--color-border);border-radius:10px;padding:.9rem 1.1rem;box-shadow:var(--shadow-soft);">
                    <p style="font-size:.68rem;font-weight:700;text-transform:uppercase;letter-spacing:.8px;color:var(--color-text-muted);margin-bottom:.25rem;"><?= $s['label'] ?></p>
                    <p style="font-size:1.5rem;font-weight:700;color:var(--color-ink);font-family:'Fraunces',serif;"><?= $s['value'] ?></p>
                </div>
                <?php endforeach; ?>
            </div>

            <!-- Filtros -->
            <form method="GET" action="/admin/directorio" style="display:flex;gap:.75rem;flex-wrap:wrap;margin-bottom:1.5rem;align-items:flex-end;">
                <div>
                    <label style="display:block;font-size:.75rem;font-weight:700;color:var(--color-text-muted);margin-bottom:.25rem;">Rubro</label>
                    <select name="rubro" class="form-control" style="padding:.4rem .7rem;font-size:.85rem;">
                        <option value="">Todos</option>
                        <?php foreach (['barberia','odontologia','belleza','general'] as $r): ?>
                        <option value="<?= $r ?>" <?= ($filterRubro ?? '') === $r ? 'selected' : '' ?>><?= ucfirst($r) ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <div>
                    <label style="display:block;font-size:.75rem;font-weight:700;color:var(--color-text-muted);margin-bottom:.25rem;">Ciudad</label>
                    <select name="ciudad" class="form-control" style="padding:.4rem .7rem;font-size:.85rem;">
                        <option value="">Todas</option>
                        <?php foreach ($ciudades as $c): ?>
                        <option value="<?= htmlspecialchars($c) ?>" <?= ($filterCiudad ?? '') === $c ? 'selected' : '' ?>><?= htmlspecialchars($c) ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <div>
                    <label style="display:block;font-size:.75rem;font-weight:700;color:var(--color-text-muted);margin-bottom:.25rem;">Descripción</label>
                    <select name="desc_filter" class="form-control" style="padding:.4rem .7rem;font-size:.85rem;">
                        <option value="">Todas</option>
                        <option value="sin" <?= ($filterDesc ?? '') === 'sin' ? 'selected' : '' ?>>Sin descripción</option>
                        <option value="con" <?= ($filterDesc ?? '') === 'con' ? 'selected' : '' ?>>Con descripción</option>
                    </select>
                </div>
                <button type="submit" class="btn btn-primary" style="font-size:.85rem;padding:.45rem .9rem;">Filtrar</button>
                <a href="/admin/directorio" class="btn" style="font-size:.85rem;padding:.45rem .9rem;">Limpiar</a>
            </form>

            <!-- Tabla -->
            <div style="background:#fff;border:1px solid var(--color-border);border-radius:12px;box-shadow:var(--shadow-soft);overflow:hidden;">
                <div style="overflow-x:auto;">
                <table style="width:100%;font-size:.82rem;border-collapse:collapse;">
                    <thead>
                        <tr style="border-bottom:2px solid var(--color-border);background:#FAFAF8;">
                            <th style="text-align:left;padding:.7rem 1rem;font-weight:700;color:var(--color-text-muted);">Negocio</th>
                            <th style="text-align:left;padding:.7rem .75rem;font-weight:700;color:var(--color-text-muted);">Rubro</th>
                            <th style="text-align:left;padding:.7rem .75rem;font-weight:700;color:var(--color-text-muted);">Ciudad</th>
                            <th style="text-align:left;padding:.7rem .75rem;font-weight:700;color:var(--color-text-muted);">Descripción (ES)</th>
                            <th style="text-align:left;padding:.7rem .75rem;font-weight:700;color:var(--color-text-muted);">Keywords</th>
                            <th style="text-align:center;padding:.7rem .75rem;font-weight:700;color:var(--color-text-muted);">Acciones</th>
                        </tr>
                    </thead>
                    <tbody>
                    <?php foreach ($listings as $l): ?>
                    <tr style="border-bottom:1px solid #FAF6F1;">
                        <td style="padding:.65rem 1rem;">
                            <a href="/admin/tenants?id=<?= $l['id'] ?>" style="font-weight:700;color:var(--color-ink);text-decoration:none;"><?= htmlspecialchars($l['name']) ?></a>
                            <br>
                            <a href="/n/<?= htmlspecialchars($l['public_slug']) ?>" target="_blank" style="font-size:.72rem;color:var(--color-barro);text-decoration:none;">/n/<?= htmlspecialchars($l['public_slug']) ?> ↗</a>
                        </td>
                        <td style="padding:.65rem .75rem;text-transform:capitalize;"><?= htmlspecialchars($l['rubro'] ?: '—') ?></td>
                        <td style="padding:.65rem .75rem;"><?= htmlspecialchars($l['ciudad'] ?: '—') ?></td>
                        <td style="padding:.65rem .75rem;max-width:260px;">
                            <?php if (!empty($l['descripcion_dir'])): ?>
                            <p style="color:var(--color-ink);line-height:1.4;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;max-width:260px;" title="<?= htmlspecialchars($l['descripcion_dir']) ?>"><?= htmlspecialchars($l['descripcion_dir']) ?></p>
                            <?php else: ?>
                            <span style="color:#C0392B;font-size:.75rem;font-weight:600;">Sin descripción</span>
                            <?php endif; ?>
                        </td>
                        <td style="padding:.65rem .75rem;max-width:180px;">
                            <?php if (!empty($l['dir_keywords'])): ?>
                            <p style="font-size:.72rem;color:var(--color-text-muted);white-space:nowrap;overflow:hidden;text-overflow:ellipsis;max-width:180px;"><?= htmlspecialchars($l['dir_keywords']) ?></p>
                            <?php else: ?>
                            <span style="color:var(--color-text-muted);font-size:.72rem;">—</span>
                            <?php endif; ?>
                        </td>
                        <td style="padding:.65rem .75rem;text-align:center;white-space:nowrap;">
                            <form method="POST" action="/admin/directorio/regenerar" style="display:inline;">
                                <input type="hidden" name="tenant_id" value="<?= $l['id'] ?>">
                                <button type="submit" class="btn" style="font-size:.72rem;padding:3px 8px;" title="Regenerar descripción con IA">Regenerar desc.</button>
                            </form>
                            <a href="/n/<?= htmlspecialchars($l['public_slug']) ?>" target="_blank" class="btn" style="font-size:.72rem;padding:3px 8px;margin-left:4px;">Ver página</a>
                        </td>
                    </tr>
                    <?php endforeach; ?>
                    <?php if (empty($listings)): ?>
                    <tr><td colspan="6" style="padding:2rem 1rem;text-align:center;color:var(--color-text-muted);">
                        Ningún negocio en el directorio con los filtros aplicados.
                    </td></tr>
                    <?php endif; ?>
                    </tbody>
                </table>
                </div>
            </div>

            <!-- Negocios sin slug (fuera del directorio) -->
            <?php if (!empty($sinSlug)): ?>
            <div style="margin-top:2rem;background:#fff;border:1px solid var(--color-border);border-radius:12px;padding:1.5rem;box-shadow:var(--shadow-soft);">
                <h3 style="font-size:.9rem;margin-bottom:1rem;color:var(--color-text-muted);">Negocios sin slug público (fuera del directorio)</h3>
                <div style="display:flex;flex-wrap:wrap;gap:.5rem;">
                <?php foreach ($sinSlug as $t): ?>
                <a href="/admin/tenants?id=<?= $t['id'] ?>" style="background:#F4EDE3;color:var(--color-ink);border-radius:8px;padding:4px 10px;font-size:.78rem;font-weight:600;text-decoration:none;"><?= htmlspecialchars($t['name']) ?></a>
                <?php endforeach; ?>
                </div>
            </div>
            <?php endif; ?>

        </div>
    </main>
</div>
<?php
$content = ob_get_clean();
require dirname(__DIR__) . '/layout.php';
