{% extends "layout.html" %}

{% block content %}
<div class="settings-container">
    <!-- Header Section -->
    <div class="page-header">
        <div class="header-content">
            <div class="header-left">
                <h1 class="page-title">
                    <span class="page-icon"><i class="ti ti-settings"></i></span>
                    System Administrator Settings
                </h1>
                <p class="page-subtitle">Global system configuration and management</p>
            </div>
            <div class="header-actions">
                <a href="{{ url_for('system_admin.system_admin_dashboard') }}" class="btn btn-secondary">
                    <i class="ti ti-arrow-left"></i>
                    Back to Dashboard
                </a>
            </div>
        </div>
    </div>

    <!-- System Statistics -->
    <div class="stats-section">
        <div class="stat-card">
            <div class="stat-value">{{ total_companies }}</div>
            <div class="stat-label">Total Companies</div>
        </div>
        <div class="stat-card">
            <div class="stat-value">{{ total_users }}</div>
            <div class="stat-label">Total Users</div>
        </div>
        <div class="stat-card">
            <div class="stat-value">{{ total_system_admins }}</div>
            <div class="stat-label">System Administrators</div>
        </div>
    </div>

    <!-- System Settings Form -->
    <div class="card">
        <div class="card-header">
            <h2 class="card-title">
                <span class="icon"><i class="ti ti-adjustments"></i></span>
                System Configuration
            </h2>
        </div>
        <div class="card-body">
        <form method="POST" class="settings-form">
            <div class="setting-group">
                <div class="setting-header">
                    <h4>User Registration</h4>
                    <p>Control whether new users can register accounts</p>
                </div>
                <div class="setting-control">
                    <label class="toggle-label">
                        <input type="checkbox" name="registration_enabled" 
                               {% if settings.get('registration_enabled', True) %}checked{% endif %}>
                        <span class="toggle-slider"></span>
                        <span class="toggle-text">Allow new user registration</span>
                    </label>
                    <small class="setting-description">
                        When enabled, new users can create accounts through the registration page. 
                        When disabled, only administrators can create new user accounts.
                    </small>
                </div>
            </div>

            <div class="setting-group">
                <div class="setting-header">
                    <h4>Email Verification</h4>
                    <p>Require email verification for new accounts</p>
                </div>
                <div class="setting-control">
                    <label class="toggle-label">
                        <input type="checkbox" name="email_verification_required" 
                               {% if settings.get('email_verification_required', True) %}checked{% endif %}>
                        <span class="toggle-slider"></span>
                        <span class="toggle-text">Require email verification</span>
                    </label>
                    <small class="setting-description">
                        When enabled, new users must verify their email address before they can log in. 
                        This helps ensure valid email addresses and improves security.
                    </small>
                </div>
            </div>

            <div class="setting-group">
                <div class="setting-header">
                    <h4>Tracking Script</h4>
                    <p>Enable custom tracking script (e.g., Google Analytics)</p>
                </div>
                <div class="setting-control">
                    <label class="toggle-label">
                        <input type="checkbox" name="tracking_script_enabled" 
                               {% if settings.get('tracking_script_enabled', False) %}checked{% endif %}>
                        <span class="toggle-slider"></span>
                        <span class="toggle-text">Enable tracking script</span>
                    </label>
                    <small class="setting-description">
                        When enabled, the custom tracking script will be included on all pages. 
                        Use this for analytics tracking, monitoring, or other custom JavaScript.
                    </small>
                </div>
            </div>

            <div class="setting-group full-width">
                <div class="setting-header">
                    <h4>Tracking Script Code</h4>
                    <p>Enter your tracking script code (HTML/JavaScript)</p>
                </div>
                <div class="setting-control">
                    <textarea name="tracking_script_code" 
                              class="form-control code-textarea"
                              rows="8"
                              placeholder="<!-- Paste your tracking script here (e.g., Google Analytics, custom JavaScript) -->"
                    >{{ settings.get('tracking_script_code', '') }}</textarea>
                    <small class="setting-description">
                        This code will be inserted at the bottom of every page before the closing &lt;/body&gt; tag.
                        Common use cases: Google Analytics, Facebook Pixel, custom JavaScript, monitoring scripts.
                    </small>
                </div>
            </div>

            <div class="form-actions">
                <button type="submit" class="btn btn-primary">Save Settings</button>
                <a href="{{ url_for('system_admin.system_admin_dashboard') }}" class="btn btn-secondary">Cancel</a>
            </div>
        </form>
        </div>
    </div>

    <!-- System Information -->
    <div class="card">
        <div class="card-header">
            <h2 class="card-title">
                <span class="icon"><i class="ti ti-info-circle"></i></span>
                System Information
            </h2>
        </div>
        <div class="card-body">
        <div class="info-grid">
            <div class="info-card">
                <h4>Application Version</h4>
                <p>{{ g.branding.app_name }} v1.0</p>
            </div>
            <div class="info-card">
                <h4>Database</h4>
                <p>PostgreSQL</p>
            </div>
            <div class="info-card">
                <h4>System Admin Access</h4>
                <p>Full system control</p>
            </div>
        </div>
        </div>
    </div>

    <!-- Danger Zone -->
    <div class="danger-zone">
        <div class="danger-header">
            <h2 class="danger-title">
                <i class="ti ti-alert-triangle"></i>
                Danger Zone
            </h2>
        </div>
        <div class="danger-content">
            <div class="danger-item">
                <div class="danger-info">
                    <h4>System Maintenance</h4>
                    <p>Advanced system operations should be performed with caution. 
                       Always backup the database before making significant changes.</p>
                </div>
                <div class="danger-actions">
                    <button class="btn btn-danger" onclick="alert('Database backup functionality would be implemented here.')">
                        Backup Database
                    </button>
                </div>
            </div>
            
            <div class="danger-item">
                <div class="danger-info">
                    <h4>System Health Check</h4>
                    <p>Run diagnostic checks to identify potential issues with the system.</p>
                </div>
                <div class="danger-actions">
                    <a href="{{ url_for('system_admin.system_admin_health') }}" class="btn btn-warning">
                        Run Health Check
                    </a>
                </div>
            </div>
        </div>
    </div>

    <!-- Quick Actions -->
    <div class="card">
        <div class="card-header">
            <h2 class="card-title">
                <span class="icon"><i class="ti ti-rocket"></i></span>
                Quick Actions
            </h2>
        </div>
        <div class="card-body">
        <div class="actions-grid">
            <a href="{{ url_for('users.system_admin_users') }}" class="action-card">
                <div class="action-icon"><i class="ti ti-users"></i></div>
                <div class="action-content">
                    <h4>Manage All Users</h4>
                    <p>View, edit, and manage users across all companies</p>
                </div>
            </a>
            
            <a href="{{ url_for('system_admin.system_admin_companies') }}" class="action-card">
                <div class="action-icon"><i class="ti ti-building"></i></div>
                <div class="action-content">
                    <h4>Manage Companies</h4>
                    <p>View and manage all companies in the system</p>
                </div>
            </a>
            
            <a href="{{ url_for('system_admin.system_admin_time_entries') }}" class="action-card">
                <div class="action-icon"><i class="ti ti-clock"></i></div>
                <div class="action-content">
                    <h4>View Time Entries</h4>
                    <p>Browse time tracking data across all companies</p>
                </div>
            </a>
        </div>
        </div>
    </div>
</div>

<style>
/* Container */
.settings-container {
    max-width: 1400px;
    margin: 0 auto;
    padding: 2rem;
}

/* Page Header */
.page-header {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    border-radius: 16px;
    padding: 2rem;
    margin-bottom: 2rem;
    color: white;
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
}

.header-content {
    display: flex;
    justify-content: space-between;
    align-items: center;
    flex-wrap: wrap;
    gap: 2rem;
}

.page-title {
    font-size: 2rem;
    font-weight: 700;
    margin: 0;
    display: flex;
    align-items: center;
    gap: 0.75rem;
}

.page-icon {
    font-size: 2.5rem;
    display: inline-block;
    animation: rotate 8s linear infinite;
}

@keyframes rotate {
    from { transform: rotate(0deg); }
    to { transform: rotate(360deg); }
}

.page-subtitle {
    font-size: 1.1rem;
    opacity: 0.9;
    margin: 0.5rem 0 0 0;
}

/* Stats Section */
.stats-section {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    gap: 1.5rem;
    margin-bottom: 2rem;
}

.stat-card {
    background: white;
    padding: 1.5rem;
    border-radius: 12px;
    text-align: center;
    border: 1px solid #e5e7eb;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
    transition: all 0.3s ease;
}

.stat-card:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 16px rgba(0, 0, 0, 0.1);
}

.stat-value {
    font-size: 2.5rem;
    font-weight: 700;
    margin-bottom: 0.5rem;
    color: #667eea;
}

.stat-label {
    font-size: 0.9rem;
    color: #6b7280;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    font-weight: 600;
}

/* Cards */
.card {
    background: white;
    border-radius: 12px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
    border: 1px solid #e5e7eb;
    margin-bottom: 1.5rem;
    overflow: hidden;
    transition: all 0.3s ease;
}

.card:hover {
    box-shadow: 0 4px 16px rgba(0, 0, 0, 0.1);
    transform: translateY(-2px);
}

.card-header {
    background: #f8f9fa;
    padding: 1.5rem;
    border-bottom: 1px solid #e5e7eb;
}

.card-title {
    font-size: 1.25rem;
    font-weight: 600;
    margin: 0;
    color: #1f2937;
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.card-title .icon {
    font-size: 1.5rem;
}

.card-body {
    padding: 1.5rem;
}

.settings-form {
    display: flex;
    flex-direction: column;
    gap: 2rem;
}

.setting-group {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 2rem;
    padding: 1.5rem;
    border: 1px solid #e5e7eb;
    border-radius: 8px;
    background: #f8f9fa;
}

.setting-group.full-width {
    grid-template-columns: 1fr;
}

.code-textarea {
    font-family: 'Monaco', 'Menlo', 'Ubuntu Mono', monospace;
    font-size: 0.875rem;
    line-height: 1.4;
    background: #f8f9fa;
    border: 1px solid #ced4da;
    border-radius: 4px;
    padding: 0.75rem;
    resize: vertical;
    min-height: 120px;
}

.form-control {
    width: 100%;
    box-sizing: border-box;
}

.setting-header h4 {
    margin: 0 0 0.5rem 0;
    color: #1f2937;
    font-weight: 600;
}

.setting-header p {
    margin: 0;
    color: #6c757d;
    font-size: 0.9rem;
}

.toggle-label {
    display: flex;
    align-items: center;
    gap: 1rem;
    cursor: pointer;
    margin-bottom: 0.5rem;
}

.toggle-label input[type="checkbox"] {
    display: none;
}

.toggle-slider {
    position: relative;
    width: 50px;
    height: 24px;
    background: #e5e7eb;
    border-radius: 24px;
    transition: background 0.3s;
}

.toggle-slider::before {
    content: '';
    position: absolute;
    width: 20px;
    height: 20px;
    border-radius: 50%;
    background: white;
    top: 2px;
    left: 2px;
    transition: transform 0.3s;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.15);
}

.toggle-label input[type="checkbox"]:checked + .toggle-slider {
    background: #667eea;
}

.toggle-label input[type="checkbox"]:checked + .toggle-slider::before {
    transform: translateX(26px);
}

.toggle-text {
    font-weight: 500;
    color: #1f2937;
}

.setting-description {
    color: #6c757d;
    font-size: 0.875rem;
    line-height: 1.4;
}

.form-actions {
    display: flex;
    gap: 1rem;
    padding-top: 1.5rem;
    border-top: 1px solid #e5e7eb;
    margin-top: 2rem;
}

.info-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
    gap: 1rem;
}

.info-card {
    background: #f8f9fa;
    border: 1px solid #e5e7eb;
    border-radius: 8px;
    padding: 1.5rem;
}

.info-card h4 {
    margin: 0 0 0.5rem 0;
    color: #374151;
    font-size: 0.875rem;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    font-weight: 600;
}

.info-card p {
    margin: 0;
    color: #1f2937;
    font-size: 1.1rem;
    font-weight: 500;
}

/* Danger Zone */
.danger-zone {
    margin-top: 3rem;
    background: #fef2f2;
    border: 2px solid #fecaca;
    border-radius: 12px;
    overflow: hidden;
}

.danger-header {
    background: #fee2e2;
    padding: 1.5rem;
    border-bottom: 1px solid #fecaca;
}

.danger-title {
    font-size: 1.25rem;
    font-weight: 600;
    color: #991b1b;
    margin: 0;
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.danger-title i {
    font-size: 1.5rem;
}

.danger-content {
    padding: 2rem;
    display: flex;
    flex-direction: column;
    gap: 1.5rem;
}

.danger-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    background: white;
    padding: 1.5rem;
    border-radius: 8px;
    border: 1px solid #fecaca;
    gap: 2rem;
}

.danger-info h4 {
    margin: 0 0 0.5rem 0;
    color: #991b1b;
    font-weight: 600;
}

.danger-info p {
    margin: 0;
    color: #7f1d1d;
    font-size: 0.9rem;
    line-height: 1.5;
}

.actions-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
    gap: 1.5rem;
}

.action-card {
    display: flex;
    gap: 1.5rem;
    padding: 1.5rem;
    background: #f8f9fa;
    border-radius: 8px;
    text-decoration: none;
    transition: all 0.2s ease;
    border: 2px solid transparent;
    align-items: center;
}

.action-card:hover {
    background: white;
    border-color: #667eea;
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(102, 126, 234, 0.15);
    text-decoration: none;
}

.action-icon {
    font-size: 2.5rem;
    flex-shrink: 0;
}

.action-icon i {
    font-size: 2.5rem;
    color: #667eea;
}

.action-content h4 {
    margin: 0 0 0.25rem 0;
    color: #1f2937;
    font-weight: 600;
    font-size: 1.1rem;
}

.action-content p {
    margin: 0;
    color: #6b7280;
    font-size: 0.9rem;
}

/* Buttons */
.btn {
    padding: 0.75rem 1.5rem;
    border: none;
    border-radius: 8px;
    font-weight: 600;
    font-size: 1rem;
    cursor: pointer;
    transition: all 0.2s ease;
    display: inline-flex;
    align-items: center;
    gap: 0.5rem;
    text-decoration: none;
}

.btn-primary {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: white;
}

.btn-primary:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(102, 126, 234, 0.3);
}

.btn-secondary {
    background: white;
    color: #667eea;
    border: 2px solid rgba(255, 255, 255, 0.3);
}

.btn-secondary:hover {
    background: rgba(255, 255, 255, 0.1);
    border-color: rgba(255, 255, 255, 0.5);
}

.btn-danger {
    background: #dc2626;
    color: white;
}

.btn-danger:hover {
    background: #b91c1c;
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(220, 38, 38, 0.3);
}

.btn-warning {
    background: #f59e0b;
    color: white;
}

.btn-warning:hover {
    background: #d97706;
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(245, 158, 11, 0.3);
}

/* Responsive Design */
@media (max-width: 768px) {
    .settings-container {
        padding: 1rem;
    }
    
    .page-header {
        padding: 1.5rem;
    }
    
    .header-content {
        flex-direction: column;
        text-align: center;
    }
    
    .setting-group {
        grid-template-columns: 1fr;
    }
    
    .danger-item {
        flex-direction: column;
        align-items: flex-start;
        gap: 1rem;
    }
}

/* Animations */
@keyframes slideIn {
    from {
        opacity: 0;
        transform: translateY(20px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.card {
    animation: slideIn 0.3s ease-out;
    animation-fill-mode: both;
}

.card:nth-child(1) { animation-delay: 0.1s; }
.card:nth-child(2) { animation-delay: 0.2s; }
.card:nth-child(3) { animation-delay: 0.3s; }
</style>
{% endblock %}