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

<div class="card-stat" style="margin-bottom: 2rem; display: block;">
    <div style="display: flex; align-items: center; justify-content: space-between; margin-bottom: 1.5rem;">
        <div>
            <h3 style="margin: 0 0 0.5rem; color: white;">Backup & Data Flush</h3>
            <p style="color: var(--text-muted); margin: 0;">Manage database backups and perform system-wide data flushes.</p>
        </div>
        <div class="icon-box" style="background: rgba(245, 158, 11, 0.1);">
            <i class="ph ph-database"></i>
        </div>
    </div>

    <div style="background: rgba(15, 23, 42, 0.5); padding: 1.5rem; border-radius: 12px; border: 1px solid var(--glass-border); margin-bottom: 2rem;">
        <h4 style="color: var(--text-light); margin-top: 0; margin-bottom: 1rem; display: flex; align-items: center; gap: 0.5rem;">
            <i class="ph ph-info" style="color: var(--superadmin-color);"></i> Database Information
        </h4>
        <div style="display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 1rem;">
            <div>
                <span style="color: var(--text-muted); font-size: 0.9rem;">Current Schema</span>
                <div style="color: white; font-size: 1.25rem; font-weight: 600; margin-top: 0.25rem;"><?php echo htmlspecialchars($schema ?? 'Unknown'); ?></div>
            </div>
            <div>
                <span style="color: var(--text-muted); font-size: 0.9rem;">Server Date & Time</span>
                <div style="color: white; font-size: 1.25rem; font-weight: 600; margin-top: 0.25rem;"><?php echo date('Y-m-d H:i:s'); ?></div>
            </div>
        </div>
    </div>

    <div style="display: grid; grid-template-columns: 1fr 1fr; gap: 2rem;">
        <!-- Backup Section -->
        <div style="background: rgba(15, 23, 42, 0.5); padding: 2rem; border-radius: 12px; border: 1px solid var(--glass-border); text-align: center;">
            <div style="margin-bottom: 1.5rem;">
                <i class="ph ph-download-simple" style="font-size: 3rem; color: var(--superadmin-color);"></i>
            </div>
            <h4 style="color: white; margin: 0 0 1rem; font-size: 1.25rem;">Download Backup</h4>
            <p style="color: var(--text-muted); margin-bottom: 2rem; font-size: 0.95rem; line-height: 1.5;">
                Download a complete .sql dump of the database including schema structures and all data.
            </p>
            <a href="/superadmin/databaseExport" class="btn-gold" style="width: 100%; justify-content: center;">
                <i class="ph ph-file-sql"></i> Generate & Download SQL
            </a>
        </div>

        <!-- Truncate Section -->
        <div style="background: rgba(239, 68, 68, 0.05); padding: 2rem; border-radius: 12px; border: 1px solid rgba(239, 68, 68, 0.2); text-align: center;">
            <div style="margin-bottom: 1.5rem;">
                <i class="ph ph-warning-circle" style="font-size: 3rem; color: #ef4444;"></i>
            </div>
            <h4 style="color: #ef4444; margin: 0 0 1rem; font-size: 1.25rem;">Danger Zone: Data Flush</h4>
            <p style="color: var(--text-muted); margin-bottom: 2rem; font-size: 0.95rem; line-height: 1.5;">
                This will securely truncate <strong>ALL</strong> tables in the current schema. This action cannot be undone. All data will be permanently lost!
            </p>
            <form action="/superadmin/databaseTruncate" method="POST" onsubmit="return confirmTruncate();">
                <button type="submit" style="width: 100%; justify-content: center; background: rgba(239, 68, 68, 0.1); color: #ef4444; border: 1px solid rgba(239, 68, 68, 0.4); padding: 0.75rem 1.5rem; border-radius: 8px; font-weight: 600; cursor: pointer; display: inline-flex; align-items: center; gap: 0.5rem; transition: all 0.3s ease;" onmouseover="this.style.background='#ef4444'; this.style.color='white';" onmouseout="this.style.background='rgba(239, 68, 68, 0.1)'; this.style.color='#ef4444';">
                    <i class="ph ph-trash"></i> Truncate Entire Database
                </button>
            </form>
        </div>
    </div>
</div>

<script>
function confirmTruncate() {
    const message = "WARNING: You are about to DELETE ALL DATA from ALL TABLES in the '<?php echo htmlspecialchars($schema ?? ''); ?>' schema.\n\nThis includes users, settings, and tenants. THIS CANNOT BE UNDONE!\n\nAre you absolutely sure you want to proceed?";
    
    if (confirm(message)) {
        const finalConfirm = prompt("Please type 'TRUNCATE' to confirm data destruction:");
        if (finalConfirm === 'TRUNCATE') {
            return true;
        } else {
            alert("Truncation cancelled. You didn't type 'TRUNCATE' correctly.");
        }
    }
    return false;
}
</script>

</div> <!-- main-content -->
</div> <!-- dashboard-layout -->
</body>
</html>
