{{!
    This file is part of Moodle - http://moodle.org/
    Plugin Registration Template - Clean, Professional Interface
}}

<style>
.adeptus-registration-container {
    max-width: 800px;
    margin: 0 auto;
    padding: 20px;
}

.adeptus-registration-header {
    background: linear-gradient(135deg, #0ea5e9 0%, #2563eb 100%);
    color: white;
    padding: 30px;
    border-radius: 10px 10px 0 0;
    text-align: center;
    margin-bottom: 0;
}

.adeptus-registration-header h1 {
    margin: 0;
    font-size: 28px;
    font-weight: 600;
}

.adeptus-registration-header p {
    margin: 10px 0 0 0;
    opacity: 0.9;
    font-size: 16px;
}

.adeptus-registration-form {
    background: white;
    border: 1px solid #e1e5e9;
    border-radius: 0 0 10px 10px;
    padding: 30px;
    box-shadow: 0 2px 10px rgba(0,0,0,0.1);
}

.adeptus-form-section {
    margin-bottom: 25px;
}

.adeptus-form-section h3 {
    color: #333;
    font-size: 18px;
    margin-bottom: 15px;
    padding-bottom: 8px;
    border-bottom: 2px solid #2563eb;
}

.adeptus-form-row {
    display: flex;
    gap: 20px;
    margin-bottom: 15px;
}

.adeptus-form-group {
    flex: 1;
}

.adeptus-form-group label {
    display: block;
    font-weight: 600;
    color: #555;
    margin-bottom: 5px;
    font-size: 14px;
}

.adeptus-form-group input,
.adeptus-form-group textarea {
    width: 100%;
    padding: 12px;
    border: 2px solid #e1e5e9;
    border-radius: 6px;
    font-size: 14px;
    background-color: #f8f9fa;
    color: #666;
    transition: border-color 0.3s ease;
}

.adeptus-form-group input:focus,
.adeptus-form-group textarea:focus {
    outline: none;
    border-color: #2563eb;
    background-color: white;
}

.adeptus-form-group input[readonly],
.adeptus-form-group textarea[readonly] {
    background-color: #f8f9fa;
    color: #666;
    cursor: not-allowed;
}

.adeptus-form-group .form-help {
    font-size: 12px;
    color: #888;
    margin-top: 4px;
}

.adeptus-submit-section {
    text-align: center;
    margin-top: 30px;
    padding-top: 20px;
    border-top: 1px solid #e1e5e9;
}

.adeptus-submit-btn {
    background: linear-gradient(135deg, #0ea5e9 0%, #2563eb 100%);
    color: white;
    border: none;
    padding: 15px 40px;
    border-radius: 25px;
    font-size: 16px;
    font-weight: 600;
    cursor: pointer;
    transition: transform 0.2s ease, box-shadow 0.2s ease;
    box-shadow: 0 4px 15px rgba(37, 99, 235, 0.3);
}

.adeptus-submit-btn:hover {
    transform: translateY(-2px);
    box-shadow: 0 6px 20px rgba(37, 99, 235, 0.4);
}

.adeptus-submit-btn:active {
    transform: translateY(0);
}

.adeptus-error {
    background: #fee;
    border: 1px solid #fcc;
    color: #c33;
    padding: 15px;
    border-radius: 6px;
    margin-bottom: 20px;
    font-weight: 500;
    display: none;
}

.adeptus-info-box {
    background: #e3f2fd;
    border: 1px solid #2196f3;
    color: #1976d2;
    padding: 15px;
    border-radius: 6px;
    margin-bottom: 20px;
    font-weight: 500;
}

.adeptus-site-exists {
    background: #fff3cd;
    border: 1px solid #ffc107;
    color: #856404;
}

.adeptus-loading {
    display: none;
    text-align: center;
    padding: 20px;
}

.adeptus-loading .spinner {
    border: 3px solid #f3f3f3;
    border-top: 3px solid #2563eb;
    border-radius: 50%;
    width: 30px;
    height: 30px;
    animation: spin 1s linear infinite;
    margin: 0 auto 10px;
}

@keyframes spin {
    0% { transform: rotate(0deg); }
    100% { transform: rotate(360deg); }
}

/* Success View Styles */
.adeptus-success-header {
    background: linear-gradient(135deg, #28a745 0%, #20c997 100%);
    color: white;
    padding: 40px;
    border-radius: 10px;
    text-align: center;
    margin-bottom: 30px;
    box-shadow: 0 4px 15px rgba(40, 167, 69, 0.3);
}

.adeptus-success-header h1 {
    margin: 0;
    font-size: 32px;
    font-weight: 600;
}

.adeptus-success-header p {
    margin: 15px 0 0 0;
    opacity: 0.9;
    font-size: 18px;
}

.adeptus-success-content {
    max-width: 600px;
    margin: 0 auto;
}

.adeptus-success-card {
    background: white;
    border: 1px solid #e1e5e9;
    border-radius: 10px;
    padding: 30px;
    box-shadow: 0 2px 10px rgba(0,0,0,0.1);
    margin-bottom: 30px;
    text-align: center;
}

.success-icon {
    font-size: 48px;
    color: #28a745;
    margin-bottom: 20px;
}

.adeptus-success-card h3 {
    color: #333;
    font-size: 24px;
    margin-bottom: 25px;
    font-weight: 600;
}

.connection-details {
    text-align: left;
    max-width: 400px;
    margin: 0 auto;
}

.detail-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 12px 0;
    border-bottom: 1px solid #f0f0f0;
}

.detail-row:last-child {
    border-bottom: none;
}

.detail-row .label {
    font-weight: 600;
    color: #555;
    font-size: 14px;
}

.detail-row .value {
    color: #333;
    font-size: 14px;
    font-family: monospace;
    background: #f8f9fa;
    padding: 4px 8px;
    border-radius: 4px;
    max-width: 200px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.status-connected {
    color: #28a745 !important;
    font-weight: 600;
}

.adeptus-action-buttons {
    display: flex;
    gap: 15px;
    justify-content: center;
    flex-wrap: wrap;
}

.adeptus-btn-primary,
.adeptus-btn-secondary {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    padding: 15px 30px;
    border-radius: 25px;
    font-size: 16px;
    font-weight: 600;
    text-decoration: none;
    transition: transform 0.2s ease, box-shadow 0.2s ease;
    box-shadow: 0 4px 15px rgba(0,0,0,0.1);
}

.adeptus-btn-primary {
    background: linear-gradient(135deg, #0ea5e9 0%, #2563eb 100%);
    color: white;
}

.adeptus-btn-primary:hover {
    transform: translateY(-2px);
    box-shadow: 0 6px 20px rgba(37, 99, 235, 0.4);
    color: white;
}

.adeptus-btn-secondary {
    background: white;
    color: #2563eb;
    border: 2px solid #2563eb;
}

.adeptus-btn-secondary:hover {
    transform: translateY(-2px);
    box-shadow: 0 6px 20px rgba(37, 99, 235, 0.2);
    color: #2563eb;
}

@media (max-width: 768px) {
    .adeptus-form-row {
        flex-direction: column;
        gap: 10px;
    }
    
    .adeptus-registration-container {
        padding: 10px;
    }
    
    .adeptus-registration-form {
        padding: 20px;
    }
}
.adeptus-installation-container {
    max-width: 1200px;
    margin: 0 auto;
    padding: 20px;
}

.installation-header {
    background: linear-gradient(135deg, #0ea5e9 0%, #2563eb 100%);
    color: white;
    padding: 40px;
    border-radius: 15px;
    text-align: center;
    margin-bottom: 30px;
    box-shadow: 0 8px 25px rgba(37, 99, 235, 0.3);
}

.installation-header h1 {
    margin: 0;
    font-size: 32px;
    font-weight: 600;
}

.installation-header p {
    margin: 15px 0 0 0;
    opacity: 0.9;
    font-size: 18px;
}

.installation-progress {
    display: flex;
    justify-content: center;
    margin-bottom: 30px;
}

.progress-step {
    display: flex;
    align-items: center;
    margin: 0 20px;
}

.progress-step .step-number {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 600;
    margin-right: 10px;
}

.progress-step.completed .step-number {
    background: #28a745;
    color: white;
}

.progress-step.current .step-number {
    background: #2563eb;
    color: white;
}

.progress-step.pending .step-number {
    background: #e9ecef;
    color: #6c757d;
}

.progress-step .step-label {
    font-weight: 500;
    color: #333;
}

.progress-step.completed .step-label {
    color: #28a745;
}

.progress-step.current .step-label {
    color: #2563eb;
}

.progress-step.pending .step-label {
    color: #6c757d;
}

.progress-connector {
    width: 60px;
    height: 2px;
    background: #e9ecef;
    margin: 0 10px;
}

.progress-connector.completed {
    background: #28a745;
}

</style>

<div class="adeptus-registration-container adeptus-installation-container">
    {{#is_registered}}
    <!-- Already Registered - Congratulations View -->
    <div class="adeptus-success-header">
        <h1><i class="fa fa-check-circle"></i> Congratulations!</h1>
        <p>Your Adeptus Insights plugin is successfully connected and ready to use.</p>
    </div>
    
    <div class="adeptus-success-content">
        <div class="adeptus-success-card">
            <div class="success-icon">
                <i class="fa fa-link"></i>
            </div>
            <h3>Connection Status</h3>
            <div class="connection-details">
                <div class="detail-row">
                    <span class="label">Installation ID:</span>
                    <span class="value">{{installation_id}}</span>
                </div>
                <div class="detail-row">
                    <span class="label">API Key:</span>
                    <span class="value">{{api_key}}</span>
                </div>
                <div class="detail-row">
                    <span class="label">Site Name:</span>
                    <span class="value">{{site_name}}</span>
                </div>
                <div class="detail-row">
                    <span class="label">Status:</span>
                    <span class="value status-connected">Connected</span>
                </div>
            </div>
        </div>
        
        <div class="adeptus-action-buttons">
            <a href="{{site_url}}/report/adeptus_insights/index.php" class="adeptus-btn-primary">
                <i class="fa fa-dashboard"></i> Go to Dashboard
            </a>
            <a href="{{site_url}}/report/adeptus_insights/subscription.php" class="adeptus-btn-secondary">
                <i class="fa fa-credit-card"></i> Manage Subscription
            </a>
        </div>
    </div>
    {{/is_registered}}
    
    {{^is_registered}}
    <!-- Not Registered - Registration Form -->
    {{#error_message}}
    <div class="adeptus-error">
        <strong>Registration Error:</strong> {{error_message}}
    </div>
    {{/error_message}}
    
    {{#site_already_exists}}
    <div class="adeptus-info-box adeptus-site-exists">
        <strong><i class="fa fa-info-circle"></i> Site Already Registered:</strong> 
        This site is already registered on the backend. You will be redirected to the main page.
    </div>
    {{/site_already_exists}}

    <!-- Installation Header -->
    <div class="installation-header">
       <h1><i class="fa fa-rocket"></i> Adeptus Insights Registration</h1>
        <p>Complete the registration to activate your plugin</p>
    </div>

    <!-- Installation Progress -->
    <div class="installation-progress">
        <div class="progress-step current">
            <div class="step-number">1</div>
            <div class="step-label">Plugin Registration</div>
        </div>
        <div class="progress-connector"></div>
        <div class="progress-step pending">
            <div class="step-number">2</div>
            <div class="step-label">Subscription Setup</div>
        </div>
        <div class="progress-connector"></div>
        <div class="progress-step pending">
            <div class="step-number">3</div>
            <div class="step-label">Installation Complete</div>
        </div>
    </div>

    <div class="adeptus-error" id="validation-error">
        <strong>Missing Required Information:</strong> <span id="validation-message"></span>
    </div>
    
    
    <form method="post" class="adeptus-registration-form" id="registration-form">
        <input type="hidden" name="sesskey" value="{{sesskey}}">
        <input type="hidden" name="action" value="register">
        
        <!-- Site Information Section -->
        <div class="adeptus-form-section">
            <h3><i class="fa fa-globe"></i> Site Information</h3>
            
            <div class="adeptus-form-row">
                <div class="adeptus-form-group">
                    <label for="site_name">Site Name *</label>
                    <input type="text" id="site_name" name="site_name" value="{{site_name}}" readonly>
                    <div class="form-help">Your Moodle site name</div>
                </div>
                
                <div class="adeptus-form-group">
                    <label for="site_url">Site URL *</label>
                    <input type="url" id="site_url" name="site_url" value="{{site_url}}" readonly>
                    <div class="form-help">Your Moodle site URL</div>
                </div>
            </div>
        </div>
        
        <!-- Administrator Information Section -->
        <div class="adeptus-form-section">
            <h3><i class="fa fa-user"></i> Administrator Information</h3>
            
            <div class="adeptus-form-row">
                <div class="adeptus-form-group">
                    <label for="admin_name">Administrator Name *</label>
                    <input type="text" id="admin_name" name="admin_name" value="{{admin_name}}" readonly>
                    <div class="form-help">Your full name as the administrator</div>
                </div>
                
                <div class="adeptus-form-group">
                    <label for="admin_email">Administrator Email *</label>
                    <input type="email" id="admin_email" name="admin_email" value="{{admin_email}}" readonly>
                    <div class="form-help">Your email address for account management</div>
                </div>
            </div>
        </div>
        
        <!-- System Information Section -->
        <div class="adeptus-form-section">
            <h3><i class="fa fa-cog"></i> System Information</h3>
            
            <div class="adeptus-form-row">
                <div class="adeptus-form-group">
                    <label for="moodle_version">Moodle Version *</label>
                    <input type="text" id="moodle_version" name="moodle_version" value="{{moodle_version}}" readonly>
                    <div class="form-help">Your current Moodle version</div>
                </div>
                
                <div class="adeptus-form-group">
                    <label for="php_version">PHP Version *</label>
                    <input type="text" id="php_version" name="php_version" value="{{php_version}}" readonly>
                    <div class="form-help">Your current PHP version</div>
                </div>
            </div>
            
            <div class="adeptus-form-row">
                <div class="adeptus-form-group">
                    <label for="plugin_version">Plugin Version *</label>
                    <input type="text" id="plugin_version" name="plugin_version" value="{{plugin_version}}" readonly>
                    <div class="form-help">Current Adeptus Insights plugin version</div>
                </div>
            </div>
        </div>
        
        <!-- Submit Section -->
        <div class="adeptus-submit-section">
            <button type="submit" class="adeptus-submit-btn" id="register-btn">
                <i class="fa fa-check"></i> Register Plugin
            </button>
        </div>
        <!-- Loading Overlay -->
        <div class="adeptus-loading" id="loading-overlay">
            <div class="spinner"></div>
            <p>Registering your plugin...</p>
            <p>Please wait while we set up your account.</p>
        </div>
    </form>
    {{/is_registered}}
</div>

<script>
document.addEventListener('DOMContentLoaded', function() {
    const form = document.getElementById('registration-form');
    
    // Only run validation if form exists (not registered)
    if (form) {
        const submitBtn = document.getElementById('register-btn');
        const loadingOverlay = document.getElementById('loading-overlay');
        const validationError = document.getElementById('validation-error');
        const validationMessage = document.getElementById('validation-message');
        
        // Required field validation
        const requiredFields = [
            { id: 'site_name', name: 'Site Name', value: '{{site_name}}' },
            { id: 'site_url', name: 'Site URL', value: '{{site_url}}' },
            { id: 'admin_name', name: 'Administrator Name', value: '{{admin_name}}' },
            { id: 'admin_email', name: 'Administrator Email', value: '{{admin_email}}' },
            { id: 'moodle_version', name: 'Moodle Version', value: '{{moodle_version}}' },
            { id: 'php_version', name: 'PHP Version', value: '{{php_version}}' },
            { id: 'plugin_version', name: 'Plugin Version', value: '{{plugin_version}}' }
        ];
        
        function validateFields() {
            const missingFields = [];
            
            requiredFields.forEach(field => {
                const value = field.value.trim();
                if (!value || value === '' || value === 'undefined' || value === 'null') {
                    missingFields.push(field.name);
                }
            });
            
            if (missingFields.length > 0) {
                // Show validation error
                validationError.style.display = 'block';
                validationMessage.innerHTML = `The following required information is missing: <strong>${missingFields.join(', ')}</strong>. Please contact your system administrator to ensure these Moodle configuration values are properly set before registering the plugin.`;
                
                // Disable submit button
                submitBtn.disabled = true;
                submitBtn.style.opacity = '0.5';
                submitBtn.style.cursor = 'not-allowed';
                submitBtn.innerHTML = '<i class="fa fa-ban"></i> Registration Disabled';
                
                return false;
            } else {
                // Hide validation error
                validationError.style.display = 'none';
                
                // Enable submit button
                submitBtn.disabled = false;
                submitBtn.style.opacity = '1';
                submitBtn.style.cursor = 'pointer';
                submitBtn.innerHTML = '<i class="fa fa-check"></i> Register Plugin';
                
                return true;
            }
        }
        
        // Run validation on page load
        validateFields();
        
        form.addEventListener('submit', function(e) {
            // Validate before submitting
            if (!validateFields()) {
                e.preventDefault();
                return false;
            }
            
            // Show loading overlay
            loadingOverlay.style.display = 'block';
            submitBtn.disabled = true;
            submitBtn.innerHTML = '<i class="fa fa-spinner fa-spin"></i> Registering...';
            
            // Form will submit normally
        });
    }
});
</script> 