<?php $csrfToken = \PpPhp\Core\Security::CSRF()->Token(); ?>

<div class="modal fade project-modal transfer-modal" id="duplicateProjectModal" tabindex="-1" aria-hidden="true">
    <div class="modal-dialog modal-lg modal-dialog-centered">
        <div class="modal-content">
            <div class="modal-header px-4 py-3">
                <div><div class="transfer-kicker"><i class="bi bi-copy"></i> Safe copy</div><h5 class="modal-title mb-0">Duplicate project</h5></div>
                <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <form id="duplicateProjectForm">
                <div class="modal-body p-4">
                    <div class="transfer-note"><i class="bi bi-shield-check"></i><div><strong>The original stays untouched.</strong><span>Editable settings are copied. Choose explicitly whether the new project shares a remote database or starts portable.</span></div></div>
                    <div class="row g-3 mt-1">
                        <div class="col-md-7"><label class="form-label small fw-semibold">Project name</label><input class="form-control" name="name" id="duplicateProjectName" maxlength="100" required></div>
                        <div class="col-md-5"><label class="form-label small fw-semibold">Project slug</label><input class="form-control" name="slug" id="duplicateProjectSlug" pattern="[a-z0-9]+(?:-[a-z0-9]+)*" maxlength="100" required></div>
                        <div class="col-12"><label class="form-label small fw-semibold">Description</label><textarea class="form-control" name="description" id="duplicateProjectDescription" rows="2" maxlength="1000"></textarea></div>
                    </div>
                    <fieldset class="transfer-fieldset mt-3">
                        <legend>Database for the duplicate</legend>
                        <label class="transfer-choice" id="duplicateShareChoice"><input type="radio" name="connection_mode" value="share" checked><span class="choice-icon blue"><i class="bi bi-link-45deg"></i></span><span><strong>Share the current connection</strong><small>Copies the saved connection profile; database records are not duplicated.</small></span></label>
                        <label class="transfer-choice d-none" id="duplicateSqliteCopyChoice"><input type="radio" name="connection_mode" value="sqlite_copy"><span class="choice-icon teal"><i class="bi bi-files"></i></span><span><strong>Copy the SQLite database</strong><small>Creates an independent copy of the project-local SQLite file.</small></span></label>
                        <label class="transfer-choice"><input type="radio" name="connection_mode" value="sqlite"><span class="choice-icon violet"><i class="bi bi-device-ssd"></i></span><span><strong>Start with portable SQLite</strong><small>Keeps the design but creates a new empty local database.</small></span></label>
                    </fieldset>
                    <label class="form-check transfer-check mt-3"><input class="form-check-input" type="checkbox" name="include_custom" value="1" checked><span><strong>Include Custom Files</strong><small>Copies code, assets, and uploads currently stored in the project’s Custom Files workspace.</small></span></label>
                </div>
                <div class="modal-footer px-4 py-3"><button type="button" class="btn btn-light border" data-bs-dismiss="modal">Cancel</button><button type="submit" class="btn btn-primary" id="duplicateProjectSubmit"><i class="bi bi-copy me-1"></i> Create duplicate</button></div>
            </form>
        </div>
    </div>
</div>

<div class="modal fade project-modal transfer-modal" id="exportProjectModal" tabindex="-1" aria-hidden="true">
    <div class="modal-dialog modal-lg modal-dialog-centered">
        <div class="modal-content">
            <div class="modal-header px-4 py-3">
                <div><div class="transfer-kicker"><i class="bi bi-box-arrow-up"></i> Portable package</div><h5 class="modal-title mb-0">Export <span id="exportProjectName"></span></h5></div>
                <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <form id="exportProjectForm" method="post">
                <input type="hidden" name="_token" value="<?= htmlspecialchars($csrfToken) ?>">
                <div class="modal-body p-4">
                    <div class="package-identity"><span class="package-logo"><i class="bi bi-file-earmark-zip"></i></span><div><strong>.ppphp editable-project package</strong><span>Checksummed, versioned, and ready for guided import.</span></div><span class="badge text-bg-success-subtle text-success border border-success-subtle"><i class="bi bi-key me-1"></i>No credentials</span></div>
                    <div class="transfer-includes mt-3">
                        <div><i class="bi bi-check-circle-fill"></i><span><strong>Project configuration</strong><small>Tables, pages, Designer, Events, Security, themes, and module state.</small></span></div>
                        <div><i class="bi bi-check-circle-fill"></i><span><strong>Logical schema design</strong><small>Selected tables, field metadata, views, positions, and relationships—not remote database data.</small></span></div>
                    </div>
                    <input type="hidden" name="include_custom" value="0"><label class="form-check transfer-check mt-3"><input class="form-check-input" type="checkbox" name="include_custom" value="1" checked><span><strong>Include Custom Files</strong><small>May include executable PHP or script files; import will flag these for review.</small></span></label>
                    <label class="form-check transfer-check mt-2 d-none" id="exportSqliteOption"><input class="form-check-input" type="checkbox" name="include_sqlite" value="1"><span><strong>Include the SQLite database file</strong><small>Includes its schema and data. Review privacy before sharing the package.</small></span></label>
                    <div class="transfer-alert mt-3"><i class="bi bi-info-circle"></i>Database credentials and structured password/token/API-key settings are removed. Review literals embedded in Events or Custom Files before sharing. Generated output is not included.</div>
                </div>
                <div class="modal-footer px-4 py-3"><button type="button" class="btn btn-light border" data-bs-dismiss="modal">Cancel</button><button type="submit" class="btn btn-primary"><i class="bi bi-download me-1"></i> Export package</button></div>
            </form>
        </div>
    </div>
</div>

<div class="modal fade project-modal transfer-modal" id="importProjectModal" tabindex="-1" aria-hidden="true">
    <div class="modal-dialog modal-lg modal-dialog-centered modal-dialog-scrollable">
        <div class="modal-content">
            <div class="modal-header px-4 py-3">
                <div><div class="transfer-kicker"><i class="bi bi-box-arrow-in-down"></i> Guided restore</div><h5 class="modal-title mb-0">Import project</h5></div>
                <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div class="transfer-steps"><span class="active" data-import-step-indicator="upload"><b>1</b> Inspect</span><i class="bi bi-chevron-right"></i><span data-import-step-indicator="review"><b>2</b> Review &amp; import</span></div>
            <form id="importInspectForm">
                <div class="modal-body p-4" data-import-step="upload">
                    <label class="package-drop" id="projectPackageDrop">
                        <input type="file" name="package" id="projectPackageFile" accept=".ppphp,application/zip" required>
                        <span class="package-drop-icon"><i class="bi bi-cloud-arrow-up"></i></span>
                        <strong>Choose a .ppphp project package</strong>
                        <span>Nothing is written to Projects until validation and conflict review complete.</span>
                        <small>Maximum <?= htmlspecialchars($projectPackageLimit ?? '100 MB') ?></small>
                    </label>
                    <div class="transfer-includes compact mt-3"><div><i class="bi bi-shield-check"></i><span><strong>Safety inspection</strong><small>Format, paths, sizes, compression ratios, manifest, and SHA-256 checksums.</small></span></div><div><i class="bi bi-eye"></i><span><strong>Conflict preview</strong><small>Review name, slug, database strategy, warnings, and custom code before commit.</small></span></div></div>
                </div>
                <div class="modal-footer px-4 py-3" data-import-footer="upload"><button type="button" class="btn btn-light border" data-bs-dismiss="modal">Cancel</button><button type="submit" class="btn btn-primary" id="inspectPackageSubmit"><i class="bi bi-search me-1"></i> Inspect package</button></div>
            </form>
            <form id="importCommitForm" class="d-none">
                <input type="hidden" name="token" id="importPackageToken">
                <div class="modal-body p-4" data-import-step="review">
                    <div class="package-preview" id="importPackagePreview"></div>
                    <div class="row g-3 mt-1">
                        <div class="col-md-7"><label class="form-label small fw-semibold">Project name</label><input class="form-control" name="name" id="importProjectName" maxlength="100" required></div>
                        <div class="col-md-5"><label class="form-label small fw-semibold">Project slug</label><input class="form-control" name="slug" id="importProjectSlug" pattern="[a-z0-9]+(?:-[a-z0-9]+)*" maxlength="100" required></div>
                        <div class="col-12"><label class="form-label small fw-semibold">Description</label><textarea class="form-control" name="description" id="importProjectDescription" rows="2" maxlength="1000"></textarea></div>
                    </div>
                    <fieldset class="transfer-fieldset mt-3"><legend>Database connection</legend>
                        <label class="transfer-choice"><input type="radio" name="connection_mode" value="sqlite" checked><span class="choice-icon violet"><i class="bi bi-device-ssd"></i></span><span><strong>Portable SQLite workspace</strong><small>Creates a local database; uses the packaged SQLite file when one is included.</small></span></label>
                        <?php $remoteProjects = array_values(array_filter($projects, static fn(array $p): bool => ($p['db_type'] ?? '') !== 'sqlite' && empty($p['archived_at']))); ?>
                        <?php if ($remoteProjects): ?>
                        <label class="transfer-choice"><input type="radio" name="connection_mode" value="existing"><span class="choice-icon blue"><i class="bi bi-link-45deg"></i></span><span class="flex-grow-1"><strong>Use an existing project connection</strong><small>Copies credentials securely on the server; the package never receives them.</small><select class="form-select form-select-sm mt-2" name="connection_project" id="importConnectionProject"><?php foreach ($remoteProjects as $p): ?><option value="<?= htmlspecialchars($p['slug']) ?>"><?= htmlspecialchars($p['name'] . ' · ' . $p['db_label']) ?></option><?php endforeach; ?></select></span></label>
                        <?php endif; ?>
                    </fieldset>
                    <label class="form-check transfer-check mt-3 d-none" id="importCodeConfirmation"><input class="form-check-input" type="checkbox" name="allow_code" value="1"><span><strong>I reviewed the custom active-code files</strong><small>Imported PHP, scripts, HTML/SVG, or server configuration can execute code. Only continue with a package you trust.</small></span></label>
                </div>
                <div class="modal-footer px-4 py-3"><button type="button" class="btn btn-light border" id="importBackButton"><i class="bi bi-arrow-left me-1"></i> Back</button><button type="submit" class="btn btn-primary" id="importProjectSubmit"><i class="bi bi-box-arrow-in-down me-1"></i> Import project</button></div>
            </form>
        </div>
    </div>
</div>
