<div class="d-flex justify-content-between align-items-center mb-4">
    <h2>Edytuj stronę: <?= htmlspecialchars($site['name']) ?></h2>
    <div class="d-flex gap-2">
        <a href="/sites/<?= $site['id'] ?>/seo" class="btn btn-primary">
            <i class="bi bi-graph-up me-1"></i>SEO Panel
        </a>
        <a href="/sites/<?= $site['id'] ?>/dashboard" class="btn btn-dark">
            <i class="bi bi-sliders me-1"></i>WP Dashboard
        </a>
        <form method="post" action="/publish/site/<?= $site['id'] ?>" class="d-inline" data-confirm="Opublikowac artykul na tej stronie teraz?">
            <button type="submit" class="btn btn-success">
                <i class="bi bi-play-circle me-1"></i>Publikuj teraz
            </button>
        </form>
    </div>
</div>

<div class="row g-4">
    <div class="col-lg-7">
        <div class="card">
            <div class="card-header"><h5 class="mb-0">Ustawienia strony</h5></div>
            <div class="card-body">
                <form method="post" action="/sites/<?= $site['id'] ?>">
                    <div class="mb-3">
                        <label for="name" class="form-label">Nazwa strony</label>
                        <input type="text" class="form-control" id="name" name="name" value="<?= htmlspecialchars($site['name']) ?>" required>
                    </div>

                    <div class="mb-3">
                        <label for="url" class="form-label">URL WordPressa</label>
                        <input type="url" class="form-control" id="url" name="url" value="<?= htmlspecialchars($site['url']) ?>" required>
                    </div>

                    <div class="mb-3">
                        <label for="semstorm_domain" class="form-label">Domena SEMSTORM (opcjonalnie)</label>
                        <input type="text" class="form-control" id="semstorm_domain" name="semstorm_domain" value="<?= htmlspecialchars($site['semstorm_domain'] ?? '') ?>" placeholder="example.com">
                        <div class="form-text">Jeśli puste, system użyje hosta z URL WordPressa.</div>
                    </div>
                    <div class="mb-3">
                        <label for="dataforseo_domain" class="form-label">Domena DataForSEO (opcjonalnie)</label>
                        <input type="text" class="form-control" id="dataforseo_domain" name="dataforseo_domain" value="<?= htmlspecialchars($site['dataforseo_domain'] ?? '') ?>" placeholder="example.com">
                        <div class="form-text">Jesli puste, system uzyje domeny SEMSTORM lub hosta z URL.</div>
                    </div>


                    <div class="mb-3">
                        <label for="api_user" class="form-label">Użytkownik API</label>
                        <input type="text" class="form-control" id="api_user" name="api_user" value="<?= htmlspecialchars($site['api_user']) ?>" required>
                    </div>

                    <div class="mb-3">
                        <label for="api_token" class="form-label">Application Password</label>
                        <input type="text" class="form-control" id="api_token" name="api_token" value="<?= htmlspecialchars($site['api_token']) ?>" required>
                    </div>

                    <div class="mb-3">
                        <label for="publish_interval_hours" class="form-label">Interwał publikacji (godziny)</label>
                        <input type="number" class="form-control" id="publish_interval_hours" name="publish_interval_hours" value="<?= $site['publish_interval_hours'] ?>" min="1" max="720">
                    </div>

                    <div class="mb-3">
                        <div class="form-check">
                            <input type="checkbox" class="form-check-input" id="is_multisite" name="is_multisite" value="1" <?= $site['is_multisite'] ? 'checked' : '' ?>>
                            <label class="form-check-label" for="is_multisite">Strona wielotematyczna</label>
                        </div>
                    </div>

                    <div class="mb-3">
                        <div class="form-check">
                            <input type="checkbox" class="form-check-input" id="is_active" name="is_active" value="1" <?= $site['is_active'] ? 'checked' : '' ?>>
                            <label class="form-check-label" for="is_active">Aktywna</label>
                        </div>
                    </div>

                    <?php $hasCredentials = !empty($site['ftp_host']) || !empty($site['db_host']) || !empty($site['wp_admin_user']); ?>
                    <div class="accordion mb-3" id="credentialsAccordion">
                        <div class="accordion-item border-0">
                            <h2 class="accordion-header">
                                <button class="accordion-button collapsed bg-light px-0" type="button" data-bs-toggle="collapse" data-bs-target="#credentialsPanel">
                                    <i class="bi bi-key me-2"></i>Dane dostępowe (FTP, baza danych, panel WP)
                                    <?php if ($hasCredentials): ?>
                                        <span class="badge bg-success ms-2">zapisane</span>
                                    <?php else: ?>
                                        <span class="badge bg-secondary ms-2">brak</span>
                                    <?php endif; ?>
                                </button>
                            </h2>
                            <div id="credentialsPanel" class="accordion-collapse collapse">
                                <div class="accordion-body px-0 pb-0">

                                    <fieldset class="border rounded p-3 mb-3">
                                        <legend class="float-none w-auto px-2 fs-6 fw-bold mb-0">
                                            <i class="bi bi-hdd-network me-1"></i>FTP
                                        </legend>
                                        <div class="row mb-2">
                                            <div class="col-md-8">
                                                <label for="ftp_host" class="form-label small">Host</label>
                                                <input type="text" class="form-control form-control-sm" id="ftp_host" name="ftp_host" value="<?= htmlspecialchars($site['ftp_host'] ?? '') ?>" placeholder="ftp.example.com">
                                            </div>
                                            <div class="col-md-4">
                                                <label for="ftp_port" class="form-label small">Port</label>
                                                <input type="number" class="form-control form-control-sm" id="ftp_port" name="ftp_port" value="<?= htmlspecialchars($site['ftp_port'] ?? '21') ?>">
                                            </div>
                                        </div>
                                        <div class="row mb-2">
                                            <div class="col-md-6">
                                                <label for="ftp_user" class="form-label small">Użytkownik</label>
                                                <input type="text" class="form-control form-control-sm" id="ftp_user" name="ftp_user" value="<?= htmlspecialchars($site['ftp_user'] ?? '') ?>">
                                            </div>
                                            <div class="col-md-6">
                                                <label for="ftp_pass" class="form-label small">Hasło</label>
                                                <input type="password" class="form-control form-control-sm" id="ftp_pass" name="ftp_pass" value="<?= htmlspecialchars($site['ftp_pass'] ?? '') ?>">
                                            </div>
                                        </div>
                                        <div>
                                            <label for="ftp_path" class="form-label small">Ścieżka</label>
                                            <input type="text" class="form-control form-control-sm" id="ftp_path" name="ftp_path" value="<?= htmlspecialchars($site['ftp_path'] ?? '') ?>" placeholder="/public_html">
                                        </div>
                                    </fieldset>

                                    <fieldset class="border rounded p-3 mb-3">
                                        <legend class="float-none w-auto px-2 fs-6 fw-bold mb-0">
                                            <i class="bi bi-database me-1"></i>Baza danych
                                        </legend>
                                        <div class="row mb-2">
                                            <div class="col-md-6">
                                                <label for="db_host" class="form-label small">Host</label>
                                                <input type="text" class="form-control form-control-sm" id="db_host" name="db_host" value="<?= htmlspecialchars($site['db_host'] ?? '') ?>" placeholder="localhost">
                                            </div>
                                            <div class="col-md-6">
                                                <label for="db_name" class="form-label small">Nazwa bazy</label>
                                                <input type="text" class="form-control form-control-sm" id="db_name" name="db_name" value="<?= htmlspecialchars($site['db_name'] ?? '') ?>">
                                            </div>
                                        </div>
                                        <div class="row mb-2">
                                            <div class="col-md-4">
                                                <label for="db_user" class="form-label small">Użytkownik</label>
                                                <input type="text" class="form-control form-control-sm" id="db_user" name="db_user" value="<?= htmlspecialchars($site['db_user'] ?? '') ?>">
                                            </div>
                                            <div class="col-md-4">
                                                <label for="db_pass" class="form-label small">Hasło</label>
                                                <input type="password" class="form-control form-control-sm" id="db_pass" name="db_pass" value="<?= htmlspecialchars($site['db_pass'] ?? '') ?>">
                                            </div>
                                            <div class="col-md-4">
                                                <label for="db_prefix" class="form-label small">Prefix</label>
                                                <input type="text" class="form-control form-control-sm" id="db_prefix" name="db_prefix" value="<?= htmlspecialchars($site['db_prefix'] ?? 'wp_') ?>">
                                            </div>
                                        </div>
                                    </fieldset>

                                    <fieldset class="border rounded p-3">
                                        <legend class="float-none w-auto px-2 fs-6 fw-bold mb-0">
                                            <i class="bi bi-wordpress me-1"></i>Panel administratora
                                        </legend>
                                        <div class="row mb-2">
                                            <div class="col-md-6">
                                                <label for="wp_admin_user" class="form-label small">Login</label>
                                                <input type="text" class="form-control form-control-sm" id="wp_admin_user" name="wp_admin_user" value="<?= htmlspecialchars($site['wp_admin_user'] ?? '') ?>">
                                            </div>
                                            <div class="col-md-6">
                                                <label for="wp_admin_pass" class="form-label small">Hasło</label>
                                                <input type="password" class="form-control form-control-sm" id="wp_admin_pass" name="wp_admin_pass" value="<?= htmlspecialchars($site['wp_admin_pass'] ?? '') ?>">
                                            </div>
                                        </div>
                                        <div>
                                            <label for="wp_admin_email" class="form-label small">E-mail</label>
                                            <input type="email" class="form-control form-control-sm" id="wp_admin_email" name="wp_admin_email" value="<?= htmlspecialchars($site['wp_admin_email'] ?? '') ?>">
                                        </div>
                                    </fieldset>

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

                    <div class="d-flex gap-2">
                        <button type="submit" class="btn btn-primary">Zapisz zmiany</button>
                        <a href="/sites" class="btn btn-outline-secondary">Anuluj</a>
                        <button class="btn btn-outline-success btn-test-connection ms-auto" data-site-id="<?= $site['id'] ?>" type="button">
                            <i class="bi bi-wifi me-1"></i>Test
                        </button>
                    </div>
                </form>
            </div>
        </div>
    </div>

    <div class="col-lg-5">
        <div class="card">
            <div class="card-header"><h5 class="mb-0">Tematy strony</h5></div>
            <div class="card-body">
                <p class="text-muted small mb-3">
                    Zarządzanie tematami zostało przeniesione do dedykowanego widoku, aby nie duplikować miejsc edycji.
                </p>
                <a href="/sites/<?= $site['id'] ?>/topics" class="btn btn-outline-info w-100">
                    <i class="bi bi-tags me-1"></i>Przejdź do zarządzania tematami
                </a>
            </div>
        </div>
    </div>
</div>


