<?php 
    $base_url = str_replace('/install', '', base_path());
    $license_key = isset($license) ? $license : '';
?>

<!-- Progress Steps -->
<div class="row mb-4">
    <div class="col-12">
        <div class="d-flex justify-content-center align-items-center">
            <div class="text-center">
                <div class="bg-success text-white rounded-circle d-inline-flex align-items-center justify-content-center shadow-sm" style="width: 50px; height: 50px;">
                    <i class="bi bi-check-lg fs-5"></i>
                </div>
                <div class="mt-2 fw-semibold text-success">Installation</div>
            </div>
            <div class="bg-success mx-4 rounded" style="height: 4px; width: 120px;"></div>
            <div class="text-center">
                <div class="bg-success text-white rounded-circle d-inline-flex align-items-center justify-content-center shadow-sm" style="width: 50px; height: 50px;">
                    <i class="bi bi-check-lg fs-5"></i>
                </div>
                <div class="mt-2 fw-semibold text-success">Complete</div>
            </div>
        </div>
    </div>
</div>

<!-- Security Alert -->
<div class="row mb-4">
    <div class="col-12">
        <div class="alert alert-warning border-warning shadow-sm mb-0">
            <div class="d-flex align-items-start">
                <div class="flex-shrink-0">
                    <i class="bi bi-exclamation-triangle-fill fs-3 text-warning"></i>
                </div>
                <div class="flex-grow-1 ms-3">
                    <h5 class="alert-heading mb-2 fw-bold">
                        <i class="bi bi-shield-lock me-2"></i>
                        Important: Secure Your Installation
                    </h5>
                    <p class="mb-3">For security reasons, you should remove the installer directory now to prevent unauthorized access or re-installation.</p>
                    <div class="d-flex gap-2 align-items-center flex-wrap">
                        <button 
                            type="button" 
                            class="btn btn-warning btn-lg remove-installer-btn"
                            data-license="<?= htmlspecialchars($license_key, ENT_QUOTES, 'UTF-8'); ?>"
                            data-endpoint="<?= rtrim($base_url, '/') ?>/index.php/installversion/remove_installer_dir">
                            <i class="bi bi-trash me-2"></i>
                            Remove Installer Folder Now
                        </button>
                        <small class="text-muted remove-installer-feedback"></small>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<div class="row">
    <!-- Success Message -->
    <div class="col-lg-8">
        <div class="card shadow-sm">
            <div class="card-header bg-success text-white">
                <h5 class="card-title mb-0">
                    <i class="bi bi-check-circle-fill me-2"></i>
                    Installation Completed Successfully
                </h5>
            </div>
            <div class="card-body p-3 text-center">
                <!-- Success Icon -->
                <div class="mb-3">
                    <div class="bg-success text-white rounded-circle d-inline-flex align-items-center justify-content-center" style="width: 60px; height: 60px;">
                        <i class="bi bi-check-lg fs-3"></i>
                    </div>
                </div>
                
                <!-- Success Title -->
                <h4 class="fw-bold text-dark mb-2">Your System is Ready!</h4>
                <p class="text-muted mb-3">Your Affiliate Management System has been installed successfully.</p>
                
                <!-- Logo -->
                <div class="mb-3">
                    <?php if (file_exists('../install/assets/aff_pro_logo.png')): ?>
                        <img src="../install/assets/aff_pro_logo.png" alt="Affiliate Pro Logo" class="img-fluid" style="max-height: 40px;">
                    <?php else: ?>
                        <div class="bg-primary text-white rounded px-3 py-2 d-inline-block fw-semibold">
                            AFFILIATE PRO
                        </div>
                    <?php endif; ?>
                </div>
                
                <!-- Action Buttons -->
                <div class="row justify-content-center">
                    <div class="col-md-8">
                        <div class="d-grid gap-2">
                            <a href="<?= $base_url.'/admin' ?>" class="btn btn-primary">
                                <i class="bi bi-shield-lock-fill me-2"></i>
                                Access Admin Dashboard
                            </a>
                            <a href="<?= $base_url ?>" class="btn btn-outline-secondary">
                                <i class="bi bi-house-door-fill me-2"></i>
                                Visit Homepage
                            </a>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Next Steps -->
    <div class="col-lg-4">
        <div class="card shadow-sm">
            <div class="card-header bg-info text-white">
                <h5 class="card-title mb-0">
                    <i class="bi bi-lightbulb-fill me-2"></i>
                    Recommended Next Steps
                </h5>
            </div>
            <div class="card-body p-0">
                <div class="list-group list-group-flush">
                    <div class="list-group-item d-flex align-items-center py-2">
                        <div class="bg-primary text-white rounded-circle d-inline-flex align-items-center justify-content-center me-2" style="width: 24px; height: 24px;">
                            <i class="bi bi-gear-fill" style="font-size: 0.75rem;"></i>
                        </div>
                        <div>
                            <h6 class="mb-0 fw-semibold small">Configure Settings</h6>
                            <small class="text-muted">Set up system preferences</small>
                        </div>
                    </div>
                    
                    <div class="list-group-item d-flex align-items-center py-2">
                        <div class="bg-success text-white rounded-circle d-inline-flex align-items-center justify-content-center me-2" style="width: 24px; height: 24px;">
                            <i class="bi bi-plus-circle-fill" style="font-size: 0.75rem;"></i>
                        </div>
                        <div>
                            <h6 class="mb-0 fw-semibold small">Create Programs</h6>
                            <small class="text-muted">Define commission structures</small>
                        </div>
                    </div>
                    
                    <div class="list-group-item d-flex align-items-center py-2">
                        <div class="bg-info text-white rounded-circle d-inline-flex align-items-center justify-content-center me-2" style="width: 24px; height: 24px;">
                            <i class="bi bi-people-fill" style="font-size: 0.75rem;"></i>
                        </div>
                        <div>
                            <h6 class="mb-0 fw-semibold small">Invite Affiliates</h6>
                            <small class="text-muted">Build your network</small>
                        </div>
                    </div>
                    
                    <div class="list-group-item d-flex align-items-center py-2">
                        <div class="bg-warning text-white rounded-circle d-inline-flex align-items-center justify-content-center me-2" style="width: 24px; height: 24px;">
                            <i class="bi bi-graph-up-arrow" style="font-size: 0.75rem;"></i>
                        </div>
                        <div>
                            <h6 class="mb-0 fw-semibold small">Track Performance</h6>
                            <small class="text-muted">Monitor activity</small>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
document.addEventListener('DOMContentLoaded', function() {
    setTimeout(function() {
        const adminBtn = document.querySelector('.btn-primary');
        if (adminBtn && !document.activeElement.matches('button, a, input')) {
            adminBtn.focus();
            adminBtn.style.boxShadow = '0 0 0 3px rgba(13, 110, 253, 0.25)';
            setTimeout(function() {
                adminBtn.style.boxShadow = '';
            }, 3000);
        }
    }, 200);

    const removeBtn = document.querySelector('.remove-installer-btn');
    if (removeBtn) {
        removeBtn.addEventListener('click', function() {
            const feedback = document.querySelector('.remove-installer-feedback');
            if (!removeBtn.dataset.license) {
                if (feedback) feedback.textContent = 'License key missing. Please remove the folder manually.';
                return;
            }
            removeBtn.disabled = true;
            if (feedback) {
                feedback.classList.remove('text-success', 'text-danger');
                feedback.textContent = 'Removing installer directory...';
            }

            fetch(removeBtn.dataset.endpoint, {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
                    'X-Requested-With': 'XMLHttpRequest'
                },
                body: 'license=' + encodeURIComponent(removeBtn.dataset.license)
            })
            .then(response => response.json())
            .then(json => {
                if (json.success) {
                    if (feedback) {
                        feedback.classList.add('text-success');
                        feedback.textContent = json.message || 'Installer directory removed.';
                    }
                } else {
                    removeBtn.disabled = false;
                    if (feedback) {
                        feedback.classList.add('text-danger');
                        feedback.textContent = json.message || 'Unable to remove installer directory.';
                    }
                }
            })
            .catch(() => {
                removeBtn.disabled = false;
                if (feedback) {
                    feedback.classList.add('text-danger');
                    feedback.textContent = 'Request failed. Please try again or remove manually.';
                }
            });
        });
    }
});
</script>