<?php
$success = $_SESSION['lp_success'] ?? null; if ($success) unset($_SESSION['lp_success']);
$error = $_SESSION['lp_error'] ?? null; if ($error) unset($_SESSION['lp_error']);
require_once __DIR__ . '/../header.php';
?>
<div class="content-wrapper">
<section class="content pt-3">
<div class="container-fluid pb-5">

<!-- ══════════════════════════════════════════════════
     PAGE HEADER
══════════════════════════════════════════════════ -->
<div class="d-flex flex-wrap justify-content-between align-items-start gap-3 mb-4">
    <div>
        <h3 class="mb-1 fw-bold">
            <i class="bi bi-stars me-2 text-primary"></i>
            <?= __('landing.index_title_new', 'AI Landing Studio') ?>
        </h3>
        <p class="text-muted mb-0 small"><?= __('landing.index_subtitle_new', 'من هنا تبدأ رحلة إنشاء صفحة الهبوط: تحليل، استراتيجية، أقسام، نسخ بديلة، ثم نشر') ?></p>
    </div>
    <div class="d-flex gap-2">
        <a href="/admin/ai-landing-pages/create" class="btn btn-primary btn-sm">
            <i class="bi bi-plus-circle me-1"></i>
            <?= __('landing.enter_studio', 'Enter Studio') ?>
        </a>
    </div>
</div>

<?php if ($success): ?>
<div class="alert alert-success alert-dismissible fade show">
    <i class="bi bi-check-circle-fill me-2"></i><?= htmlspecialchars($success) ?>
    <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
</div>
<?php endif; ?>
<?php if ($error): ?>
<div class="alert alert-danger alert-dismissible fade show">
    <i class="bi bi-exclamation-circle-fill me-2"></i><?= htmlspecialchars($error) ?>
    <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
</div>
<?php endif; ?>

<!-- ══════════════════════════════════════════════════
     PIPELINE CARDS
══════════════════════════════════════════════════ -->
<div class="row g-3 mb-4">
    <div class="col-md-4">
        <div class="card border-0 shadow-sm h-100" style="border-top:3px solid #f97316 !important">
            <div class="card-body">
                <div class="d-flex align-items-center gap-3 mb-3">
                    <div class="rounded-3 d-flex align-items-center justify-content-center flex-shrink-0"
                         style="width:46px;height:46px;background:linear-gradient(135deg,#f97316,#ea580c)">
                        <i class="bi bi-image fs-5 text-white"></i>
                    </div>
                    <span class="small text-uppercase fw-bold text-muted"><?= __('landing.pipeline_analyze', '1. Analyze') ?></span>
                </div>
                <h6 class="fw-bold mb-2"><?= __('landing.dashboard_card_1_title', 'ابدأ من Screenshot أو Design') ?></h6>
                <p class="text-muted small mb-0"><?= __('landing.dashboard_card_1_copy', 'ارفع صورة مرجعية وسيتولى الوكيل تحليل التخطيط، الرسائل، الـ CTA، والألوان.') ?></p>
            </div>
        </div>
    </div>
    <div class="col-md-4">
        <div class="card border-0 shadow-sm h-100" style="border-top:3px solid #2563eb !important">
            <div class="card-body">
                <div class="d-flex align-items-center gap-3 mb-3">
                    <div class="rounded-3 d-flex align-items-center justify-content-center flex-shrink-0"
                         style="width:46px;height:46px;background:linear-gradient(135deg,#2563eb,#1d4ed8)">
                        <i class="bi bi-puzzle fs-5 text-white"></i>
                    </div>
                    <span class="small text-uppercase fw-bold text-muted"><?= __('landing.pipeline_assemble', '3. Assemble') ?></span>
                </div>
                <h6 class="fw-bold mb-2"><?= __('landing.dashboard_card_2_title', 'راجع المفاهيم والأقسام') ?></h6>
                <p class="text-muted small mb-0"><?= __('landing.dashboard_card_2_copy', 'اختر concept، عدّل sections، اقفل ما تريد، ثم اجمع النسخة النهائية.') ?></p>
            </div>
        </div>
    </div>
    <div class="col-md-4">
        <div class="card border-0 shadow-sm h-100" style="border-top:3px solid #16a34a !important">
            <div class="card-body">
                <div class="d-flex align-items-center gap-3 mb-3">
                    <div class="rounded-3 d-flex align-items-center justify-content-center flex-shrink-0"
                         style="width:46px;height:46px;background:linear-gradient(135deg,#16a34a,#15803d)">
                        <i class="bi bi-collection fs-5 text-white"></i>
                    </div>
                    <span class="small text-uppercase fw-bold text-muted"><?= __('landing.pipeline_variants', '4. Variants') ?></span>
                </div>
                <h6 class="fw-bold mb-2"><?= __('landing.dashboard_card_3_title', 'ولّد نسخًا متعددة') ?></h6>
                <p class="text-muted small mb-0"><?= __('landing.dashboard_card_3_copy', 'قارن بين النسخ المختلفة واعتمد الأكثر إقناعًا قبل النشر.') ?></p>
            </div>
        </div>
    </div>
</div>

<!-- ══════════════════════════════════════════════════
     LANDING PAGES TABLE
══════════════════════════════════════════════════ -->
<div class="card border-0 shadow-sm">
    <div class="card-header bg-white border-bottom py-3 d-flex justify-content-between align-items-center">
        <h6 class="mb-0 fw-semibold">
            <i class="bi bi-table me-2 text-muted"></i>
            <?= __('landing.list_title', 'Landing Pages') ?>
        </h6>
        <span class="badge bg-secondary rounded-pill"><?= count($pages) ?></span>
    </div>
    <div class="card-body p-0">
        <?php if (empty($pages)): ?>
        <div class="p-5 text-center">
            <i class="bi bi-stars display-4 text-muted d-block mb-3 opacity-50"></i>
            <h5 class="fw-bold mb-2"><?= __('landing.empty', 'No landing pages yet') ?></h5>
            <p class="text-muted mb-3"><?= __('landing.empty_text', 'Create your first AI-powered landing page.') ?></p>
            <a href="/admin/ai-landing-pages/create" class="btn btn-primary btn-sm">
                <i class="bi bi-plus-circle me-1"></i><?= __('landing.enter_studio', 'Enter Studio') ?>
            </a>
        </div>
        <?php else: ?>
        <div class="table-responsive">
            <table class="table table-hover align-middle mb-0">
                <thead class="table-light">
                    <tr>
                        <th class="ps-3"><?= __('landing.name', 'Name') ?></th>
                        <th><?= __('landing.product', 'Product') ?></th>
                        <th><?= __('landing.slug', 'Slug') ?></th>
                        <th><?= __('landing.status', 'Status') ?></th>
                        <th><?= __('landing.updated', 'Last Updated') ?></th>
                        <th class="text-end pe-3"><?= __('landing.actions', 'Actions') ?></th>
                    </tr>
                </thead>
                <tbody>
                <?php foreach ($pages as $lp): ?>
                <tr>
                    <td class="ps-3">
                        <div class="fw-semibold"><?= htmlspecialchars($lp['name']) ?></div>
                    </td>
                    <td class="small text-muted"><?= htmlspecialchars($lp['product_title'] ?? '#'.$lp['product_id']) ?></td>
                    <td><code class="text-muted small"><?= htmlspecialchars($lp['slug']) ?></code></td>
                    <td>
                        <?php if (($lp['status'] ?? '') === 'published'): ?>
                            <span class="badge bg-success-subtle text-success border border-success-subtle px-2 py-1">
                                <i class="bi bi-check-circle-fill me-1"></i><?= __('landing.published', 'Published') ?>
                            </span>
                        <?php else: ?>
                            <span class="badge bg-secondary-subtle text-secondary border border-secondary-subtle px-2 py-1">
                                <i class="bi bi-pencil me-1"></i><?= __('landing.draft', 'Draft') ?>
                            </span>
                        <?php endif; ?>
                    </td>
                    <td class="text-muted" style="font-size:.78rem;white-space:nowrap">
                        <?= date('d/m/Y', strtotime($lp['updated_at'])) ?><br>
                        <span><?= date('H:i', strtotime($lp['updated_at'])) ?></span>
                    </td>
                    <td class="text-end pe-3">
                        <div class="d-flex gap-1 justify-content-end">
                            <a href="/admin/ai-landing-pages/edit/<?= (int)$lp['id'] ?>"
                               class="btn btn-sm btn-outline-primary"
                               title="<?= __('landing.open_studio', 'Studio') ?>">
                                <i class="bi bi-stars"></i>
                            </a>
                            <a href="/admin/ai-landing-pages/chat/<?= (int)$lp['id'] ?>"
                               class="btn btn-sm btn-outline-secondary"
                               title="<?= __('landing.chat_expert_title', 'Chat') ?>">
                                <i class="bi bi-chat-square-text"></i>
                            </a>
                            <a href="/admin/ai-landing-pages/preview/<?= (int)$lp['id'] ?>"
                               target="_blank"
                               class="btn btn-sm btn-outline-secondary"
                               title="<?= __('landing.preview', 'Preview') ?>">
                                <i class="bi bi-eye"></i>
                            </a>
                            <form method="post"
                                  action="/admin/ai-landing-pages/delete/<?= (int)$lp['id'] ?>"
                                  onsubmit="return confirm('<?= __('landing.confirm_delete', 'Delete this landing page?') ?>')">
                                <?= \Core\System\Csrf::field() ?>
                                <button type="submit" class="btn btn-sm btn-outline-danger"
                                        title="<?= __('landing.delete', 'Delete') ?>">
                                    <i class="bi bi-trash3"></i>
                                </button>
                            </form>
                        </div>
                    </td>
                </tr>
                <?php endforeach; ?>
                </tbody>
            </table>
        </div>
        <?php endif; ?>
    </div>
</div>

</div>
</section>
</div>

<?php require_once __DIR__ . '/../footer.php'; ?>
